Question

I have a message of text and a list of terms. I'd like to create an array that shows which terms are found in the message. For instance:

message = "the quick brown fox jumps over the lazy dog"
terms = ["quick", "fox", "horse", "Lorem", "Ipsum", "the"]

result = idealMethod(message, terms)
=>   [1,1,0,0,0,1] 

Because "quick" was the first item in the list of terms and was also in the message a 1 is placed in the first position in the result. Here's another example:

message2 = "Every Lorem has a fox"
result2 = idealMethod(message2, terms)
=> [0,1,0,1,0,0]

Update: The terms need to be exact matches. For instance if my search term include sam I don't want a match for same

Was it helpful?

Solution

I think you want:

words = set(message.split(" "))
result = [int(word in words) for word in terms]

Note that split() splits on whitespace by default, so you could leave out the " ".

OTHER TIPS

You can use list comprehension and leverage fact, that True is evaluated as 1 in numeric context:

words = set(message.split())
result = [int(term in words) for term in terms]
Out[24]: [1, 1, 0, 0, 0]

EDIT changed to look only for whole word matches, after clarification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top