henkan 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/dist/index.cjs.js +106 -71
  2. package/dist/index.cjs.js.map +3 -3
  3. package/dist/index.mjs +104 -71
  4. package/dist/index.mjs.map +3 -3
  5. package/dist/types/types.d.ts +24 -3
  6. package/dist/types/types.d.ts.map +1 -1
  7. package/dist/types/utils.d.ts.map +1 -1
  8. package/docs/api/README.md +1 -0
  9. package/docs/api/functions/capitalizeString.md +1 -1
  10. package/docs/api/functions/convertJMdict.md +1 -1
  11. package/docs/api/functions/convertKanjiDic.md +1 -1
  12. package/docs/api/functions/convertKradFile.md +1 -1
  13. package/docs/api/functions/convertRadkFile.md +1 -1
  14. package/docs/api/functions/convertTanakaCorpus.md +1 -1
  15. package/docs/api/functions/generateAnkiNote.md +1 -1
  16. package/docs/api/functions/generateAnkiNotesFile.md +1 -1
  17. package/docs/api/functions/getKanji.md +1 -1
  18. package/docs/api/functions/getKanjiExtended.md +1 -1
  19. package/docs/api/functions/getWord.md +1 -1
  20. package/docs/api/functions/isStringArray.md +1 -1
  21. package/docs/api/functions/isValidArray.md +1 -1
  22. package/docs/api/functions/isValidArrayWithFirstElement.md +1 -1
  23. package/docs/api/functions/makeSSML.md +1 -1
  24. package/docs/api/functions/shuffleArray.md +1 -1
  25. package/docs/api/functions/synthesizeSpeech.md +1 -1
  26. package/docs/api/interfaces/DictKanji.md +14 -4
  27. package/docs/api/interfaces/DictKanjiForm.md +4 -4
  28. package/docs/api/interfaces/DictKanjiMisc.md +5 -5
  29. package/docs/api/interfaces/DictKanjiReading.md +3 -3
  30. package/docs/api/interfaces/DictKanjiReadingMeaning.md +3 -3
  31. package/docs/api/interfaces/DictKanjiReadingMeaningGroup.md +3 -3
  32. package/docs/api/interfaces/DictKanjiWithRadicals.md +3 -3
  33. package/docs/api/interfaces/DictMeaning.md +11 -11
  34. package/docs/api/interfaces/DictRadical.md +4 -4
  35. package/docs/api/interfaces/DictReading.md +5 -5
  36. package/docs/api/interfaces/DictWord.md +8 -8
  37. package/docs/api/interfaces/ExamplePart.md +7 -7
  38. package/docs/api/interfaces/Grammar.md +15 -15
  39. package/docs/api/interfaces/GrammarMeaning.md +3 -3
  40. package/docs/api/interfaces/Kana.md +11 -11
  41. package/docs/api/interfaces/Kanji.md +31 -21
  42. package/docs/api/interfaces/KanjiComponent.md +3 -3
  43. package/docs/api/interfaces/KanjiForm.md +3 -3
  44. package/docs/api/interfaces/NoteAndTag.md +31 -0
  45. package/docs/api/interfaces/Phrase.md +4 -4
  46. package/docs/api/interfaces/Radical.md +16 -16
  47. package/docs/api/interfaces/Reading.md +4 -4
  48. package/docs/api/interfaces/ResultEntry.md +7 -7
  49. package/docs/api/interfaces/TanakaExample.md +7 -7
  50. package/docs/api/interfaces/Translation.md +3 -3
  51. package/docs/api/interfaces/UsefulRegExps.md +9 -9
  52. package/docs/api/interfaces/Word.md +14 -14
  53. package/docs/api/type-aliases/Dict.md +1 -1
  54. package/docs/api/type-aliases/DictName.md +1 -1
  55. package/docs/api/type-aliases/EntryType.md +1 -1
  56. package/docs/api/type-aliases/JLPT.md +1 -1
  57. package/docs/api/type-aliases/Result.md +1 -1
  58. package/package.json +3 -3
package/dist/index.cjs.js CHANGED
@@ -1214,6 +1214,13 @@ function convertJMdict(xmlString, examples) {
1214
1214
  const dict = [];
1215
1215
  import_xml2js.default.parseString(dictParsed, (err, result) => {
1216
1216
  if (err) throw err;
1217
+ let tanakaBaseParts = void 0;
1218
+ if (examples)
1219
+ tanakaBaseParts = new Set(
1220
+ examples.map(
1221
+ (example) => example.parts.map((part) => part.baseForm)
1222
+ ).flat()
1223
+ );
1217
1224
  if (result.JMdict && typeof result.JMdict === "object" && isValidArray(result.JMdict.entry))
1218
1225
  for (const entry of result.JMdict.entry) {
1219
1226
  const entryObj = {
@@ -1309,21 +1316,19 @@ function convertJMdict(xmlString, examples) {
1309
1316
  ) : void 0;
1310
1317
  let kanjiFormExamples = false;
1311
1318
  let readingExamples = false;
1312
- if (kanjiForms2) {
1313
- outer: for (const example of examples)
1314
- for (const part of example.parts)
1315
- if (kanjiForms2.has(part.baseForm)) {
1316
- kanjiFormExamples = true;
1317
- break outer;
1318
- }
1319
+ if (kanjiForms2 && kanjiForms2.size > 0 && tanakaBaseParts) {
1320
+ for (const kf of kanjiForms2)
1321
+ if (tanakaBaseParts.has(kf)) {
1322
+ kanjiFormExamples = true;
1323
+ break;
1324
+ }
1319
1325
  }
1320
- if (!kanjiFormExamples) {
1321
- outer: for (const example of examples)
1322
- for (const part of example.parts)
1323
- if (readings2.has(part.baseForm)) {
1324
- readingExamples = true;
1325
- break outer;
1326
- }
1326
+ if (!kanjiFormExamples && readings2.size > 0 && tanakaBaseParts) {
1327
+ for (const r of readings2)
1328
+ if (tanakaBaseParts.has(r)) {
1329
+ readingExamples = true;
1330
+ break;
1331
+ }
1327
1332
  }
1328
1333
  if (kanjiFormExamples || readingExamples)
1329
1334
  entryObj.hasPhrases = true;
@@ -1392,8 +1397,11 @@ function convertKanjiDic(xmlString) {
1392
1397
  }
1393
1398
  if (isValidArray(group.meaning)) {
1394
1399
  for (const meaning of group.meaning)
1395
- if (typeof meaning === "string")
1400
+ if (typeof meaning === "string") {
1401
+ if (kanjiObj.isKokuji === void 0 && meaning === "(kokuji)")
1402
+ kanjiObj.isKokuji = true;
1396
1403
  groupObj.meanings.push(meaning);
1404
+ }
1397
1405
  }
1398
1406
  if (groupObj.readings.length > 0 || groupObj.meanings.length > 0)
1399
1407
  rmObj.groups.push(groupObj);
@@ -1575,11 +1583,12 @@ function lookupWordNote(key, notes, tags, required, fallback) {
1575
1583
  );
1576
1584
  if (!info) {
1577
1585
  if (required) throw new Error(`Invalid note info for ${key}`);
1578
- notes.push(fallback != null ? fallback : key);
1579
- return;
1586
+ if (notes) notes.push(fallback != null ? fallback : key);
1587
+ return { note: fallback != null ? fallback : key };
1580
1588
  }
1581
- tags.push(info[0]);
1582
- notes.push(info[1]);
1589
+ if (tags) tags.push(`word::${info[0]}`);
1590
+ if (notes) notes.push(info[1]);
1591
+ return { note: info[1], tag: `word::${info[0]}` };
1583
1592
  }
1584
1593
  var wordAddNoteArray = (arr, cb) => {
1585
1594
  if (!arr) return;
@@ -1596,40 +1605,50 @@ function getWord(dict, id, kanjiDic, examples, dictWord, noteTypeName, deckPath)
1596
1605
  translations: [],
1597
1606
  noteID: `word_${dictWord.id}`,
1598
1607
  ...noteTypeName ? { noteTypeName } : {},
1599
- ...deckPath ? { deckPath } : {}
1608
+ ...deckPath ? { deckPath } : {},
1609
+ tags: []
1600
1610
  };
1611
+ if (dictWord.isCommon === true) word.common = true;
1601
1612
  if (dictWord.kanjiForms)
1602
1613
  word.kanjiForms = dictWord.kanjiForms.map(
1603
- (dictKanjiForm) => {
1604
- if (dictKanjiForm.commonness && dictKanjiForm.commonness.length > 0 && word.common === void 0)
1605
- word.common = true;
1606
- return {
1607
- kanjiForm: dictKanjiForm.form,
1608
- ...dictKanjiForm.notes ? {
1609
- notes: dictKanjiForm.notes.map(
1610
- (note) => capitalizeString(note)
1611
- )
1612
- } : {}
1613
- };
1614
- }
1614
+ (dictKanjiForm) => ({
1615
+ kanjiForm: dictKanjiForm.form,
1616
+ ...dictKanjiForm.notes ? {
1617
+ notes: dictKanjiForm.notes.map((note) => {
1618
+ var _a;
1619
+ const noteAndTag = lookupWordNote(
1620
+ note,
1621
+ void 0,
1622
+ word.tags,
1623
+ false,
1624
+ note
1625
+ );
1626
+ return capitalizeString((_a = noteAndTag.note) != null ? _a : note);
1627
+ })
1628
+ } : {}
1629
+ })
1615
1630
  );
1616
- word.readings = dictWord.readings.map((dictReading) => {
1617
- if (dictReading.commonness && dictReading.commonness.length > 0 && word.common === void 0)
1618
- word.common = true;
1619
- return {
1620
- reading: dictReading.reading,
1621
- ...dictReading.kanjiFormRestrictions || dictReading.notes ? {
1622
- notes: [
1623
- ...dictReading.kanjiFormRestrictions ? dictReading.kanjiFormRestrictions.map(
1624
- (restriction) => `Reading restricted to ${restriction}`
1625
- ) : [],
1626
- ...dictReading.notes ? dictReading.notes.map(
1627
- (note) => capitalizeString(note)
1628
- ) : []
1629
- ]
1630
- } : {}
1631
- };
1632
- });
1631
+ word.readings = dictWord.readings.map((dictReading) => ({
1632
+ reading: dictReading.reading,
1633
+ ...dictReading.kanjiFormRestrictions || dictReading.notes ? {
1634
+ notes: [
1635
+ ...dictReading.kanjiFormRestrictions ? dictReading.kanjiFormRestrictions.map(
1636
+ (restriction) => `Reading restricted to ${restriction}`
1637
+ ) : [],
1638
+ ...dictReading.notes ? dictReading.notes.map((note) => {
1639
+ var _a;
1640
+ const noteAndTag = lookupWordNote(
1641
+ note,
1642
+ void 0,
1643
+ word.tags,
1644
+ false,
1645
+ note
1646
+ );
1647
+ return capitalizeString((_a = noteAndTag.note) != null ? _a : note);
1648
+ }) : []
1649
+ ]
1650
+ } : {}
1651
+ }));
1633
1652
  let usuallyInKanaMeanings = 0;
1634
1653
  word.translations = dictWord.meanings.map((dictMeaning) => {
1635
1654
  if (!dictMeaning.translations)
@@ -1650,7 +1669,6 @@ function getWord(dict, id, kanjiDic, examples, dictWord, noteTypeName, deckPath)
1650
1669
  }
1651
1670
  );
1652
1671
  const notes = [];
1653
- word.tags = [];
1654
1672
  wordAddNoteArray(
1655
1673
  dictMeaning.kanjiFormRestrictions,
1656
1674
  (restriction) => notes.push(`Meaning restricted to ${restriction}`)
@@ -1721,7 +1739,7 @@ function getWord(dict, id, kanjiDic, examples, dictWord, noteTypeName, deckPath)
1721
1739
  }
1722
1740
  if (word.kanji.length === 0) delete word.kanji;
1723
1741
  }
1724
- if (examples) {
1742
+ if (examples && dictWord.hasPhrases === true) {
1725
1743
  const readings = new Set(
1726
1744
  word.readings.filter(
1727
1745
  (reading) => !reading.notes || reading.notes && !reading.notes.some(
@@ -1793,6 +1811,7 @@ function getWord(dict, id, kanjiDic, examples, dictWord, noteTypeName, deckPath)
1793
1811
  }
1794
1812
  }
1795
1813
  function getKanji(kanjiChar, dict, jmDict, svgList, noteTypeName, deckPath) {
1814
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1796
1815
  try {
1797
1816
  const dictKanji = dict.find(
1798
1817
  (entry) => entry.kanji === kanjiChar
@@ -1907,26 +1926,28 @@ function getKanji(kanjiChar, dict, jmDict, svgList, noteTypeName, deckPath) {
1907
1926
  let codePoint = kanji.kanji.codePointAt(0);
1908
1927
  if (codePoint !== void 0) {
1909
1928
  codePoint = codePoint.toString(16);
1929
+ const fileNames = [
1930
+ `0${codePoint}.svg`,
1931
+ `${codePoint}.svg`
1932
+ ];
1910
1933
  const svg = svgList.find(
1911
- (svgFile) => svgFile.toLowerCase() === `0${codePoint}.svg` || svgFile.toLowerCase() === `${codePoint}.svg`
1934
+ (svgFile) => fileNames.includes(svgFile.toLowerCase())
1912
1935
  );
1913
1936
  if (svg) kanji.svg = svg;
1914
1937
  }
1915
1938
  }
1916
1939
  kanji.tags = [];
1917
- if (kanji.meanings && kanji.meanings.some((meaning) => meaning === "(kokuji)")) {
1918
- kanji.tags.push("kokuji");
1919
- kanji.meanings = kanji.meanings.filter(
1920
- (meaning) => meaning !== "(kokuji)"
1921
- );
1940
+ if (dictKanji.isKokuji === true) {
1941
+ kanji.kokuji = true;
1942
+ kanji.tags.push("kanji::kokuji");
1922
1943
  }
1923
1944
  kanji.tags.push(
1924
- ...!kanji.onyomi ? ["no::onyomi"] : [],
1925
- ...!kanji.kunyomi ? ["no::kunyomi"] : [],
1926
- ...kanji.nanori ? ["has::nanori"] : [],
1927
- ...kanji.svg ? ["has::svg"] : [],
1928
- ...kanji.strokes && kanji.strokes.length > 0 ? [`strokes::${kanji.strokes}`] : [],
1929
- ...kanji.words ? ["has::words"] : []
1945
+ `kanji::onyomi::${(_b = (_a = kanji.onyomi) == null ? void 0 : _a.length) != null ? _b : 0}`,
1946
+ `kanji::kunyomi::${(_d = (_c = kanji.kunyomi) == null ? void 0 : _c.length) != null ? _d : 0}`,
1947
+ `kanji::nanori::${(_f = (_e = kanji.nanori) == null ? void 0 : _e.length) != null ? _f : 0}`,
1948
+ `kanji::strokes::${(_g = kanji.strokes) != null ? _g : "unknown"}`,
1949
+ `kanji::words::${(_i = (_h = kanji.words) == null ? void 0 : _h.length) != null ? _i : 0}`,
1950
+ ...kanji.svg ? ["kanji::has_svg"] : []
1930
1951
  );
1931
1952
  return kanji;
1932
1953
  } else throw new Error(`Kanji ${kanjiChar} not found`);
@@ -1935,6 +1956,7 @@ function getKanji(kanjiChar, dict, jmDict, svgList, noteTypeName, deckPath) {
1935
1956
  }
1936
1957
  }
1937
1958
  function getKanjiExtended(kanjiChar, info, dict, useJpdbWords, jmDict, svgList, noteTypeName, deckPath) {
1959
+ var _a, _b, _c, _d;
1938
1960
  try {
1939
1961
  const kanji = getKanji(
1940
1962
  kanjiChar,
@@ -1950,12 +1972,25 @@ function getKanjiExtended(kanjiChar, info, dict, useJpdbWords, jmDict, svgList,
1950
1972
  kanji.mnemonic = info.mnemonic;
1951
1973
  if (useJpdbWords === true && info.words && info.words.length > 0)
1952
1974
  kanji.words = info.words;
1953
- if (kanji.mnemonic && kanji.mnemonic.length > 0 && kanji.tags)
1954
- kanji.tags.push("has::mnemonic");
1955
- if (kanji.components && kanji.components.length > 0 && kanji.tags)
1956
- kanji.tags.push(`components::${kanji.components.length}`);
1957
- if (kanji.words && kanji.tags && !kanji.tags.includes("has::words"))
1958
- kanji.tags.push("has::words");
1975
+ if (kanji.tags) {
1976
+ kanji.tags.push(`kanji::components::${(_b = (_a = kanji.components) == null ? void 0 : _a.length) != null ? _b : 0}`);
1977
+ if (kanji.mnemonic && kanji.mnemonic.length > 0)
1978
+ kanji.tags.push("kanji::has_mnemonic");
1979
+ if (useJpdbWords === true && kanji.words) {
1980
+ if (!kanji.tags.some((tag, index) => {
1981
+ var _a2, _b2;
1982
+ if (tag.startsWith("kanji::words::")) {
1983
+ kanji.tags.splice(
1984
+ index,
1985
+ 1,
1986
+ `kanji::words::${(_b2 = (_a2 = kanji.words) == null ? void 0 : _a2.length) != null ? _b2 : 0}`
1987
+ );
1988
+ return true;
1989
+ } else return false;
1990
+ }))
1991
+ kanji.tags.push(`kanji::words::${(_d = (_c = kanji.words) == null ? void 0 : _c.length) != null ? _d : 0}`);
1992
+ }
1993
+ }
1959
1994
  if (kanji.fromJpdb === true && (kanji.mnemonic || kanji.components && kanji.components.length > 0 || kanji.words))
1960
1995
  kanji.source = `https://jpdb.io/kanji/${kanji.kanji}#a`;
1961
1996
  return kanji;
@@ -2160,12 +2195,12 @@ function generateAnkiNote(entry) {
2160
2195
  (onyomiEntry) => createEntry(
2161
2196
  `<span class="kanji kanji-onyomi">${onyomiEntry}</span>`
2162
2197
  )
2163
- ).join("") : '<span class="kanji kanji-onyomi">(no onyomi) (kokuji)</span>',
2198
+ ).join("") : `<span class="kanji kanji-onyomi">(no onyomi) ${entry.kokuji === true ? "(kokuji)" : ""}</span`,
2164
2199
  entry.kunyomi ? entry.kunyomi.map(
2165
2200
  (kunyomiEntry) => createEntry(
2166
2201
  `<span class="kanji kanji-kunyomi">${kunyomiEntry}</span>`
2167
2202
  )
2168
- ).join("") : '<span class="kanji kanji-kunyomi">(no kunyomi)</span>',
2203
+ ).join("") : `<span class="kanji kanji-kunyomi">(no kunyomi) ${entry.kokuji === true ? "(kokuji)" : ""}</span>`,
2169
2204
  entry.nanori ? entry.nanori.map(
2170
2205
  (nanoriEntry) => createEntry(
2171
2206
  `<span class="kanji kanji-nanori">${nanoriEntry}</span>`