-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.js
117 lines (90 loc) · 3.45 KB
/
text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const algorithmia = require('algorithmia')
const algorithmiaApiKey = require('../credentials/algorithmia.json').apiKey
const sentenceBoundaryDetection = require('sbd')
const watsonApiKey = require('../credentials/watson-nlu.json').apikey
const NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js')
const nlu = new NaturalLanguageUnderstandingV1({
iam_apikey: watsonApiKey,
version: '2018-04-05',
url: 'https://gateway.watsonplatform.net/natural-language-understanding/api/'
})
const state = require('./state.js')
async function robot() {
console.log('> [text-robot] Starting...')
const content = state.load()
await fetchContentFromWikipedia(content)
sanitizeContent(content)
breakContentIntoSentences(content)
limitMaximumSentences(content)
await fetchKeywordsOfAllSentences(content)
state.save(content)
async function fetchContentFromWikipedia(content) {
console.log('> [text-robot] Fetching content from Wikipedia')
const algorithmiaAuthenticated = algorithmia(algorithmiaApiKey)
const wikipediaAlgorithm = algorithmiaAuthenticated.algo('web/WikipediaParser/0.1.2')
const wikipediaResponse = await wikipediaAlgorithm.pipe(content.searchTerm)
const wikipediaContent = wikipediaResponse.get()
content.sourceContentOriginal = wikipediaContent.content
console.log('> [text-robot] Fetching done!')
}
function sanitizeContent(content) {
const withoutBlankLinesAndMarkdown = removeBlankLinesAndMarkdown(content.sourceContentOriginal)
const withoutDatesInParentheses = removeDatesInParentheses(withoutBlankLinesAndMarkdown)
content.sourceContentSanitized = withoutDatesInParentheses
function removeBlankLinesAndMarkdown(text) {
const allLines = text.split('\n')
const withoutBlankLinesAndMarkdown = allLines.filter((line) => {
if (line.trim().length === 0 || line.trim().startsWith('=')) {
return false
}
return true
})
return withoutBlankLinesAndMarkdown.join(' ')
}
}
function removeDatesInParentheses(text) {
return text.replace(/\((?:\([^()]*\)|[^()])*\)/gm, '').replace(/ /g,' ')
}
function breakContentIntoSentences(content) {
content.sentences = []
const sentences = sentenceBoundaryDetection.sentences(content.sourceContentSanitized)
sentences.forEach((sentence) => {
content.sentences.push({
text: sentence,
keywords: [],
images: []
})
})
}
function limitMaximumSentences(content) {
content.sentences = content.sentences.slice(0, content.maximumSentences)
}
async function fetchKeywordsOfAllSentences(content) {
console.log('> [text-robot] Starting to fetch keywords from Watson')
for (const sentence of content.sentences) {
console.log(`> [text-robot] Sentence: "${sentence.text}"`)
sentence.keywords = await fetchWatsonAndReturnKeywords(sentence.text)
console.log(`> [text-robot] Keywords: ${sentence.keywords.join(', ')}\n`)
}
}
async function fetchWatsonAndReturnKeywords(sentence) {
return new Promise((resolve, reject) => {
nlu.analyze({
text: sentence,
features: {
keywords: {}
}
}, (error, response) => {
if (error) {
reject(error)
return
}
const keywords = response.keywords.map((keyword) => {
return keyword.text
})
resolve(keywords)
})
})
}
}
module.exports = robot