Recently I built a Usenet newsreader in Python and for that I built a keyword search which supports AND and OR functionalities (e.g., python AND django should bring up articles containing both. I defined a
find function for this which takes a keyword query and a string and returns a
bool signifying whether the string qualifies (i.e., for the above example, the string qualifies if it contains both 'python' and 'django').
Here's the snippet:
search = 'python AND django OR ruby AND rails' query = search.split(' OR ') query = [phrase.split(' AND ') for phrase in query] def find(query, string): is_there = False for chunk in query: #OR chunk_qualified = False for word in chunk: #AND if word not in string: break else: chunk_qualified = True if chunk_qualified: is_there = True break return is_there
It works, but can I do it in a better manner (less code, more efficient)?