Add pitch categories template helper (#1636)

* Move utility function

* Expose dictionary entry as a hidden property for internal use

* Add pitchCategories helper
This commit is contained in:
toasted-nutbread 2021-04-28 21:57:49 -04:00 committed by GitHub
parent 512391346b
commit 40b29cb0d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 28 deletions

View File

@ -53,7 +53,7 @@ class AnkiNoteDataCreator {
const context2 = this.createCachedValue(this._getPublicContext.bind(this, context)); const context2 = this.createCachedValue(this._getPublicContext.bind(this, context));
const pitches = this.createCachedValue(this._getPitches.bind(this, dictionaryEntry)); const pitches = this.createCachedValue(this._getPitches.bind(this, dictionaryEntry));
const pitchCount = this.createCachedValue(this._getPitchCount.bind(this, pitches)); const pitchCount = this.createCachedValue(this._getPitchCount.bind(this, pitches));
return { const result = {
marker, marker,
get definition() { return self.getCachedValue(definition); }, get definition() { return self.getCachedValue(definition); },
glossaryLayoutMode, glossaryLayoutMode,
@ -70,6 +70,13 @@ class AnkiNoteDataCreator {
get pitchCount() { return self.getCachedValue(pitchCount); }, get pitchCount() { return self.getCachedValue(pitchCount); },
get context() { return self.getCachedValue(context2); } get context() { return self.getCachedValue(context2); }
}; };
Object.defineProperty(result, 'dictionaryEntry', {
configurable: false,
enumerable: false,
writable: false,
value: dictionaryEntry
});
return result;
} }
/** /**

View File

@ -768,7 +768,7 @@ class DisplayGenerator {
_getPitchAccentCategories(reading, pronunciations, wordClasses, headwordIndex) { _getPitchAccentCategories(reading, pronunciations, wordClasses, headwordIndex) {
if (pronunciations.length === 0) { return null; } if (pronunciations.length === 0) { return null; }
const isVerbOrAdjective = this._isVerbOrAdjective(wordClasses); const isVerbOrAdjective = DictionaryDataUtil.isNonNounVerbOrAdjective(wordClasses);
const categories = new Set(); const categories = new Set();
for (const pronunciation of pronunciations) { for (const pronunciation of pronunciations) {
if (pronunciation.headwordIndex !== headwordIndex) { continue; } if (pronunciation.headwordIndex !== headwordIndex) { continue; }
@ -781,29 +781,4 @@ class DisplayGenerator {
} }
return categories.size > 0 ? [...categories].join(' ') : null; 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);
}
} }

View File

@ -188,6 +188,31 @@ class DictionaryDataUtil {
return disambiguations; 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 // Private
static _createFrequencyGroupsFromMap(map) { static _createFrequencyGroupsFromMap(map) {

View File

@ -16,6 +16,7 @@
*/ */
/* global /* global
* DictionaryDataUtil
* Handlebars * Handlebars
*/ */
@ -146,7 +147,8 @@ class TemplateRenderer {
['getKanaMorae', this._getKanaMorae.bind(this)], ['getKanaMorae', this._getKanaMorae.bind(this)],
['typeof', this._getTypeof.bind(this)], ['typeof', this._getTypeof.bind(this)],
['join', this._join.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) { for (const [name, helper] of helpers) {
@ -466,4 +468,20 @@ class TemplateRenderer {
} }
return result; 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];
}
} }