0

I'm trying to insert into a dictionary certain values from the Streaming API. One of these is the value of the term that is used in the filter method using track=keyword. I've written some code but on the print statement I get an "Encountered Exception: 'term'" error. 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.

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)?

Thanks

user94628
  • 3,641
  • 17
  • 51
  • 88
  • So to put it simple, you just want to match the instances which CONTAIN the substrings of your `setTerm` list ? Maybe simply use the `in` operator http://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-method ? Or did I miss something ? –  Nov 15 '12 at 07:11

1 Answers1

0

Have you tried putting all your search terms into a single expression?

i.e. setTerms = '(BBC)|(XFactor)|(Obama)', then seeing if it matches the whole piece string (not just individual word?

readikus
  • 369
  • 1
  • 6
  • 17