【发布时间】:2020-07-05 12:26:38
【问题描述】:
我正在尝试将列表中的特定单词映射到另一个标记化句子列表,如果在句子中找到该单词,那么我将 1 附加到其类别列表,并将 0 附加到其余类别。 例如:
category_a=["stain","sweat","wet","burn"]
category_b=["love","bad","favorite"]
category_c=["packaging","delivery"]
tokenized_sentences=['this deodorant does not stain my clothes','i love this product','i sweat all day']
for i in category_a:
for j in tokenized_sentences:
if(i in nltk.word_tokenize(j)):
list_a.append(j)
tag_a,tag_b,tag_c=([],)*3
tag_a.append(1)
tag_b.append(0)
tag_c.append(0)
final=tag_a+tag_b+tag_c
category_b 和 category_c 也是如此
Expected output:this deodorant does not stain my clothes-->[1,0,0]
i love this product-->[0,1,0]
i sweat all day-->[1,0,0]
great fragrance-->[0,0,0]
我得到每个句子的重复输出,例如:我喜欢这个产品-->[1,0,0] 我喜欢这个产品--> [1,0,0] 和 也喜欢这样:[我喜欢这个产品,我整天出汗]-->[0,1,0]
Also, if a sentence has words from two different categories Ex: 'this product does not stain and i love it'
the expected output would be [1,1,0]
请帮我解决问题并获得所需格式的输出。
【问题讨论】: