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,
expression: firstDef.expression,
reading: firstDef.reading,
furiganaSegments: firstDef.furiganaSegments,
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),
source: firstDef.source
});

View File

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

View File

@ -76,8 +76,12 @@ class DisplayGenerator {
}
if (expressionContainer !== null) {
const segments = [{text: details.expression, furigana: details.reading}]; // TODO : Use proper furigana segmentation
DisplayGenerator._appendFurigana(expressionContainer, segments, this._appendKanjiLinks.bind(this));
let furiganaSegments = details.furiganaSegments;
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);