yomichan/ext/bg/js/deinflector.js

125 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-03-21 00:52:14 +00:00
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* 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-18 02:45:19 +00:00
constructor(term, rules=[], reason='') {
2016-03-21 00:52:14 +00:00
this.children = [];
2016-08-07 20:01:27 +00:00
this.term = term;
2016-12-18 02:45:19 +00:00
this.rules = rules;
this.reason = reason;
2016-03-21 00:52:14 +00:00
}
validate(validator) {
2016-09-11 19:29:18 +00:00
return validator(this.term).then(sets => {
2016-12-18 02:45:19 +00:00
for (const rules of sets) {
if (this.rules.length === 0) {
2016-03-21 00:52:14 +00:00
return true;
}
2016-09-11 01:57:00 +00:00
2016-12-18 02:45:19 +00:00
for (const rule of this.rules) {
if (rules.includes(rule)) {
2016-09-11 01:57:00 +00:00
return true;
}
}
2016-03-21 00:52:14 +00:00
}
2016-09-11 01:57:00 +00:00
return false;
});
2016-03-21 00:52:14 +00:00
}
2016-12-18 02:45:19 +00:00
deinflect(validator, reasons) {
2016-09-11 01:57:00 +00:00
const promises = [
this.validate(validator).then(valid => {
2016-12-18 02:45:19 +00:00
const child = new Deinflection(this.term, this.rules);
2016-09-11 01:57:00 +00:00
this.children.push(child);
})
];
2016-03-21 00:52:14 +00:00
2016-12-18 02:45:19 +00:00
for (const reason in reasons) {
for (const variant of reasons[reason]) {
let allowed = this.rules.length === 0;
for (const rule of this.rules) {
if (variant.rulesIn.includes(rule)) {
2016-03-21 00:52:14 +00:00
allowed = true;
break;
}
}
2016-09-20 04:12:45 +00:00
if (!allowed || !this.term.endsWith(variant.kanaIn)) {
2016-03-21 01:27:11 +00:00
continue;
}
2016-09-20 04:12:45 +00:00
const term = this.term.slice(0, -variant.kanaIn.length) + variant.kanaOut;
2016-09-17 03:28:59 +00:00
if (term.length === 0) {
continue;
}
2016-12-18 02:45:19 +00:00
const child = new Deinflection(term, variant.rulesOut, reason);
2016-09-11 01:57:00 +00:00
promises.push(
2016-12-18 02:45:19 +00:00
child.deinflect(validator, reasons).then(valid => {
2016-09-11 01:57:00 +00:00
if (valid) {
this.children.push(child);
}
}
));
2016-03-21 00:52:14 +00:00
}
}
2016-09-11 01:57:00 +00:00
return Promise.all(promises).then(() => {
return this.children.length > 0;
});
2016-03-21 00:52:14 +00:00
}
gather() {
if (this.children.length === 0) {
2016-12-18 02:45:19 +00:00
return [{root: this.term, rules: this.rules, reasons: []}];
2016-03-21 00:52:14 +00:00
}
const paths = [];
for (const child of this.children) {
for (const path of child.gather()) {
2016-12-18 02:45:19 +00:00
if (this.reason.length > 0) {
path.reasons.push(this.reason);
2016-03-21 00:52:14 +00:00
}
path.source = this.term;
paths.push(path);
}
}
return paths;
}
}
class Deinflector {
constructor() {
2016-12-18 02:45:19 +00:00
this.reasons = {};
2016-03-21 00:52:14 +00:00
}
2016-12-18 02:45:19 +00:00
setReasons(reasons) {
this.reasons = reasons;
2016-03-21 00:52:14 +00:00
}
deinflect(term, validator) {
2016-03-21 01:27:11 +00:00
const node = new Deinflection(term);
2016-12-18 02:45:19 +00:00
return node.deinflect(validator, this.reasons).then(success => success ? node.gather() : []);
2016-03-21 00:52:14 +00:00
}
}