我想检查一下文字列表中是否有单词序列:
word_list =" never"," not"," buy"," here"," again", "更多","你好","不","将","表"
text ="我不会在这里购买更多"
预期输出不会在这里购买更多
但不是:
会(重复序列)
不会(不完整的序列)
我这样做(用非常小的词来排序)
我的剧本:
word_list = "never", "not", "buy", "here", "again", "more", "hello", "not", "will"
text = "I do will will not buy more here"
text = text.split(" ")
sequences = []
counter = 0
for words in text:
for word in word_list:
if word in text:
sequences.append(word)
counter =+ counter
# to avoid meaningless sequences like (incomplete sequence): "will not", "I will", "more here"...
sequences_two_words = []
for sequence in sequences:
if len(sequence) <= 2:
pass
else:
sequences_two_words.append(sequence)
# to avoid sequences like (repeated sequence): "will will"
sequences_not_repeat = []
for not_repeat in sequences_two_words:
if not_repeat[0] == not_repeat[1]:
pass
else:
sequences_not_repeat.append(not_repeat)
# to avoid sequences like (sequence with very small words): "I do"
sequences_not_little = []
for little_len in sequences_not_repeat:
if len(little_len[1]) <= 2:
pass
else:
sequences_not_little.append(little_len)
print(sequences_not_little)
我的输出:
[]
[]
[]
[]
[]
[]
[]
[]
答案 0 :(得分:1)
word_list = "never", "not", "buy", "here", "again", "more", "hello", "not", "will", "table"
text = "I do will will not buy more here"
text_split = text.lower().split(" ")
sequences = []
sequence = ()
prev = False
for word in text_split:
if word in word_list:
# len(word) > 2 removes: I do (sequence with very small words)
# prev != word removes: [will will (repeated sequence)]
if len(word) > 2 and prev != word:
sequence += (word, )
else:
if len(sequence) > 2: # removes: will not (incomplete sequence)
sequences.append(sequence)
sequence = ()
prev = word
if len(sequence) > 2:
sequences.append(sequence)
print(sequences) # array sequences you want