FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
librime-lua-script/lua/tools/english_dict.lua at main · shewer/librime-lua-script · GitHub
shewer
librime-lua-script
Repository navigation
Code
Issues
4
(4)
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
librime-lua-script
/
lua
/
tools
/
english_dict.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
458 lines (411 loc) · 12.2 KB
Breadcrumbs
librime-lua-script
/
lua
/
tools
/
english_dict.lua
Copy path
File metadata and controls
executable file
·
458 lines (411 loc) · 12.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!
/usr/bin/env lua
--
--
english_dict.lua
--
Copyright (C) 2020 Shewer Lu <shewer@gmail.com>
--
--
Distributed under terms of the MIT license.
--
--[[
ex :
Eng=require('tools/english_dict' )
e = Eng( 'english')
for w in e:iter('th/i:ad') do -- * ? / :
print(w.word,w:get_info(1) )
end
list = e:match('th/i:ad') --List table
for i,v in ipairs(list)
print(w.word,w:get_info(1)
end
pw=function(w) print(w.word,w:get_info()) end
list:each(pw)
--
--
--]]
require
'
tools/string
'
require
'
tools/_file
'
local
List
=
List
or
require
'
tools/list
'
--
USERDIR= ( USERDIR or os.getenv("APPDATA") or "" ) .. [[\Rime]]
--
字典 字根 查碼 table
--
--
local eng_suffixe1={ ["Control+f"] ="*ful" , ["Control+y"]= "*ly" , ["Control+n"]= "*tion" , ["Control+a"] = "*able" ,
--
["Control+i"] = "*ing" , ["Control+m"]= "*ment" , ["Control+r"]= "*er", }
--
env.keyname2={ f ="*ful" , y= "*ly" , n= "*tion" , a = "*able" ,
--
i = "ing" , m= "*ment" , r= "*er",
--
}
--
f="ful" --> /f or Control+f
--
require 'tools/object'
--
設定 存入bin格式
--
local chunk_bin= true
local
NR
=
package.config
:
sub
(
1
,
1
):
match
(
"
/
"
)
and
"
\n
"
or
"
\r
"
local
eng_suffix
=
{
f
=
"
ful
"
,
y
=
"
ly
"
,
t
=
"
tion
"
,
s
=
"
sion
"
,
a
=
"
able
"
,
i
=
"
ing
"
,
m
=
"
ment
"
,
r
=
"
er
"
,
g
=
"
ght
"
,
n
=
"
ness
"
,
l
=
"
less
"
, }
local
eng_suffix_list
=
{ }
--
詞類
local
eng_parts
=
{
"
a
"
,
"
abbr
"
,
"
ad
"
,
"
art
"
,
"
aux
"
,
"
phr
"
,
"
pl
"
,
"
pp
"
,
"
prep
"
,
"
pron
"
,
"
conj
"
,
"
int
"
,
"
v
"
,
"
vi
"
,
"
vt
"
}
setmetatable
(
eng_parts
,{
__index
=
table
} )
--
可接收輸入字串 %a[%a%-%.%:_[]%?%*]* 萬用字 ? 0-1 *0-N 萬用字 符合字元[%a%-%.:_] [] 限制字元範圍
--
/ 快捷字串 : 詞類
--
字頭[/快捷1[/快捷2][:詞類]
--
--
分割詞類 字頭[/快捷1[/快捷2] [:詞類] --> 字頭[/快捷1[/快捷2] 詞類
--
分割快捷碼 字頭 [/快捷1 [/快捷2] --> 字頭 快捷 快捷
--
截取 字頭字串 %a[%a%-%.%:_]*
--
如果字字串 1 字元 查全表
--
如果字頭字串 大於2 查表至 不符合時 中斷查詢 因爲 字典是按照 字元排序
--
--
如果input 長度 1 送出全表 不用查
--
--
--
--
詞類 快捷 字串 查表 轉換 *ing *tion ... 查不到的 字串加上 * v* a* adv ad* ...
--
--
--
--
input: string [%a][%a/: %*%.%?_%-]
--
?* -> "([%?%*])","([%a%.%-_]%%%1" --? * [$a%.%-_]? [%a%.%-]*
--
--
1 gen per_suffix input input:match("^%a[%a%-%.-]+") %a[%a-._]+
--
2 pre_suffix input conver rex with escap gsub("([%.%-_])","%%%1")
--
--
--
3 pattern , wild_word
--
1 split(":") 英文單字:詞類
--
--
2 split("/") 字根 萬用字 快捷碼分割
--
"ab*/i/n" ab* i n
--
--
--
--
"i" -> *ing n -> tion not match: g -> *g
--
取得 字頭 英文字串 a-z A-Z . - _
local
function
pre_suffix_word
(
wild_word
)
return
wild_word
:
match
(
"
^[-.%a][%a%.%-_]*
"
),
wild_word
end
--
轉換 reg pattern 隔離字元 - . %- %. 萬用字元 ? * .? .*
local
function
conver_rex
(
str
)
return
str
:
gsub
(
"
([%-%.])
"
,
"
%%%1
"
):
gsub
(
"
([?*])
"
,
"
.%1
"
)
end
--
切割 字串 轉換字串 再組合 return 字頭 全字 詞類
local
function
split_str
(
str
)
str
=
type
(
str
)
==
"
string
"
and
str
or
"
"
local
w
,
p
=
table.unpack
(
str
:
split
(
"
:
"
))
local
pw
=
w
:
match
(
"
^[-.%a][%a%.%-_ ]*
"
)
local
ws
=
List
(
w
:
split
(
"
/
"
) )
local
ww
=
ws
:
shift
()
ww
=
ww
..
ws
:
map
(
function
(
elm
)
return
"
*
"
..
(
eng_suffix
[
elm
]
or
elm
)
end
):
concat
()
return
pw
,
ww
,
p
or
"
"
end
--
轉換 reg 字串 字頭 全字 詞類
local
function
conv_pattern
(
org_text
)
local
pw
,
ww
,
p
=
split_str
(
org_text
)
pw
=
"
^
"
..
conver_rex
(
pw
:
lower
()
or
"
"
)
ww
=
"
^
"
..
conver_rex
(
ww
:
lower
())
p
=
p
:
len
()
>
0
--
and "%s" .. conver_rex(p:lower() ) .. "[%l%-%.]*%."
and
conver_rex
(
p
:
lower
() )
..
"
[%l%-%.]*%.
"
or
"
"
--
[ ] p [%a]*%."
return
pw
,
ww
,
p
end
local
function
conv_pattern1
(
org_text
,
level
)
level
=
level
or
3
local
pw
,
ww
,
p
=
split_str
(
org_text
)
pw
=
pw
:
lower
():
sub
(
1
,
level
)
ww
=
ww
:
lower
()
p
=
p
:
lower
()
pw
=
"
^
"
..
conver_rex
(
pw
)
ww
=
"
^
"
..
conver_rex
(
ww
)
p
=
p
:
len
()
>
0
and
"
%s
"
..
conver_rex
(
p
)
..
"
[%a-%.]*%.
"
or
"
"
--
[ ] p [%a]*%."
return
pw
,
ww
,
p
end
--
local Word= require 'tools/english_word'
local
function
New
(
self
,...)
local
obj
=
setmetatable
({} ,
self
)
return
not
obj
.
_initialize
and
obj
or
obj
:
_initialize
(
...
)
end
---
Word
--
local
class
=
require
'
tools/class
'
--
Word
--
class method Phrase_chunk(row_chunk) Parse_text(row_tab)
--
instance method get_info(mode_num) to_s() prefix_match(prefix_str) match:(text)
--
local Word=Class("Word")
--
Word.__name= "Word"
--[[
function Word:__eq(obj)
return self.word == obj
end
--]]
--
local
Word
=
class
()
--
{}
Word
.
__name
=
"
Word
"
function
Word
:
_initialize
(
tab
)
if
type
(
tab
)
==
"
table
"
and
tab
.
word
and
tab
.
word
:
len
()
>
0
then
--
and tab.translation and tab.phonetic then
for
k
,
v
in
next
,
tab
do
self
[
k
]
=
v
end
self
.
phonetic
=
self
.
phonetic
or
"
"
return
self
end
end
--
<word>\t<translation>
function
Word
.
Parse_text
(
line
)
local
tab
=
{}
local
word
,
translation
=
table.unpack
(
line
:
split
(
"
\t
"
))
if
word
:
len
()
<
1
then
return
end
tab
.
word
=
word
translation
=
translation
or
"
"
local
head
,
tail
=
translation
:
find
(
'
^%[.*%];
'
)
if
head
and
tail
then
tab
.
phonetic
=
translation
:
sub
(
head
,
tail
-
1
)
tab
.
translation
=
translation
:
sub
(
tail
+
1
)
else
tab
.
phonetic
=
"
"
tab
.
translation
=
translation
end
return
Word
(
tab
)
end
function
Word
.
Parse_chunk
(
str
,
replace
)
if
type
(
str
)
==
"
string
"
then
str
=
replace
and
str
:
gsub
(
"
\\
n
"
,
"
\\
n
"
):
gsub
(
"
\\
r
"
,
"
\r
"
)
or
str
local
tab
=
load
(
"
return
"
..
str
)()
return
Word
(
tab
)
end
end
function
Word
:
to_s
()
local
l
=
List
()
for
k
,
v
in
next
,
self
do
l
:
push
(
string.format
(
"
%s=%q
"
,
k
,
v
) )
end
return
"
{
"
..
l
:
concat
(
'
,
'
)
..
"
}
"
end
--
利用此func 設定comment 輸出格式
--
function
Word
:
get_info
(
mode
)
mode
=
tonumber
(
mode
)
local
info
=
self
if
not
info
then
return
"
"
end
mode
=
mode
and
mode
%
7
or
0
if
mode
==
1
then
return
(
info
.
phonetic
..
"
"
..
info
.
translation
):
gsub
(
"
\\
n
"
,
NR
)
elseif
mode
==
2
then
return
info
.
translation
:
gsub
(
"
\\
n
"
,
"
"
)
elseif
mode
==
3
then
return
info
.
translation
:
gsub
(
"
\\
n
"
,
NR
)
elseif
mode
==
4
then
return
info
.
phonetic
elseif
mode
==
5
then
return
info
.
word
elseif
mode
==
6
then
return
"
"
else
return
(
info
.
phonetic
..
"
"
..
info
.
translation
):
gsub
(
"
\\
n
"
,
"
"
)
end
end
function
Word
:
prefix_match
(
prefix
,
case_match
)
if
case_match
then
return
self
.
word
:
find
(
prefix
)
==
1
else
return
self
.
word
:
lower
():
find
(
prefix
:
lower
())
==
1
end
return
false
end
function
Word
:
chk_parts
(
parts
)
return
(
self
.
translation
:
match
(
parts
)
and
true
)
or
false
end
function
Word
:
match
(
text
,
case_match
)
local
pw
,
ww
,
pn
=
conv_pattern
(
text
)
--
Log(DEBUG,'word match :', pw,ww,pn,self.word, self.word:match(ww) and true or false ,self:prefix_match(pw))
local
w_match
=
case_match
and
self
.
word
:
match
(
ww
)
or
self
.
word
:
lower
():
match
(
ww
)
local
p_match
=
#
pn
<
1
and
true
or
self
.
translation
:
match
(
pn
)
return
w_match
and
p_match
and
true
or
false
end
Word
.
is_match
=
Word
.
match
--
local class = require 'tools/class'
--
return class(Word)
--
local MT={}
--
MT.__index=MT
--
MT.__call=New
--
Dict instance method
--
iter(text) return iter function for match text pattern
--
get(word) return
--
LuaDict
local
function
init_tree
(
tree_tab
,
index
,
word
,
level
)
local
prefix
=
word
:
sub
(
1
,
level
):
lower
()
for
i
=
#
prefix
,
1
,
-
1
do
local
w
=
prefix
:
sub
(
1
,
i
)
if
tree_tab
[
w
]
then
break
end
tree_tab
[
w
]
=
index
init_tree
(
tree_tab
,
index
,
word
,
level
-
1
)
end
end
local
LuaDict
=
class
()
--
setmetatable({},MT)
--
LuaDict.__index = LuaDict
LuaDict
.
__name
=
'
LuaDict
'
LuaDict
.
_db
=
{}
function
LuaDict
:
_initialize
(
full_path
,
level
)
level
=
level
and
level
>
1
and
level
or
3
if
T03
and
GD
then
GD
()
end
if
not
isFile
(
full_path
)
then
return
end
--
self._db = loadfile(full_path)()
self
.
_db
=
dofile
(
full_path
)
self
.
_tree
=
{}
self
.
_words
=
{}
for
i
,
v
in
next
,
self
.
_db
do
local
w
=
Word
(
v
)
self
.
_db
[
i
]
=
w
self
.
_words
[
v
.
word
]
=
i
init_tree
(
self
.
_tree
,
i
,
w
.
word
,
level
)
end
return
self
end
function
LuaDict
:
_prefix_index
(
pw
)
--
local pw,ww,pn = conv_pattern(text)
local
index
=
1
if
#
pw
<
1
then
return
index
end
--
找最近的index
for
i
=
#
pw
,
1
,
-
1
do
local
tree_index
=
self
.
_tree
[
pw
:
sub
(
1
,
i
) ]
if
tree_index
then
index
=
tree_index
break
end
end
--
找 prefix word index
local
count
=
0
repeat
local
ww
=
self
.
_db
[
index
]
if
self
.
_db
[
index
]:
prefix_match
(
pw
)
then
--
找到索引 如果count > 200 增加 pw 索引
if
count
>
200
then
self
.
_tree
[
pw
]
=
index
end
break
end
count
=
count
+
1
index
=
index
+
1
until
index
>
#
self
.
_db
return
index
end
function
LuaDict
:
iter
(
text
)
local
pw
,
ww
,
pn
=
split_str
(
text
)
--
if not index then GD() end
return
coroutine.wrap
(
function
()
local
index
=
self
:
_prefix_index
(
pw
)
while
self
.
_db
[
index
]
and
self
.
_db
[
index
]:
prefix_match
(
pw
)
do
if
self
.
_db
[
index
]:
match
(
text
)
then
coroutine.yield
(
self
.
_db
[
index
])
end
index
=
index
+
1
end
end
)
end
function
LuaDict
:
get
(
word
)
local
index
=
self
.
_words
[
word
]
return
self
.
_db
[
index
]
end
--
Dict instance method
--
iter(text) return iter function for match text pattern
--
get(word) return
--
LevelDict
local
LevelDict
=
LevelDb
and
class
()
--
setmetatable({},MT)
if
LevelDict
then
--
LevelDict.__index = LevelDict
LevelDict
.
__name
=
'
LewelDict
'
function
LevelDict
:
_initialize
(
full_path
)
if
rime_api
.
LevelDb
.
open
(
full_path
)
then
self
.
_dbname
=
full_path
return
self
end
end
function
LevelDict
:
_getdict
()
return
rime_api
.
LevelDb
.
get_db
(
self
.
_dbname
)
end
function
LevelDict
:
iter
(
text
,
case_match
)
--
local pw,ww,pn = conv_pattern(text)
local
pw
,
ww
,
pn
=
split_str
(
text
)
local
dbacc
=
self
:
_getdict
():
query
(
pw
:
lower
())
return
coroutine.wrap
(
function
()
for
k
,
v
in
dbacc
:
iter
()
do
local
w
=
Word
.
Parse_chunk
(
v
)
if
w
and
w
:
match
(
text
,
case_match
)
then
coroutine.yield
(
w
)
end
end
end
)
end
function
LevelDict
:
get
(
word
)
--
have upcae word ex Abort abort\tAbort
word
=
word
:
match
(
"
%u
"
)
and
string.format
(
"
%s
\t
%s
"
,
word
:
lower
(),
word
)
or
word
local
chk_str
=
self
:
_getdict
():
fetch
(
word
)
return
chk_str
and
Word
.
Parse_chunk
(
chk_str
)
end
end
--
English(dict_name)
--
instance method
--
iter(text) return Word for w in e:iter(text) do ... end
--
match(text) return Word of List
--
word(word) return Word
--
local
English
=
class
()
--
setmetatable({},MT)
--
English.__index=English
English
.
__name
=
"
English
"
English
.
_dicts
=
{}
function
English
:
_getdict
()
return
English
.
_dicts
[
self
.
_dict_name
]
end
--[[
function English:_setdict()
English._dicts[self._dict_name] = self._dict
end
--]]
function
English
:
_initialize
(
dict_name
,
reload
)
dict_name
=
dict_name
or
"
english_tw
"
self
.
_dict_name
=
dict_name
--
local dictdb = self:_getdb()
if
reload
or
not
self
:
_getdict
()
then
self
:
reload
()
end
return
self
:
_getdict
()
and
self
or
nil
end
function
English
:
reload
(
force
)
local
dict_name
=
self
.
_dict_name
local
full_path
=
get_full_path
(
dict_name
)
--
1 try LevelDict
if
LevelDict
and
full_path
and
isDir
(
full_path
)
then
self
.
_dicts
[
self
.
_dict_name
]
=
LevelDict
(
full_path
)
return
self
.
_dicts
[
self
.
_dict_name
]
and
true
or
false
end
--
2 try LuaDict
full_path
=
get_full_path
(
dict_name
..
"
.txtl
"
)
if
LuaDict
and
full_path
and
isFile
(
full_path
)
then
self
.
_dicts
[
self
.
_dict_name
]
=
LuaDict
(
full_path
)
return
self
.
_dicts
[
self
.
_dict_name
]
and
true
or
false
end
Log
(
ERROR
,
"
English_dict
"
..
dict_name
..
'
not find
'
)
end
function
English
:
iter
(
org_text
,
case_match
)
return
self
:
_getdict
():
iter
(
org_text
,
case_match
)
end
function
English
:
match
(
org_text
)
local
tab
=
List
()
for
w
in
self
:
iter
(
org_text
)
do
tab
:
push
(
w
)
end
return
tab
end
function
English
:
word
(
word
)
return
self
:
_getdict
():
get
(
word
)
end
--
for debug and check
--[[
debug function
English.Split=split_str --1 ('seteu/i/a:m') 字首seteu 單字 seteu*ing*able 詞類 m
English.Conver_rex=conver_rex --2 ('seteu*?ing:m') 展開/ : seteu.*.?ing:ment 2
English.Conver_pattern= conv_pattern --3 ('seteu/i/a:m') 字首 ^seteu ^seteu.*ing.*able%sm[%a%-%.]*%.
English.Word=Word
--]]
English
.
LevelDict
=
LevelDict
English
.
LuaDict
=
LuaDict
return
English
Back
|
FazBrowse Home
|
New Git URL