yomichan/ext/bg/js/deinflector.js

120 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-03-20 17:52:14 -07:00
/*
2017-08-14 21:43:09 -07:00
* Copyright (C) 2016-2017 Alex Yatskov <alex@foosoft.net>
2016-03-20 17:52:14 -07:00
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
class Deinflection {
2016-12-20 21:54:01 -08:00
constructor(term, {rules=[], definitions=[], reason=''} = {}) {
2016-08-07 13:01:27 -07:00
this.term = term;
2016-12-17 18:45:19 -08:00
this.rules = rules;
2016-12-20 21:54:01 -08:00
this.definitions = definitions;
2016-12-17 18:45:19 -08:00
this.reason = reason;
2016-12-20 21:54:01 -08:00
this.children = [];
2016-03-20 17:52:14 -07:00
}
2017-07-10 14:30:34 -07:00
async deinflect(definer, reasons) {
2017-02-26 11:12:54 -08:00
for (const reason in reasons) {
for (const variant of reasons[reason]) {
2016-12-20 21:54:01 -08:00
let accept = this.rules.length === 0;
if (!accept) {
2017-02-26 11:12:54 -08:00
for (const rule of this.rules) {
2016-12-19 20:03:06 -08:00
if (variant.rulesIn.includes(rule)) {
2016-12-20 21:54:01 -08:00
accept = true;
2016-12-19 20:03:06 -08:00
break;
}
2016-03-20 17:52:14 -07:00
}
}
2016-12-20 21:54:01 -08:00
if (!accept || !this.term.endsWith(variant.kanaIn)) {
2016-03-20 18:27:11 -07:00
continue;
}
2016-09-19 21:12:45 -07:00
const term = this.term.slice(0, -variant.kanaIn.length) + variant.kanaOut;
2016-09-16 20:28:59 -07:00
if (term.length === 0) {
continue;
}
2016-12-20 21:54:01 -08:00
const child = new Deinflection(term, {reason, rules: variant.rulesOut});
2017-07-10 14:30:34 -07:00
if (await child.deinflect(definer, reasons)) {
this.children.push(child);
}
2016-03-20 17:52:14 -07:00
}
}
2017-07-10 14:30:34 -07:00
const definitions = await definer(this.term);
if (this.rules.length === 0) {
this.definitions = definitions;
} else {
for (const rule of this.rules) {
for (const definition of definitions) {
if (definition.rules.includes(rule)) {
this.definitions.push(definition);
}
}
2016-12-20 21:54:01 -08:00
}
2017-07-10 14:30:34 -07:00
}
if (this.definitions.length > 0 && this.children.length > 0) {
const child = new Deinflection(this.term, {rules: this.rules, definitions: this.definitions});
this.children.push(child);
}
2016-12-20 21:54:01 -08:00
2017-07-10 14:30:34 -07:00
return this.definitions.length > 0 || this.children.length > 0;
2016-03-20 17:52:14 -07:00
}
gather() {
if (this.children.length === 0) {
2016-12-19 21:23:17 -08:00
return [{
2016-12-20 21:54:01 -08:00
source: this.term,
2016-12-19 21:23:17 -08:00
rules: this.rules,
definitions: this.definitions,
2017-03-25 12:09:57 -07:00
reasons: this.reason.length > 0 ? [this.reason] : []
2016-12-19 21:23:17 -08:00
}];
2016-03-20 17:52:14 -07:00
}
2016-12-20 21:54:01 -08:00
const results = [];
2017-02-26 11:12:54 -08:00
for (const child of this.children) {
for (const result of child.gather()) {
2016-12-17 18:45:19 -08:00
if (this.reason.length > 0) {
2016-12-20 21:54:01 -08:00
result.reasons.push(this.reason);
2016-03-20 17:52:14 -07:00
}
2016-12-20 21:54:01 -08:00
result.source = this.term;
results.push(result);
2016-03-20 17:52:14 -07:00
}
}
2016-12-20 21:54:01 -08:00
return results;
2016-03-20 17:52:14 -07:00
}
}
class Deinflector {
2017-07-10 14:30:34 -07:00
constructor(reasons) {
2016-12-17 18:45:19 -08:00
this.reasons = reasons;
2016-03-20 17:52:14 -07:00
}
2017-07-10 14:30:34 -07:00
async deinflect(term, definer) {
2016-03-20 18:27:11 -07:00
const node = new Deinflection(term);
2017-07-10 14:30:34 -07:00
if (await node.deinflect(definer, this.reasons)) {
return node.gather();
} else {
return [];
}
2016-03-20 17:52:14 -07:00
}
}