Я пытаюсь использовать nltk в моем приложении django rest для извлечения существительных плохих глаголов:
Моя функция 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. Каждый объект имеет различные ключи / атрибуты, такие как идентификатор, заголовок и т. Д. И соответствующие значения.
Я хочу извлечь существительные и глаголы только из значений, а не ключей.
Как я могу это сделать?