Make search results a bit better
This commit is contained in:
17
Scraper.js
17
Scraper.js
@@ -425,6 +425,7 @@ export default class Scraper {
|
||||
return 'Wait';
|
||||
}
|
||||
|
||||
const { searchWeights } = this.question;
|
||||
const deltas = this.question.lastDeltas;
|
||||
const normalizedWeights = this.normalizeWeights();
|
||||
let suggestion;
|
||||
@@ -472,7 +473,12 @@ export default class Scraper {
|
||||
let splitChoices = normalizedWeights.filter(weight => weight >= 20);
|
||||
if (splitChoices.length === 3 || splitChoices.length === 0) {
|
||||
this.question.suggestedPicks = [true, true, true];
|
||||
return 'Split 1 2 3';
|
||||
suggestion = 'Split 1 2 3';
|
||||
if (searchWeights) {
|
||||
const searchPick = searchWeights.indexOf(Math.max(...searchWeights));
|
||||
suggestion += `\t\tLikely ${searchPick + 1}`;
|
||||
}
|
||||
return suggestion;
|
||||
}
|
||||
|
||||
splitChoices = normalizedWeights.map((weight, index) => {
|
||||
@@ -488,14 +494,23 @@ export default class Scraper {
|
||||
}
|
||||
|
||||
this.question.suggestedPicks = [false, false, false];
|
||||
let suggestionSearchWeights = [];
|
||||
|
||||
splitChoices.forEach(choiceIndex => {
|
||||
if (choiceIndex !== null) {
|
||||
suggestionSearchWeights.push(searchWeights[choiceIndex]);
|
||||
suggestion += ` ${choiceIndex + 1}`;
|
||||
this.question.suggestedPicks[choiceIndex] = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (suggestion.toLowerCase().includes('split')) {
|
||||
if (searchWeights) {
|
||||
const searchPick = searchWeights.indexOf(Math.max(...suggestionSearchWeights));
|
||||
suggestion += `\t\tLikely ${searchPick + 1}`;
|
||||
}
|
||||
}
|
||||
|
||||
return suggestion;
|
||||
}
|
||||
|
||||
|
||||
15
SearchTest.js
Normal file
15
SearchTest.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import Searcher from './Searcher';
|
||||
|
||||
const search = new Searcher();
|
||||
const questionObj = {
|
||||
question: 'What is the question?',
|
||||
answers: [
|
||||
{ text: 'Yes' },
|
||||
{ text: 'No' },
|
||||
{ text: 'Maybe' },
|
||||
],
|
||||
};
|
||||
search.search(questionObj, (answer) => {
|
||||
const suggestedPick = answer.indexOf(Math.max(...answer));
|
||||
console.log(questionObj.answers[suggestedPick].text);
|
||||
});
|
||||
46
Searcher.js
46
Searcher.js
@@ -29,9 +29,15 @@ export default class Searcher {
|
||||
let answer1 = 0;
|
||||
let answer2 = 0;
|
||||
let answer3 = 0;
|
||||
|
||||
let shouldBeNegated = false;
|
||||
|
||||
if (this.containsNegation(text)) {
|
||||
shouldBeNegated = true;
|
||||
}
|
||||
|
||||
const options = {
|
||||
q: `${text} "${choice1}"|"${choice2}"|"${choice3}"`,
|
||||
q: `${this.cleanText(text)} "${choice1}"|"${choice2}"|"${choice3}"`,
|
||||
apiKey: GOOGLE_SETTINGS.API_KEY,
|
||||
cx: GOOGLE_SETTINGS.CX,
|
||||
};
|
||||
@@ -79,8 +85,7 @@ export default class Searcher {
|
||||
})
|
||||
});
|
||||
|
||||
const textLower = text.replace('\'', '');
|
||||
if (textLower.includes('NOT') || textLower.includes('never') || textLower.includes('isnt') || textLower.includes('didnt')) {
|
||||
if (shouldBeNegated) {
|
||||
const totalAnswers = answer1 + answer2 + answer3;
|
||||
answer1 = totalAnswers - answer1;
|
||||
answer2 = totalAnswers - answer2;
|
||||
@@ -92,4 +97,39 @@ export default class Searcher {
|
||||
|
||||
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',
|
||||
'never',
|
||||
'not',
|
||||
'rejected', // experimental
|
||||
'shortest', // experimental
|
||||
'smallest', // experimental
|
||||
'wont',
|
||||
];
|
||||
|
||||
return negationWords.some(word => cleaned.includes(word));
|
||||
}
|
||||
|
||||
cleanText(text) {
|
||||
// TODO
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "babel-node index.js --presets env,es2016,stage-2"
|
||||
"start": "babel-node index.js --presets env,es2016,stage-2",
|
||||
"testSearch": "babel-node SearchTest.js --presets env,es2016,stage-2"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
Reference in New Issue
Block a user