Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Refactor furigana segment data #1502

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/display-templates.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="debug-info"><a class="debug-log-link">Log debug info to console</a></div>
</div></template>
<template id="expression-template" data-remove-whitespace-text="true"><div class="expression">
<div class="expression-text-container" lang="ja">
<div class="expression-text-container">
<span class="expression-text-outer source-text">
<span class="expression-current-indicator"></span>
<span class="expression-text"></span>
Expand Down
48 changes: 46 additions & 2 deletions ext/js/data/anki-note-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
* The public properties and data should be backwards compatible.
*/
class AnkiNoteData {
constructor({
constructor(japaneseUtil, marker, {
definition,
resultOutputMode,
mode,
glossaryLayoutMode,
compactTags,
context,
injectedMedia=null
}, marker) {
}) {
this._japaneseUtil = japaneseUtil;
this._definition = definition;
this._resultOutputMode = resultOutputMode;
this._mode = mode;
Expand All @@ -47,6 +48,7 @@ class AnkiNoteData {
this._uniqueReadings = null;
this._publicContext = null;
this._cloze = null;
this._furiganaSegmentsCache = null;

this._prepareDefinition(definition, injectedMedia, context);
}
Expand Down Expand Up @@ -236,5 +238,47 @@ class AnkiNoteData {
enumerable: true,
get: this._getClozeCached.bind(this)
});

for (const definition2 of this._getAllDefinitions(definition)) {
if (definition2.type === 'term') {
this._defineFuriganaSegments(definition2);
}
for (const expression of definition2.expressions) {
this._defineFuriganaSegments(expression);
}
}
}

_defineFuriganaSegments(object) {
Object.defineProperty(object, 'furiganaSegments', {
configurable: true,
enumerable: true,
get: this._getFuriganaSegments.bind(this, object)
});
}

_getFuriganaSegments(object) {
if (this._furiganaSegmentsCache !== null) {
const cachedResult = this._furiganaSegmentsCache.get(object);
if (typeof cachedResult !== 'undefined') { return cachedResult; }
} else {
this._furiganaSegmentsCache = new Map();
}

const {expression, reading} = object;
const result = this._japaneseUtil.distributeFurigana(expression, reading);
this._furiganaSegmentsCache.set(object, result);
return result;
}

_getAllDefinitions(definition) {
const definitions = [definition];
for (let i = 0; i < definitions.length; ++i) {
const childDefinitions = definitions[i].definitions;
if (Array.isArray(childDefinitions)) {
definitions.push(...childDefinitions);
}
}
return definitions;
}
}
13 changes: 6 additions & 7 deletions ext/js/display/display-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,9 @@ class DisplayGenerator {
disambiguationContainer.dataset[attribute] = value;
}
for (const {expression, reading} of disambiguation) {
const segments = this._japaneseUtil.distributeFurigana(expression, reading);
const disambiguationItem = document.createElement('span');
disambiguationItem.className = 'tag-details-disambiguation';
disambiguationItem.lang = 'ja';
this._appendFurigana(disambiguationItem, segments, (container, text) => {
this._appendFurigana(disambiguationItem, expression, reading, (container, text) => {
container.appendChild(document.createTextNode(text));
});
disambiguationContainer.appendChild(disambiguationItem);
Expand Down Expand Up @@ -232,7 +230,7 @@ class DisplayGenerator {
// Private

_createTermExpression(details) {
const {termFrequency, furiganaSegments, expression, reading, termTags, pitches} = details;
const {termFrequency, expression, reading, termTags, pitches} = details;

const searchQueries = [];
if (expression) { searchQueries.push(expression); }
Expand All @@ -253,7 +251,7 @@ class DisplayGenerator {

this._setTextContent(node.querySelector('.expression-reading'), reading);

this._appendFurigana(expressionContainer, furiganaSegments, this._appendKanjiLinks.bind(this));
this._appendFurigana(expressionContainer, expression, reading, this._appendKanjiLinks.bind(this));
this._appendMultiple(tagContainer, this._createTag.bind(this), termTags);
this._appendMultiple(tagContainer, this._createSearchTag.bind(this), searchQueries);

Expand Down Expand Up @@ -651,7 +649,6 @@ class DisplayGenerator {
}

_appendKanjiLinks(container, text) {
container.lang = 'ja';
const jp = this._japaneseUtil;
let part = '';
for (const c of text) {
Expand Down Expand Up @@ -692,7 +689,9 @@ class DisplayGenerator {
return count;
}

_appendFurigana(container, segments, addText) {
_appendFurigana(container, expression, reading, addText) {
container.lang = 'ja';
const segments = this._japaneseUtil.distributeFurigana(expression, reading);
for (const {text, furigana} of segments) {
if (furigana) {
const ruby = document.createElement('ruby');
Expand Down
21 changes: 7 additions & 14 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,7 @@ class Translator {
this._sortTags(definitionTagsExpanded);
this._sortTags(termTagsExpanded);

const furiganaSegments = this._japaneseUtil.distributeFurigana(expression, reading);
const termDetailsList = [this._createTermDetails(sourceTerm, expression, reading, furiganaSegments, termTagsExpanded)];
const termDetailsList = [this._createTermDetails(sourceTerm, expression, reading, termTagsExpanded)];
const sourceTermExactMatchCount = (sourceTerm === expression ? 1 : 0);

return {
Expand All @@ -1100,7 +1099,6 @@ class Translator {
expression,
reading,
expressions: termDetailsList,
furiganaSegments,
glossary,
definitionTags: definitionTagsExpanded,
termTags: termTagsExpanded,
Expand All @@ -1118,12 +1116,12 @@ class Translator {
* @returns A single 'termGrouped' definition.
*/
_createGroupedTermDefinition(definitions) {
const {reasons, source, rawSource, sourceTerm, expressions: [{expression, reading, furiganaSegments}]} = definitions[0];
const {reasons, source, rawSource, sourceTerm, expressions: [{expression, reading}]} = definitions[0];
const score = this._getMaxDefinitionScore(definitions);
const dictionaryOrder = this._getBestDictionaryOrder(definitions);
const dictionaryNames = this._getUniqueDictionaryNames(definitions);
const termTags = this._getUniqueTermTags(definitions);
const termDetailsList = [this._createTermDetails(sourceTerm, expression, reading, furiganaSegments, termTags)];
const termDetailsList = [this._createTermDetails(sourceTerm, expression, reading, termTags)];
const sourceTermExactMatchCount = (sourceTerm === expression ? 1 : 0);
return {
type: 'termGrouped',
Expand All @@ -1141,7 +1139,6 @@ class Translator {
expression,
reading,
expressions: termDetailsList,
furiganaSegments, // Contains duplicate data
// glossary
// definitionTags
termTags,
Expand Down Expand Up @@ -1173,7 +1170,6 @@ class Translator {
expression: expressions,
reading: readings,
expressions: termDetailsList,
// furiganaSegments
// glossary
// definitionTags
// termTags
Expand Down Expand Up @@ -1221,7 +1217,6 @@ class Translator {
expression: [...expressions],
reading: [...readings],
expressions: termDetailsList,
// furiganaSegments
glossary: [...glossary],
definitionTags,
// termTags
Expand All @@ -1240,7 +1235,7 @@ class Translator {
*/
_createTermDetailsList(definitions) {
const termInfoMap = new Map();
for (const {expression, reading, sourceTerm, furiganaSegments, termTags} of definitions) {
for (const {expression, reading, sourceTerm, termTags} of definitions) {
let readingMap = termInfoMap.get(expression);
if (typeof readingMap === 'undefined') {
readingMap = new Map();
Expand All @@ -1251,7 +1246,6 @@ class Translator {
if (typeof termInfo === 'undefined') {
termInfo = {
sourceTerm,
furiganaSegments,
termTagsMap: new Map()
};
readingMap.set(reading, termInfo);
Expand All @@ -1267,22 +1261,21 @@ class Translator {

const termDetailsList = [];
for (const [expression, readingMap] of termInfoMap.entries()) {
for (const [reading, {termTagsMap, sourceTerm, furiganaSegments}] of readingMap.entries()) {
for (const [reading, {termTagsMap, sourceTerm}] of readingMap.entries()) {
const termTags = [...termTagsMap.values()];
this._sortTags(termTags);
termDetailsList.push(this._createTermDetails(sourceTerm, expression, reading, furiganaSegments, termTags));
termDetailsList.push(this._createTermDetails(sourceTerm, expression, reading, termTags));
}
}
return termDetailsList;
}

_createTermDetails(sourceTerm, expression, reading, furiganaSegments, termTags) {
_createTermDetails(sourceTerm, expression, reading, termTags) {
const termFrequency = this._scoreToTermFrequency(this._getTermTagsScoreSum(termTags));
return {
sourceTerm,
expression,
reading,
furiganaSegments, // Contains duplicate data
termTags,
termFrequency,
frequencies: [],
Expand Down
2 changes: 1 addition & 1 deletion ext/js/templates/template-renderer-frame-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
const japaneseUtil = new JapaneseUtil(null);
const templateRenderer = new TemplateRenderer(japaneseUtil);
templateRenderer.registerDataType('ankiNote', {
modifier: ({data, marker}) => new AnkiNoteData(data, marker).createPublic()
modifier: ({data, marker}) => new AnkiNoteData(japaneseUtil, marker, data).createPublic()
});
const templateRendererFrameApi = new TemplateRendererFrameApi(templateRenderer);
templateRendererFrameApi.prepare();
Expand Down
Loading