Skip to content

Commit

Permalink
rename to textPreprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Feb 4, 2024
1 parent 17ceb75 commit 6557689
Show file tree
Hide file tree
Showing 26 changed files with 164 additions and 164 deletions.
6 changes: 3 additions & 3 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,9 @@
},
"additionalProperties": {
"type": "object",
"required": ["textTransformations"],
"required": ["textPreprocessors"],
"properties": {
"textTransformations": {
"textPreprocessors": {
"type": "object",
"propertyNames": {
"type": "string"
Expand All @@ -806,7 +806,7 @@
},
"default": {
"ja": {
"textTransformations": {
"textPreprocessors": {
"convertHalfWidthCharacters": "false",
"convertNumericCharacters": "false",
"convertAlphabeticCharacters": "false",
Expand Down
14 changes: 7 additions & 7 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {DictionaryDatabase} from '../dictionary/dictionary-database.js';
import {Environment} from '../extension/environment.js';
import {ObjectPropertyAccessor} from '../general/object-property-accessor.js';
import {distributeFuriganaInflected, isCodePointJapanese, isStringPartiallyJapanese, convertKatakanaToHiragana as jpConvertKatakanaToHiragana} from '../language/ja/japanese.js';
import {getLanguages, getTextTransformations} from '../language/language-util.js';
import {getLanguages, getTextPreprocessors} from '../language/language-util.js';
import {Translator} from '../language/translator.js';
import {AudioDownloader} from '../media/audio-downloader.js';
import {getFileExtensionFromAudioMediaType, getFileExtensionFromImageMediaType} from '../media/media-util.js';
Expand Down Expand Up @@ -186,7 +186,7 @@ export class Backend {
['findAnkiNotes', this._onApiFindAnkiNotes.bind(this)],
['openCrossFramePort', this._onApiOpenCrossFramePort.bind(this)],
['getLanguages', this._onApiGetLanguages.bind(this)],
['getTextTransformations', this._onApiGetTextTransformations.bind(this)]
['getTextPreprocessors', this._onApiGetTextPreprocessors.bind(this)]
]);
/* eslint-enable no-multi-spaces */

Expand Down Expand Up @@ -915,9 +915,9 @@ export class Backend {
return getLanguages();
}

/** @type {import('api').ApiHandler<'getTextTransformations'>} */
_onApiGetTextTransformations({language}) {
return getTextTransformations(language);
/** @type {import('api').ApiHandler<'getTextPreprocessors'>} */
_onApiGetTextPreprocessors({language}) {
return getTextPreprocessors(language);
}

// Command handlers
Expand Down Expand Up @@ -2384,7 +2384,7 @@ export class Backend {
searchResolution
}
} = options;
const textTransformationsOptions = options.languages[language].textTransformations || {};
const textPreprocessorsOptions = options.languages[language].textPreprocessors || {};
const textReplacements = this._getTranslatorTextReplacements(textReplacementsOptions);
let excludeDictionaryDefinitions = null;
if (mode === 'merge' && !enabledDictionaryMap.has(mainDictionary)) {
Expand All @@ -2410,7 +2410,7 @@ export class Backend {
enabledDictionaryMap,
excludeDictionaryDefinitions,
language,
textTransformationsOptions
textPreprocessorsOptions
};
}

Expand Down
8 changes: 4 additions & 4 deletions ext/js/comm/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ export class API {
}

/**
* @param {import('api').ApiParam<'getTextTransformations', 'language'>} language
* @returns {Promise<import('api').ApiReturn<'getTextTransformations'>>}
* @param {import('api').ApiParam<'getTextPreprocessors', 'language'>} language
* @returns {Promise<import('api').ApiReturn<'getTextPreprocessors'>>}
*/
getTextTransformations(language) {
return this._invoke('getTextTransformations', {language});
getTextPreprocessors(language) {
return this._invoke('getTextPreprocessors', {language});
}

// Utilities
Expand Down
12 changes: 6 additions & 6 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,11 +1135,11 @@ export class OptionsUtil {

/**
* - Added general.language.
* - Modularized text transformations.
* - Modularized text preprocessors.
* @type {import('options-util').UpdateFunction}
*/
_updateVersion25(options) {
const textTransformations = [
const textPreprocessors = [
'convertHalfWidthCharacters',
'convertNumericCharacters',
'convertAlphabeticCharacters',
Expand All @@ -1153,13 +1153,13 @@ export class OptionsUtil {

profileOptions.languages = {
ja: {
textTransformations: {}
textPreprocessors: {}
}
};

for (const transformation of textTransformations) {
profileOptions.languages.ja.textTransformations[transformation] = profileOptions.translation[transformation];
delete profileOptions.translation[transformation];
for (const preprocessor of textPreprocessors) {
profileOptions.languages.ja.textPreprocessors[preprocessor] = profileOptions.translation[preprocessor];
delete profileOptions.translation[preprocessor];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {capitalizeFirstLetter, decapitalize} from '../text-transformations.js';
import {capitalizeFirstLetter, decapitalize} from '../text-preprocessors.js';

export const textTransformations = [
export const textPreprocessors = [
decapitalize,
capitalizeFirstLetter
];
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {basicTextTransformationOptions} from '../text-transformations.js';
import {basicTextPreprocessorOptions} from '../text-preprocessors.js';
import {convertAlphabeticToKana} from './japanese-wanakana.js';
import {collapseEmphaticSequences, convertHalfWidthKanaToFullWidth, convertHiraganaToKatakana, convertKatakanaToHiragana, convertNumericToFullWidth} from './japanese.js';

/** @type {import('language').TextTransformation<boolean[]>}*/
/** @type {import('language').TextPreprocessor<boolean[]>}*/
const collapseEmphaticSequencesTransform = {
id: 'collapseEmphaticSequences',
name: 'Collapse emphatic character sequences',
Expand All @@ -38,41 +38,41 @@ const collapseEmphaticSequencesTransform = {
}
};

/** @type {import('language').TextTransformation<any>[]} */
export const textTransformations = [
/** @type {import('language').TextPreprocessor<any>[]} */
export const textPreprocessors = [
{
id: 'convertHalfWidthCharacters',
name: 'Convert half width characters to full width',
description: 'ヨミチャン → ヨミチャン',
options: basicTextTransformationOptions,
options: basicTextPreprocessorOptions,
transform: (str, setting, sourceMap) => setting ? convertHalfWidthKanaToFullWidth(str, sourceMap) : str
},
{
id: 'convertNumericCharacters',
name: 'Convert numeric characters to full width',
description: '1234 → 1234',
options: basicTextTransformationOptions,
options: basicTextPreprocessorOptions,
transform: (str, setting) => setting ? convertNumericToFullWidth(str) : str
},
{
id: 'convertAlphabeticCharacters',
name: 'Convert alphabetic characters to hiragana',
description: 'yomichan → よみちゃん',
options: basicTextTransformationOptions,
options: basicTextPreprocessorOptions,
transform: (str, setting, sourceMap) => setting ? convertAlphabeticToKana(str, sourceMap) : str
},
{
id: 'convertHiraganaToKatakana',
name: 'Convert hiragana to katakana',
description: 'よみちゃん → ヨミチャン',
options: basicTextTransformationOptions,
options: basicTextPreprocessorOptions,
transform: (str, setting) => setting ? convertHiraganaToKatakana(str) : str
},
{
id: 'convertKatakanaToHiragana',
name: 'Convert katakana to hiragana',
description: 'ヨミチャン → よみちゃん',
options: basicTextTransformationOptions,
options: basicTextPreprocessorOptions,
transform: (str, setting) => setting ? convertKatakanaToHiragana(str) : str
},
collapseEmphaticSequencesTransform
Expand Down
6 changes: 3 additions & 3 deletions ext/js/language/language-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export function getLanguages() {

/**
* @param {string} iso
* @returns {import('language').TextTransformation[]}
* @returns {import('language').TextPreprocessor[]}
*/
export function getTextTransformations(iso) {
return languages.get(iso)?.textTransformations || [];
export function getTextPreprocessors(iso) {
return languages.get(iso)?.textPreprocessors || [];
}


8 changes: 4 additions & 4 deletions ext/js/language/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import {textTransformations as textTransformationsEN} from './en/text-transformations.js';
import {textTransformations as textTransformationsJA} from './ja/text-transformations.js';
import {textPreprocessors as textPreprocessorsEN} from './en/text-preprocessors.js';
import {textPreprocessors as textPreprocessorsJA} from './ja/text-preprocessors.js';

/** @type {Map<string, import('language').Language>} */
export const languages = new Map([
Expand All @@ -25,14 +25,14 @@ export const languages = new Map([
iso: 'ja',
flag: '🇯🇵',
exampleText: '読め',
textTransformations: textTransformationsJA
textPreprocessors: textPreprocessorsJA
}],
['en', {
name: 'English',
iso: 'en',
flag: '🇬🇧',
exampleText: 'read',
textTransformations: textTransformationsEN
textPreprocessors: textPreprocessorsEN
}]
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/** @type {import('language').TextTransformationOption<boolean>[]} */
export const basicTextTransformationOptions = [
/** @type {import('language').TextPreprocessorOption<boolean>[]} */
export const basicTextPreprocessorOptions = [
['false', 'Disabled', [false]],
['true', 'Enabled', [true]],
['variant', 'Use both variants', [false, true]]
];

/** @type {import('language').TextTransformation} */
/** @type {import('language').TextPreprocessor} */
export const decapitalize = {
id: 'decapitalize',
name: 'Decapitalize text',
description: 'CAPITALIZED TEXT → capitalized text',
options: basicTextTransformationOptions,
options: basicTextPreprocessorOptions,
transform: (str, setting) => setting ? str.toLowerCase() : str
};

/** @type {import('language').TextTransformation} */
/** @type {import('language').TextPreprocessor} */
export const capitalizeFirstLetter = {
id: 'capitalizeFirstLetter',
name: 'Capitalize first letter',
description: 'lowercase text → Lowercase text',
options: basicTextTransformationOptions,
options: basicTextPreprocessorOptions,
transform: (str, setting) => setting ? str.charAt(0).toUpperCase() + str.slice(1) : str
};

48 changes: 24 additions & 24 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {applyTextReplacement} from '../general/regex-util.js';
import {TextSourceMap} from '../general/text-source-map.js';
import {isCodePointJapanese} from './ja/japanese.js';
import {LanguageTransformer} from './language-transformer.js';
import {getTextTransformations} from './language-util.js';
import {getTextPreprocessors} from './language-util.js';
/**
* Class which finds term and kanji dictionary entries for text.
*/
Expand Down Expand Up @@ -414,24 +414,24 @@ export class Translator {
}
}

// Deinflections and text transformations
// Deinflections and text preprocessing

/**
* @param {string} text
* @param {import('translation').FindTermsOptions} options
* @returns {import('translation-internal').DatabaseDeinflection[]}
*/
_getAlgorithmDeinflections(text, options) {
const textTransformations = this._getTextTransformations(options);
const textPreprocessors = this._getTextPreprocessors(options);

const textTransformationsVectorSpace = new Map();
for (const [id, transformation] of textTransformations) {
textTransformationsVectorSpace.set(id, this._getTextTransformationVariants(transformation));
const textPreprocessorsVectorSpace = new Map();
for (const [id, preprocessors] of textPreprocessors) {
textPreprocessorsVectorSpace.set(id, this._getTextPreprocessorVariants(preprocessors));
}

const variantVectorSpace = new Map([
['textReplacements', this._getTextReplacementsVariants(options)],
...textTransformationsVectorSpace
...textPreprocessorsVectorSpace
]);

/** @type {import('translation-internal').DatabaseDeinflection[]} */
Expand All @@ -448,8 +448,8 @@ export class Translator {
text2 = this._applyTextReplacements(text2, sourceMap, textReplacements);
}

for (const {transformation} of textTransformations.values()) {
const {id, transform} = transformation;
for (const {preprocessor} of textPreprocessors.values()) {
const {id, transform} = preprocessor;
const setting = arrayVariant.get(id);
text2 = transform(text2, setting, sourceMap);
}
Expand Down Expand Up @@ -492,27 +492,27 @@ export class Translator {

/**
* @param {import('translation').FindTermsOptions} options
* @returns {Map<string, import('translation-internal').TextTransformation>}
* @returns {Map<string, import('translation-internal').TextPreprocessor>}
*/
_getTextTransformations(options) {
const {textTransformationsOptions, language} = options;
const textTransformationsData = getTextTransformations(language);
_getTextPreprocessors(options) {
const {textPreprocessorsOptions, language} = options;
const textPreprocessorsData = getTextPreprocessors(language);

/** @type {Map<string, import('translation-internal').TextTransformation>} */
const textTransformations = new Map();
/** @type {Map<string, import('translation-internal').TextPreprocessor>} */
const textPreprocessors = new Map();

for (const transformation of textTransformationsData) {
const {id} = transformation;
const value = textTransformationsOptions[id];
for (const preprocessor of textPreprocessorsData) {
const {id} = preprocessor;
const value = textPreprocessorsOptions[id];
if (value) {
textTransformations.set(id, {
transformation,
textPreprocessors.set(id, {
preprocessor,
setting: value
});
}
}

return textTransformations;
return textPreprocessors;
}

/**
Expand Down Expand Up @@ -544,11 +544,11 @@ export class Translator {
}

/**
* @param {import('translation-internal').TextTransformation} transformation
* @param {import('translation-internal').TextPreprocessor} preprocessor
* @returns {unknown[]}
*/
_getTextTransformationVariants(transformation) {
const {setting, transformation: {options}} = transformation;
_getTextPreprocessorVariants(preprocessor) {
const {setting, preprocessor: {options}} = preprocessor;
for (const [optionValue, , optionSetting] of options) {
if (optionValue === setting) {
return optionSetting;
Expand Down
2 changes: 1 addition & 1 deletion ext/js/pages/settings/languages-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class LanguagesController {
value: {
...options.languages,
[language]: {
textTransformations: {}
textPreprocessors: {}
}
}
}]);
Expand Down
Loading

0 comments on commit 6557689

Please sign in to comment.