Skip to content

Commit

Permalink
search resolution option (#436)
Browse files Browse the repository at this point in the history
* add search resolution setting

* move finding next substring to method

* use regex literal

* fix comments

* fix comments

* add options update function

* update test
  • Loading branch information
StefanVukovic99 authored Dec 29, 2023
1 parent 8d5d215 commit 580983b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 6 deletions.
11 changes: 10 additions & 1 deletion ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,18 @@
"convertHiraganaToKatakana",
"convertKatakanaToHiragana",
"collapseEmphaticSequences",
"textReplacements"
"textReplacements",
"searchResolution"
],
"properties": {
"searchResolution": {
"type": "string",
"enum": [
"letter",
"word"
],
"default": "letter"
},
"convertHalfWidthCharacters": {
"type": "string",
"enum": ["false", "true", "variant"],
Expand Down
4 changes: 3 additions & 1 deletion ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2417,7 +2417,8 @@ export class Backend {
convertHiraganaToKatakana,
convertKatakanaToHiragana,
collapseEmphaticSequences,
textReplacements: textReplacementsOptions
textReplacements: textReplacementsOptions,
searchResolution
}
} = options;
const textReplacements = this._getTranslatorTextReplacements(textReplacementsOptions);
Expand All @@ -2444,6 +2445,7 @@ export class Backend {
convertHiraganaToKatakana,
convertKatakanaToHiragana,
collapseEmphaticSequences,
searchResolution,
textReplacements,
enabledDictionaryMap,
excludeDictionaryDefinitions
Expand Down
15 changes: 14 additions & 1 deletion ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ export class OptionsUtil {
{async: false, update: this._updateVersion18.bind(this)},
{async: false, update: this._updateVersion19.bind(this)},
{async: false, update: this._updateVersion20.bind(this)},
{async: true, update: this._updateVersion21.bind(this)}
{async: true, update: this._updateVersion21.bind(this)},
{async: false, update: this._updateVersion22.bind(this)}
];
/* eslint-enable no-multi-spaces */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1173,6 +1174,18 @@ export class OptionsUtil {
return options;
}

/**
* @type {import('options-util').ModernUpdateFunctionAsync}
*/
_updateVersion22(options) {
// Added translation.searchResolution
for (const {options: profileOptions} of options.profiles) {
profileOptions.translation.searchResolution = 'letter';
}

return options;
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
22 changes: 20 additions & 2 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,12 @@ export class Translator {
text2 = jp.collapseEmphaticSequences(text2, collapseEmphaticFull, sourceMap);
}

for (let i = text2.length; i > 0; --i) {
const source = text2.substring(0, i);
for (
let source = text2, i = text2.length;
i > 0;
i = this._getNextSubstringLength(options.searchResolution, i, source)
) {
source = text2.substring(0, i);
if (used.has(source)) { break; }
used.add(source);
const rawSource = sourceMap.source.substring(0, sourceMap.getSourceLength(i));
Expand All @@ -342,6 +346,20 @@ export class Translator {
return deinflections;
}

/**
* @param {string} searchResolution
* @param {number} currentLength
* @param {string} source
* @returns {number}
*/
_getNextSubstringLength(searchResolution, currentLength, source) {
if (searchResolution === 'word') {
return source.search(/[^\p{Letter}][\p{Letter}\p{Number}]*$/u);
} else {
return currentLength - 1;
}
}

/**
* @param {string} text
* @param {TextSourceMap} sourceMap
Expand Down
12 changes: 12 additions & 0 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,18 @@ <h1>Yomitan Settings</h1>
</div>
</div>
<div class="settings-group advanced-only">
<div class="settings-item advanced-only"><div class="settings-item-inner settings-item-inner-wrappable">
<div class="settings-item-left">
<div class="settings-item-label">Dictionary search resolution</div>
<div class="settings-item-description" lang="en"><p>"A dog"&#x3000;&rarr;&#x3000;search for "A dog","A do", "A d", "A"</p> or <p>"A dog"&#x3000;&rarr;&#x3000;"A dog", "A"</p></div>
</div>
<div class="settings-item-right">
<select data-setting="translation.searchResolution">
<option value="letter">Letter</option>
<option value="word">Word</option>
</select>
</div>
</div></div>
<div class="settings-item settings-item-button" data-modal-action="show,translation-text-replacement-patterns"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Configure custom text replacement patterns&hellip;</div>
Expand Down
3 changes: 2 additions & 1 deletion test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ function createProfileOptionsUpdatedTestData1() {
convertHiraganaToKatakana: 'false',
convertKatakanaToHiragana: 'variant',
collapseEmphaticSequences: 'false',
searchResolution: 'letter',
textReplacements: {
searchOriginal: true,
groups: []
Expand Down Expand Up @@ -601,7 +602,7 @@ function createOptionsUpdatedTestData1() {
}
],
profileCurrent: 0,
version: 21,
version: 22,
global: {
database: {
prefixWildcardsSupported: false
Expand Down
3 changes: 3 additions & 0 deletions types/ext/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,11 @@ export type TranslationOptions = {
convertKatakanaToHiragana: TranslationConvertType;
collapseEmphaticSequences: TranslationCollapseEmphaticSequences;
textReplacements: TranslationTextReplacementOptions;
searchResolution: SearchResolution;
};

export type SearchResolution = 'letter' | 'word';

export type TranslationTextReplacementOptions = {
searchOriginal: boolean;
groups: TranslationTextReplacementGroup[][];
Expand Down
5 changes: 5 additions & 0 deletions types/ext/translation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import type * as Dictionary from './dictionary';
import type {SearchResolution} from 'settings';

// Kanji

Expand Down Expand Up @@ -116,6 +117,10 @@ export type FindTermsOptions = {
* A set of dictionary names which should have definitions removed.
*/
excludeDictionaryDefinitions: Set<string> | null;
/**
* Whether every substring should be searched for, or only whole words.
*/
searchResolution: SearchResolution;
};

/**
Expand Down

0 comments on commit 580983b

Please sign in to comment.