-
Notifications
You must be signed in to change notification settings - Fork 4
/
generate-moedict-json.rb
92 lines (82 loc) · 2.4 KB
/
generate-moedict-json.rb
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
require 'pry'
require './models/application_record.rb'
require './models/stem'
require './models/term'
require './models/description'
require './models/example'
require './models/synonym'
# index.json
def index_json
terms_hash = {}
Term.includes(:descriptions).find_each do |term|
next if terms_hash[term.lower_name].present?
terms_hash[term.lower_name] = term.descriptions.pluck(:content).join[0..10]
end
result = terms_hash.sort_by {|k,_| -k.length }.map{|term, description| "#{term}\ufffa#{description}" }
File.write("s/index.json", result.to_json)
end
# stem-words.json
def stem_words_json
stems_hash = {}
Stem.includes(:terms).find_each do |stem|
stems_hash[stem.name] = stem.terms.pluck(:lower_name).uniq
stems_hash[stem.name].map! do |term_name|
term = Term.includes(:descriptions).find_by(lower_name: term_name)
description = term.descriptions.pluck(:content).join[0..10]
"#{term_name}\ufffa#{description}"
end
end
File.write("s/stem-words.json", stems_hash.to_json)
end
# revdict-amis-def.txt
# 萌典前端使用的標記
# \ufff9: 阿美語例句
# \ufffa: 英文例句
# \ufffb: 漢語例句
def revdict_amis_def_txt
File.open('s/revdict-amis-def.txt', 'w') do |file|
Term.includes(:descriptions).find_each do |term|
content = term.descriptions.pluck(:content).join
file.puts "\ufffa#{term.lower_name}\ufffb#{content}"
end
end
end
# revdict-amis-ex.txt
# 萌典前端使用的標記
# \ufff9: 阿美語例句
# \ufffa: 英文例句
# \ufffb: 漢語例句
def revdict_amis_ex_txt
File.open('s/revdict-amis-ex.txt', 'w') do |file|
Term.includes(:descriptions).find_each do |term|
content = Example.where(description_id: term.descriptions.select(:id)).pluck(:content).join
file.puts "\ufffa#{term.lower_name}\ufffb#{content}"
end
end
end
def copy_content_to_linked_content
Example.find_in_batches do |examples|
Example.transaction do
examples.each do |example|
example.update(linked_content: example.content)
end
end
end
Synonym.find_in_batches do |synonyms|
Synonym.transaction do
synonyms.each do |synonym|
synonym.update(linked_content: synonym.content)
end
end
end
end
# 跑一次大約要 300 秒
[
'index_json',
'stem_words_json',
'revdict_amis_def_txt',
'revdict_amis_ex_txt',
'copy_content_to_linked_content'
].each do |method_name|
eval(method_name)
end