Skip to content

Commit

Permalink
use Map in areArraysEqualIgnoreOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Jan 9, 2024
1 parent a8b48fa commit f159b39
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
8 changes: 4 additions & 4 deletions ext/js/dictionary/dictionary-data-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ export class DictionaryDataUtil {
const pitchAccent2 = /** @type {import('dictionary').PitchAccent} */ (pronunciation2);
return (
pronunciation1.position === pitchAccent2.position &&
this.areArraysEqual(pronunciation1.nasalPositions, pitchAccent2.nasalPositions) &&
this.areArraysEqual(pronunciation1.devoicePositions, pitchAccent2.devoicePositions)
this._areArraysEqual(pronunciation1.nasalPositions, pitchAccent2.nasalPositions) &&
this._areArraysEqual(pronunciation1.devoicePositions, pitchAccent2.devoicePositions)
);
}
case 'phonetic-transcription':
Expand All @@ -351,7 +351,7 @@ export class DictionaryDataUtil {
* @param {T[]} array2
* @returns {boolean}
*/
static areArraysEqual(array1, array2) {
static _areArraysEqual(array1, array2) {
const ii = array1.length;
if (ii !== array2.length) { return false; }
for (let i = 0; i < ii; ++i) {
Expand All @@ -372,7 +372,7 @@ export class DictionaryDataUtil {
for (let i = 0; i < ii; ++i) {
const tag1 = tagList1[i];
const tag2 = tagList2[i];
if (tag1.name !== tag2.name || !this.areArraysEqual(tag1.dictionaries, tag2.dictionaries)) {
if (tag1.name !== tag2.name || !this._areArraysEqual(tag1.dictionaries, tag2.dictionaries)) {
return false;
}
}
Expand Down
35 changes: 29 additions & 6 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {DictionaryDataUtil} from '../dictionary/dictionary-data-util.js';
import {RegexUtil} from '../general/regex-util.js';
import {TextSourceMap} from '../general/text-source-map.js';
import {Deinflector} from './deinflector.js';
Expand Down Expand Up @@ -249,18 +248,14 @@ export class Translator {
}

/**
*
* @param {import('dictionary').TermDictionaryEntry} existingEntry
* @param {import('dictionary').InflectionHypothesis[]} inflectionHypotheses
*/
_mergeInflectionHypotheses(existingEntry, inflectionHypotheses) {
const existingHypotheses = existingEntry.inflectionHypotheses;

for (const {source, inflections} of inflectionHypotheses) {
const duplicate = existingHypotheses.find((hypothesis) => DictionaryDataUtil.areArraysEqual(
[...hypothesis.inflections].sort(),
[...inflections].sort()
));
const duplicate = existingHypotheses.find((hypothesis) => this._areArraysEqualIgnoreOrder(hypothesis.inflections, inflections));
if (!duplicate) {
existingEntry.inflectionHypotheses.push({source, inflections});
} else if (duplicate.source !== source) {
Expand All @@ -269,6 +264,34 @@ export class Translator {
}
}

/**
* @param {string[]} array1
* @param {string[]} array2
* @returns {boolean}
*/
_areArraysEqualIgnoreOrder(array1, array2) {
if (array1.length !== array2.length) {
return false;
}

const frequencyCounter = new Map();

for (const element of array1) {
frequencyCounter.set(element, (frequencyCounter.get(element) || 0) + 1);
}

for (const element of array2) {
const frequency = frequencyCounter.get(element);
if (!frequency) {
return false;
}
frequencyCounter.set(element, frequency - 1);
}

return true;
}


/**
* @param {string} text
* @param {Map<string, import('translation').FindTermDictionary>} enabledDictionaryMap
Expand Down

0 comments on commit f159b39

Please sign in to comment.