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/buildPronoun.js

124 lines
3.6 KiB
JavaScript
Raw Normal View History

import { Pronoun } from "./classes";
import Compressor from "./compressor";
2020-11-15 05:13:14 -08:00
import {buildDict, isEmoji} from "./helpers";
import MORPHEMES from '../data/pronouns/morphemes';
export const addAliasesToPronouns = (pronouns) => {
const pronounsWithAliases = {}
for (let base in pronouns) {
if (pronouns.hasOwnProperty(base)) {
const pronoun = pronouns[base];
pronounsWithAliases[base] = pronoun;
for (let alias of pronoun.aliases) {
pronounsWithAliases[alias] = pronoun;
}
}
}
return pronounsWithAliases;
}
export const getPronoun = (pronouns, id) => {
return addAliasesToPronouns(pronouns)[id];
}
2020-12-04 13:09:57 -08:00
const buildPronounFromTemplate = (key, template) => {
return new Pronoun(
key,
template.description,
template.normative || false,
buildDict(function*(morphemes) {
for (let k in morphemes) {
if (morphemes.hasOwnProperty(k)) {
yield [k, morphemes[k].replace(/#/g, key)];
}
}
}, template.morphemes),
[template.plural || false],
[template.pluralHonorific || false],
template.aliases || [],
template.history || '',
false,
);
}
export const buildPronoun = (pronouns, path) => {
const pronounsWithAliases = addAliasesToPronouns(pronouns);
const pronounStr = path.split(',');
let base = null;
for (let option of pronounStr[0].split('&')) {
if (!base) {
base = pronounsWithAliases[option]
} else {
base = base.merge(pronounsWithAliases[option])
}
}
2020-11-15 05:13:14 -08:00
let pronoun = pronounStr.length === 1
? base
: Pronoun.from(Compressor.uncompress(pronounStr, base ? base.toArray() : null));
2020-11-15 05:13:14 -08:00
2020-11-15 06:55:44 -08:00
if (!process.env.CONFIG) {
return pronoun;
}
2020-11-15 05:13:14 -08:00
if (!pronoun && process.env.CONFIG.pronouns.emoji !== false && isEmoji(path)) {
pronoun = buildPronounFromTemplate(path, process.env.CONFIG.pronouns.emoji);
}
if (!pronoun && process.env.CONFIG.pronouns.null !== false && path.startsWith(':') && path.length < 12) {
pronoun = buildPronounFromTemplate(path.substring(1), process.env.CONFIG.pronouns.null);
}
2020-12-03 06:38:58 -08:00
const p = path.split('/').filter(s => !!s);
if (!pronoun && process.env.CONFIG.pronouns.slashes !== false && p.length === MORPHEMES.length) {
pronoun = new Pronoun(
`${p[0]}/${p[1]}`,
'',
false,
buildDict(function*() {
let i = 0;
for (let m of MORPHEMES) {
yield [m, p[i++]];
}
}),
[ p[p.length - 1].endsWith('selves') ], // TODO English specific, extract somewhere
[ false ],
[],
null,
false,
)
}
2020-11-15 05:13:14 -08:00
return pronoun;
}
export const parsePronouns = (pronounsRaw) => {
return buildDict(function* () {
for (let t of pronounsRaw) {
const aliases = t.key.split(',');
yield [
aliases[0],
new Pronoun(
aliases[0],
t.description,
t.normative,
buildDict(function* () {
for (let morpheme of MORPHEMES) {
yield [morpheme, t[morpheme]];
}
}),
[t.plural],
[t.pluralHonorific],
aliases.slice(1),
t.history,
2020-11-28 07:52:48 -08:00
t.pronounceable,
)
];
}
});
}
2020-11-15 05:13:14 -08:00