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 3, 2018
1 parent 3a76165 commit 1a5408b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
19 changes: 18 additions & 1 deletion nlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@
## Features
* Text Similarity Comparison
* Sentiment Classification for [douban.com](https://www.douban.com/)
* News Classification
* News Classification

## RESTful API Notation
* IP: http://www.lucasx.top
* Port: 8001
* APIs
* sentiment analysis (by [snownlp](https://github.com/isnowfy/snownlp))
* API: sentiment
* param: sentence
* method: GET
* word segmentation
* API: wordseg
* param: sentence
* method: GET

## Reference
1. [snownlp](https://github.com/isnowfy/snownlp)
2. [jieba](https://github.com/fxsjy/jieba)
1 change: 1 addition & 0 deletions nlp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
urlpatterns = [
path('welcome', views.welcome, name='welcome'),
url('wordseg', views.word_seg, name='wordseg'),
url('sentiment', views.sentiment, name='sentiment'),
]
39 changes: 39 additions & 0 deletions nlp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,42 @@ def word_seg(request):
json_result = json.dumps(result, ensure_ascii=False)

return HttpResponse(json_result)


def sentiment(request):
"""
calculate sentiment of a specific sentence
:param request:
:return:
"""
sentence = request.GET.get('sentence')
result = OrderedDict()

if sentence is None:
result['code'] = 1
result['msg'] = 'Invalid Sentence Input'
result['data'] = None
else:
result['code'] = 0
result['msg'] = 'success'

from snownlp import SnowNLP

s = SnowNLP(sentence)

senti_score = s.sentiments
if senti_score >= 0.65:
tp = 'positive'
elif senti_score <= 0.4:
tp = 'negative'
else:
tp = 'neutral'

result['data'] = {
'type': tp,
'score': senti_score
}

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

return HttpResponse(json_result)

0 comments on commit 1a5408b

Please sign in to comment.