Completed
Push — master ( 67441f...bffef9 )
by Glenn
28s
created

skf/api/chatbot/business.py (6 issues)

Severity
1
import json, nltk, os
0 ignored issues
show
The import nltk seems to be unused.
Loading history...
2
from flask import Flask
3
from nltk.stem.lancaster import LancasterStemmer
0 ignored issues
show
Unused LancasterStemmer imported from nltk.stem.lancaster
Loading history...
4
from skf.api.security import log, val_num, val_alpha_num, val_alpha_num_special
0 ignored issues
show
Unused val_num imported from skf.api.security
Loading history...
Unused val_alpha_num_special imported from skf.api.security
Loading history...
Unused log imported from skf.api.security
Loading history...
Unused val_alpha_num imported from skf.api.security
Loading history...
5
from skf.api.chatbot.scripts import intent_classifier
6
from skf.api.chatbot.scripts import entity_classifier1
7
from skf.api.chatbot.scripts import entity_classifier2
8
from skf.api.chatbot.scripts import code_classify
9
10
app = Flask(__name__)
11
12
def answer(question):
13
        intent=intent_classifier.predict(question)
14
        if intent=="Description" or intent=="Solution":
15
                   des_sol(question,intent)
16
        else:
17
                   lang=None
18
                   code(question,intent,lang)
19
20
21
def des_sol(question,intent):
22
        entity=entity_classifier1.entity_recognizer(question.lower())
23
        if entity is None:
24
           entity=entity_classifier2.entity(question)
25
      
26
        read_file = open(os.path.join(app.root_path, "api/chatbot/datasets/desc_sol.json"), 'r')
27
        data = json.load(read_file)
28
        ite=data['items']
29
        if type(entity)==str:
30
            for d in ite:
31
                 if entity.lower()==d['title'].lower():
32
                      if intent=="Description":
33
                          print("Description for "+d['title']+" is : "+ d[intent])
34
                          intent="NULL"
35
                          break
36
                      else:
37
                          print("Solution for "+d['title']+" is : "+ d[intent])
38
                          intent="NULL"
39
                          break
40
        else:
41
             if len(entity)>0:
42
                print("Please select from these options ")
43
                for i in entity:
44
                    print(str(i)+":"+entity[i])
45
                #TO_BE_CHANGEDn=int(input("enter your choice "))
46
                #n=int(n)
47
                question=entity[n]
48
                des_sol(question,intent)
49
             else:
50
                print("Please be more specific ")
51
                question=input("enter your question again ")
52
                answer(question)
53
54
55
def code(question,intent,language):
56
        code_entity=code_classify.entity(question)
57
        read_file = open(os.path.join(app.root_path, "api/chatbot/datasets/code_data.json"), 'r') 
58
        code_data = json.load(read_file)
59
        code_ite=code_data['items']
60
        code_languages=[]
61
        count=0
62
        if len(code_entity)==2 and type(code_entity[0])==str:
63
            entity=str(code_entity[0].strip("\n").lower())
64
            if language is None:
65
               language=str(code_entity[-1].strip("\n").lower())
66
            else:
67
               language=language
68
            for d in code_ite:
69
                 if entity==d['title'].lower():
70
                    code_languages.append(d['code_lang'])
71
            for d in code_ite:
72
                 if entity==d['title'].lower() and language in code_languages:
73
                    if language==d['code_lang'].lower():
74
                       print("Code for "+ d['content'])
75
                       print("\n Code language is " + d['code_lang'])
76
                       count=count+1
77
            if count==0:
78
                    code_l={}
79
                    entity=str(code_entity[0].strip("\n").lower())
80
                    for i in range(len(code_languages)):
81
                        code_l[i+1]=code_languages[i]      
82
                    print("The language you typed is not availabe. Select from the following:")
83
                    for i in code_l:
84
                        print(str(i)+":"+code_l[i])
85
                    #TO_BE_CHANGEDn=int(input("Enter your choice: "))
86
                    lang=code_l[n]
87
                    for d in code_ite:
88
                        if entity==d['title'].lower() and lang in code_languages:
89
                              if lang==d['code_lang'].lower():
90
                                 print("Code for "+ d['content'])
91
                                 print("\n Code language is " + d['code_lang'])
92
                                 count=count+1
93
            #TO_BE_CHANGEDques=input("\n Do you have more Questions Y/N ")
94
            if(ques=="y" or ques=="Y"):
95
                   #TO_BE_CHANGEDquestion=input("Enter new question ")
96
                   answer(question)
97
            else:
98
                   print("Thanks for using")
99
        else:
100
             if language is None:
101
               language=str(code_entity[-1].strip("\n").lower())
102
             else:
103
               language=language
104
             print("Please select from these options ")
105
             for i in code_entity[0]:
106
                 print(str(i)+":"+code_entity[0][i])
107
             #TO_BE_CHANGEDn=int(input("enter your choice "))
108
             question=code_entity[0][n]
109
             code(question,"Code",language)
110
#TO_BE_CHANGEDquestion=input("Enter Question ")
111
#TO_BE_CHANGEDanswer(question)
112
113