-
Notifications
You must be signed in to change notification settings - Fork 626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplificações de código e paralelização de fetchs de keywords do Watson. #76
Conversation
leodutra
commented
Mar 22, 2019
- Simplifica código e evita que funções sejam recriada a cada chamada de robot()
- Agrupa import de state.js com outros imports
- Move funções para fora de closure, evitando recriação por escopo
- Simplifica avaliação de withoutBlankLinesAndMarkdown
- Melhora regex para substituição de espaços contíguos
- Paraleliza o fetch de keywords do Watson
- Utiliza reject em new Promise ao inves de "throw error"
- Simplifica mapping de keywords
maximumSentences: 7 | ||
maximumSentences: 7, | ||
searchTerm: askAndReturnSearchTerm(), | ||
prefix: askAndReturnPrefix() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Criação direta do objeto, o que cria apenas uma hidden class no motor do V8.
const selectedPrefixText = prefixes[selectedPrefixIndex] | ||
function askAndReturnSearchTerm() { | ||
return readline.question('Type a Wikipedia search term: ') | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funções foram movidas para fora do escopo de robots, assim não é criada a expectativa de que a engine de JS vai sempre otimizá-las e evitar recriação em possíveis futuras chamadas de robot().
Também é mais fácil de entender que a função não requer o mesmo escopo, por não utilizar variáveis definidas na wrapper.
@@ -4,15 +4,14 @@ const sentenceBoundaryDetection = require('sbd') | |||
|
|||
const watsonApiKey = require('../credentials/watson-nlu.json').apikey | |||
const NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js') | |||
|
|||
const state = require('./state.js') | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import agrupado com outros imports no início do código.
@@ -51,7 +51,7 @@ function sanitizeContent(content) { | |||
} | |||
|
|||
function removeDatesInParentheses(text) { | |||
return text.replace(/\((?:\([^()]*\)|[^()])*\)/gm, '').replace(/\s+/g,' ') | |||
return text.replace(/\((?:\([^()]*\)|[^()])*\)/gm, '').replace(/\s{2,}/g,' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essa pequena mudança captura qualquer quantidade de espaços e substitui por apenas 1.
return false | ||
} | ||
const withoutBlankLinesAndMarkdown = allLines.filter( | ||
line => line.trim() && !line.trim().startsWith('=') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avaliação inversa da anterior, ótima porque não precisa avaliar segunda condição do OU e porque não requer uso de dois returns.
content.sentences.map( | ||
async sentence => sentence.keywords = await fetchWatsonAndReturnKeywords(sentence.text) | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prata do PR.
Paraleliza fetch de keywords. O for..of
faz um await de cada vez... o Promise.all
com map()
não.
@@ -88,12 +88,10 @@ async function fetchWatsonAndReturnKeywords(sentence) { | |||
} | |||
}, (error, response) => { | |||
if (error) { | |||
reject(error) | |||
return reject(error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Elimina necessidade de merge do PR #61
const keywords = response.keywords.map((keyword) => { | ||
return keyword.text | ||
}) | ||
const keywords = response.keywords.map(keyword => keyword.text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cheirinho de Johnson & Johnson.