Thi*_*ker 0 django nltk django-rest-framework
我有 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)
Run Code Online (Sandbox Code Playgroud)
有人可以帮我建立这个功能吗?导入什么和逻辑?
使用后nltk 标记
>>> 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')]
Run Code Online (Sandbox Code Playgroud)
名词由 标记NN,动词由标记VB,因此您可以相应地使用它们。
注意:
如果您尚未安装/下载punkt并averaged_perceptron_tagger使用 nltk,则可能必须使用以下方法进行设置:
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3238 次 |
| 最近记录: |