By Nam Dao


2020-06-30 03:34:21 8 Comments

I want a Python function that can get a keyword from an input of a sentence and a list of keywords. The function will iterate over the keywords list and search in the sentence the given keywords. If there is a keyword in the sentence, the function will return the keyword. Else it will return None.

For example, I have a list ["ai", "Machine Learning", "Computer Science"] and a sentence "My major is Computer Science, and I am currently working as a retailer for a store". If the function is implemented correctly, it will output the keyword Computer Science. However, at the moment, my function is outputting both Computer Science and ai (retailer also has ai in it) since I am using Python IN. Is there any way that I can improve my function to get the more proper result?

Here is my attempted code:

sentence = "My major is Computer Science, and I am currently working as a retailer for a store"

keywords_list = ["ai", "Machine Learning", "Computer Science"]

output_list = list()

for keyword in keywords_list:
    if keyword in sentence:
        output_list.append(keyword)

3 comments

@Ann Zen 2020-06-30 04:28:08

Here is what you can do:

sentence = "My major is Computer Science, and I am currently working as a retailer for a store"

keywords_list = ["ai", "Machine Learning", "Computer Science"]

alph = 'abcdefghijklmnopqrstuvwxyz'

output_list = list()

for keyword in keywords_list:
    
    k = [''.join([w for w in key if w.lower() in alph]) for key in keyword.split()] # List of all the words in keyword, with all punctuations removed
    s = [''.join([w for w in wor if w.lower() in alph]) for wor in sentence.split()] # List of all the words in sentence, with all punctuations removed
    
    if any([k == s[i:len(k)+i] for i in range(len(s)-len(k))]): # If any of the lists in keyword equals to any slice of the sentence list
        output_list.append(keyword) # That means that the keyword is in
        
print(output_list)

Output:

['Computer Science']

@Mathew 2020-06-30 03:51:09

sentence = input("sentence here: ").casefold()
keywords = ["ai", "Machine Learning", "Computer Science"]
# making all keywords lower case
for keyword in keywords:
    keywords[keywords.index(keyword)] = keyword.casefold()

result= ""
#searching
for keyword in keywords:
    if keyword in sentence:
        result += "{} ".format(keyword)
if result == "":
    result = None
print(result)

@Nam Dao 2020-06-30 04:00:12

I have tried this and this is not working. :(

@Mathew 2020-06-30 04:02:50

How is that? if you're copying the code you should reformat the code from your IDE

@Ronie Martinez 2020-06-30 04:06:24

You cannot split by spaces because the keywords may contain spaces

@Mathew 2020-06-30 04:07:14

I didn't notice that

@Mathew 2020-06-30 04:17:26

I've edited my answer, this should work now

@Nam Dao 2020-06-30 04:29:13

@Mathew the code above still outputs the wrong result. As I said in the post, ai is also in retailer. Anyway, I found the solution. Thank you so much!!!

@Ronie Martinez 2020-06-30 04:16:38

I think your best path for this is using a regex because if you just use in, it will match since ai is in the word retailer. You can use word boundaries in regex (\b).

>>> import re
>>> keywords = "|".join(keywords_list)
>>> re.findall(f"\\b({keywords})\\b", sentence)
['Computer Science']

Related Questions

Sponsored Content

23 Answered Questions

[SOLVED] What is the purpose of the word 'self'?

  • 2010-04-25 20:22:28
  • richzilla
  • 861523 View
  • 1139 Score
  • 23 Answer
  • Tags:   python class oop self

31 Answered Questions

[SOLVED] Regular expression to match a line that doesn't contain a word

31 Answered Questions

[SOLVED] Split Strings into words with multiple word boundary delimiters

  • 2009-06-29 17:49:35
  • ooboo
  • 693855 View
  • 672 Score
  • 31 Answer
  • Tags:   python string split

12 Answered Questions

1 Answered Questions

6 Answered Questions

[SOLVED] python max function using 'key' and lambda expression

2 Answered Questions

[SOLVED] Iterate over multiple list and creating list with outcome of result in Python

  • 2017-09-21 15:40:17
  • Sandy
  • 50 View
  • 0 Score
  • 2 Answer
  • Tags:   python

Sponsored Content