简体   繁体   English

模式匹配Twitter Streaming API

[英]Pattern matching Twitter Streaming API

I'm trying to insert into a dictionary certain values from the Streaming API. 我正在尝试将Streaming API中的某些值插入字典中。 One of these is the value of the term that is used in the filter method using track=keyword. 其中之一是在使用track = keyword的过滤器方法中使用的术语的值。 I've written some code but on the print statement I get an "Encountered Exception: 'term'" error. 我已经写了一些代码,但在打印语句上却收到“遇到异常:'term'”错误。 This is my partial code: 这是我的部分代码:

 for term in setTerms:
     a = re.compile(term, re.IGNORECASE)
     if re.search(a, status.text):
         message['term'] = term
     else:
         pass

      print message['text'], message['term']

This is the filter code: 这是过滤器代码:

setTerms = ['BBC','XFactor','Obama']
streamer.filter(track = setTerms)

It matches the string, but I also need to be able to match all instances eg. 它匹配字符串,但我还需要能够匹配所有实例。 BBC should also match with #BBC, @BBC or BBC1 etc. BBC也应与#BBC,@ BBC或BBC1等匹配。

So my question how would i get a term in setTerms eg BBC to match all these instances in if re.search(term, status.text)? 所以我的问题是,如果re.search(term,status.text),我如何在setTerms中获得一个术语,例如BBC以匹配所有这些实例?

Thanks 谢谢

Have you tried putting all your search terms into a single expression? 您是否尝试过将所有搜索词放在一个表达式中?

ie setTerms = '(BBC)|(XFactor)|(Obama)', then seeing if it matches the whole piece string (not just individual word? 即setTerms ='(BBC)|(XFactor)|(Obama)',然后查看它是否与整个字符串匹配(不仅仅是单个单词?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM