-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlink-json-to-terms.rb
91 lines (78 loc) · 2.78 KB
/
link-json-to-terms.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
require 'pry'
require 'json'
require './models/application_record.rb'
require './models/term'
require './models/description'
require './models/example'
require './models/synonym'
# 總共大約要 49 分
index_json = File.read("s/index.json")
terms = JSON.parse(index_json)
terms_with_spaces = []
terms_without_spaces = []
terms.each do |term|
if term.include?(' ')
terms_with_spaces << term
else
terms_without_spaces << term
end
end
total = terms_with_spaces.size
terms_with_spaces.each_with_index do |term, i|
counter = i + 1
print "\r** << #{format('%5d', counter)} / #{total}, #{format('%.2f', (counter.to_f / total * 100))}% >> **\tID: #{term}"
current_term_ids = Term.where(lower_name: term).pluck(:id)
current_description_ids = Description.where(term_id: current_term_ids).pluck(:id)
ApplicationRecord.transaction do
Example.where.not(description_id: current_description_ids)
.where('content LIKE ?', "%#{term}%")
.find_each do |example|
new_content = example.linked_content.gsub(/(#{term})/i, '`\1~')
example.update(linked_content: new_content)
end
Synonym.where.not(description_id: current_description_ids)
.where('linked_content LIKE ?', "%#{term}%")
.find_each do |synonym|
new_content = synonym.linked_content.gsub(/(#{term})/i, '`\1~')
synonym.update(linked_content: new_content)
end
end
end
puts "done terms_with_spaces"
total = terms_without_spaces.size
terms_without_spaces.each_with_index do |term, i|
counter = i + 1
print "\r** << #{format('%5d', counter)} / #{total}, #{format('%.2f', (counter.to_f / total * 100))}% >> **\tID: #{term}"
current_term_ids = Term.where(lower_name: term).pluck(:id)
current_description_ids = Description.where(term_id: current_term_ids).pluck(:id)
ApplicationRecord.transaction do
Example.where.not(description_id: current_description_ids)
.where('linked_content LIKE ?', "%#{term}%")
.find_each do |example|
new_content = example.linked_content.split(/(`.*?~)/).map do |part|
if part.include?("`")
part
else
part.split(/( )/).map do |fragment|
fragment.gsub(/(#{term})/i, '`\1~')
end.join
end
end.join
example.update(linked_content: new_content)
end
Synonym.where.not(description_id: current_description_ids)
.where('linked_content LIKE ?', "%#{term}%")
.find_each do |synonym|
new_content = synonym.linked_content.split(/(`.*?~)/).map do |part|
if part.include?("`")
part
else
part.split(/( )/).map do |fragment|
fragment.gsub(/(#{term})/i, '`\1~')
end.join
end
end.join
synonym.update(linked_content: new_content)
end
end
end