有没有办法从不用于过滤推文的推文中获取单词?
Is there way to obtain words from a tweet that aren't used to filter tweets?
我正在使用由这些标签过滤的 Tweepy 流式传输推文 ["corona"、"quarantine"、"covid19"]
例如,如果我有这条推文,“我从楼梯上摔下来吃了一个苹果,所以没有医生#quarantine”
我想获取像“stairs”、“apple”和“doctor”这样的字符串作为一组关键字
有什么办法吗?
我是 python 的初学者,我正在使用 Youtube 上的视频教程来开始这个项目
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
lis = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, lis)
stream.filter(track=['covid19','corona','quarantine'])
您可以使用列表理解:
tags = ["corona", "quarantine", "covid19"]
tweet = "I fell down the stairs and ate an apple so no doctor #quarantine"
# print each word in the tweet that is longer than two characters and
# does not contain any of the tag words
print([word for word in tweet.split() if len(word) > 2 and not any(tag in word for tag in tags)])
这不是一个完美的解决方案,主要是因为它排除了 包含 标签的单词,即如果其中一个标签是 wash
,那么单词 washington
将被排除在外。但这是一个开始。
这个怎么样? -
如果你想将推文分解成单词,那么-
s = 'fell down the stairs and ate an apple so no doctor #quarantine'
allwords = s.split(' ')
allwords
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor','#quarantine']
然后你可以用#标签分隔单词 -
hastags = [i for i in allwords if i[:1]=='#']
hastags
#output
['#quarantine']
接下来您可以通过执行此操作来过滤我们具有 # 标签的单词 -
otherwords = [i for i in allwords if i not in hastags]
otherwords
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor']
对于更大的数据集和一长串特定的标签,我建议这样做 -
tags = ["corona", "quarantine", "covid19"]
[i for i in s.split(' ') if i.strip('#') not in tags]
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor']
如果您遇到这样一种情况,即用于过滤推文的标签前面可能没有#,但您仍想过滤掉它们,那么 -
tags = ["corona", "quarantine", "covid19"]
print([i for i in s.split(' ') if i.strip('#') not in tags and i not in tags])
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor']
我正在使用由这些标签过滤的 Tweepy 流式传输推文 ["corona"、"quarantine"、"covid19"]
例如,如果我有这条推文,“我从楼梯上摔下来吃了一个苹果,所以没有医生#quarantine” 我想获取像“stairs”、“apple”和“doctor”这样的字符串作为一组关键字
有什么办法吗?
我是 python 的初学者,我正在使用 Youtube 上的视频教程来开始这个项目
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
lis = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, lis)
stream.filter(track=['covid19','corona','quarantine'])
您可以使用列表理解:
tags = ["corona", "quarantine", "covid19"]
tweet = "I fell down the stairs and ate an apple so no doctor #quarantine"
# print each word in the tweet that is longer than two characters and
# does not contain any of the tag words
print([word for word in tweet.split() if len(word) > 2 and not any(tag in word for tag in tags)])
这不是一个完美的解决方案,主要是因为它排除了 包含 标签的单词,即如果其中一个标签是 wash
,那么单词 washington
将被排除在外。但这是一个开始。
这个怎么样? -
如果你想将推文分解成单词,那么-
s = 'fell down the stairs and ate an apple so no doctor #quarantine'
allwords = s.split(' ')
allwords
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor','#quarantine']
然后你可以用#标签分隔单词 -
hastags = [i for i in allwords if i[:1]=='#']
hastags
#output
['#quarantine']
接下来您可以通过执行此操作来过滤我们具有 # 标签的单词 -
otherwords = [i for i in allwords if i not in hastags]
otherwords
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor']
对于更大的数据集和一长串特定的标签,我建议这样做 -
tags = ["corona", "quarantine", "covid19"]
[i for i in s.split(' ') if i.strip('#') not in tags]
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor']
如果您遇到这样一种情况,即用于过滤推文的标签前面可能没有#,但您仍想过滤掉它们,那么 -
tags = ["corona", "quarantine", "covid19"]
print([i for i in s.split(' ') if i.strip('#') not in tags and i not in tags])
#output
['fell', 'down', 'the', 'stairs', 'and', 'ate', 'an', 'apple', 'so', 'no', 'doctor']