你好,我第一次使用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
上面提到的语音转动作和用户界面是动作。
我已经开始创建令牌,不知道接下来该做什么,请指导。
from nltk import sent_tokenizesample_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 nltkfrom nltk.tag import pos_tagfrom nltk.tokenize import word_tokenizesample_text = """Hi prakash, how are you ?. We need to complete the speech to action by todaythen 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, NNPpos_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)
请指导如何从段落中提取动作(动作演示、用户界面)。
回答:
如果你使用NLTK,你可以获取令牌的POS标签,并使用这些标签创建一个正则表达式或模式。例如,动作将是一个动词。(为了获得更好的标记,你可能需要Spacy
。还有另一个名为Pattern
的库可用于这些目的)
但我不确定这是否对大规模应用有很大帮助。
注意:有经过良好训练的命名实体识别器可用,你可以尝试使用它们。