yukichant 5.0.0 → 5.0.1

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +23 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yukichant",
3
- "version": "5.0.0",
3
+ "version": "5.0.1",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
package/src/index.js CHANGED
@@ -13,7 +13,11 @@ export let default_encoder = (uint8text,{ meisi, dousi }, option = {}) => {
13
13
  //読みやすさのため動詞の最後には読点を入れる
14
14
  let _dousi = Object.fromEntries(
15
15
  Object.entries(dousi).map(([k,entry])=> {
16
- return [k, { ...entry, words: entry.words.map(d=>`${d}。`) } ]
16
+ // 後方互換性のため、entryが配列(旧形式)の場合はオブジェクトに変換する
17
+ const normalizedEntry = Array.isArray(entry)
18
+ ? { words: entry, readings: entry } // 旧形式には読みがないので単語を読みに流用
19
+ : entry;
20
+ return [k, { ...normalizedEntry, words: normalizedEntry.words.map(d=>`${d}。`) } ]
17
21
  })
18
22
  )
19
23
 
@@ -23,8 +27,12 @@ export let default_encoder = (uint8text,{ meisi, dousi }, option = {}) => {
23
27
  .slice(0,-1)
24
28
  .map((code,i)=> (i + 1) % 4 ? meisi[code] : _dousi[code])
25
29
  .map(v=> {
26
- const index = Math.floor(Math.random() * v.words.length)
27
- return { word: v.words[index], reading: v.readings[index] }
30
+ // 後方互換性のため、vが配列(旧形式)の場合はオブジェクトに変換する
31
+ const normalizedV = Array.isArray(v)
32
+ ? { words: v, readings: v }
33
+ : v;
34
+ const index = Math.floor(Math.random() * normalizedV.words.length)
35
+ return { word: normalizedV.words[index], reading: normalizedV.readings[index] }
28
36
  })
29
37
 
30
38
  // 最後の文字列を必ず動詞で終えることで呪文詠唱となる。
@@ -32,8 +40,12 @@ export let default_encoder = (uint8text,{ meisi, dousi }, option = {}) => {
32
40
  .slice(-1)
33
41
  .map(code=> _dousi[code])
34
42
  .map(v=> {
35
- const index = Math.floor(Math.random() * v.words.length)
36
- return { word: v.words[index], reading: v.readings[index] }
43
+ // 後方互換性のため、vが配列(旧形式)の場合はオブジェクトに変換する
44
+ const normalizedV = Array.isArray(v)
45
+ ? { words: v, readings: v }
46
+ : v;
47
+ const index = Math.floor(Math.random() * normalizedV.words.length)
48
+ return { word: normalizedV.words[index], reading: normalizedV.readings[index] }
37
49
  })
38
50
 
39
51
  const words = [ ...heads , ...last ]
@@ -60,14 +72,18 @@ export let default_decoder = (typoCorrection) => async (encodeText,option = {} ,
60
72
  let decodeHash = {}
61
73
  let allWord = []
62
74
  Object.entries(meisi).forEach(([k,entry])=> {
63
- const v = entry.words
75
+ // 後方互換性のため、entryが配列(旧形式)の場合はオブジェクトに変換する
76
+ const normalizedEntry = Array.isArray(entry) ? { words: entry } : entry;
77
+ const v = normalizedEntry.words
64
78
  allWord = [ ...allWord, ...v]
65
79
  v.forEach(v2=> {
66
80
  decodeHash[v2] = k
67
81
  })
68
82
  })
69
83
  Object.entries(dousi).forEach(([k,entry])=> {
70
- const v = entry.words
84
+ // 後方互換性のため、entryが配列(旧形式)の場合はオブジェクトに変換する
85
+ const normalizedEntry = Array.isArray(entry) ? { words: entry } : entry;
86
+ const v = normalizedEntry.words
71
87
  allWord = [ ...allWord, ...v]
72
88
  v.forEach(v2=> {
73
89
  decodeHash[v2] = k