1+ const MainChat = require ( "./intents/Main_Chat.json" ) ;
2+ const WelcomeChat = require ( "./intents/Default_Welcome.json" ) ;
3+ const FallbackChat = require ( "./intents/Default_Fallback.json" ) ;
4+ const unitConverterChat = require ( "./intents/unit_converter.json" ) . qus ;
5+
6+ const stringSimilarity = require ( "string-similarity" ) ;
7+ const _ = require ( 'lodash' ) ;
8+ const { upperCaseFirst } = require ( "upper-case-first" ) ;
9+ const extractValues = require ( "extract-values" ) ;
10+
11+ const cors = require ( "cors" ) ;
12+ const express = require ( "express" ) ;
13+ const compression = require ( "compression" ) ;
14+ const rateLimit = require ( "express-rate-limit" ) ;
15+ const morgan = require ( "morgan" ) ;
16+
17+ const app = express ( ) ;
18+ const port = process . env . PORT || 3000 ;
19+
20+ var allQustions = [ ] ;
21+ var answer = { } ;
22+ var humanInput = "how are you" ;
23+
24+ for ( let i = 0 ; i < MainChat . length ; i ++ ) {
25+ for ( let j = 0 ; j < MainChat [ i ] [ "qus" ] . length ; j ++ ) {
26+ allQustions . push ( MainChat [ i ] [ "qus" ] [ j ] )
27+ }
28+ }
29+
30+ const sendAllQuestions = ( req , res ) => {
31+ var humanQuestions = [ ] ;
32+
33+ allQustions . forEach ( qus => {
34+ if ( qus . length >= 10 ) {
35+ if ( / ^ ( c a n | a r e | m a y | h o w | w h a t | w h e n | w h o | d o | w h e r e | y o u r | f r o m | i s | w i l l | w h y ) / gi. test ( qus ) ) {
36+ humanQuestions . push ( upperCaseFirst ( qus ) + "?" ) ;
37+ } else {
38+ humanQuestions . push ( upperCaseFirst ( qus ) + '.' ) ;
39+ }
40+ }
41+ } ) ;
42+ res . json ( _ . shuffle ( humanQuestions ) )
43+ }
44+
45+ const sendWelcomeMessage = ( req , res ) => {
46+ res . json ( { responseText : _ . sample ( WelcomeChat ) } )
47+ }
48+
49+ const sendAnswer = ( req , res ) => {
50+
51+ var isFallback = false ;
52+ var responseText = 'n/a' ;
53+ var rating = 0 ;
54+ var similarQuestion = 'n/a' ;
55+ var action ;
56+
57+ let query = req . query . q ;
58+ let humanInput = query . replace ( / ( \? | \. ) $ / gm, "v" ) ;
59+ let regExforUnitConverter = / ( c o n v e r t | c h a n g e ) .{ 1 , 2 } ( \d { 1 , 8 } ) / gm;
60+
61+ if ( regExforUnitConverter . test ( humanInput ) ) {
62+
63+ let similarQuestionObj = stringSimilarity . findBestMatch ( humanInput , unitConverterChat ) . bestMatch ;
64+ let valuesObj = extractValues ( humanInput , similarQuestionObj . target , { delimiters : [ "%" , "%" ] } ) ;
65+
66+ const { amount, unit_from, unit_to } = valuesObj ;
67+ console . log ( amount , unit_from , unit_to ) ;
68+
69+ } else {
70+
71+ let similarQuestionObj = stringSimilarity . findBestMatch ( humanInput , allQustions ) . bestMatch ;
72+ let similarQuestionRating = similarQuestionObj . rating ;
73+ similarQuestion = similarQuestionObj . target ;
74+
75+ if ( similarQuestionRating > 0.5 ) {
76+ for ( let i = 0 ; i < MainChat . length ; i ++ ) {
77+ for ( let j = 0 ; j < MainChat [ i ] [ "qus" ] . length ; j ++ ) {
78+ if ( similarQuestion == MainChat [ i ] [ "qus" ] [ j ] ) {
79+ responseText = _ . sample ( MainChat [ i ] [ "ans" ] ) ;
80+ rating = similarQuestionRating ;
81+ }
82+ }
83+ }
84+ } else {
85+ responseText = _ . sample ( FallbackChat ) ;
86+ isFallback = true ;
87+ }
88+ }
89+
90+ res . json ( {
91+ responseText,
92+ rating,
93+ similarQuestion,
94+ isFallback
95+ } ) ;
96+ }
97+
98+ app . use ( cors ( ) ) ;
99+ app . use ( compression ( ) ) ;
100+ app . use ( "/api/*" , morgan ( "tiny" ) ) ;
101+ app . get ( "/api/allQuestions" , sendAllQuestions ) ;
102+ app . get ( "/api/welcome" , sendWelcomeMessage ) ;
103+ app . get ( "/api/question" , sendAnswer ) ;
104+
105+ app . listen ( port , ( ) => console . log ( `app listening on port ${ port } !` ) ) ;
0 commit comments