我正在尝试在我的django rest应用程序中使用nltk来提取名词不良动词:
我的WIP功能如下所示:
@api_view(['GET'])
def test(request):
verbs=[]
tasks = Task.objects.all()
serializer = TaskSerializer(tasks, many=True)
print(serializer.data)
text = nltk.word_tokenize(str(serializer.data))
tags = nltk.pos_tag(text)
#print(tags)
for tag in tags:
if tag[1][0] == 'V':
verbs.extend(tag)
return Response(verbs)
行print(serializer.data)
打印如下:
[OrderedDict([(u'id', 17), ('title', u'Browse through the list of books'), ('how_often', u'DO'), ('how_important_task', u'EI'), ('role', u'reader'), ('why_perform_task', u''), ('why_important_task', None), ('sequence_of_actions', u''), ('tools_used', u''), ('special_training_required', False), ('what_training_required', u''), ('what_can_go_wrong', u''), ('effects_of_task', u''), ('special_vocabulary_used', u''), ('people_involved', u''), ('any_improvements', u''), ('how_important_improvement', u''), ('benefits_of_improvement', u''), ('stakeholder', 2L), ('project', 1L)]),
OrderedDict([(u'id', 18), ('title', u'Search for a book'), ('how_often', u'DS'), ('how_important_task', u'EI'), ('role', u'reader'), ('why_perform_task', u''), ('why_important_task', None), ('sequence_of_actions', u''), ('tools_used', u''), ('special_training_required', False), ('what_training_required', u''), ('what_can_go_wrong', u''), ('effects_of_task', u''), ('special_vocabulary_used', u''), ('people_involved', u''), ('any_improvements', u''), ('how_important_improvement', u'RI'), ('benefits_of_improvement', u''), ('stakeholder', 2L), ('project', 1L)]),
OrderedDict([(u'id', 19), ('title', u'Request a book'), ('how_often', u'WO'), ('how_important_task', u'RI'), ('role', u'reader'), ('why_perform_task', u''), ('why_important_task', None), ('sequence_of_actions', u''), ('tools_used', u''), ('special_training_required', None), ('what_training_required', u''), ('what_can_go_wrong', u''), ('effects_of_task', u''), ('special_vocabulary_used', u''), ('people_involved', u''), ('any_improvements', u''), ('how_important_improvement', u''), ('benefits_of_improvement', u''), ('stakeholder', 2L), ('project', 2L)]),
OrderedDict([(u'id', 26), ('title', u'See latest arrivals of the books'), ('how_often', u'MO'), ('how_important_task', u'LI'), ('role', u'reader'), ('why_perform_task', u''), ('why_important_task', None), ('sequence_of_actions', u''), ('tools_used', u''), ('special_training_required', None), ('what_training_required', u''), ('what_can_go_wrong', u''), ('effects_of_task', u''), ('special_vocabulary_used', u''), ('people_involved', u''), ('any_improvements', u''), ('how_important_improvement', u''), ('benefits_of_improvement', u''), ('stakeholder', 2L), ('project', 1L)])]
正如您所看到的,总共返回了4个Task对象。每个对象都有各种键/属性,如id,title等和相应的值。
我只想从值而不是键中提取名词和动词。
我该怎么办呢?
答案 0 :(得分:1)
您可以尝试从values
创建文本,然后使用它进行操作:
text = ''.join([' '.join([str(y) for y in x.values()]) for x in serializer.data])
text = nltk.word_tokenize(text)