Files
tasJS/Searcher.js
2018-07-13 19:31:07 -04:00

137 lines
3.4 KiB
JavaScript

'use strict';
const { google } = require('googleapis');
const customsearch = google.customsearch('v1');
import { LOG_TYPES } from './constants';
import { GOOGLE_SETTINGS, TEST_MODE } from './config';
export default class Searcher {
constructor(logger) {
this.logger = logger;
}
async search(question, callback) {
const { answers, question: theQuestion } = question;
const questionPackage = {
text: theQuestion,
answers: {
choice1: answers[0].text,
choice2: answers[1].text,
choice3: answers[2].text,
},
};
const { text } = questionPackage;
const { choice1, choice2, choice3 } = questionPackage.answers;
let answer1 = 0;
let answer2 = 0;
let answer3 = 0;
let shouldBeNegated = false;
if (this.containsNegation(text)) {
shouldBeNegated = true;
}
const options = {
q: `${this.cleanText(text)} "${choice1}"|"${choice2}"|"${choice3}"`,
apiKey: GOOGLE_SETTINGS.API_KEY,
cx: GOOGLE_SETTINGS.CX,
};
const checkMatch = (match, choice) => {
return match === choice.toLowerCase()
|| choice.toLowerCase().includes(match)
|| match.includes(choice.toLowerCase());
}
async function getSearchResults(options) {
const res = await customsearch.cse.list({
cx: options.cx,
q: options.q,
auth: options.apiKey
});
const { items = [] } = res.data || {};
items.forEach(item => {
const { htmlTitle, htmlSnippet } = item;
let title = [];
let snippet = [];
const titleMatches = htmlTitle.match(/<b>(.*?)<\/b>/g);
const snippetMatches = htmlSnippet.match(/<b>(.*?)<\/b>/g);
if (titleMatches) {
title = titleMatches.map(val => {
return val.replace(/<\/?b>/g,'').toLowerCase();
});
}
if (snippetMatches) {
snippet = snippetMatches.map(val => {
return val.replace(/<\/?b>/g,'').toLowerCase();
});
}
const matches = [...title, ...snippet];
matches.forEach(match => {
if (checkMatch(match, choice1)) answer1++;
if (checkMatch(match, choice2)) answer2++;
if (checkMatch(match, choice3)) answer3++;
})
});
if (shouldBeNegated) {
const totalAnswers = answer1 + answer2 + answer3;
answer1 = totalAnswers - answer1;
answer2 = totalAnswers - answer2;
answer3 = totalAnswers - answer3;
}
callback([answer1, answer2, answer3]);
};
getSearchResults(options).catch(() => { callback(false); });
}
containsNegation(text) {
const removeQuotedText = (str) => {
const re = /"(.*?)"/g;
const result = [];
let current;
while (current = re.exec(str)) {
str.replace(current.pop(), '');
}
return str;
}
const stripQuotes = removeQuotedText(text);
const cleaned = stripQuotes.replace('\'', '').toLowerCase();
const negationWords = [
'fewest', // experimental
'furthest', // experimental
'hasnt',
'isnt',
'least',
'never',
'not',
'rejected', // experimental
'shortest', // experimental
'smallest', // experimental
'wont',
];
return negationWords.some(word => cleaned.includes(word));
}
cleanText(text) {
// TODO
return text;
}
}