2020-02-06 02:00:02 +00:00
|
|
|
/*
|
2020-04-10 18:06:55 +00:00
|
|
|
* Copyright (C) 2020 Yomichan Authors
|
2020-02-06 02:00:02 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class QueryParserGenerator {
|
2020-11-08 17:35:32 +00:00
|
|
|
createParseResult(terms, preview) {
|
|
|
|
const type = preview ? 'preview' : 'normal';
|
2020-02-06 02:00:02 +00:00
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
for (const term of terms) {
|
2020-11-08 17:35:32 +00:00
|
|
|
const termNode = document.createElement('span');
|
|
|
|
termNode.className = 'query-parser-term';
|
|
|
|
termNode.dataset.type = type;
|
2020-02-06 02:00:02 +00:00
|
|
|
for (const segment of term) {
|
|
|
|
if (!segment.text.trim()) { continue; }
|
2020-04-13 19:55:33 +00:00
|
|
|
if (!segment.reading.trim()) {
|
2020-11-08 17:35:32 +00:00
|
|
|
this._addSegmentText(segment.text, termNode);
|
2020-02-06 02:00:02 +00:00
|
|
|
} else {
|
2020-11-08 17:35:32 +00:00
|
|
|
termNode.appendChild(this._createSegment(segment));
|
2020-02-06 02:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-08 17:35:32 +00:00
|
|
|
fragment.appendChild(termNode);
|
2020-02-06 02:00:02 +00:00
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
createParserSelect(parseResults, selectedParser) {
|
2020-11-08 17:35:32 +00:00
|
|
|
const select = document.createElement('select');
|
|
|
|
select.className = 'query-parser-select form-control';
|
2020-02-06 02:00:02 +00:00
|
|
|
for (const parseResult of parseResults) {
|
2020-11-08 17:35:32 +00:00
|
|
|
const option = document.createElement('option');
|
|
|
|
option.className = 'query-parser-select-option';
|
|
|
|
option.value = parseResult.id;
|
2020-04-12 00:53:24 +00:00
|
|
|
switch (parseResult.source) {
|
|
|
|
case 'scanning-parser':
|
2020-11-08 17:35:32 +00:00
|
|
|
option.textContent = 'Scanning parser';
|
2020-04-12 00:53:24 +00:00
|
|
|
break;
|
|
|
|
case 'mecab':
|
2020-11-08 17:35:32 +00:00
|
|
|
option.textContent = `MeCab: ${parseResult.dictionary}`;
|
2020-04-12 00:53:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2020-11-08 17:35:32 +00:00
|
|
|
option.textContent = 'Unrecognized dictionary';
|
2020-04-14 18:13:46 +00:00
|
|
|
break;
|
2020-04-12 00:53:24 +00:00
|
|
|
}
|
2020-11-08 17:35:32 +00:00
|
|
|
option.defaultSelected = selectedParser === parseResult.id;
|
|
|
|
select.appendChild(option);
|
|
|
|
}
|
|
|
|
return select;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
_createSegment(segment) {
|
|
|
|
const segmentNode = document.createElement('ruby');
|
|
|
|
segmentNode.className = 'query-parser-segment';
|
|
|
|
|
|
|
|
const textNode = document.createElement('span');
|
|
|
|
textNode.className = 'query-parser-segment-text';
|
|
|
|
|
|
|
|
const readingNode = document.createElement('rt');
|
|
|
|
readingNode.className = 'query-parser-segment-reading';
|
|
|
|
|
|
|
|
segmentNode.appendChild(textNode);
|
|
|
|
segmentNode.appendChild(readingNode);
|
|
|
|
|
|
|
|
this._addSegmentText(segment.text, textNode);
|
|
|
|
readingNode.textContent = segment.reading;
|
|
|
|
|
|
|
|
return segmentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
_addSegmentText(text, container) {
|
|
|
|
for (const character of text) {
|
|
|
|
const node = document.createElement('span');
|
|
|
|
node.className = 'query-parser-char';
|
|
|
|
node.textContent = character;
|
|
|
|
container.appendChild(node);
|
2020-02-06 02:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|