diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-03-07 20:38:40 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-07 20:38:40 -0500 |
commit | 643dbfb12a66b98b2fabe82416322f0218474567 (patch) | |
tree | 9746ca8eadd4c8a28d1b72c5ddcc7632feaab9d2 /ext | |
parent | 19f6bf5a3a669cda311e5a3f162131d8a9823aef (diff) |
Refactor term frequency data (#1503)
* Add getTermFrequency
* Update DisplayGenerator to use getTermFrequency
* Remove termFrequency from Translator data
* Backwards compatibility
* Update tests
Diffstat (limited to 'ext')
-rw-r--r-- | ext/js/data/anki-note-data.js | 14 | ||||
-rw-r--r-- | ext/js/display/display-generator.js | 4 | ||||
-rw-r--r-- | ext/js/language/dictionary-data-util.js | 14 | ||||
-rw-r--r-- | ext/js/language/translator.js | 2 |
4 files changed, 30 insertions, 4 deletions
diff --git a/ext/js/data/anki-note-data.js b/ext/js/data/anki-note-data.js index 529bad18..a8a82a79 100644 --- a/ext/js/data/anki-note-data.js +++ b/ext/js/data/anki-note-data.js @@ -245,6 +245,7 @@ class AnkiNoteData { } for (const expression of definition2.expressions) { this._defineFuriganaSegments(expression); + this._defineTermFrequency(expression); } } } @@ -257,6 +258,14 @@ class AnkiNoteData { }); } + _defineTermFrequency(object) { + Object.defineProperty(object, 'termFrequency', { + configurable: true, + enumerable: true, + get: this._getTermFrequency.bind(this, object) + }); + } + _getFuriganaSegments(object) { if (this._furiganaSegmentsCache !== null) { const cachedResult = this._furiganaSegmentsCache.get(object); @@ -271,6 +280,11 @@ class AnkiNoteData { return result; } + _getTermFrequency(object) { + const {termTags} = object; + return DictionaryDataUtil.getTermFrequency(termTags); + } + _getAllDefinitions(definition) { const definitions = [definition]; for (let i = 0; i < definitions.length; ++i) { diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index c0945781..3977815b 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -230,7 +230,7 @@ class DisplayGenerator { // Private _createTermExpression(details) { - const {termFrequency, expression, reading, termTags, pitches} = details; + const {expression, reading, termTags, pitches} = details; const searchQueries = []; if (expression) { searchQueries.push(expression); } @@ -242,7 +242,7 @@ class DisplayGenerator { const tagContainer = node.querySelector('.expression-tag-list'); node.dataset.readingIsSame = `${reading === expression}`; - node.dataset.frequency = termFrequency; + node.dataset.frequency = DictionaryDataUtil.getTermFrequency(termTags); const pitchAccentCategories = this._getPitchAccentCategories(pitches); if (pitchAccentCategories !== null) { diff --git a/ext/js/language/dictionary-data-util.js b/ext/js/language/dictionary-data-util.js index b3a354a7..dff9d212 100644 --- a/ext/js/language/dictionary-data-util.js +++ b/ext/js/language/dictionary-data-util.js @@ -143,6 +143,20 @@ class DictionaryDataUtil { return results2; } + static getTermFrequency(termTags) { + let totalScore = 0; + for (const {score} of termTags) { + totalScore += score; + } + if (totalScore > 0) { + return 'popular'; + } else if (totalScore < 0) { + return 'rare'; + } else { + return 'normal'; + } + } + // Private static _createFrequencyGroupsFromMap(map) { diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js index 5aa8ee9c..e8aba5b1 100644 --- a/ext/js/language/translator.js +++ b/ext/js/language/translator.js @@ -1271,13 +1271,11 @@ class Translator { } _createTermDetails(sourceTerm, expression, reading, termTags) { - const termFrequency = this._scoreToTermFrequency(this._getTermTagsScoreSum(termTags)); return { sourceTerm, expression, reading, termTags, - termFrequency, frequencies: [], pitches: [] }; |