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 {
|
|
|
|
constructor(term, tags=[], rule='') {
|
|
|
|
this.children = [];
|
|
|
|
this.term = term;
|
|
|
|
this.tags = tags;
|
|
|
|
this.rule = rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
validate(validator) {
|
|
|
|
for (const tags of validator(this.term)) {
|
|
|
|
if (this.tags.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:13:43 +00:00
|
|
|
for (const tag of this.tags) {
|
2016-04-17 03:11:27 +00:00
|
|
|
if (tags.indexOf(tag) !== -1) {
|
2016-03-21 00:52:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
deinflect(validator, rules) {
|
|
|
|
if (this.validate(validator)) {
|
2016-04-17 03:11:27 +00:00
|
|
|
const child = new Deinflection(this.term, this.tags);
|
2016-03-21 00:52:14 +00:00
|
|
|
this.children.push(child);
|
|
|
|
}
|
|
|
|
|
2016-03-24 01:12:33 +00:00
|
|
|
for (const rule in rules) {
|
|
|
|
const variants = rules[rule];
|
2016-03-23 04:47:56 +00:00
|
|
|
for (const v of variants) {
|
2016-03-21 00:52:14 +00:00
|
|
|
let allowed = this.tags.length === 0;
|
2016-03-25 03:13:43 +00:00
|
|
|
for (const tag of this.tags) {
|
2016-04-20 05:04:55 +00:00
|
|
|
//
|
|
|
|
// TODO: Handle addons through tags.json or rules.json
|
|
|
|
//
|
|
|
|
|
2016-04-17 03:11:27 +00:00
|
|
|
if (v.tagsIn.indexOf(tag) !== -1) {
|
2016-03-21 00:52:14 +00:00
|
|
|
allowed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 04:47:56 +00:00
|
|
|
if (!allowed || !this.term.endsWith(v.kanaIn)) {
|
2016-03-21 01:27:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-03-24 01:12:33 +00:00
|
|
|
const term = this.term.slice(0, -v.kanaIn.length) + v.kanaOut;
|
2016-03-23 04:47:56 +00:00
|
|
|
const child = new Deinflection(term, v.tagsOut, rule);
|
2016-03-21 01:27:11 +00:00
|
|
|
if (child.deinflect(validator, rules)) {
|
2016-03-24 01:12:33 +00:00
|
|
|
this.children.push(child);
|
2016-03-21 01:27:11 +00:00
|
|
|
}
|
2016-03-21 00:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.children.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gather() {
|
|
|
|
if (this.children.length === 0) {
|
2016-04-17 03:11:27 +00:00
|
|
|
return [{root: this.term, tags: this.tags, rules: []}];
|
2016-03-21 00:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const paths = [];
|
|
|
|
for (const child of this.children) {
|
2016-03-24 01:12:33 +00:00
|
|
|
for (const path of child.gather()) {
|
2016-03-21 00:52:14 +00:00
|
|
|
if (this.rule.length > 0) {
|
2016-03-24 01:12:33 +00:00
|
|
|
path.rules.push(this.rule);
|
2016-03-21 00:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path.source = this.term;
|
|
|
|
paths.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class Deinflector {
|
|
|
|
constructor() {
|
|
|
|
this.rules = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
setRules(rules) {
|
|
|
|
this.rules = rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
deinflect(term, validator) {
|
2016-03-21 01:27:11 +00:00
|
|
|
const node = new Deinflection(term);
|
2016-03-21 00:52:14 +00:00
|
|
|
if (node.deinflect(validator, this.rules)) {
|
|
|
|
return node.gather();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|