Skip to content

Commit

Permalink
update NLP module
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasxlu committed Dec 2, 2018
1 parent 34f215b commit f3d48f9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
- [x] Age Estimation
* Image Recognition
- [x] Scene Recognition
- [x] Flower Recognition
- [x] Flower Recognition
- [x] Porn Image Recognition
1 change: 1 addition & 0 deletions cv/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

urlpatterns = [
path('welcome', views.welcome, name='welcome'),
url('fbp', views.fbp, name='fbp'),
]
30 changes: 30 additions & 0 deletions cv/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import json
from random import randint
from collections import OrderedDict

from django.http import HttpResponse
from django.shortcuts import render


Expand All @@ -12,3 +17,28 @@ def welcome(request):
"""
return render(request, 'welcome.html')


def fbp(request):
"""
calculate facial beauty score
:param request:
faceImagePath: face image path
:return:
"""
face_img_path = request.GET.get('faceImagePath')
result = OrderedDict()

if not face_img_path or face_img_path.strip() == '':
result['code'] = 1
result['msg'] = 'Invalid Path for Face Image'
result['data'] = None
else:
result['code'] = 0
result['msg'] = 'success'
result['data'] = {
'beauty': randint(0, 9)
}

json_result = json.dumps(result, ensure_ascii=False)

return HttpResponse(json_result)
1 change: 1 addition & 0 deletions nlp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

urlpatterns = [
path('welcome', views.welcome, name='welcome'),
url('wordseg', views.word_seg, name='wordseg'),
]
47 changes: 47 additions & 0 deletions nlp/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import json
from collections import OrderedDict
from random import randint

import jieba as jieba
import jieba.analyse
from django.http import HttpResponse
from django.shortcuts import render

USER_DICT = './userdict.txt'
STOPWORDS_FILE = './stopwords.txt'


# Create your views here.

Expand All @@ -10,3 +20,40 @@ def welcome(request):
:return:
"""
return render(request, 'welcome.html')


def word_seg(request):
"""
sentence segmentation by jieba
:param request: sentence
:return:
"""
sentence = request.GET.get('sentence')
result = OrderedDict()

if not sentence or sentence.strip() == '':
result['code'] = 1
result['msg'] = 'Invalid Sentence Input'
result['data'] = None
else:
result['code'] = 0
result['msg'] = 'success'
jieba.load_userdict(USER_DICT)
jieba.analyse.set_stop_words(STOPWORDS_FILE)
tags = jieba.analyse.extract_tags(sentence, topK=30, withWeight=True)

seg_words = []
tfidf = []

for tag in tags:
seg_words.append(tag[0])
tfidf.append(tag[1])

result['data'] = {
'words': seg_words,
'tf-idf': tfidf
}

json_result = json.dumps(result, ensure_ascii=False)

return HttpResponse(json_result)

0 comments on commit f3d48f9

Please sign in to comment.