句子顺序词表的最佳识别方法

2024-04-23 10:45:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个单词列表,我想在一个句子中找到一个序列。我想检查是可以用“正则表达式”还是我应该用if条件检查句子?你知道吗

n_ali = set(['ali','aliasghar'])
n_leyla = set(['leyla','lili',leila])
positive_adj = set(['good','nice','handsome'])
negative_adj = set(['bad','hate','lousy'])


Sentence = "aliasghar is nice man. ali is handsome man of my life. lili has so many bad attitude who is next to my friend. "

我想找到以下模式:

  • 阿里+积极的
  • 名词+否定形容词
  • 利拉+正的
  • 莱拉+否定的

我正在VS2015中使用Python3.5,我是NLTK的新手。我知道如何创建一个“正则表达式”检查一个单词,但我不知道什么是最好的方法为类似的名字列表。请帮助我,并建议我什么是最好的方式来实施这一方法。你知道吗


Tags: 列表ismyali单词句子badnice
1条回答
网友
1楼 · 发布于 2024-04-23 10:45:37

你应该考虑删除停止语。你知道吗

import nltk
from nltk.corpus import stopwords
>>> words = [word for word in nltk.word_tokenize(sentence) if word not in stopwords.words('english')]
>>> words
['aliasghar', 'nice', 'man', '.', 'ali', 'handsome', 'man', 'life', '.', 'lili', 'many', 'bad', 'attitude', 'next', 'friend', '.']

好吧,现在你有了你想要的数据(大部分)。让我们使用简单的循环来分别为alileila成对存储结果。你知道吗

>>> ali_adj = []
>>> leila_adj = []
>>> for i, word in enumerate(words[:-1]):
...     if word in n_ali and (words[i+1] in positive_adj.union(negative_adj)):
...             ali_adj.append((word, words[i+1]))
...     if word in n_leyla and (words[i+1] in positive_adj.union(negative_adj)):
...             leila_adj.append((word, words[i+1]))
... 
>>> 
>>> ali_adj
[('aliasghar', 'nice'), ('ali', 'handsome')]
>>> leila_adj
[]

注意,我们找不到任何形容词来描述leila,因为“many”不是停止词。你可能需要手动清理句子。你知道吗

相关问题 更多 >