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

Commit 858fe7a

Browse files
String frequency support (#1989)
* Restore support for string frequency values * Add support for {value, displayValue} frequencies * Update test data * Improve number parsing of string frequencies * Improve reading detection * Expose a displayValue property for frequency information * Update docs * Expose displayValue to Anki note data * Fix translator * Update display generation * Update test data * Update counts
1 parent f6c3461 commit 858fe7a

14 files changed

+13601
-843
lines changed

docs/interfaces/dictionary-entry.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ namespace Translation {
181181
/**
182182
* The frequency for the character, as a number of occurrences or an overall rank.
183183
*/
184-
frequency: number | string;
184+
frequency: number;
185+
/**
186+
* A display value to show to the user.
187+
*/
188+
displayValue: string | null;
189+
/**
190+
* Whether or not the displayValue string was parsed to determine the frequency value.
191+
*/
192+
displayValueParsed: boolean;
185193
}
186194

187195
// Terms
@@ -400,7 +408,15 @@ namespace Translation {
400408
/**
401409
* The frequency for the term, as a number of occurrences or an overall rank.
402410
*/
403-
frequency: number | string;
411+
frequency: number;
412+
/**
413+
* A display value to show to the user.
414+
*/
415+
displayValue: string | null;
416+
/**
417+
* Whether or not the displayValue string was parsed to determine the frequency value.
418+
*/
419+
displayValueParsed: boolean;
404420
}
405421

406422
/**

ext/data/schemas/dictionary-kanji-meta-bank-v3-schema.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"definitions": {
4+
"frequency": {
5+
"oneOf": [
6+
{
7+
"type": ["string", "number"]
8+
},
9+
{
10+
"type": "object",
11+
"additionalProperties": false,
12+
"required": [
13+
"value"
14+
],
15+
"properties": {
16+
"value": {
17+
"type": "number"
18+
},
19+
"displayValue": {
20+
"type": "string"
21+
}
22+
}
23+
}
24+
]
25+
}
26+
},
327
"type": "array",
428
"description": "Custom metadata for kanji characters.",
529
"additionalItems": {
@@ -17,7 +41,7 @@
1741
"description": "Type of data. \"freq\" corresponds to frequency information."
1842
},
1943
{
20-
"type": ["number"],
44+
"$ref": "#/definitions/frequency",
2145
"description": "Data for the character."
2246
}
2347
]

ext/data/schemas/dictionary-term-meta-bank-v3-schema.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"definitions": {
4+
"frequency": {
5+
"oneOf": [
6+
{
7+
"type": ["string", "number"]
8+
},
9+
{
10+
"type": "object",
11+
"additionalProperties": false,
12+
"required": [
13+
"value"
14+
],
15+
"properties": {
16+
"value": {
17+
"type": "number"
18+
},
19+
"displayValue": {
20+
"type": "string"
21+
}
22+
}
23+
}
24+
]
25+
}
26+
},
327
"type": "array",
428
"description": "Custom metadata for terms.",
529
"additionalItems": {
@@ -28,7 +52,7 @@
2852
{
2953
"oneOf": [
3054
{
31-
"type": ["number"],
55+
"$ref": "#/definitions/frequency",
3256
"description": "Frequency information for the term."
3357
},
3458
{
@@ -44,7 +68,7 @@
4468
"description": "Reading for the term."
4569
},
4670
"frequency": {
47-
"type": ["number"],
71+
"$ref": "#/definitions/frequency",
4872
"description": "Frequency information for the term."
4973
}
5074
}

ext/display-templates.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@
7171
<rt class="frequency-disambiguation-reading"></rt>
7272
</ruby></span>
7373
<span class="frequency-separator"></span>
74-
<span class="frequency-value"></span>
74+
<span class="frequency-value-list"></span>
7575
</span></span>
7676
</span></span></template>
7777
<template id="kanji-frequency-item-template" data-remove-whitespace-text="true"><span class="frequency-item"><span class="tag tag-has-body frequency-tag" data-category="frequency" data-frequency-type="kanji">
7878
<span class="tag-label"><span class="tag-label-content"></span></span>
7979
<span class="tag-body"><span class="tag-body-content frequency-body">
80-
<span class="frequency-value"></span>
80+
<span class="frequency-value-list"></span>
8181
</span></span>
8282
</span></span></template>
8383

ext/js/data/sandbox/anki-note-data-creator.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class AnkiNoteDataCreator {
243243

244244
_getKanjiFrequencies(dictionaryEntry) {
245245
const results = [];
246-
for (const {index, dictionary, dictionaryIndex, dictionaryPriority, character, frequency} of dictionaryEntry.frequencies) {
246+
for (const {index, dictionary, dictionaryIndex, dictionaryPriority, character, frequency, displayValue, displayValueParsed} of dictionaryEntry.frequencies) {
247247
results.push({
248248
index,
249249
dictionary,
@@ -252,7 +252,7 @@ class AnkiNoteDataCreator {
252252
priority: dictionaryPriority
253253
},
254254
character,
255-
frequency
255+
frequency: displayValueParsed ? displayValue : frequency
256256
});
257257
}
258258
return results;
@@ -374,7 +374,7 @@ class AnkiNoteDataCreator {
374374
_getTermFrequencies(dictionaryEntry) {
375375
const results = [];
376376
const {headwords} = dictionaryEntry;
377-
for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency} of dictionaryEntry.frequencies) {
377+
for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency, displayValue, displayValueParsed} of dictionaryEntry.frequencies) {
378378
const {term, reading} = headwords[headwordIndex];
379379
results.push({
380380
index: results.length,
@@ -387,7 +387,7 @@ class AnkiNoteDataCreator {
387387
expression: term,
388388
reading,
389389
hasReading,
390-
frequency
390+
frequency: displayValueParsed ? displayValue : frequency
391391
});
392392
}
393393
return results;
@@ -459,7 +459,7 @@ class AnkiNoteDataCreator {
459459
_getTermExpressionFrequencies(dictionaryEntry, i) {
460460
const results = [];
461461
const {headwords, frequencies} = dictionaryEntry;
462-
for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency} of frequencies) {
462+
for (const {headwordIndex, dictionary, dictionaryIndex, dictionaryPriority, hasReading, frequency, displayValue, displayValueParsed} of frequencies) {
463463
if (headwordIndex !== i) { continue; }
464464
const {term, reading} = headwords[headwordIndex];
465465
results.push({
@@ -473,7 +473,7 @@ class AnkiNoteDataCreator {
473473
expression: term,
474474
reading,
475475
hasReading,
476-
frequency
476+
frequency: displayValueParsed ? displayValue : frequency
477477
});
478478
}
479479
return results;

ext/js/display/display-generator.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -548,18 +548,15 @@ class DisplayGenerator {
548548

549549
this._setTextContent(node.querySelector('.tag-label-content'), dictionary);
550550

551-
const frequency = values.join(', ');
552-
553551
this._setTextContent(node.querySelector('.frequency-disambiguation-term'), term, 'ja');
554552
this._setTextContent(node.querySelector('.frequency-disambiguation-reading'), (reading !== null ? reading : ''), 'ja');
555-
this._setTextContent(node.querySelector('.frequency-value'), frequency, 'ja');
553+
this._populateFrequencyValueList(node.querySelector('.frequency-value-list'), values);
556554

557555
node.dataset.term = term;
558556
node.dataset.reading = reading;
559557
node.dataset.hasReading = `${reading !== null}`;
560558
node.dataset.readingIsSame = `${reading === term}`;
561559
node.dataset.dictionary = dictionary;
562-
node.dataset.frequency = `${frequency}`;
563560
node.dataset.details = dictionary;
564561

565562
return node;
@@ -569,19 +566,50 @@ class DisplayGenerator {
569566
const {character, values} = details;
570567
const node = this._templates.instantiate('kanji-frequency-item');
571568

572-
const frequency = values.join(', ');
573-
574569
this._setTextContent(node.querySelector('.tag-label-content'), dictionary);
575-
this._setTextContent(node.querySelector('.frequency-value'), frequency, 'ja');
570+
this._populateFrequencyValueList(node.querySelector('.frequency-value-list'), values);
576571

577572
node.dataset.character = character;
578573
node.dataset.dictionary = dictionary;
579-
node.dataset.frequency = `${frequency}`;
580574
node.dataset.details = dictionary;
581575

582576
return node;
583577
}
584578

579+
_populateFrequencyValueList(node, values) {
580+
let fullFrequency = '';
581+
for (let i = 0, ii = values.length; i < ii; ++i) {
582+
const {frequency, displayValue} = values[i];
583+
const frequencyString = `${frequency}`;
584+
const text = displayValue !== null ? displayValue : frequency;
585+
586+
if (i > 0) {
587+
const node2 = document.createElement('span');
588+
node2.className = 'frequency-value';
589+
node2.dataset.frequency = `${frequency}`;
590+
node2.textContent = ', ';
591+
node.appendChild(node2);
592+
fullFrequency += ', ';
593+
}
594+
595+
const node2 = document.createElement('span');
596+
node2.className = 'frequency-value';
597+
node2.dataset.frequency = frequencyString;
598+
if (displayValue !== null) {
599+
node2.dataset.displayValue = `${displayValue}`;
600+
if (displayValue !== frequencyString) {
601+
node2.title = frequencyString;
602+
}
603+
}
604+
this._setTextContent(node2, text, 'ja');
605+
node.appendChild(node2);
606+
607+
fullFrequency += text;
608+
}
609+
610+
node.dataset.frequency = fullFrequency;
611+
}
612+
585613
_appendKanjiLinks(container, text) {
586614
const jp = this._japaneseUtil;
587615
let part = '';

ext/js/language/sandbox/dictionary-data-util.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class DictionaryDataUtil {
4848
const {headwords, frequencies} = dictionaryEntry;
4949

5050
const map1 = new Map();
51-
for (const {headwordIndex, dictionary, hasReading, frequency} of frequencies) {
51+
for (const {headwordIndex, dictionary, hasReading, frequency, displayValue} of frequencies) {
5252
const {term, reading} = headwords[headwordIndex];
5353

5454
let map2 = map1.get(dictionary);
@@ -61,18 +61,18 @@ class DictionaryDataUtil {
6161
const key = this._createMapKey([term, readingKey]);
6262
let frequencyData = map2.get(key);
6363
if (typeof frequencyData === 'undefined') {
64-
frequencyData = {term, reading: readingKey, values: new Set()};
64+
frequencyData = {term, reading: readingKey, values: new Map()};
6565
map2.set(key, frequencyData);
6666
}
6767

68-
frequencyData.values.add(frequency);
68+
frequencyData.values.set(this._createMapKey([frequency, displayValue]), {frequency, displayValue});
6969
}
7070
return this._createFrequencyGroupsFromMap(map1);
7171
}
7272

7373
static groupKanjiFrequencies(frequencies) {
7474
const map1 = new Map();
75-
for (const {dictionary, character, frequency} of frequencies) {
75+
for (const {dictionary, character, frequency, displayValue} of frequencies) {
7676
let map2 = map1.get(dictionary);
7777
if (typeof map2 === 'undefined') {
7878
map2 = new Map();
@@ -81,11 +81,11 @@ class DictionaryDataUtil {
8181

8282
let frequencyData = map2.get(character);
8383
if (typeof frequencyData === 'undefined') {
84-
frequencyData = {character, values: new Set()};
84+
frequencyData = {character, values: new Map()};
8585
map2.set(character, frequencyData);
8686
}
8787

88-
frequencyData.values.add(frequency);
88+
frequencyData.values.set(this._createMapKey([frequency, displayValue]), {frequency, displayValue});
8989
}
9090
return this._createFrequencyGroupsFromMap(map1);
9191
}
@@ -222,7 +222,7 @@ class DictionaryDataUtil {
222222
for (const [dictionary, map2] of map.entries()) {
223223
const frequencies = [];
224224
for (const frequencyData of map2.values()) {
225-
frequencyData.values = [...frequencyData.values];
225+
frequencyData.values = [...frequencyData.values.values()];
226226
frequencies.push(frequencyData);
227227
}
228228
results.push({dictionary, frequencies});

0 commit comments

Comments
 (0)