2020-07-31 00:45:52 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2020-2021 Yomichan Authors
|
2020-07-31 00:45:52 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DictionaryDataUtil {
|
2021-03-25 23:55:31 +00:00
|
|
|
static groupTermTags(dictionaryEntry) {
|
|
|
|
const {headwords} = dictionaryEntry;
|
|
|
|
const headwordCount = headwords.length;
|
|
|
|
const uniqueCheck = (headwordCount > 1);
|
|
|
|
const resultsIndexMap = new Map();
|
2021-01-09 21:02:03 +00:00
|
|
|
const results = [];
|
2021-03-25 23:55:31 +00:00
|
|
|
for (let i = 0; i < headwordCount; ++i) {
|
|
|
|
const {tags} = headwords[i];
|
|
|
|
for (const tag of tags) {
|
2021-01-09 21:02:03 +00:00
|
|
|
if (uniqueCheck) {
|
|
|
|
const {name, category, notes, dictionary} = tag;
|
|
|
|
const key = this._createMapKey([name, category, notes, dictionary]);
|
2021-03-25 23:55:31 +00:00
|
|
|
const index = resultsIndexMap.get(key);
|
2021-01-09 21:02:03 +00:00
|
|
|
if (typeof index !== 'undefined') {
|
|
|
|
const existingItem = results[index];
|
2021-03-25 23:55:31 +00:00
|
|
|
existingItem.headwordIndices.push(i);
|
2021-01-09 21:02:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
resultsIndexMap.set(key, results.length);
|
2021-01-09 21:02:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const item = {tag, headwordIndices: [i]};
|
2021-01-09 21:02:03 +00:00
|
|
|
results.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
static groupTermFrequencies(dictionaryEntry) {
|
|
|
|
const {headwords, frequencies} = dictionaryEntry;
|
|
|
|
|
2021-01-09 15:41:17 +00:00
|
|
|
const map1 = new Map();
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {headwordIndex, dictionary, hasReading, frequency} of frequencies) {
|
|
|
|
const {term, reading} = headwords[headwordIndex];
|
|
|
|
|
2021-01-09 15:41:17 +00:00
|
|
|
let map2 = map1.get(dictionary);
|
|
|
|
if (typeof map2 === 'undefined') {
|
|
|
|
map2 = new Map();
|
|
|
|
map1.set(dictionary, map2);
|
|
|
|
}
|
|
|
|
|
|
|
|
const readingKey = hasReading ? reading : null;
|
2021-03-25 23:55:31 +00:00
|
|
|
const key = this._createMapKey([term, readingKey]);
|
2021-01-09 15:41:17 +00:00
|
|
|
let frequencyData = map2.get(key);
|
|
|
|
if (typeof frequencyData === 'undefined') {
|
2021-03-25 23:55:31 +00:00
|
|
|
frequencyData = {term, reading: readingKey, values: new Set()};
|
2021-01-09 15:41:17 +00:00
|
|
|
map2.set(key, frequencyData);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
frequencyData.values.add(frequency);
|
2021-01-09 15:41:17 +00:00
|
|
|
}
|
|
|
|
return this._createFrequencyGroupsFromMap(map1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static groupKanjiFrequencies(frequencies) {
|
|
|
|
const map1 = new Map();
|
|
|
|
for (const {dictionary, character, frequency} of frequencies) {
|
|
|
|
let map2 = map1.get(dictionary);
|
|
|
|
if (typeof map2 === 'undefined') {
|
|
|
|
map2 = new Map();
|
|
|
|
map1.set(dictionary, map2);
|
|
|
|
}
|
|
|
|
|
|
|
|
let frequencyData = map2.get(character);
|
|
|
|
if (typeof frequencyData === 'undefined') {
|
2021-03-25 23:55:31 +00:00
|
|
|
frequencyData = {character, values: new Set()};
|
2021-01-09 15:41:17 +00:00
|
|
|
map2.set(character, frequencyData);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
frequencyData.values.add(frequency);
|
2021-01-09 15:41:17 +00:00
|
|
|
}
|
|
|
|
return this._createFrequencyGroupsFromMap(map1);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
static getPitchAccentInfos(dictionaryEntry) {
|
|
|
|
const {headwords, pronunciations} = dictionaryEntry;
|
2020-08-22 17:10:50 +00:00
|
|
|
|
2020-07-31 00:45:52 +00:00
|
|
|
const allExpressions = new Set();
|
|
|
|
const allReadings = new Set();
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const {term, reading} of headwords) {
|
|
|
|
allExpressions.add(term);
|
2020-12-06 02:16:24 +00:00
|
|
|
allReadings.add(reading);
|
2021-03-25 23:55:31 +00:00
|
|
|
}
|
2020-07-31 00:45:52 +00:00
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
const pitchAccentInfoMap = new Map();
|
|
|
|
for (const {headwordIndex, dictionary, pitches} of pronunciations) {
|
|
|
|
const {term, reading} = headwords[headwordIndex];
|
|
|
|
let dictionaryPitchAccentInfoList = pitchAccentInfoMap.get(dictionary);
|
|
|
|
if (typeof dictionaryPitchAccentInfoList === 'undefined') {
|
|
|
|
dictionaryPitchAccentInfoList = [];
|
|
|
|
pitchAccentInfoMap.set(dictionary, dictionaryPitchAccentInfoList);
|
|
|
|
}
|
|
|
|
for (const {position, tags} of pitches) {
|
|
|
|
let pitchAccentInfo = this._findExistingPitchAccentInfo(reading, position, tags, dictionaryPitchAccentInfoList);
|
|
|
|
if (pitchAccentInfo === null) {
|
|
|
|
pitchAccentInfo = {
|
|
|
|
terms: new Set(),
|
|
|
|
reading,
|
|
|
|
position,
|
|
|
|
tags,
|
|
|
|
exclusiveTerms: [],
|
|
|
|
exclusiveReadings: []
|
|
|
|
};
|
|
|
|
dictionaryPitchAccentInfoList.push(pitchAccentInfo);
|
2020-07-31 00:45:52 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
pitchAccentInfo.terms.add(term);
|
2020-07-31 00:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const multipleReadings = (allReadings.size > 1);
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const dictionaryPitchAccentInfoList of pitchAccentInfoMap.values()) {
|
|
|
|
for (const pitchAccentInfo of dictionaryPitchAccentInfoList) {
|
|
|
|
const {terms, reading, exclusiveTerms, exclusiveReadings} = pitchAccentInfo;
|
|
|
|
if (!this._areSetsEqual(terms, allExpressions)) {
|
|
|
|
exclusiveTerms.push(...this._getSetIntersection(terms, allExpressions));
|
2020-07-31 00:45:52 +00:00
|
|
|
}
|
|
|
|
if (multipleReadings) {
|
2021-03-25 23:55:31 +00:00
|
|
|
exclusiveReadings.push(reading);
|
2020-07-31 00:45:52 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
pitchAccentInfo.terms = [...terms];
|
2020-07-31 00:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const results2 = [];
|
2021-03-25 23:55:31 +00:00
|
|
|
for (const [dictionary, pitches] of pitchAccentInfoMap.entries()) {
|
2020-07-31 00:45:52 +00:00
|
|
|
results2.push({dictionary, pitches});
|
|
|
|
}
|
|
|
|
return results2;
|
|
|
|
}
|
|
|
|
|
2021-03-08 01:38:40 +00:00
|
|
|
static getTermFrequency(termTags) {
|
|
|
|
let totalScore = 0;
|
|
|
|
for (const {score} of termTags) {
|
|
|
|
totalScore += score;
|
|
|
|
}
|
|
|
|
if (totalScore > 0) {
|
|
|
|
return 'popular';
|
|
|
|
} else if (totalScore < 0) {
|
|
|
|
return 'rare';
|
|
|
|
} else {
|
|
|
|
return 'normal';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:55:31 +00:00
|
|
|
static getDisambiguations(headwords, headwordIndices, allTermsSet, allReadingsSet) {
|
|
|
|
if (allTermsSet.size <= 1 && allReadingsSet.size <= 1) { return []; }
|
|
|
|
|
|
|
|
const terms = new Set();
|
|
|
|
const readings = new Set();
|
|
|
|
for (const headwordIndex of headwordIndices) {
|
|
|
|
const {term, reading} = headwords[headwordIndex];
|
|
|
|
terms.add(term);
|
|
|
|
readings.add(reading);
|
|
|
|
}
|
|
|
|
|
|
|
|
const disambiguations = [];
|
2021-03-28 20:04:55 +00:00
|
|
|
const addTerms = !this._areSetsEqual(terms, allTermsSet);
|
|
|
|
const addReadings = !this._areSetsEqual(readings, allReadingsSet);
|
|
|
|
if (addTerms) {
|
|
|
|
disambiguations.push(...this._getSetIntersection(terms, allTermsSet));
|
|
|
|
}
|
|
|
|
if (addReadings) {
|
|
|
|
if (addTerms) {
|
|
|
|
for (const term of terms) {
|
|
|
|
readings.delete(term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disambiguations.push(...this._getSetIntersection(readings, allReadingsSet));
|
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
return disambiguations;
|
|
|
|
}
|
|
|
|
|
2021-01-08 01:24:22 +00:00
|
|
|
// Private
|
|
|
|
|
2021-01-09 15:41:17 +00:00
|
|
|
static _createFrequencyGroupsFromMap(map) {
|
|
|
|
const results = [];
|
|
|
|
for (const [dictionary, map2] of map.entries()) {
|
2021-03-25 23:55:31 +00:00
|
|
|
const frequencies = [];
|
2021-01-09 15:41:17 +00:00
|
|
|
for (const frequencyData of map2.values()) {
|
2021-03-25 23:55:31 +00:00
|
|
|
frequencyData.values = [...frequencyData.values];
|
|
|
|
frequencies.push(frequencyData);
|
2021-01-09 15:41:17 +00:00
|
|
|
}
|
2021-03-25 23:55:31 +00:00
|
|
|
results.push({dictionary, frequencies});
|
2021-01-09 15:41:17 +00:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-07-31 00:45:52 +00:00
|
|
|
static _findExistingPitchAccentInfo(reading, position, tags, pitchAccentInfoList) {
|
|
|
|
for (const pitchInfo of pitchAccentInfoList) {
|
|
|
|
if (
|
|
|
|
pitchInfo.reading === reading &&
|
|
|
|
pitchInfo.position === position &&
|
|
|
|
this._areTagListsEqual(pitchInfo.tags, tags)
|
|
|
|
) {
|
|
|
|
return pitchInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _areTagListsEqual(tagList1, tagList2) {
|
|
|
|
const ii = tagList1.length;
|
|
|
|
if (tagList2.length !== ii) { return false; }
|
|
|
|
|
|
|
|
for (let i = 0; i < ii; ++i) {
|
|
|
|
const tag1 = tagList1[i];
|
|
|
|
const tag2 = tagList2[i];
|
|
|
|
if (tag1.name !== tag2.name || tag1.dictionary !== tag2.dictionary) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-08 01:24:22 +00:00
|
|
|
|
|
|
|
static _areSetsEqual(set1, set2) {
|
|
|
|
if (set1.size !== set2.size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const value of set1) {
|
|
|
|
if (!set2.has(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _getSetIntersection(set1, set2) {
|
|
|
|
const result = [];
|
|
|
|
for (const value of set1) {
|
|
|
|
if (set2.has(value)) {
|
|
|
|
result.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-09 21:02:03 +00:00
|
|
|
|
|
|
|
static _createMapKey(array) {
|
|
|
|
return JSON.stringify(array);
|
|
|
|
}
|
2020-07-31 00:45:52 +00:00
|
|
|
}
|