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

Add support for filtering frequency metadata based on readings #450

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions ext/bg/data/dictionary-term-meta-bank-v3-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,30 @@
{},
{"enum": ["freq"]},
{
"type": ["string", "number"],
"description": "Frequency information for the term or expression."
"oneOf": [
{
"type": ["string", "number"],
"description": "Frequency information for the term or expression."
},
{
"type": ["object"],
"required": [
"reading",
"frequency"
],
"additionalProperties": false,
"properties": {
"reading": {
"type": "string",
"description": "Reading for the term or expression."
},
"frequency": {
"type": ["string", "number"],
"description": "Frequency information for the term or expression."
}
}
}
]
}
]
},
Expand Down
16 changes: 15 additions & 1 deletion ext/bg/js/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ class Translator {
switch (mode) {
case 'freq':
for (const term of termsUnique[index]) {
term.frequencies.push({expression, frequency: data, dictionary});
const frequencyData = this.getFrequencyData(expression, data, dictionary, term);
if (frequencyData === null) { continue; }
term.frequencies.push(frequencyData);
}
break;
case 'pitch':
Expand Down Expand Up @@ -562,6 +564,18 @@ class Translator {
return tagMetaList;
}

getFrequencyData(expression, data, dictionary, term) {
if (data !== null && typeof data === 'object') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use isObject as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I think is fine to leave as-is; the Array.isArray check is not necessary for this case since it should have been validated during import. Additionally, even if somehow the value was an array, it won't be any more wrong than having an incorrectly formatted object.

const {frequency, reading} = data;

const termReading = term.reading || expression;
if (reading !== termReading) { return null; }

return {expression, frequency, dictionary};
}
return {expression, frequency: data, dictionary};
}

async getPitchData(expression, data, dictionary, term) {
const reading = data.reading;
const termReading = term.reading || expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
["打", "freq", 1],
["打つ", "freq", 2],
["打ち込む", "freq", 3],
["打", "freq", {"reading": "だ", "frequency": 4}],
["打", "freq", {"reading": "ダース", "frequency": 5}],
["打つ", "freq", {"reading": "うつ", "frequency": 6}],
["打つ", "freq", {"reading": "ぶつ", "frequency": 7}],
["打ち込む", "freq", {"reading": "うちこむ", "frequency": 8}],
["打ち込む", "freq", {"reading": "ぶちこむ", "frequency": 9}],
[
"打ち込む",
"pitch",
Expand Down
16 changes: 8 additions & 8 deletions test/test-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ async function testDatabase1() {
true
);
vm.assert.deepStrictEqual(counts, {
counts: [{kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 6, tagMeta: 14}],
total: {kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 6, tagMeta: 14}
counts: [{kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 12, tagMeta: 14}],
total: {kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 12, tagMeta: 14}
});

// Test find* functions
Expand Down Expand Up @@ -626,9 +626,9 @@ async function testFindTermMetaBulk1(database, titles) {
}
],
expectedResults: {
total: 1,
total: 3,
modes: [
['freq', 1]
['freq', 3]
]
}
},
Expand All @@ -639,9 +639,9 @@ async function testFindTermMetaBulk1(database, titles) {
}
],
expectedResults: {
total: 1,
total: 3,
modes: [
['freq', 1]
['freq', 3]
]
}
},
Expand All @@ -652,9 +652,9 @@ async function testFindTermMetaBulk1(database, titles) {
}
],
expectedResults: {
total: 3,
total: 5,
modes: [
['freq', 1],
['freq', 3],
['pitch', 2]
]
}
Expand Down