Skip to content
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

Added LanguageDetector module #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"core-js": "^2.6.5",
"iconv-lite": "^0.4.24",
"jszip": "^3.2.1",
"languagedetect": "^1.2.0",
"sentiment-polish": "^1.0.0",
"vue": "^2.6.6",
"vue-router": "^3.0.1",
Expand Down
30 changes: 30 additions & 0 deletions src/modules/LanguageDetector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @author Michał Kiełtyka
* @type {module:languagedetect}
*/

const LanguageDetect = require('languagedetect');
const lngDetector = new LanguageDetect();

/**
* Takes every message into LanguageDetect().detect(message) which returns languages with confidence scores.
* Then, sums confidences and divide them by number of messages.
*
* Note: Not very reliable way to detect languages but may give some better results when tested on bigger dataset.
*
* @param {string[]} messages Messages to analyze.
* @returns {Object} Object with keys as languages and values as averaged confidence scores (between messages).
*/
export function getAverageConfidenceScoresOfDetectedLanguages(messages) {
let languagesWithConfidencesMap = {};
for (let i = 0; i < messages.length; i++) {
let languagesWithConfidencesArray = lngDetector.detect(messages[i]);
languagesWithConfidencesArray.forEach(function(entry) {
languagesWithConfidencesMap[entry[0]] = (languagesWithConfidencesMap[entry[0]] || 0) + entry[1];
});
}
Object.keys(languagesWithConfidencesMap).forEach(function (key) {
languagesWithConfidencesMap[key] = languagesWithConfidencesMap[key] / messages.length;
});
return languagesWithConfidencesMap;
}