Skip to content

Commit

Permalink
feat(api): modularise st #9
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomrebelo committed Jan 26, 2023
1 parent 825afe7 commit ebc2302
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ APP.get("/lines/:delimiter/:lang/:input/", async (req, res) => {
const text = req.params.input;
const sentences = text.split(delimiter);
const lang = req.params.lang;
const results = await analysis(text, lang, sentences);
const results = await analysis(sentences, lang);
res.status(results[0]).send(JSON.stringify(results[1]));
});

APP.get("/text/:lang/:input", async (req, res) => {
const text = req.params.input;
const lang = req.params.lang;
const results = await analysis(text, lang);
const sentences = (await _sentenceTokenizer(text)).flat();
const results = await analysis(sentences, lang);
res.status(results[0]).send(JSON.stringify(results[1]));
});

Expand All @@ -48,6 +49,10 @@ const errHandler = (code, msg) => {
}
}

const _sentenceTokenizer = async (text) => {
return sentenceTokeniser(text);
}

const _lexiconGlobalResults = async (sentences) => {

// compute global lexicon value
Expand All @@ -69,14 +74,12 @@ const _lexiconGlobalResults = async (sentences) => {
return res.length === 0 ? [['neutral', 1]] : res;
}

const analysis = async (text, lang, sentences = []) => {
const analysis = async (sentences = [], lang) => {
const text = sentences.flat().join(' ');
// classification analysis
const classificationResults = await classification(text, lang);
if (!classificationResults.success) return [400, errHandler(400, `Error in the classification method`)];

// sentence tokenizer (if necessary)
if (sentences.length === 0) sentences = (await sentenceTokeniser(text)).flat();

// lexicon-based analysis
let lexiconResults = { "global": null, "sentences": [] };
for (const sentence of sentences) {
Expand Down

0 comments on commit ebc2302

Please sign in to comment.