@@ -7,6 +7,7 @@ const stringSimilarity = require("string-similarity");
77const { upperCaseFirst } = require ( "upper-case-first" ) ;
88
99const cors = require ( "cors" ) ;
10+ const axios = require ( "axios" ) ;
1011const morgan = require ( "morgan" ) ;
1112const dotenv = require ( "dotenv" ) ;
1213const express = require ( "express" ) ;
@@ -16,6 +17,7 @@ const mainChat = require("./intents/Main_Chat.json");
1617const welcomeChat = require ( "./intents/Default_Welcome.json" ) ;
1718const fallbackChat = require ( "./intents/Default_Fallback.json" ) ;
1819const unitConverterChat = require ( "./intents/unit_converter.json" ) ;
20+ const wikipediaChat = require ( "./intents/wikipedia.json" ) ;
1921
2022dotenv . config ( ) ;
2123
@@ -71,7 +73,7 @@ const sendWelcomeMessage = (req, res) => {
7173 } ) ;
7274} ;
7375
74- const sendAnswer = ( req , res ) => {
76+ const sendAnswer = async ( req , res ) => {
7577 let isFallback = false ;
7678 let responseText = null ;
7779 let rating = 0 ;
@@ -82,6 +84,7 @@ const sendAnswer = (req, res) => {
8284 const query = decodeURIComponent ( req . query . q ) . replace ( / \s + / g, " " ) . trim ( ) || "Hello" ;
8385 const humanInput = lowerCase ( query . replace ( / ( \? | \. | ! ) $ / gmi, "" ) ) ;
8486 const regExforUnitConverter = / ( c o n v e r t | c h a n g e | i n ) .{ 1 , 2 } ( \d { 1 , 8 } ) / gmi;
87+ const regExforWikipedia = / ( s e a r c h f o r | t e l l m e a b o u t | w h a t i s | w h o i s ) (? ! .y o u ) ( .{ 1 , 30 } ) / gmi;
8588
8689 if ( regExforUnitConverter . test ( humanInput ) ) {
8790 const similarQuestionObj = stringSimilarity . findBestMatch ( humanInput , unitConverterChat ) . bestMatch ;
@@ -105,13 +108,32 @@ const sendAnswer = (req, res) => {
105108 responseText = "One or more unit missing." ;
106109 console . log ( error ) ;
107110 }
111+ } else if ( regExforWikipedia . test ( humanInput ) ) {
112+ const similarQuestionObj = stringSimilarity . findBestMatch ( humanInput , wikipediaChat ) . bestMatch ;
113+ similarQuestion = similarQuestionObj . target ;
114+ const valuesObj = extractValues ( humanInput , similarQuestion , {
115+ delimiters : [ "{" , "}" ] ,
116+ } ) ;
117+
118+ const { topic } = valuesObj ;
119+
120+ const wikipediaResponse = ( await axios ( `https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=1&explaintext=1&titles=${ topic } ` ) ) . data ;
121+ console . log ( wikipediaResponse ) ;
122+ const wikipediaResponsePageNo = Object . keys ( wikipediaResponse . query . pages ) [ 0 ] ;
123+ const wikipediaResponseText = wikipediaResponse . query . pages [ wikipediaResponsePageNo ] . extract ;
124+
125+ responseText = wikipediaResponseText ;
126+ if ( wikipediaResponseText == undefined || wikipediaResponseText == "" ) {
127+ responseText = "Sorry, we can't find any article with this exact name." ;
128+ isFallback = true ;
129+ }
108130 } else {
109131 const similarQuestionObj = stringSimilarity . findBestMatch ( humanInput , allQustions ) . bestMatch ;
110132 const similarQuestionRating = similarQuestionObj . rating ;
111133 similarQuestion = similarQuestionObj . target ;
112134 action = "main_chat" ;
113135
114- if ( similarQuestionRating > 0.5 ) {
136+ if ( similarQuestionRating > 0.6 ) {
115137 for ( let i = 0 ; i < mainChat . length ; i ++ ) {
116138 for ( let j = 0 ; j < mainChat [ i ] . questions . length ; j ++ ) {
117139 if ( similarQuestion == mainChat [ i ] . questions [ j ] ) {
0 commit comments