Django nltk视图格式正确响应

时间:2018-05-03 15:59:07

标签: django django-rest-framework nltk

我有一个必须从我的Task对象返回提取的名词和动词的视图:

@api_view(['GET'])        
def test(request):

    verbs=[]
    tasks = Task.objects.all()

    serializer = TaskSerializer(tasks, many=True)
    print(serializer.data)

    text = ''.join([' '.join([str(y) for y in x.values()]) for x in serializer.data])

    text = nltk.word_tokenize(str(text)) #str(text)
    tags = nltk.pos_tag(text)

    return Response(filter(lambda x:x[1]=='VB', tags))

我的print语句打印:

[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'check 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对象。现在我只从对象值中提取名词/动词并忽略键。

我想要进一步实现的是我不想检查所有值中的名词或动词,而只检查三个字段,即 title role tools_used

我想将此视图中的数据返回为:

[
    {
        "title":[nouns:[],verbs:[]],
        "role": [nouns:[], verbs:[]],
        "tools_used": [nouns:[], verbs:[]],

    },
    {
        "title":[nouns:[],verbs:[]],
        "role": [nouns:[], verbs:[]],
        "tools_used": [nouns:[], verbs:[]],

    }
]

对于每个任务对象和每个感兴趣的字段,我想要名词和动词的数组/列表。

我不确定如何将for循环限制为仅这3个字段,然后格式化我的响应。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

你可以尝试一下:

set