django序列化数据提取名词
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django序列化数据提取名词相关的知识,希望对你有一定的参考价值。
我想在我的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等和相应的值。
我想只从值而不是键中提取名词和动词。
我该怎么办呢?
答案
您可以尝试从values
创建文本,然后使用它进行操作:
text = ''.join([' '.join([str(y) for y in x.values()]) for x in serializer.data])
text = nltk.word_tokenize(text)
以上是关于django序列化数据提取名词的主要内容,如果未能解决你的问题,请参考以下文章