Issues (5)

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

1
import json, nltk, os
2
from flask import Flask, jsonify, request
3
from nltk.stem.lancaster import LancasterStemmer
4
from skf import settings
5
from skf.database import db
6
from skf.database.chatbot_log import chatbot_log
7
from skf.api.security import log, val_num, val_alpha_num, val_alpha_num_special
8
from skf.api.chatbot.scripts import intent_classifier
9
from skf.api.chatbot.scripts import entity_classifier1
10
from skf.api.chatbot.scripts import entity_classifier2
11
from skf.api.chatbot.scripts import code_classify
12
13
14
app = Flask(__name__)
15
16
def des_sol(question,intent):
17
        entity=entity_classifier1.entity_recognizer(question.lower())
18
        if entity is None:
19
           entity=entity_classifier2.entity(question)
20
        intent=intent
21
        read_file = open(os.path.join(app.root_path, "datasets/desc_sol.json"), 'r')
22
        data = json.load(read_file)
23
        ite=data['items']
24
        if type(entity)==str:
25
            for d in ite:
26
                 if entity.lower()==d['title'].lower():
27
                      if intent=="Description":
28
                          desc="Description for "+d['title']+" is : "+ d[intent]
29
                          intent="NULL"
30
                          return desc
31
                          break
32
                      else:
33
                          sol="Solution for "+d['title']+" is : "+ d[intent]
34
                          intent="NULL"
35
                          return sol
36
                          break
37
        else:
38
             if len(entity)>0:
39
                for i in entity:
40
                    entity[i]=intent+" "+entity[i]
41
                return entity
42
             else:
43
                log=open(os.path.join(app.root_path,"logs.txt"),"a") 
44
                msg="Please be more specific"
45
                if settings.CHATBOT_LOG == "db":
46
                    result = chatbot_log(question)
47
                    db.session.add(result)
48
                    db.session.commit()
49
                else:
50
                    log=open(os.path.join(app.root_path,"logs.txt"),"a")
51
                    log.write(question+"\n")
52
                    log.close()
53
                return msg
54
55
def code(question,intent,language):
56
        code_entity=code_classify.entity(question)
57
        read_file = open(os.path.join(app.root_path, "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
                       code_a="Code for "+ d['content']+"\n Code language is " + d['code_lang']
75
                       count=count+1
76
                       return code_a
77
            if count==0:
78
                    ent={}
79
                    for i in range(len(code_languages)):
80
                        entity=intent+" "+str(code_entity[0].strip("\n").lower())+" in "+code_languages[i]
81
                        print(entity)
82
                        ent[i]=entity
83
                    return ent
84
                    for d in code_ite:
85
                        if entity==d['title'].lower() and lang in code_languages:
0 ignored issues
show
Comprehensibility Best Practice introduced by Glenn
Undefined variable 'lang'
Loading history...
86
                              if lang==d['code_lang'].lower():
0 ignored issues
show
Comprehensibility Best Practice introduced by Glenn
Undefined variable 'lang'
Loading history...
87
                                 return d['content']
88
                                 count=count+1
89
        else:
90
             if language is None:
91
               language=str(code_entity[-1].strip("\n").lower())
92
             elif language:
93
               language=language
94
             else:
95
                msg="Please be more specific"
96
                if settings.CHATBOT_LOG == "db":
97
                    result = chatbot_log(question)
98
                    db.session.add(result)
99
                    db.session.commit()
100
                else:
101
                    log=open(os.path.join(app.root_path,"logs.txt"),"a")
102
                    log.write(question+"\n")
103
                    log.close()
104
                return msg
105
             code_list={}
106
             for i in code_entity[0]:
107
                 code_list[i]=intent+" "+code_entity[0][i]
108
             return code_list
109