Skip to content

Commit 7a178ca

Browse files
committed
Add Wikipedia API
1 parent b88cdf8 commit 7a178ca

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const stringSimilarity = require("string-similarity");
77
const { upperCaseFirst } = require("upper-case-first");
88

99
const cors = require("cors");
10+
const axios = require("axios");
1011
const morgan = require("morgan");
1112
const dotenv = require("dotenv");
1213
const express = require("express");
@@ -16,6 +17,7 @@ const mainChat = require("./intents/Main_Chat.json");
1617
const welcomeChat = require("./intents/Default_Welcome.json");
1718
const fallbackChat = require("./intents/Default_Fallback.json");
1819
const unitConverterChat = require("./intents/unit_converter.json");
20+
const wikipediaChat = require("./intents/wikipedia.json");
1921

2022
dotenv.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 = /(convert|change|in).{1,2}(\d{1,8})/gmi;
87+
const regExforWikipedia = /(search for|tell me about|what is|who is)(?!.you) (.{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]) {

intents/support.json

Whitespace-only changes.

intents/unit_converter.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
"change {amount} {unitFrom} to {unitTo}",
44
"can you convert {amount} {unitFrom} to {unitTo}",
55
"convert {amount} {unitFrom} into {unitTo}",
6-
"how many {unitTo} in {amount} {unitFrom}"
6+
"how many {unitTo} in {amount} {unitFrom}",
7+
"how many {unitTo} are in {amount} {unitFrom}",
8+
"what is {amount} {unitFrom} in {unitTo}"
79
]

intents/wikipedia.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
"search for {topic}",
3+
"tell me about {topic}",
4+
"what is {topic}",
5+
"who is {topic}"
6+
]

0 commit comments

Comments
 (0)