Skip to content

Commit

Permalink
start adding types
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanVukovic99 committed Oct 15, 2024
1 parent b3a131d commit b942ea3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
19 changes: 12 additions & 7 deletions 3-tidy-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const { sortTags, similarSort, mergePersonTags, consoleOverwrite, clearConsoleLi
/** @type {LemmaDict} */
const lemmaDict = {};

/** @type {FormsMap} */
const formsMap = new Map();

/** @type {AutomatedForms} */
const automatedForms = new Map();

/**
Expand Down Expand Up @@ -67,7 +70,6 @@ function handleLevel(glossTree, level) {
const childDefs = handleLevel(children, nextLevel);

const listType = level === 1 ? "li" : "number";
/** @type {StructuredContent[]} */
const content = level === 1 ? def : [{ "tag": "span", "data": { "listType": "number" }, "content": `${defIndex}. ` }, def];

nestDefs.push([
Expand Down Expand Up @@ -99,16 +101,16 @@ function handleNest(glossTree, sense) {
* @param {string} form
* @param {string} pos
* @param {string} lemma
* @param {string[]} inflections
* @param {string[]|Set<string>} inflections
*/
function addDeinflections(form, pos, lemma, inflections) {
if (targetIso === 'fr') {
form = form.replace(/(qu\')?(ils\/elles|il\/elle\/on)\s*/, '');
}

const lemmaForms = formsMap.get(lemma) || new Map();
const lemmaForms = formsMap.get(lemma) || /** @type {Map<Form, Map<PoS, string[]>>} */ (new Map());
formsMap.set(lemma, lemmaForms);
const formPOSs = lemmaForms.get(form) || new Map();
const formPOSs = lemmaForms.get(form) || /** @type {Map<PoS, string[]>} */ (new Map());
lemmaForms.set(form, formPOSs);
formPOSs.get(pos) || formPOSs.set(pos, []);

Expand Down Expand Up @@ -267,7 +269,7 @@ function handleLine(parsedLine) {
}

/**
* @param {Form[]|undefined} forms
* @param {FormInfo[]|undefined} forms
* @param {string} word
* @param {string} pos
*/
Expand All @@ -285,7 +287,9 @@ function processForms(forms, word, pos) {
const isIdentity = !tags.some(value => !identityTags.includes(value));
if (isIdentity) return;

/** @type {Map<Form, Map<PoS, string[]|Set<string>>>} */
const wordMap = automatedForms.get(word) || new Map();
/** @type {Map<string, Set<string>|string[]>} */
const formMap = wordMap.get(form) || new Map();
formMap.get(pos) || formMap.set(pos, new Set());
wordMap.set(form, formMap);
Expand Down Expand Up @@ -483,7 +487,7 @@ function getCanonicalWordForm({word, forms}) {

/**
* @param {string|undefined} word
* @param {Form[]} forms
* @param {FormInfo[]} forms
* @returns {string|undefined}
*/
function getCanonicalForm(word, forms) {
Expand Down Expand Up @@ -619,13 +623,14 @@ lr.on('end', () => {

const formsFilePath = `${writeFolder}/${sourceIso}-${targetIso}-forms.json`;

/** @type {{[chunkIndex: string]: FormsMap}} */
const mapChunks = Array.from(formsMap.entries()).reduce((acc, [key, value], index) => {
logProgress("Chunking form dict", index, formsMap.size);
const chunkIndex = Math.floor(index / 10000);
acc[chunkIndex] ??= new Map();
acc[chunkIndex].set(key, value);
return acc;
}, {});
}, /** @type {{[chunkIndex: string]: FormsMap}} */ ({}));

if(!mapChunks['0']) {
mapChunks['0'] = new Map();
Expand Down
1 change: 1 addition & 0 deletions 4-make-yomitan.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-nocheck
const path = require('path');
const { readFileSync, writeFileSync, existsSync, readdirSync, mkdirSync, unlinkSync } = require('fs');
const { sortTags, writeInBatches, consoleOverwrite,
Expand Down
5 changes: 4 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"*": ["./types/*"],
"ext/json-schema": ["./types/ext/json-schema"]
},
"exclude": ["node_modules", "**/node_modules/*"]
"exclude": [
"node_modules",
"**/node_modules/*"
]
}

10 changes: 8 additions & 2 deletions types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare global {
word?: string;
pos?: string;
sounds?: Sound[];
forms?: Form[];
forms?: FormInfo[];
senses?: KaikkiSense[];
}

Expand All @@ -26,7 +26,7 @@ declare global {
note?: string;
}

type Form = {
type FormInfo = {
form?: string;
tags?: string[];
}
Expand Down Expand Up @@ -92,6 +92,12 @@ declare global {
content: StructuredContent,
}

type Lemma = string;
type Form = string;
type PoS = string;
type FormsMap = Map<Lemma, Map<Form, Map<PoS, string[]>>>;
type AutomatedForms = Map<Lemma, Map<Form, Map<PoS, Set<string>|string[]>>>;

type NestedObject = {
[key: string]: NestedObject | any;
}
Expand Down
13 changes: 12 additions & 1 deletion util/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-nocheck
const path = require('path');
const { readFileSync, writeFileSync, existsSync } = require('fs');
const date = require('date-and-time');
Expand Down Expand Up @@ -35,7 +36,10 @@ function sortTags(targetIso, tags) {
}

// sorts inflection entries to be nearby similar inflections

/**
* @param {string[]} tags
* @returns {string[]}
*/
function similarSort(tags) {
return tags.sort((a, b) => {
const aWords = a.split(' ');
Expand All @@ -62,6 +66,11 @@ function similarSort(tags) {
// input: ['first-person singular present', 'third-person singular present']
// output: ['first/third-person singular present']

/**
* @param {string} targetIso
* @param {string[]} tags
* @returns {string[]}
*/
function mergePersonTags(targetIso, tags) {
const persons = ["first-person", "second-person", "third-person"];

Expand All @@ -70,7 +79,9 @@ function mergePersonTags(targetIso, tags) {
return items.sort((a, b) => persons.indexOf(a) - persons.indexOf(b));
}

/** @type {string[]} */
const result = [];
/** @type {Object<string, string[]>} */
const mergeObj = {};

for (const item of tags) {
Expand Down

0 comments on commit b942ea3

Please sign in to comment.