I've extracted keywords based on 1-gram, 2-gram, 3-gram within a tokenized sentence
list_of_keywords = []
for i in range(0, len(stemmed_words)):
temp = []
for j in range(0, len(stemmed_words[i])):
temp.append([' '.join(x) for x in list(everygrams(stemmed_words[i][j], 1, 3)) if ' '.join(x) in set(New_vocabulary_list)])
list_of_keywords.append(temp)
Run Code Online (Sandbox Code Playgroud)
I've obtained keywords list as
['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
['sleep', 'anxiety', 'lack of sleep']
Run Code Online (Sandbox Code Playgroud)
How can I simply the results by removing all substring within the list and remain:
['high blood pressure']
['anxiety', 'lack of sleep']
Run Code Online (Sandbox Code Playgroud)
Chr*_*per 13
You could use this one liner:
b = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = [ i for i in b if not any( [ i in a for a in b if a != i] )]
Run Code Online (Sandbox Code Playgroud)
I admit this is O(n2) and maybe will be slow in performance for large inputs.
这基本上是对以下内容的列表理解:
word_list = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = []
for this_word in word_list:
words_without_this_word = [ other_word for other_word in word_list if other_word != this_word]
found = False
for other_word in words_without_this_word:
if this_word in other_word:
found = True
if not found:
result.append(this_word)
result
Run Code Online (Sandbox Code Playgroud)