使用NLTK提取名词和动词

时间:2018-05-03 09:42:15

标签: django django-rest-framework nltk

我有django休息应用程序和模型任务。我是自然处理的新手,我想建立一个返回名词和动词列表的函数。它看起来像这样:

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

    nouns = []
    verbs = []
    """
    List all nouns and verbs from available tasks
    """
    if request.query_params.get('projectId'):
            # get a specific project 
            projectId = request.query_params.get('projectId')
            project = Project.objects.get(id=projectId)
            tasks = project.project_tasks.all()

            # extract nouns and verbs from tasks here


            return Response(# return appropriate nouns)

有人可以帮我构建这个功能吗?什么导入和逻辑?

1 个答案:

答案 0 :(得分:1)

使用nltk pos-tagger

>>> import nltk
>>> text = nltk.word_tokenize("They refuse to permit us to obtain the refuse permit")
>>> pos_tagged = nltk.pos_tag(text)
>>> pos_tagged
[('They', 'PRP'), ('refuse', 'VBP'), ('to', 'TO'), ('permit', 'VB'), ('us', 'PRP'),
('to', 'TO'), ('obtain', 'VB'), ('the', 'DT'), ('refuse', 'NN'), ('permit', 'NN')]
>>> nouns = filter(lambda x:x[1]=='NN',pos_tagged)
>>> nouns
[('refuse', 'NN'), ('permit', 'NN')]

名词由NN标记,动词由VB标记,因此您可以相应地使用它们。

注意: 如果您没有使用nltk设置/下载punktaveraged_perceptron_tagger,则可能需要使用以下命令:

import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')