4b. introduce summarizer ; use summarizer for top news reading
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
const constants = {};
|
const constants = {};
|
||||||
|
|
||||||
constants.skillStage = {
|
constants.skillStage = {
|
||||||
IN_DEVELOPMENT : 'development',
|
IN_DEVELOPMENT: 'development',
|
||||||
LIVE : 'live'
|
LIVE: 'live',
|
||||||
}
|
};
|
||||||
|
|
||||||
constants.amazonResultCodes = {
|
constants.amazonResultCodes = {
|
||||||
OK: 200,
|
OK: 200,
|
||||||
@@ -36,12 +36,17 @@ constants.voiceResponseStrings = {
|
|||||||
QUESTION_NOT_FOUND: 'Sorry, I didnt understand',
|
QUESTION_NOT_FOUND: 'Sorry, I didnt understand',
|
||||||
GENERIC_CONTINUE: 'Say something to continue',
|
GENERIC_CONTINUE: 'Say something to continue',
|
||||||
DIDNT_ASK_ANYTHING: 'There was no question to answer to',
|
DIDNT_ASK_ANYTHING: 'There was no question to answer to',
|
||||||
|
ERROR_SUMMARIZING_CONTENT: 'Sorry, there was problem with summarizing news',
|
||||||
|
ERROR_FETCHING_CONTENT: 'Failed to get content',
|
||||||
};
|
};
|
||||||
|
|
||||||
//Timing is given in [ms]
|
//Timing is given in [ms]
|
||||||
constants.voiceResponseTimings = {
|
constants.voiceResponseTimings = {
|
||||||
PAUSE_BETWEEN_QUESTIONS: 650,
|
PAUSE_BETWEEN_QUESTIONS: 650,
|
||||||
PAUSE_AFTER_WELCOME_MESSAGE: 650,
|
PAUSE_AFTER_WELCOME_MESSAGE: 650,
|
||||||
|
PAUSE_BETWEEN_TITLES: 500,
|
||||||
|
PAUSE_BETWEEN_TITLE_AND_CONTENT: 500,
|
||||||
|
PAUSE_BETWEEN_NEWS: 800,
|
||||||
};
|
};
|
||||||
|
|
||||||
constants.stringConstraints = {
|
constants.stringConstraints = {
|
||||||
@@ -65,8 +70,8 @@ constants.stringConstraints = {
|
|||||||
|
|
||||||
constants.answerType = {
|
constants.answerType = {
|
||||||
PREDEFINED: 0,
|
PREDEFINED: 0,
|
||||||
EXTERNAL_SOURCE_WP_JSON : 1,
|
EXTERNAL_SOURCE_WP_TITLES: 1,
|
||||||
EXTERNAL_SOURCE_RSS : 2
|
EXTERNAL_SOURCE_WP_NEWS: 2,
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = constants;
|
module.exports = constants;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
let request = require ('request');
|
let request = require ('request');
|
||||||
let Parser = require ('rss-parser');
|
let Parser = require ('rss-parser');
|
||||||
|
let summarizer = require ('node-summary');
|
||||||
|
const constants = require ('../config/constants');
|
||||||
|
|
||||||
let parser = new Parser ();
|
let parser = new Parser ();
|
||||||
|
|
||||||
@@ -9,44 +11,88 @@ getDataFromRSSFeed = function (url) {
|
|||||||
//feed.items.forEach(item => {
|
//feed.items.forEach(item => {
|
||||||
// console.log(item.title + ':' + item.link)
|
// console.log(item.title + ':' + item.link)
|
||||||
//});
|
//});
|
||||||
}
|
};
|
||||||
|
|
||||||
getDataFromWPJSON = function (sourceUrl, page = 1, maxPosts = 10) {
|
getDataFromWPJSON = function (sourceUrl, page = 1, maxPosts = 10) {
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
var options = {
|
var options = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: `${sourceUrl}/wp-json/wp/v2/posts`,
|
url: `${sourceUrl}/wp-json/wp/v2/posts`,
|
||||||
qs:{
|
qs: {
|
||||||
page:page,
|
page: page,
|
||||||
per_page:maxPosts
|
per_page: maxPosts,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
request (options, (error, response, body)=> {
|
request (options, (error, response, body) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
reject (error);
|
reject (error);
|
||||||
} else {
|
} else {
|
||||||
resolve(JSON.parse (body));
|
resolve (JSON.parse (body));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
|
summarizeText = function (title, text) {
|
||||||
|
return new Promise ((resolve, reject) => {
|
||||||
|
summarizer.summarize (title, text, (err, summary) => {
|
||||||
|
if (err) {
|
||||||
|
reject (err);
|
||||||
|
} else {
|
||||||
|
resolve (summary);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getAnswerFromWP : function (sourceUrl){
|
getAnswerFromWP: function (sourceUrl) {
|
||||||
//This function will extract needed data from JSON, which we got from getDataFromWPJSON
|
//This function will extract needed data from JSON, which we got from getDataFromWPJSON
|
||||||
//At the moment, it's taking titles and creates answer
|
//At the moment, it's taking titles and creates answer
|
||||||
return new Promise((resolve,reject)=>{
|
return new Promise ((resolve, reject) => {
|
||||||
getDataFromWPJSON(sourceUrl).then(rawData=>{
|
getDataFromWPJSON (sourceUrl)
|
||||||
let result='';
|
.then (rawData => {
|
||||||
rawData.forEach(post=>{
|
let result = '';
|
||||||
result += post.title.rendered + '<break time="300ms"/> '
|
rawData.forEach (post => {
|
||||||
|
result += post.title.rendered + `<break time="${constants.voiceResponseTimings.PAUSE_BETWEEN_TITLES}ms"/> `;
|
||||||
|
});
|
||||||
|
resolve (result);
|
||||||
|
})
|
||||||
|
.catch (err => {
|
||||||
|
reject (constants.voiceResponseString.ERROR_FETCHING_CONTENT);
|
||||||
});
|
});
|
||||||
resolve(result);
|
|
||||||
}).catch(err=>{
|
|
||||||
reject('Failed to get answer');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
}
|
|
||||||
|
|
||||||
|
getLatestNewsFromWP: function (sourceUrl, postCount = 10, includeTitle = false) {
|
||||||
|
return new Promise ((resolve, reject) => {
|
||||||
|
getDataFromWPJSON (sourceUrl, 1, postCount)
|
||||||
|
.then (rawData => {
|
||||||
|
let result = '';
|
||||||
|
let summarizers = [];
|
||||||
|
rawData.forEach (post => {
|
||||||
|
summarizers.push (
|
||||||
|
summarizeText (post.title.rendered, post.content.rendered).then(summary=>{
|
||||||
|
result += (includeTitle) ? post.title.rendered : '';
|
||||||
|
result += (includeTitle) ? `<break time="${constants.voiceResponseTimings.PAUSE_BETWEEN_TITLE_AND_CONTENT}ms"/>` : '';
|
||||||
|
result += summary;
|
||||||
|
result += `<break time="${constants.voiceResponseTimings.PAUSE_BETWEEN_NEWS}ms"/>`;
|
||||||
|
}).catch(err=>{
|
||||||
|
result += constants.voiceResponseString.ERROR_SUMMARIZING_CONTENT;
|
||||||
|
result += `<break time="${constants.voiceResponseTimings.PAUSE_BETWEEN_NEWS}ms"/>`;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
Promise.all(summarizers).then(()=>{
|
||||||
|
resolve(result);
|
||||||
|
}).catch(()=>{
|
||||||
|
reject(result);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch (err => {
|
||||||
|
reject (constants.voiceResponseString.ERROR_FETCHING_CONTENT);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -88,16 +88,19 @@ module.exports = {
|
|||||||
case constants.answerType.PREDEFINED:
|
case constants.answerType.PREDEFINED:
|
||||||
answerPromiseProps.resolve(intent.answer);
|
answerPromiseProps.resolve(intent.answer);
|
||||||
break;
|
break;
|
||||||
case constants.answerType.EXTERNAL_SOURCE_WP_JSON:
|
case constants.answerType.EXTERNAL_SOURCE_WP_TITLES:
|
||||||
predefinedSourceHelper.getAnswerFromWP(intent.externalAnswerSource).then(answer=>{
|
predefinedSourceHelper.getAnswerFromWP(intent.externalAnswerSource).then(answer=>{
|
||||||
answerPromiseProps.resolve(answer);
|
answerPromiseProps.resolve(answer);
|
||||||
}).catch(error=>{
|
}).catch(error=>{
|
||||||
answerPromiseProps.reject(error);
|
answerPromiseProps.reject(error);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case constants.answerType.EXTERNAL_SOURCE_RSS:
|
case constants.answerType.EXTERNAL_SOURCE_WP_NEWS:
|
||||||
answer = 'Not implemented yet'
|
predefinedSourceHelper.getLatestNewsFromWP(intent.externalAnswerSource).then(answer=>{
|
||||||
answerPromiseProps.resolve(answer);
|
answerPromiseProps.resolve(answer);
|
||||||
|
}).catch(error=>{
|
||||||
|
answerPromiseProps.reject(error);
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1104
backend/package-lock.json
generated
1104
backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,7 @@
|
|||||||
"express": "^4.13.0",
|
"express": "^4.13.0",
|
||||||
"isomorphic-fetch": "^2.2.1",
|
"isomorphic-fetch": "^2.2.1",
|
||||||
"mongodb": "^2.2.33",
|
"mongodb": "^2.2.33",
|
||||||
|
"node-summary": "^1.2.0",
|
||||||
"nodejs-text-summarizer": "^2.0.3",
|
"nodejs-text-summarizer": "^2.0.3",
|
||||||
"nodemailer": "^4.4.1",
|
"nodemailer": "^4.4.1",
|
||||||
"request": "^2.83.0",
|
"request": "^2.83.0",
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ class AnswerSource extends Component {
|
|||||||
value: '1',
|
value: '1',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: 'RSS feed - latest',
|
label: 'WordPress latest news',
|
||||||
value: '2',
|
value: '2',
|
||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ class IntentDetails extends Component {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case ANSWER_TYPE.EXTERNAL_SOURCE_WP_JSON:
|
case ANSWER_TYPE.EXTERNAL_SOURCE_WP_TITLES:
|
||||||
case ANSWER_TYPE.EXTERNAL_SOURCE_RSS:
|
case ANSWER_TYPE.EXTERNAL_SOURCE_WP_NEWS:
|
||||||
answerBox = (
|
answerBox = (
|
||||||
<div className="QuestionBox">
|
<div className="QuestionBox">
|
||||||
<TextField
|
<TextField
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ export const CONTACT_SELECTED_INDEX = -3;
|
|||||||
|
|
||||||
export const ANSWER_TYPE = {
|
export const ANSWER_TYPE = {
|
||||||
PREDEFINED: 0,
|
PREDEFINED: 0,
|
||||||
EXTERNAL_SOURCE_WP_JSON : 1,
|
EXTERNAL_SOURCE_WP_TITLES : 1,
|
||||||
EXTERNAL_SOURCE_RSS : 2
|
EXTERNAL_SOURCE_WP_NEWS : 2
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RESULT_CODES = {
|
export const RESULT_CODES = {
|
||||||
|
|||||||
Reference in New Issue
Block a user