嗨,我第一次使用nltk,我想使用nltk从文本中提取动作/任务
Hi prakash, how are you ?. We need to complete the speech to action by 8 June then you will have to finish the UI by 15 july
在此处要执行的操作,并且 UI 是操作。
我已经开始创建令牌,不知道下一步该怎么做,请指导。
from nltk import sent_tokenize
sample_text ="""Hi prakash, how are you ?. We need to complete the speech to action demo by 8 June then you will have to finish the Ui by 15 july"""
sentences = sent_tokenize(sample_text)
print(sentences) import nltk
from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize
sample_text = """Hi prakash, how are you ?. We need to complete the speech to action by today
then you will have to finish the UI by 15 july after that you may go finish the mobile view"""
sample_text = "need to complete the speech to action by today"
tokens = word_tokenize(sample_text.lower())
# the lower is very much required, as June and june have diffrent code NN, NNP
pos_tags = pos_tag(tokens)
result = []
for i in range(len(tokens)):
if (pos_tags[i][1] == 'VB') and (pos_tags[i][0] in ['complete','finish']):
# Here we are looking for text like (finish, complete, done)
owner = ''
for back_tag in pos_tags[:i][::-1]:
#traverse in back direction to know the owner who will (finish, complete, done)
if back_tag[1]=='PRP':
owner = back_tag[0]
break
message = ''
date = ''
for messae_index , token in enumerate(pos_tags[i:],i):
#traverse forward to know what has to be done
if token[1]=='IN':
for date_index, date_lookup in enumerate(pos_tags[messae_index:],messae_index):
if date_lookup[1]=='NN':
date = pos_tags[date_index-1][0] + ' ' + pos_tags[date_index][0]
if date_lookup[1]=='PRP':
# This is trick to stop further propegation
# Don't ask me why i am doing this, if you are still reading then read the nest line
# Save futher interation as the next sentance is i/we/you
break
break
else:
message = message + ' ' + token[0]
result += [dict(owner=owner, message=message, date=date)]
print(result)
请指导如何从段落中提取动作(动作演示,UI)。
答案 0 :(得分:5)
如果您使用的是NLTK,则可以获取令牌的POS标签,并使用这些标签提供正则表达式或模式。例如,一个动作将是一个动词。 (为获得更好的标记,您可能需要Spacy
。出于这些目的,还有一个名为Pattern
的库)
但是我不确定这是否会对扩展应用程序有很大帮助。
N.B:有训练有素的命名实体识别器,您可以尝试一下。
答案 1 :(得分:2)
这是我的想法: 如果我尝试使用nltk.tag.pos_tag为您的句子识别词性,我将获得以下信息:
import nltk
from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize
s = 'Hi prakash, how are you ?. We need to complete the speech to action by 8 June then you will have to finish the UI by 15 july'
tokens = word_tokenize(s)
print(pos_tag(tokens))
输出:
[('Hi', 'NNP'), ('prakash', 'NN'), (',', ','), ('how', 'WRB'), ('are', 'VBP'), ('you', 'PRP'), ('?', '.'), ('.', '.'), ('We', 'PRP'), ('need', 'VBP'), ('to', 'TO'), ('complete', 'VB'), ('the', 'DT'), ('speech', 'NN'), ('to', 'TO'), ('action', 'NN'), ('by', 'IN'), ('8', 'CD'), ('June', 'NNP'), ('then', 'RB'), ('you', 'PRP'), ('will', 'MD'), ('have', 'VB'), ('to', 'TO'), ('finish', 'VB'), ('the', 'DT'), ('UI', 'NNP'), ('by', 'IN'), ('15', 'CD'), ('july', 'NN')]
如果您观察到,则每个动作词,即“语音转换为动作”或“ UI”都出现在前面的动词标记之后,分别是“完成”和“完成”。
我建议通过以下步骤尝试解决此问题:
1)在句子中查找动词。(如下所示)
for i in range(len(tokens)):
if pos_tag(tokens)[][1] == 'VB':
2)如果找到,则根据其pos标签提取下一个单词。 (可能会检索所有下一个单词,直到找到'IN'标签为止)
这可能适用于您的数据集。