Skip to content

Commit

Permalink
Merge branch 'master' into anki-clipboard-image
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Mar 24, 2024
2 parents 69abb9f + 2c5af21 commit 7db4244
Show file tree
Hide file tree
Showing 26 changed files with 1,952 additions and 1,176 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -642,15 +642,19 @@
"ext/js/general/text-source-map.js",
"ext/js/language/ar/arabic-text-preprocessors.js",
"ext/js/language/de/german-text-preprocessors.js",
"ext/js/language/en/english-transforms.js",
"ext/js/language/ja/japanese-text-preprocessors.js",
"ext/js/language/ja/japanese-transforms.js",
"ext/js/language/ja/japanese-wanakana.js",
"ext/js/language/ja/japanese.js",
"ext/js/language/la/latin-text-preprocessors.js",
"ext/js/language/language-descriptors.js",
"ext/js/language/language-transformer.js",
"ext/js/language/language-transforms.js",
"ext/js/language/languages.js",
"ext/js/language/multi-language-transformer.js",
"ext/js/language/ru/russian-text-preprocessors.js",
"ext/js/language/sq/albanian-transforms.js",
"ext/js/language/text-preprocessors.js",
"ext/js/language/translator.js",
"ext/js/media/audio-downloader.js",
Expand Down
11 changes: 2 additions & 9 deletions benches/language-transformer.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import fs from 'fs';
import {fileURLToPath} from 'node:url';
import path from 'path';
import {bench, describe} from 'vitest';
import {parseJson} from '../dev/json.js';
import {japaneseTransforms} from '../ext/js/language/ja/japanese-transforms.js';
import {LanguageTransformer} from '../ext/js/language/language-transformer.js';

const dirname = path.dirname(fileURLToPath(import.meta.url));

/** @type {import('language-transformer').LanguageTransformDescriptor} */
const descriptor = parseJson(fs.readFileSync(path.join(dirname, '..', 'ext', 'js/language/ja/japanese-transforms.json'), {encoding: 'utf8'}));
const languageTransformer = new LanguageTransformer();
languageTransformer.addDescriptor(descriptor);
languageTransformer.addDescriptor(japaneseTransforms);

describe('language transformer', () => {
describe('basic tests', () => {
Expand Down
13 changes: 2 additions & 11 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {ClipboardReader} from '../comm/clipboard-reader.js';
import {Mecab} from '../comm/mecab.js';
import {createApiMap, invokeApiMapHandler} from '../core/api-map.js';
import {ExtensionError} from '../core/extension-error.js';
import {fetchJson, fetchText} from '../core/fetch-utilities.js';
import {fetchText} from '../core/fetch-utilities.js';
import {logErrorLevelToNumber} from '../core/log-utilities.js';
import {log} from '../core/log.js';
import {isObjectNotArray} from '../core/object-utilities.js';
Expand Down Expand Up @@ -279,16 +279,7 @@ export class Backend {
log.error(e);
}

/** @type {import('language-transformer').LanguageTransformDescriptor[]} */
const descriptors = [];
const languageSummaries = getLanguageSummaries();
for (const {languageTransformsFile} of languageSummaries) {
if (!languageTransformsFile) { continue; }
/** @type {import('language-transformer').LanguageTransformDescriptor} */
const descriptor = await fetchJson(languageTransformsFile);
descriptors.push(descriptor);
}
void this._translator.prepare(descriptors);
void this._translator.prepare();

await this._optionsUtil.prepare();
this._defaultAnkiFieldTemplates = (await fetchText('/data/templates/default-anki-field-templates.handlebars')).trim();
Expand Down
8 changes: 3 additions & 5 deletions ext/js/background/offscreen-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,9 @@ export class TranslatorProxy {
this._offscreen = offscreen;
}

/**
* @param {import('language-transformer').LanguageTransformDescriptor[]} descriptors
*/
async prepare(descriptors) {
await this._offscreen.sendMessagePromise({action: 'translatorPrepareOffscreen', params: {descriptors}});
/** */
async prepare() {
await this._offscreen.sendMessagePromise({action: 'translatorPrepareOffscreen'});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ext/js/background/offscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export class Offscreen {
}

/** @type {import('offscreen').ApiHandler<'translatorPrepareOffscreen'>} */
_prepareTranslatorHandler({descriptors}) {
this._translator.prepare(descriptors);
_prepareTranslatorHandler() {
this._translator.prepare();
}

/** @type {import('offscreen').ApiHandler<'findKanjiOffscreen'>} */
Expand Down
106 changes: 106 additions & 0 deletions ext/js/language/de/german-transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2024 Yomitan Authors
*
* 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/>.
*/

import {prefixInflection, suffixInflection} from '../language-transforms.js';

// https://www.dartmouth.edu/~deutsch/Grammatik/Wortbildung/Separables.html
const separablePrefixes = ['ab', 'an', 'auf', 'aus', 'auseinander', 'bei', 'da', 'dabei', 'dar', 'daran', 'dazwischen', 'durch', 'ein', 'empor', 'entgegen', 'entlang', 'entzwei', 'fehl', 'fern', 'fest', 'fort', 'frei', 'gegenüber', 'gleich', 'heim', 'her', 'herab', 'heran', 'herauf', 'heraus', 'herbei', 'herein', 'herüber', 'herum', 'herunter', 'hervor', 'hin', 'hinab', 'hinauf', 'hinaus', 'hinein', 'hinterher', 'hinunter', 'hinweg', 'hinzu', 'hoch', 'los', 'mit', 'nach', 'nebenher', 'nieder', 'statt', 'um', 'vor', 'voran', 'voraus', 'vorbei', 'vorüber', 'vorweg', 'weg', 'weiter', 'wieder', 'zu', 'zurecht', 'zurück', 'zusammen'];

/**
* @param {string} prefix
* @param {string[]} conditionsIn
* @param {string[]} conditionsOut
* @returns {import('language-transformer').Rule}
*/
function separatedPrefix(prefix, conditionsIn, conditionsOut) {
const germanLetters = 'a-zA-ZäöüßÄÖÜẞ';
const regex = new RegExp(`^([${germanLetters}]+) .+ ${prefix}$`);
return {
type: 'other',
isInflected: regex,
deinflect: (term) => {
return term.replace(regex, '$1 ' + prefix);
},
conditionsIn,
conditionsOut
};
}

const separatedPrefixInflections = separablePrefixes.map((prefix) => {
return separatedPrefix(prefix, [], []);
});

const zuInfinitiveInflections = separablePrefixes.map((prefix) => {
return prefixInflection(prefix + 'zu', prefix, [], ['v']);
});

export const germanTransforms = {
language: 'de',
conditions: {
v: {
name: 'Verb',
isDictionaryForm: true
},
n: {
name: 'Noun',
isDictionaryForm: true
},
adj: {
name: 'Adjective',
isDictionaryForm: true
}
},
transforms: [
{
name: 'nominalization',
description: 'Noun formed from a verb',
rules: [
suffixInflection('ung', 'en', [], []),
suffixInflection('lung', 'eln', [], [])
]
},
{
name: '-bar',
description: '-able adjective from a verb',
rules: [
suffixInflection('bar', 'en', [], ['v']),
suffixInflection('bar', 'n', [], ['v']) // Lieferbar
]
},
{
name: 'negative',
description: 'Negation',
rules: [
prefixInflection('un', '', [], ['adj'])
]
},
{
name: 'separated prefix',
description: 'Separable prefix',
rules: [
...separatedPrefixInflections
]
},
{
name: 'zu-infinitive',
description: 'zu-infinitive',
rules: [
...zuInfinitiveInflections
]
}
]
};
Loading

0 comments on commit 7db4244

Please sign in to comment.