开发者

Twitter Stream response for key words

i have small python program to extract tweets from twitter stream for keywords.

want to link the keyword with received tweets.how can i do that in python efficiently.

I may have more than 200 keys.

eg:

keywords = "key1,key2,key3"

received 4 t开发者_如何学Pythonweets and i want to directly link incoming text and key..

like below

1)tweets msgs key1 tweets tweets text --> key1

2)tweets msgs key2 tweets tweets text --> key2

3)tweets msgs key3 tweets tweets text --> key3

4)tweets msgs key1 tweets tweets text --> key1

Update:

Currently am using for loop to iterate the key list and use find to see if the key is present in the tweet text.But i feel there should be better and efficient way of doing this in python.

 for title in title_list:
     if tweet_lower.find(title) != -1:
        setattr(status, 'title',title)
        break


Use defaultdict from Python's collection module.

In [1]: from collections import defaultdict

In [2]: tweet_keyword_storage = defaultdict(list)

In [3]: tweet_keyword_storage['key1'].append('tweets msgs key1 tweets tweets text')

In [4]: tweet_keyword_storage['key2'].append('tweets msgs key2 tweets tweets text')

In [5]: tweet_keyword_storage['key3'].append('tweets msgs key3 tweets tweets text')

In [6]: tweet_keyword_storage['key1'].append('tweets msgs key1 tweets tweets text')

In [7]: print tweet_keyword_storage
defaultdict(<type 'list'>, {'key3': ['tweets msgs key3 tweets tweets text'], 'key2': ['tweets msgs key2 tweets tweets text'], 'key1': ['tweets msgs key1 tweets tweets text', 'tweets msgs key1 tweets tweets text']})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜