4

I have django rest application and a model Task. I am absolutely new to natural processing and I want to build a function that returns me list of nouns and verbs. It looks something like this:

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

Could someone help me build this function? What to import and with logic?

Thinker
  • 5,326
  • 13
  • 61
  • 137

1 Answers1

13

Use 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')]

Nouns are marked by NN and verbs are by VB, so you can use them accordingly.

NOTE: If you have not setup/downloaded punkt and averaged_perceptron_tagger with nltk, you might have to do that using:

import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
harshil9968
  • 3,254
  • 1
  • 16
  • 26