1
+ /**
2
+ * Yoroshiku
3
+ * @author Loreto Parisi (loretoparisi at gmail dot com)
4
+ * @2019 Loreto Parisi
5
+ */
6
+
7
+ ( function ( ) {
8
+
9
+ const KuroshiroLib = require ( './kuroshiro/core' ) ;
10
+ const KuromojiAnalyzerLib = require ( './kuromoji-analyzer/index' ) ;
11
+
12
+ var Yoroshiku ;
13
+ Yoroshiku = ( function ( ) {
14
+
15
+ /**
16
+ * Yoroshiku
17
+ * yoroshiku converts Japanese to Hiragana, Katakana or Romaji. It supports furigana and okurigana.converts Japanese to Hiragana, Katakana or Romaji. It supports furigana and okurigana.
18
+ */
19
+ function Yoroshiku ( options ) {
20
+ // default options
21
+ this . _options = {
22
+ } ;
23
+ for ( var attr in options ) this . _options [ attr ] = options [ attr ] ;
24
+
25
+ } //Yoroshiku
26
+
27
+ /**
28
+ * Unload process and kill
29
+ */
30
+ Yoroshiku . prototype . unload = function ( ) {
31
+ var self = this ;
32
+ return new Promise ( function ( resolve , reject ) {
33
+ // @TODO
34
+ return resolve ( true ) ;
35
+ } ) ;
36
+ } //unload
37
+
38
+ /**
39
+ * Transliterate text and return
40
+ */
41
+ Yoroshiku . prototype . load = function ( ) {
42
+ var self = this ;
43
+ return new Promise ( function ( resolve , reject ) {
44
+ self . kuroshiro = new KuroshiroLib ( ) ;
45
+ // dictPath: "url/to/dictFiles"
46
+ self . kuroshiro . init ( new KuromojiAnalyzerLib ( { } ) )
47
+ . then ( _ => {
48
+ return resolve ( true ) ;
49
+ } )
50
+ . catch ( error => {
51
+ return reject ( error ) ;
52
+ } ) ;
53
+ } ) ;
54
+ } //load
55
+
56
+ /**
57
+ * Transliterate
58
+ * @param text {String} Text
59
+ * @param params {Object} Options:
60
+ * to Target syllabary [hiragana, katakana, romaji] - "hiragana"
61
+ * mode Convert mode [normal, spaced, okurigana, furigana] - "normal"
62
+ * romajiSystem Romanization system [nippon, passport, hepburn] - "hepburn"
63
+ * delimiter_start Delimiter(Start) - "("
64
+ * delimiter_end Delimiter(End) - ")"
65
+ */
66
+ Yoroshiku . prototype . transliterate = function ( text , params = { } ) {
67
+ var self = this ;
68
+ var options = {
69
+ // Convert mode [normal, spaced, okurigana, furigana] - "normal"
70
+ mode : "spaced" ,
71
+ // Target syllabary [hiragana, katakana, romaji] - "hiragana"
72
+ to : "romaji" ,
73
+ // Romanization system [nippon, passport, hepburn] - "hepburn"
74
+ // nippon - ISO-3602 http://www.age.ne.jp/x/nrs/iso3602/iso3602.html
75
+ // hepburn - BS 4812 : 1972 https://archive.is/PiJ4
76
+ // passport - https://www.ezairyu.mofa.go.jp/passport/hebon.html
77
+ // comparison - http://jgrammar.life.coocan.jp/ja/data/rohmaji2.htm
78
+ romajiSystem : "hepburn" ,
79
+ // delimiter_start Delimiter(Start) - "("
80
+ delimiter_start : "(" ,
81
+ // delimiter_end Delimiter(End) - ")"
82
+ delimiter_end : ")"
83
+ } ;
84
+ for ( var attr in params ) options [ attr ] = params [ attr ] ;
85
+ return new Promise ( function ( resolve , reject ) {
86
+ self . kuroshiro . convert ( text , options )
87
+ . then ( res => {
88
+ res = res . replace ( / \s ( \n + ) \s / g, '$1' ) ; // newline
89
+ res = res . replace ( / \s ( [ \? : ; , . ^ \. \' \- \/ \+ \< \> , & ] ) / g, '$1' ) ; // punct
90
+ res = res . replace ( / [ \t \r ] + / g, ' ' ) ; // contract spaces
91
+ return resolve ( res ) ;
92
+ } )
93
+ . catch ( error => {
94
+ return reject ( error ) ;
95
+ } ) ;
96
+ } ) ;
97
+ } //transliterate
98
+
99
+ /**
100
+ * Check if input char is hiragana.
101
+ */
102
+ Yoroshiku . prototype . isHiragana = function ( char ) {
103
+ return this . kuroshiro . isHiragana ( char ) ;
104
+ } //isHiragana
105
+
106
+ /**
107
+ * Check if input char is katakana.
108
+ */
109
+ Yoroshiku . prototype . isKatakana = function ( char ) {
110
+ return this . kuroshiro . isHiragana ( char ) ;
111
+ } //isKatakana
112
+
113
+ /**
114
+ * Check if input char is kana.
115
+ */
116
+ Yoroshiku . prototype . isKana = function ( char ) {
117
+ return this . kuroshiro . isKana ( char ) ;
118
+ } //isKatakana
119
+
120
+ /**
121
+ * Check if input char is kanji.
122
+ */
123
+ Yoroshiku . prototype . isKanji = function ( char ) {
124
+ return this . kuroshiro . isKanji ( char ) ;
125
+ } //isKatakana
126
+
127
+ /**
128
+ * Check if input char is Japanese.
129
+ */
130
+ Yoroshiku . prototype . isJapanese = function ( char ) {
131
+ return this . kuroshiro . isJapanese ( char ) ;
132
+ } //isJapanese
133
+
134
+ /**
135
+ * Check if input string has hiragana.
136
+ */
137
+ Yoroshiku . prototype . hasHiragana = function ( char ) {
138
+ return this . kuroshiro . hasHiragana ( char ) ;
139
+ } //hasHiragana
140
+
141
+ /**
142
+ * Check if input string has katakana.
143
+ */
144
+ Yoroshiku . prototype . hasKatakana = function ( char ) {
145
+ return this . kuroshiro . hasKatakana ( char ) ;
146
+ } //hasKatakana
147
+
148
+ /**
149
+ * Check if input string has kana.
150
+ */
151
+ Yoroshiku . prototype . hasKana = function ( char ) {
152
+ return this . kuroshiro . hasKana ( char ) ;
153
+ } //hasKana
154
+
155
+ /**
156
+ * Check if input string has kanji.
157
+ */
158
+ Yoroshiku . prototype . hasKanji = function ( char ) {
159
+ return this . kuroshiro . hasKanji ( char ) ;
160
+ } //hasKanji
161
+
162
+ /**
163
+ * Check if input string has kanji.
164
+ */
165
+ Yoroshiku . prototype . hasJapanese = function ( char ) {
166
+ return this . kuroshiro . hasJapanese ( char ) ;
167
+ } //hasJapanese
168
+
169
+ /**
170
+ * Convert input kana string to hiragana.
171
+ */
172
+ Yoroshiku . prototype . kanaToHiragna = function ( char ) {
173
+ return this . kuroshiro . kanaToHiragna ( char ) ;
174
+ } //kanaToHiragna
175
+
176
+ /**
177
+ * Convert input kana string to katakana.
178
+ */
179
+ Yoroshiku . prototype . kanaToKatakana = function ( char ) {
180
+ return this . kuroshiro . kanaToKatakana ( char ) ;
181
+ } //kanaToKatakana
182
+
183
+ /**
184
+ * Convert input kana string to romaji. Param system accepts "nippon", "passport", "hepburn" (Default: "hepburn")
185
+ */
186
+ Yoroshiku . prototype . kanaToRomaji = function ( char ) {
187
+ return this . kuroshiro . kanaToRomaji ( char ) ;
188
+ } //kanaToRomaji
189
+
190
+ return Yoroshiku ;
191
+
192
+ } ) ( ) ;
193
+
194
+ module . exports = Yoroshiku ;
195
+
196
+ } ) . call ( this ) ;
0 commit comments