【问题标题】:extract nouns and verbs using NLTK使用 NLTK 提取名词和动词
【发布时间】:2018-05-03 09:42:14
【问题描述】:

我有 django rest 应用程序和一个模型任务。我对自然处理完全陌生,我想构建一个返回名词和动词列表的函数。它看起来像这样:

@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)

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

【问题讨论】:

    标签: django django-rest-framework nltk


    【解决方案1】:

    使用nltkpos-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')
    

    【讨论】:

    • 感谢您的回答。这个 nltk.download() 我必须通过命令行运行还是有办法通过 pip 或类似方法?
    • @Nitish 如果有帮助,请点赞并接受答案。
    • 感谢您的回答。
    • 有没有办法在lamba过滤器中输入多个标签,还是一次只接受一个标签?
    • 哎呀!回答了我自己的问题: Counter(list(filter(lambda x: (x[1] == 'VB' or x[1] == 'VBP'), tagged))).most_common(20)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    相关资源
    最近更新 更多