diff options
| author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-04-28 21:57:49 -0400 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-28 21:57:49 -0400 | 
| commit | 40b29cb0d3ff90094c44dd4cb7abd087a0fd9598 (patch) | |
| tree | 0723d8cf5c61ffe091a150b9c9a1d2281f7d72a0 | |
| parent | 512391346bd4f67b1933b768c64d4dac1b58b44d (diff) | |
Add pitch categories template helper (#1636)
* Move utility function
* Expose dictionary entry as a hidden property for internal use
* Add pitchCategories helper
| -rw-r--r-- | ext/js/data/anki-note-data-creator.js | 9 | ||||
| -rw-r--r-- | ext/js/display/display-generator.js | 27 | ||||
| -rw-r--r-- | ext/js/language/dictionary-data-util.js | 25 | ||||
| -rw-r--r-- | ext/js/templates/template-renderer.js | 20 | 
4 files changed, 53 insertions, 28 deletions
| diff --git a/ext/js/data/anki-note-data-creator.js b/ext/js/data/anki-note-data-creator.js index f4d6ab49..3046a92e 100644 --- a/ext/js/data/anki-note-data-creator.js +++ b/ext/js/data/anki-note-data-creator.js @@ -53,7 +53,7 @@ class AnkiNoteDataCreator {          const context2 = this.createCachedValue(this._getPublicContext.bind(this, context));          const pitches = this.createCachedValue(this._getPitches.bind(this, dictionaryEntry));          const pitchCount = this.createCachedValue(this._getPitchCount.bind(this, pitches)); -        return { +        const result = {              marker,              get definition() { return self.getCachedValue(definition); },              glossaryLayoutMode, @@ -70,6 +70,13 @@ class AnkiNoteDataCreator {              get pitchCount() { return self.getCachedValue(pitchCount); },              get context() { return self.getCachedValue(context2); }          }; +        Object.defineProperty(result, 'dictionaryEntry', { +            configurable: false, +            enumerable: false, +            writable: false, +            value: dictionaryEntry +        }); +        return result;      }      /** diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js index 53a004c1..51950a3e 100644 --- a/ext/js/display/display-generator.js +++ b/ext/js/display/display-generator.js @@ -768,7 +768,7 @@ class DisplayGenerator {      _getPitchAccentCategories(reading, pronunciations, wordClasses, headwordIndex) {          if (pronunciations.length === 0) { return null; } -        const isVerbOrAdjective = this._isVerbOrAdjective(wordClasses); +        const isVerbOrAdjective = DictionaryDataUtil.isNonNounVerbOrAdjective(wordClasses);          const categories = new Set();          for (const pronunciation of pronunciations) {              if (pronunciation.headwordIndex !== headwordIndex) { continue; } @@ -781,29 +781,4 @@ class DisplayGenerator {          }          return categories.size > 0 ? [...categories].join(' ') : null;      } - -    _isVerbOrAdjective(wordClasses) { -        let isVerbOrAdjective = false; -        let isSuruVerb = false; -        let isNoun = false; -        for (const wordClass of wordClasses) { -            switch (wordClass) { -                case 'v1': -                case 'v5': -                case 'vk': -                case 'vz': -                case 'adj-i': -                    isVerbOrAdjective = true; -                    break; -                case 'vs': -                    isVerbOrAdjective = true; -                    isSuruVerb = true; -                    break; -                case 'n': -                    isNoun = true; -                    break; -            } -        } -        return isVerbOrAdjective && !(isSuruVerb && isNoun); -    }  } diff --git a/ext/js/language/dictionary-data-util.js b/ext/js/language/dictionary-data-util.js index 0a3be234..81d1c290 100644 --- a/ext/js/language/dictionary-data-util.js +++ b/ext/js/language/dictionary-data-util.js @@ -188,6 +188,31 @@ class DictionaryDataUtil {          return disambiguations;      } +    static isNonNounVerbOrAdjective(wordClasses) { +        let isVerbOrAdjective = false; +        let isSuruVerb = false; +        let isNoun = false; +        for (const wordClass of wordClasses) { +            switch (wordClass) { +                case 'v1': +                case 'v5': +                case 'vk': +                case 'vz': +                case 'adj-i': +                    isVerbOrAdjective = true; +                    break; +                case 'vs': +                    isVerbOrAdjective = true; +                    isSuruVerb = true; +                    break; +                case 'n': +                    isNoun = true; +                    break; +            } +        } +        return isVerbOrAdjective && !(isSuruVerb && isNoun); +    } +      // Private      static _createFrequencyGroupsFromMap(map) { diff --git a/ext/js/templates/template-renderer.js b/ext/js/templates/template-renderer.js index 9bb7a414..07b2849c 100644 --- a/ext/js/templates/template-renderer.js +++ b/ext/js/templates/template-renderer.js @@ -16,6 +16,7 @@   */  /* global + * DictionaryDataUtil   * Handlebars   */ @@ -146,7 +147,8 @@ class TemplateRenderer {              ['getKanaMorae',     this._getKanaMorae.bind(this)],              ['typeof',           this._getTypeof.bind(this)],              ['join',             this._join.bind(this)], -            ['concat',           this._concat.bind(this)] +            ['concat',           this._concat.bind(this)], +            ['pitchCategories',  this._pitchCategories.bind(this)]          ];          for (const [name, helper] of helpers) { @@ -466,4 +468,20 @@ class TemplateRenderer {          }          return result;      } + +    _pitchCategories(context, data) { +        const {pronunciations, headwords} = data.dictionaryEntry; +        const categories = new Set(); +        for (const {headwordIndex, pitches} of pronunciations) { +            const {reading, wordClasses} = headwords[headwordIndex]; +            const isVerbOrAdjective = DictionaryDataUtil.isNonNounVerbOrAdjective(wordClasses); +            for (const {position} of pitches) { +                const category = this._japaneseUtil.getPitchCategory(reading, position, isVerbOrAdjective); +                if (category !== null) { +                    categories.add(category); +                } +            } +        } +        return [...categories]; +    }  } |