Fix furigana segmentation

This commit is contained in:
toasted-nutbread 2019-12-27 15:08:55 -05:00
parent 56ee7f8df4
commit a50e2fb0f1
3 changed files with 29 additions and 15 deletions

View File

@ -147,8 +147,9 @@ function dictTermsGroup(definitions, dictionaries) {
definitions: groupDefs, definitions: groupDefs,
expression: firstDef.expression, expression: firstDef.expression,
reading: firstDef.reading, reading: firstDef.reading,
furiganaSegments: firstDef.furiganaSegments,
reasons: firstDef.reasons, reasons: firstDef.reasons,
termTags: groupDefs[0].termTags, termTags: firstDef.termTags,
score: groupDefs.reduce((p, v) => v.score > p ? v.score : p, Number.MIN_SAFE_INTEGER), score: groupDefs.reduce((p, v) => v.score > p ? v.score : p, Number.MIN_SAFE_INTEGER),
source: firstDef.source source: firstDef.source
}); });

View File

@ -121,16 +121,10 @@ class Translator {
dictTermsSort(result.definitions, dictionaries); dictTermsSort(result.definitions, dictionaries);
const expressions = []; const expressions = [];
for (const expression of result.expressions.keys()) { for (const [expression, readingMap] of result.expressions.entries()) {
for (const reading of result.expressions.get(expression).keys()) { for (const [reading, termTags] of readingMap.entries()) {
const termTags = result.expressions.get(expression).get(reading);
const score = termTags.map((tag) => tag.score).reduce((p, v) => p + v, 0); const score = termTags.map((tag) => tag.score).reduce((p, v) => p + v, 0);
expressions.push({ expressions.push(Translator.createExpression(expression, reading, dictTagsSort(termTags), Translator.scoreToTermFrequency(score)));
expression: expression,
reading: reading,
termTags: dictTagsSort(termTags),
termFrequency: Translator.scoreToTermFrequency(score)
});
} }
} }
@ -194,7 +188,7 @@ class Translator {
const strayDefinitions = defaultDefinitions.filter((definition, index) => !mergedByTermIndices.has(index)); const strayDefinitions = defaultDefinitions.filter((definition, index) => !mergedByTermIndices.has(index));
for (const groupedDefinition of dictTermsGroup(strayDefinitions, dictionaries)) { for (const groupedDefinition of dictTermsGroup(strayDefinitions, dictionaries)) {
groupedDefinition.expressions = [{expression: groupedDefinition.expression, reading: groupedDefinition.reading}]; groupedDefinition.expressions = [Translator.createExpression(expression, reading)];
definitionsMerged.push(groupedDefinition); definitionsMerged.push(groupedDefinition);
} }
@ -241,14 +235,18 @@ class Translator {
definitionTags.push(dictTagBuildSource(definition.dictionary)); definitionTags.push(dictTagBuildSource(definition.dictionary));
const termTags = await this.expandTags(definition.termTags, definition.dictionary); const termTags = await this.expandTags(definition.termTags, definition.dictionary);
const {expression, reading} = definition;
const furiganaSegments = jpDistributeFurigana(expression, reading);
definitions.push({ definitions.push({
source: deinflection.source, source: deinflection.source,
reasons: deinflection.reasons, reasons: deinflection.reasons,
score: definition.score, score: definition.score,
id: definition.id, id: definition.id,
dictionary: definition.dictionary, dictionary: definition.dictionary,
expression: definition.expression, expression,
reading: definition.reading, reading,
furiganaSegments,
glossary: definition.glossary, glossary: definition.glossary,
definitionTags: dictTagsSort(definitionTags), definitionTags: dictTagsSort(definitionTags),
termTags: dictTagsSort(termTags), termTags: dictTagsSort(termTags),
@ -504,6 +502,17 @@ class Translator {
return tagMetaList; return tagMetaList;
} }
static createExpression(expression, reading, termTags=null, termFrequency=null) {
const furiganaSegments = jpDistributeFurigana(expression, reading);
return {
expression,
reading,
furiganaSegments,
termTags,
termFrequency
};
}
static scoreToTermFrequency(score) { static scoreToTermFrequency(score) {
if (score > 0) { if (score > 0) {
return 'popular'; return 'popular';

View File

@ -76,8 +76,12 @@ class DisplayGenerator {
} }
if (expressionContainer !== null) { if (expressionContainer !== null) {
const segments = [{text: details.expression, furigana: details.reading}]; // TODO : Use proper furigana segmentation let furiganaSegments = details.furiganaSegments;
DisplayGenerator._appendFurigana(expressionContainer, segments, this._appendKanjiLinks.bind(this)); if (!Array.isArray(furiganaSegments)) {
// This case should not occur
furiganaSegments = [{text: details.expression, furigana: details.reading}];
}
DisplayGenerator._appendFurigana(expressionContainer, furiganaSegments, this._appendKanjiLinks.bind(this));
} }
DisplayGenerator._appendMultiple(tagContainer, this.createTag.bind(this), details.termTags); DisplayGenerator._appendMultiple(tagContainer, this.createTag.bind(this), details.termTags);