首页 > 解决方案 > 如何在 NLP Api 中进行查询?

问题描述

我正在制作一个我想要帮助的 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)

标签: pythonapidjango-rest-framework

解决方案


推荐阅读