Skip to content

Commit

Permalink
make sentence parser works for spaced languages
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitruong922 committed Nov 27, 2024
1 parent 3c3f858 commit 62d9461
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ext/css/visibility-modifiers.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
:root:not([data-language=ja]):not([data-language=zh]):not([data-language=yue]) .jpzhyue-only {
display: none;
}
:root:not([data-language=ja]):not([data-language=zh]):not([data-language=yue]):not([data-language=ko]) .jpzhyueko-only {
display: none;
}
:root:is([data-language=ja], [data-language=zh], [data-language=yue], [data-language=ko]) .not-jpzhyueko {
display: none;
}
16 changes: 14 additions & 2 deletions ext/js/display/query-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {EventDispatcher} from '../core/event-dispatcher.js';
import {log} from '../core/log.js';
import {querySelectorNotNull} from '../dom/query-selector.js';
import {convertHiraganaToKatakana, convertKatakanaToHiragana, isStringEntirelyKana} from '../language/ja/japanese.js';
import {TextScanner} from '../language/text-scanner.js';
import {NO_SPACES_LANGUAGES, TextScanner} from '../language/text-scanner.js';

/**
* @augments EventDispatcher<import('query-parser').Events>
Expand All @@ -43,6 +43,8 @@ export class QueryParser extends EventDispatcher {
this._setTextToken = null;
/** @type {?string} */
this._selectedParser = null;
/** @type {boolean} */
this._shouldInsertSpacesBetweenTerms = false;
/** @type {import('settings').ParsingReadingMode} */
this._readingMode = 'none';
/** @type {number} */
Expand Down Expand Up @@ -100,6 +102,9 @@ export class QueryParser extends EventDispatcher {
}
if (typeof termSpacing === 'boolean') {
this._queryParser.dataset.termSpacing = `${termSpacing}`;
if (!NO_SPACES_LANGUAGES.has(language)) {
this._shouldInsertSpacesBetweenTerms = true;
}
}
if (typeof readingMode === 'string') {
this._setReadingMode(readingMode);
Expand Down Expand Up @@ -305,15 +310,22 @@ export class QueryParser extends EventDispatcher {
termNode.dataset.offset = `${offset}`;
for (const {text, reading} of term) {
const trimmedText = text.trim();
if (trimmedText.length === 0) { continue; }
if (reading.length === 0) {
termNode.appendChild(document.createTextNode(trimmedText));
} else {
const reading2 = this._convertReading(trimmedText, reading);
termNode.appendChild(this._createSegment(trimmedText, reading2, offset));
}
offset += trimmedText.length;
if (this._shouldInsertSpacesBetweenTerms) {
termNode.appendChild(document.createTextNode(' '));
offset += 1;
}
}
if (termNode.childNodes.length > 0) {
fragment.appendChild(termNode);
}
fragment.appendChild(termNode);
}
return fragment;
}
Expand Down
4 changes: 2 additions & 2 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {clone} from '../core/utilities.js';
import {anyNodeMatchesSelector, everyNodeMatchesSelector, getActiveModifiers, getActiveModifiersAndButtons, isPointInSelection} from '../dom/document-util.js';
import {TextSourceElement} from '../dom/text-source-element.js';

const SCAN_RESOLUTION_EXCLUDED_LANGUAGES = new Set(['ja', 'zh', 'yue', 'ko']);
export const NO_SPACES_LANGUAGES = new Set(['ja', 'zh', 'yue', 'ko']);

/**
* @augments EventDispatcher<import('text-scanner').Events>
Expand Down Expand Up @@ -478,7 +478,7 @@ export class TextScanner extends EventDispatcher {
);

if (this._scanResolution === 'word' && !disallowExpandStartOffset &&
(this._language === null || !SCAN_RESOLUTION_EXCLUDED_LANGUAGES.has(this._language))) {
(this._language === null || !NO_SPACES_LANGUAGES.has(this._language))) {
// Move the start offset to the beginning of the word
textSource.setStartOffset(this._scanLength, this._layoutAwareScan, true);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ <h1>Yomitan Settings</h1>
</p>
</div>
</div>
<div class="settings-item"><div class="settings-item-inner">
<div class="settings-item jpzhyueko-only"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Show space between parsed words</div>
</div>
Expand Down

0 comments on commit 62d9461

Please sign in to comment.