-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathlanguage-processor.js
89 lines (81 loc) · 2.81 KB
/
language-processor.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
var emojis = require('../build/emoji.json');
// English is loaded by default
var enLanguage = require('../languages/en/index');
// Add emojis
Object.assign(enLanguage.labels, emojis);
// Cache loaded languages
var languages = {
en: enLanguage
};
module.exports = {
/**
* Registers the specified language
*
* @param {String} languageCode
* - Two-digit code for the language to register
* @param {Object} language
* - The language module to register
*/
addLanguage: function (languageCode, language) {
if (!language.labels) {
throw new Error('language.labels must be defined!');
}
// Add emojis
Object.assign(language.labels, emojis);
languages[languageCode] = language;
},
/**
* Retrieves a language object from the cache,
* or tries to load it from the set of supported languages
*
* @param {String} languageCode - Two-digit code for the language to fetch
*/
getLanguage: function(languageCode) {
if (!languageCode) {
// Default to english if no language was specified
return languages.en;
}
if (!languages[languageCode]) {
// Try to load specified language
try {
// eslint-disable-next-line max-len
var language = require('../languages/' + languageCode + '/index');
// Add language to in-memory cache
this.addLanguage(languageCode, language);
} catch (err) {
throw new Error('No language found: ' + languageCode);
}
}
return languages[languageCode];
},
/**
* Returns AFINN-165 weighted labels for the specified language
*
* @param {String} languageCode - Two-digit language code
* @return {Object}
*/
getLabels: function(languageCode) {
var language = this.getLanguage(languageCode);
return language.labels;
},
/**
* Applies a scoring strategy for the current token
*
* @param {String} languageCode - Two-digit language code
* @param {Array} tokens - Tokens of the phrase to analyze
* @param {int} cursor - Cursor of the current token being analyzed
* @param {int} tokenScore - The score of the current token being analyzed
*/
applyScoringStrategy: function(languageCode, tokens, cursor, tokenScore) {
var language = this.getLanguage(languageCode);
// Fallback to default strategy if none was specified
// eslint-disable-next-line max-len
var scoringStrategy = language.scoringStrategy || defaultScoringStrategy;
return scoringStrategy.apply(tokens, cursor, tokenScore);
}
};
var defaultScoringStrategy = {
apply: function(tokens, cursor, tokenScore) {
return tokenScore;
}
};