This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/src/classes.js

479 lines
13 KiB
JavaScript
Raw Normal View History

2020-10-28 06:18:24 -07:00
import {buildDict, buildList, capitalise} from "./helpers";
import MORPHEMES from '../data/pronouns/morphemes';
2020-07-26 04:14:25 -07:00
2020-07-22 13:19:23 -07:00
export class ExamplePart {
constructor(variable, str) {
this.variable = variable;
this.str = str;
}
}
export class Example {
constructor(singularParts, pluralParts, isHonorific = false) {
this.singularParts = singularParts;
this.pluralParts = pluralParts;
this.isHonorific = isHonorific;
2020-07-22 13:19:23 -07:00
}
static parse(str) {
const parts = [];
let lastPosition = 0;
2020-10-28 06:18:24 -07:00
for (let m of str.matchAll(/{('?[a-z_]+)}/g)) {
2020-07-22 13:19:23 -07:00
const textBefore = str.substr(lastPosition, m.index - lastPosition);
if (textBefore.length) {
parts.push(new ExamplePart(false, textBefore));
}
parts.push(new ExamplePart(true, m[0].substr(1, m[0].length - 2)));
lastPosition = m.index + m[0].length;
}
const textAfter = str.substr(lastPosition);
if (textAfter.length) {
parts.push(new ExamplePart(false, textAfter));
}
return parts;
}
2020-11-10 14:41:56 -08:00
format(pronoun) {
const plural = this.isHonorific ? pronoun.pluralHonorific[0] : pronoun.plural[0];
2020-07-22 13:19:23 -07:00
2020-11-10 14:41:56 -08:00
return capitalise(this[plural ? 'pluralParts' : 'singularParts'].map(part => {
return part.variable ? pronoun.getMorpheme(part.str) : part.str;
}).join(''));
2020-07-22 13:19:23 -07:00
}
}
function clone(mainObject) {
let objectCopy = {};
for (let key in mainObject) {
if (mainObject.hasOwnProperty(key)) {
objectCopy[key] = mainObject[key];
}
}
return objectCopy;
}
export class Source {
constructor (type, author, title, extra, year, fragments = [], comment = null, link = null) {
this.type = type;
2020-07-22 13:19:23 -07:00
this.author = author;
this.title = title;
this.extra = extra;
this.year = year;
this.fragments = fragments;
this.comment = comment;
this.link = link;
}
2020-07-27 10:06:41 -07:00
static get TYPES() {
return {
'': 'clipboard-list',
Book: 'book-open',
Article: 'newspaper',
// Movie: 'film',
Series: 'tv',
Song: 'music',
Poetry: 'scroll',
Other: 'comment-alt-lines',
2020-07-27 10:06:41 -07:00
}
}
2020-11-09 11:11:47 -08:00
static get TYPES_PRIORITIES() {
return {
Book: 1,
Article: 2,
Movie: 3,
Series: 3,
Song: 0,
Poetry: 0,
Other: 4,
}
}
2020-07-22 13:19:23 -07:00
icon() {
2020-09-30 12:59:41 -07:00
return Source.TYPES[this.type];
}
}
2020-07-22 13:19:23 -07:00
const escape = s => {
2020-07-26 04:14:25 -07:00
if (Array.isArray(s)) {
s = s.join('&');
}
2020-07-22 13:19:23 -07:00
return (s || '')
2020-07-24 13:11:34 -07:00
.replace(/,/g, '')
.replace(/!/g, '')
.replace(/\./g, '')
//.replace(/\/', '%2F')
.replace(/#/g, '%23')
2020-07-26 04:14:25 -07:00
.replace(/\?/g, '%3F');
2020-07-22 13:19:23 -07:00
}
export class Pronoun {
2020-09-16 13:31:57 -07:00
constructor (canonicalName, description, normative, morphemes, plural, pluralHonorific, sources = [], aliases = [], history = null) {
this.canonicalName = canonicalName;
2020-07-22 13:19:23 -07:00
this.description = description;
2020-09-16 13:31:57 -07:00
this.normative = normative;
2020-07-22 13:19:23 -07:00
this.morphemes = morphemes
this.plural = plural;
this.pluralHonorific = pluralHonorific;
2020-07-22 13:19:23 -07:00
this.sources = sources;
2020-07-24 07:26:03 -07:00
this.aliases = aliases;
2020-07-24 11:21:30 -07:00
this.history = history;
2020-07-22 13:19:23 -07:00
}
pronoun() {
2020-09-29 06:36:49 -07:00
return this.morphemes[MORPHEMES[0]];
2020-07-22 13:19:23 -07:00
}
2020-07-26 04:14:25 -07:00
nameOptions() {
const options = new Set();
2020-09-29 06:36:49 -07:00
const optionsN = this.morphemes[MORPHEMES[0]].split('&');
const optionsG = this.morphemes[MORPHEMES[1]].split('&');
2020-07-26 04:14:25 -07:00
for (let i in optionsN) {
options.add(optionsN[i] + '/' + optionsG[i < optionsG.length - 1 ? i : optionsG.length - 1]);
}
return [...options]
}
name(glue) {
2020-07-26 04:14:25 -07:00
return this.nameOptions().join(glue)
2020-07-22 13:19:23 -07:00
}
clone() {
return new Pronoun(this.canonicalName, this.description, this.normative, clone(this.morphemes), this.plural, this.pluralHonorific);
2020-07-22 13:19:23 -07:00
}
equals(other) {
return this.toString() === other.toString();
}
2020-07-26 04:14:25 -07:00
merge(other) {
return new Pronoun(
this.canonicalName + '&' + other.canonicalName,
2020-07-26 04:14:25 -07:00
Array.isArray(this.description) ? [...this.description, other.description] : [this.description, other.description],
2020-09-16 13:46:15 -07:00
this.normative && other.normative,
2020-07-26 04:14:25 -07:00
buildDict(function* (that, other) {
for (let morpheme of MORPHEMES) {
yield [morpheme, (that.morphemes[morpheme] || '') + '&' + (other.morphemes[morpheme] || '')]
//yield [morpheme, buildMorpheme(that.morphemes[morpheme], that.plural) + '&' + buildMorpheme(other.morphemes[morpheme], other.plural)]
2020-07-26 04:14:25 -07:00
}
}, this, other),
[...this.plural, ...other.plural],
[...this.pluralHonorific, ...other.pluralHonorific],
2020-07-26 04:14:25 -07:00
);
}
getMorpheme(morpheme, counter = 0) {
2020-10-28 06:18:24 -07:00
let capital = false;
if (morpheme.startsWith("'")) {
capital = true;
morpheme = morpheme.substring(1);
}
2020-07-26 04:14:25 -07:00
if (!this.morphemes[morpheme]) {
return null;
}
const options = this.morphemes[morpheme].split('&');
2020-10-28 06:18:24 -07:00
const result = options[counter % options.length];
return capital ? capitalise(result) : result;
2020-07-26 04:14:25 -07:00
}
isPlural(counter = 0) {
return this.plural[counter % this.plural.length]
}
isPluralHonorific(counter = 0) {
return this.pluralHonorific[counter % this.pluralHonorific.length]
}
2020-07-22 13:19:23 -07:00
toArray() {
const elements = Object.values(this.morphemes).map(s => escape(s));
if (process.env.CONFIG.pronouns.plurals) {
elements.push(this.plural.map(p => p ? 1 : 0).join(''));
if (process.env.CONFIG.pronouns.honorifics) {
elements.push(this.pluralHonorific.map(p => p ? 1 : 0).join(''));
}
}
elements.push(escape(this.description));
return elements;
2020-07-22 13:19:23 -07:00
}
toString() {
return this.toArray().join(',');
}
static from(data) {
let extraFields = 1; // description
if (process.env.CONFIG.pronouns.plurals) {
extraFields += 1;
if (![0, 1].includes(parseInt(data[MORPHEMES.length]))) {
return null;
}
if (process.env.CONFIG.pronouns.honorifics) {
extraFields += 1;
if (![0, 1].includes(parseInt(data[MORPHEMES.length + 1]))) {
return null;
}
}
}
if (data.length === MORPHEMES.length + extraFields - 1) {
data.push('');
}
if (data.length !== MORPHEMES.length + extraFields
2020-07-22 13:19:23 -07:00
|| data[0].length === 0
|| data[data.length - 1].length > 48
|| data.slice(1, data.length - extraFields).filter(s => s.length > 12).length
2020-07-22 13:19:23 -07:00
) {
return null;
}
const m = {}
for (let i in MORPHEMES) {
m[MORPHEMES[parseInt(i)]] = data[parseInt(i)];
2020-07-22 13:19:23 -07:00
}
return new Pronoun(
m[MORPHEMES[0]],
data[data.length - 1],
false,
m,
data[MORPHEMES.length].split('').map(p => parseInt(p) === 1),
data[MORPHEMES.length + 1].split('').map(p => parseInt(p) === 1),
)
2020-07-22 13:19:23 -07:00
}
}
2020-08-04 07:15:41 -07:00
export class PronounGroup {
constructor(name, pronouns, description = null) {
2020-09-08 09:10:04 -07:00
this.name = name;
this.pronouns = pronouns;
2020-09-08 09:10:04 -07:00
this.description = description;
}
}
export class PronounLibrary {
constructor(groups, pronouns) {
2020-09-08 09:10:04 -07:00
this.groups = groups;
this.pronouns = pronouns;
2020-09-08 09:10:04 -07:00
}
*split(filter = null, includeOthers = true) {
let pronounsLeft = Object.keys(this.pronouns);
2020-09-08 09:10:04 -07:00
const that = this;
for (let g of this.groups) {
yield [g, buildList(function* () {
for (let t of g.pronouns) {
pronounsLeft = pronounsLeft.filter(i => i !== t);
const pronoun = that.pronouns[t] || t;
if (!filter || filter(pronoun)) {
yield pronoun;
2020-09-08 09:10:04 -07:00
}
}
})];
}
if (!pronounsLeft.length || !includeOthers) {
2020-09-08 09:10:04 -07:00
return;
}
yield [
new PronounGroup('Inne formy', pronounsLeft),
2020-09-08 09:10:04 -07:00
buildList(function* () {
for (let t of pronounsLeft) {
if (!filter || filter(that.pronouns[t])) {
yield that.pronouns[t];
2020-09-08 09:10:04 -07:00
}
}
}),
];
}
find(pronoun) {
if (!pronoun) {
2020-10-09 15:39:34 -07:00
return null;
}
for (let [group, groupPronouns] of this.split()) {
for (let t of groupPronouns) {
if (t.canonicalName === pronoun.canonicalName) {
return {group, groupPronouns};
}
}
}
return null;
}
2020-09-08 09:10:04 -07:00
}
2020-08-04 07:15:41 -07:00
export class Noun {
constructor({id, masc, fem, neutr, mascPl, femPl, neutrPl, approved = true, base_id = null}) {
2020-08-04 07:15:41 -07:00
this.id = id;
this.masc = masc.split('|');
this.fem = fem.split('|');
this.neutr = neutr.split('|');
this.mascPl = mascPl.split('|');
this.femPl = femPl.split('|');
this.neutrPl = neutrPl.split('|');
this.approved = !!approved;
this.base = base_id;
}
matches(filter) {
if (!filter) {
return true;
}
for (let field of ['masc', 'fem', 'neutr', 'mascPl', 'femPl', 'neutrPl']) {
for (let value of this[field]) {
2020-09-29 08:59:23 -07:00
if (value.toLowerCase().indexOf(filter.toLowerCase()) > -1) {
2020-08-04 07:15:41 -07:00
return true;
}
}
}
return false;
}
}
export class NounTemplate {
constructor(masc, fem, neutr, mascPl, femPl, neutrPl) {
this.masc = masc;
this.fem = fem;
this.neutr = neutr;
this.mascPl = mascPl;
this.femPl = femPl;
this.neutrPl = neutrPl;
}
fill(stem) {
return {
masc: this.masc.map(e => stem + e),
fem: this.fem.map(e => stem + e),
neutr: this.neutr.map(e => stem + e),
mascPl: this.mascPl.map(e => stem + e),
femPl: this.femPl.map(e => stem + e),
neutrPl: this.neutrPl.map(e => stem + e),
base: null,
}
}
toString() {
return Object.values(this)
.map(es => es.map(e => '-' + e).join('/'))
.join(', ')
;
}
}
2020-09-29 08:59:23 -07:00
2020-10-11 05:50:33 -07:00
export class NounDeclension {
constructor(endings) {
this.singular = {}
this.plural = {}
for (let k in endings) {
if (!endings.hasOwnProperty(k)) {
continue;
}
const value = endings[k] ? endings[k].split('/') : null;
if (k.endsWith('_pl')) {
this.plural[k.substr(0, k.length - 3)] = value;
} else {
this.singular[k] = value;
}
}
}
matches(word, plural) {
const plurality = plural ? 'plural' : 'singular';
const rep = Object.keys(this[plurality])[0];
for (let ending of this[plurality][rep] || []) {
if (word.endsWith(ending)) {
return ending.length;
}
}
return 0;
}
hasSingular() {
return Object.values(this.singular).filter(x => x !== null).length > 0;
}
hasPlural() {
return Object.values(this.plural).filter(x => x !== null).length > 0;
}
decline(word, plural) {
const plurality = plural ? 'plural' : 'singular';
const rep = Object.keys(this[plurality])[0];
const base = word.substring(0, word.length - this.matches(word, plural));
const options = this[plurality];
return buildDict(function*() {
for (let k in options) {
if (!options.hasOwnProperty(k)) {
continue;
}
yield [
k,
options[k].map(o => base + o),
];
}
});
}
}
2020-09-29 08:59:23 -07:00
export class Name {
constructor(name, origin, meaning, usage, legally, pros, cons, notablePeople, count, links) {
this.name = name;
this.origin = origin;
this.meaning = meaning;
this.usage = usage;
this.legally = legally;
this.pros = pros;
this.cons = cons;
this.notablePeople = notablePeople;
this.count = count; // TODO
this.links = links.filter(l => l.trim().length);
}
matches(filter) {
if (!filter) {
return true;
}
for (let field of ['name', 'meaning']) {
if ((this[field] || '').toLowerCase().indexOf(filter.toLowerCase()) > -1) {
return true;
}
}
return false;
}
}
2020-09-29 10:11:46 -07:00
export class Person {
constructor(name, description, pronouns, sources = []) {
this.name = name;
this.description = description;
this.pronouns = {};
for (let p of pronouns) {
const [language, display, link] = p.split(':');
if (this.pronouns[language] === undefined) {
this.pronouns[language] = [];
}
this.pronouns[language].push({display: display, link: link});
}
this.sources = sources;
}
}