如何在NLP Api中进行查询?

时间:2018-09-18 09:03:11

标签: python api django-rest-framework

我正在制作一个需要帮助的API,该如何进行查询?

我可以使用127.0.0.1:8000/Naturallanguageprocessing/

显示结果

它正在获取我包括语音,残词等的所有属性,但我必须以这种方式显示127.0.0.1:8000/?q = "querytext"&all = 1

如果我打127.0.0.1:8000/?q = "querytext" & partsofspeech = 1 & lemmatization = 1,它应该显示语音,残词等的所有部分。

它应该显示词性和词性化的部分。

下面是我的views.py。

 from django.shortcuts import render,redirect,HttpResponse
 from django.views.generic.edit import FormView
 from rest_framework.decorators import api_view
 from django.http import JsonResponse
 from django.conf import settings
 from rest_framework.response import Response
 from django.views import View
 import json
 import spacy
 import pdb
 nlp = spacy.load('en_core_web_sm')
 @api_view(["POST"])
def Naturallanguageprocessing(requestdata):
    try:
       text = str(requestdata.body)
       stopwordsremoval = []
       partsofspeech= []
       nounphrases = []
       word_lemma = []
       tokenization = []
       nameentityrecognization = []
       for word in (nlp(text)):
           a = (word.is_stop, word.text)
           b = (word.text, word.pos_, word.tag_)
           d = (word.lemma_)
           e = (word.text)
           tokenization.append(e)
           word_lemma.append(d)
           stopwordsremoval.append(a)
           partsofspeech.append(b)
       for word in (nlp(text).noun_chunks):
           c = (word.text)
           nounphrases.append(c)
       for word in (nlp(text).ents):
           f = (word.text,word.label_)
           nameentityrecognization.append(f)
           output = {"stopwordremoval": stopwordsremoval,
                  "partsofspeech": partsofspeech,
                  "nounphrases" : nounphrases,                
                  "word_lemma":word_lemma,
                  "tokenization":tokenization,
                  "nameentityrecognization":nameentityrecognization
}              
    return JsonResponse(output,safe = False)
except ValueError as e:
    return Response(e.args[0],status.HTTP_400_BAD_REQUEST)

0 个答案:

没有答案