2020-11-10 15:47:44 -08:00
|
|
|
import { Pronoun } from "./classes";
|
|
|
|
import Compressor from "./compressor";
|
2020-11-15 05:13:14 -08:00
|
|
|
import {buildDict, isEmoji} from "./helpers";
|
2020-11-10 15:47:44 -08:00
|
|
|
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 || [],
|
2020-12-25 07:05:28 -08:00
|
|
|
((template.history || '') + '@__generator__').replace(/^@/, ''),
|
2020-12-04 13:09:57 -08:00
|
|
|
false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:47:44 -08:00
|
|
|
export const buildPronoun = (pronouns, path) => {
|
2021-12-16 15:07:05 -08:00
|
|
|
const config = global.config || process.env.CONFIG;
|
|
|
|
|
2020-11-10 15:47:44 -08:00
|
|
|
const pronounsWithAliases = addAliasesToPronouns(pronouns);
|
|
|
|
|
|
|
|
const pronounStr = path.split(',');
|
|
|
|
|
|
|
|
let base = null;
|
|
|
|
for (let option of pronounStr[0].split('&')) {
|
|
|
|
if (!base) {
|
|
|
|
base = pronounsWithAliases[option]
|
2021-03-06 10:54:40 -08:00
|
|
|
} else if (pronounsWithAliases[option]) {
|
2020-11-10 15:47:44 -08:00
|
|
|
base = base.merge(pronounsWithAliases[option])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:13:14 -08:00
|
|
|
let pronoun = pronounStr.length === 1
|
2020-11-10 15:47:44 -08:00
|
|
|
? base
|
|
|
|
: Pronoun.from(Compressor.uncompress(pronounStr, base ? base.toArray() : null));
|
2020-11-15 05:13:14 -08:00
|
|
|
|
2021-12-16 15:07:05 -08:00
|
|
|
if (!config) {
|
2020-11-15 06:55:44 -08:00
|
|
|
return pronoun;
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:07:05 -08:00
|
|
|
if (!pronoun && config.pronouns.emoji !== false && isEmoji(path)) {
|
|
|
|
pronoun = buildPronounFromTemplate(path, config.pronouns.emoji);
|
2020-11-15 05:13:14 -08:00
|
|
|
}
|
|
|
|
|
2021-12-16 15:07:05 -08:00
|
|
|
if (!pronoun && config.pronouns.null && config.pronouns.null.morphemes && path.startsWith(':') && path.length < 12) {
|
|
|
|
pronoun = buildPronounFromTemplate(path.substring(1), config.pronouns.null);
|
2020-11-15 05:13:14 -08:00
|
|
|
}
|
|
|
|
|
2020-12-03 06:38:58 -08:00
|
|
|
const p = path.split('/').filter(s => !!s);
|
2021-12-16 15:07:05 -08:00
|
|
|
if (!pronoun && config.pronouns.slashes !== false) {
|
|
|
|
const slashMorphemes = config.pronouns.slashes === true
|
2021-08-19 08:23:21 -07:00
|
|
|
? MORPHEMES
|
2021-12-16 15:07:05 -08:00
|
|
|
: config.pronouns.slashes;
|
2021-09-05 08:13:08 -07:00
|
|
|
if (slashMorphemes && p.length === slashMorphemes.length) {
|
2021-08-19 08:23:21 -07:00
|
|
|
pronoun = new Pronoun(
|
|
|
|
`${p[0]}/${p[1]}`,
|
|
|
|
'',
|
|
|
|
false,
|
|
|
|
buildDict(function*() {
|
|
|
|
for (let m of MORPHEMES) {
|
|
|
|
const index = slashMorphemes.indexOf(m)
|
|
|
|
yield [m, index === -1 ? '' : p[index]];
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[ p[p.length - 1].endsWith('selves') ], // TODO English specific, extract somewhere
|
|
|
|
[ false ],
|
|
|
|
[],
|
|
|
|
'__generator__',
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
2020-12-03 06:38:58 -08:00
|
|
|
}
|
|
|
|
|
2020-11-15 05:13:14 -08:00
|
|
|
return pronoun;
|
2020-11-10 15:47:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-10 07:23:39 -08:00
|
|
|
t.thirdForm,
|
|
|
|
t.smallForm,
|
|
|
|
t.sourcesInfo,
|
2020-11-10 15:47:44 -08:00
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-11-15 05:13:14 -08:00
|
|
|
|