├── .gitattributes ├── .idea ├── inspectionProfiles │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── sentiment_analysis.iml ├── vcs.xml └── workspace.xml ├── README.md ├── db.sqlite3 ├── manage.py ├── my_model.h5 ├── requirements.txt ├── sentiment_analysis ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-37.pyc │ ├── settings.cpython-37.pyc │ ├── tools.cpython-37.pyc │ ├── urls.cpython-37.pyc │ ├── views.cpython-37.pyc │ └── wsgi.cpython-37.pyc ├── settings.py ├── tests.py ├── tools.py ├── urls.py ├── views.py └── wsgi.py ├── templates └── index.html └── word_dict.pickle /.gitattributes: -------------------------------------------------------------------------------- 1 | *.h5 filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/sentiment_analysis.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 25 | 26 | 27 | 29 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 132 | 133 | 134 | 136 | 137 | 149 | 150 | 151 | 152 | 153 | true 154 | DEFINITION_ORDER 155 | 156 | 157 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 187 | 188 | 191 | 192 | 195 | 196 | 197 | 198 | 201 | 202 | 205 | 206 | 207 | 208 | 211 | 212 | 215 | 216 | 219 | 220 | 221 | 222 | 225 | 226 | 229 | 230 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 269 | 270 | 271 | 272 | 290 | 291 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 340 | 341 | 354 | 355 | 377 | 378 | 396 | 397 | 409 | 410 | project 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 441 | 442 | 461 | 462 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 497 | 498 | 499 | 500 | 1573832795448 501 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 536 | 537 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 情绪识别api 2 | 3 | 用Django搭建的Web接口,输入一句话,通过训练好的模型来预测识别这句话的情绪。 4 | 5 | ## 示例 6 | ### 调用api 7 | 输入 8 | ``` 9 | http://114.116.230.62/test/?sentence=卧槽 10 | ``` 11 | 输出 12 | ``` 13 | {"code": 200, "msg": "厌恶"} 14 | ``` 15 | ### 可视化界面 16 | ![界面](https://img-blog.csdnimg.cn/20191203165825951.png) 17 | ## 模型训练 18 | 基于LSTM的中文情绪识别 19 | https://github.com/jie12366/sentiment_analysis 20 | 21 | ## 相关博客 22 | [中文情绪识别api](http://jie12366.xyz:8081/#/users/11/articles/46) 23 | [基于LSTM的中文多分类情感分析](http://jie12366.xyz:8081/#/users/11/articles/35) 24 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/db.sqlite3 -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sentiment_analysis.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /my_model.h5: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:5e7f0f6bde42d235a69b450b73fe7bf47836d2bd148b56af2a31065f9b3bebac 3 | size 125291360 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | django 2 | jieba==0.39 3 | Keras==2.2.4 4 | Keras-Applications==1.0.8 5 | Keras-Preprocessing==1.1.0 6 | numpy==1.17.2 7 | tensorboard==1.14.0 8 | tensorflow==1.14.0 9 | tensorflow-estimator==1.14.0 10 | -------------------------------------------------------------------------------- /sentiment_analysis/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/sentiment_analysis/__init__.py -------------------------------------------------------------------------------- /sentiment_analysis/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/sentiment_analysis/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /sentiment_analysis/__pycache__/settings.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/sentiment_analysis/__pycache__/settings.cpython-37.pyc -------------------------------------------------------------------------------- /sentiment_analysis/__pycache__/tools.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/sentiment_analysis/__pycache__/tools.cpython-37.pyc -------------------------------------------------------------------------------- /sentiment_analysis/__pycache__/urls.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/sentiment_analysis/__pycache__/urls.cpython-37.pyc -------------------------------------------------------------------------------- /sentiment_analysis/__pycache__/views.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/sentiment_analysis/__pycache__/views.cpython-37.pyc -------------------------------------------------------------------------------- /sentiment_analysis/__pycache__/wsgi.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/sentiment_analysis/__pycache__/wsgi.cpython-37.pyc -------------------------------------------------------------------------------- /sentiment_analysis/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for sentiment_analysis project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.2.7. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '#@1#)=@zmc)l^7^*49=6#z7we(9l+77k^vu+&35*$i+4)6-9sz' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = ['*'] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles' 40 | ] 41 | 42 | MIDDLEWARE = [ 43 | 'django.middleware.security.SecurityMiddleware', 44 | 'django.contrib.sessions.middleware.SessionMiddleware', 45 | 'django.middleware.common.CommonMiddleware', 46 | 'django.middleware.csrf.CsrfViewMiddleware', 47 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 48 | 'django.contrib.messages.middleware.MessageMiddleware', 49 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 50 | ] 51 | 52 | ROOT_URLCONF = 'sentiment_analysis.urls' 53 | 54 | TEMPLATES = [ 55 | { 56 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 57 | 'DIRS': [os.path.join(BASE_DIR, 'templates')] 58 | , 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'sentiment_analysis.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/2.2/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_L10N = True 114 | 115 | USE_TZ = True 116 | 117 | 118 | # Static files (CSS, JavaScript, Images) 119 | # https://docs.djangoproject.com/en/2.2/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 123 | -------------------------------------------------------------------------------- /sentiment_analysis/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /sentiment_analysis/tools.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | import re 3 | import jieba 4 | import numpy as np 5 | from keras.engine.saving import load_model 6 | from keras.preprocessing import sequence 7 | import tensorflow as tf 8 | import os 9 | from django.conf import settings 10 | 11 | MAX_SENTENCE_LENGTH = 80 12 | # 加载模型,并使模型和变量在同一图内 13 | global graph 14 | model = load_model(os.path.join(settings.BASE_DIR, 'my_model.h5')) 15 | graph = tf.get_default_graph() 16 | # 加载分词字典 17 | with open(os.path.join(settings.BASE_DIR, 'word_dict.pickle'), 'rb') as handle: 18 | word2index = pickle.load(handle) 19 | 20 | # 数据过滤 21 | def regex_filter(s_line): 22 | # 剔除英文、数字,以及空格 23 | special_regex = re.compile(r"[a-zA-Z0-9\s]+") 24 | # 剔除英文标点符号和特殊符号 25 | en_regex = re.compile(r"[.…{|}#$%&\'()*+,!-_./:~^;<=>?@★●,。]+") 26 | # 剔除中文标点符号 27 | zn_regex = re.compile(r"[《》、,“”;~?!:()【】]+") 28 | 29 | s_line = special_regex.sub(r"", s_line) 30 | s_line = en_regex.sub(r"", s_line) 31 | s_line = zn_regex.sub(r"", s_line) 32 | return s_line 33 | 34 | def predict(sentence): 35 | print(sentence) 36 | xx = np.empty(1, dtype=list) 37 | # 数据预处理 38 | sentence = regex_filter(sentence) 39 | words = jieba.cut(sentence) 40 | seq = [] 41 | for word in words: 42 | if word in word2index: 43 | seq.append(word2index[word]) 44 | else: 45 | seq.append(word2index['UNK']) 46 | xx[0] = seq 47 | xx = sequence.pad_sequences(xx, maxlen=MAX_SENTENCE_LENGTH) 48 | 49 | label2word = {1: '喜好', 2: '悲伤', 3: '厌恶', 4: '愤怒', 5: '高兴', 0: '无情绪'} 50 | with graph.as_default(): 51 | for x in model.predict(xx): 52 | x = x.tolist() 53 | label = x.index(max(x[0], x[1], x[2], x[3], x[4], x[5])) 54 | return label2word[label] -------------------------------------------------------------------------------- /sentiment_analysis/urls.py: -------------------------------------------------------------------------------- 1 | """sentiment_analysis URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path 18 | from sentiment_analysis import views 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('test/', views.test_api, name="test"), 23 | path('index/', views.to_index, name="index"), 24 | path('identify/', views.identify, name="identify") 25 | ] 26 | -------------------------------------------------------------------------------- /sentiment_analysis/views.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from django.http import JsonResponse 4 | from django.views.decorators.csrf import csrf_exempt 5 | from django.shortcuts import render 6 | from sentiment_analysis import tools 7 | 8 | @csrf_exempt 9 | def test_api(request): 10 | data = tools.predict(request.GET.get("sentence")) 11 | return JsonResponse({"code": 200, "msg": data}, json_dumps_params={'ensure_ascii': False}) 12 | 13 | @csrf_exempt 14 | def identify(request): 15 | if request.is_ajax() and request.method == "POST": 16 | sentence = request.POST.get("sentence") 17 | data = tools.predict(sentence) 18 | return JsonResponse({"code": 200, "data": data}, json_dumps_params={'ensure_ascii': False}) 19 | 20 | def to_index(request): 21 | return render(request, 'index.html') -------------------------------------------------------------------------------- /sentiment_analysis/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for sentiment_analysis project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sentiment_analysis.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 情绪识别 6 | 27 | 28 | 29 |
30 |

欢迎使用情绪识别

31 |
32 | 33 |
34 |
35 | 36 |
37 |
38 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /word_dict.pickle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jie12366/sentiment_analysis_api/736219b3a28357876e02e87ad0f2af32d5ae33cc/word_dict.pickle --------------------------------------------------------------------------------