Class: Logaling::GlossaryDB

Inherits:
Object
  • Object
show all
Defined in:
lib/logaling/glossary_db.rb

Constant Summary collapse

VERSION =
3

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGlossaryDB

Returns a new instance of GlossaryDB.



29
30
31
# File 'lib/logaling/glossary_db.rb', line 29

def initialize
  @database = nil
end

Class Method Details

.open(base_path, encoding, &blk) ⇒ Object



25
26
27
# File 'lib/logaling/glossary_db.rb', line 25

def self.open(base_path, encoding, &blk)
  blk ? GlossaryDB.new.open(base_path, encoding, &blk) : GlossaryDB.new.open(base_path, encoding)
end

Instance Method Details

#closeObject



59
60
61
62
# File 'lib/logaling/glossary_db.rb', line 59

def close
  @database.close
  @database = nil
end

#deindex_glossary(glossary, glossary_source) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/logaling/glossary_db.rb', line 64

def deindex_glossary(glossary, glossary_source)
  delete_translations_by_glossary_source(glossary_source.source_path)
  delete_glossary_source(glossary_source.source_path)
  unless glossary_source_of_the_same_project_exist?(glossary_source)
    delete_glossary(glossary.name)
  end
end

#deindex_glossary_source(glossary_source) ⇒ Object



72
73
74
75
# File 'lib/logaling/glossary_db.rb', line 72

def deindex_glossary_source(glossary_source)
  delete_translations_by_glossary_source(glossary_source.source_path)
  delete_glossary_source(glossary_source.source_path)
end

#get_all_glossaryObject



253
254
255
256
257
258
259
# File 'lib/logaling/glossary_db.rb', line 253

def get_all_glossary
  records = Groonga["glossaries"].sort([
    {:key=>"_key", :order=>'ascending'}
  ])
  records = records.collect {|record| record.value }
  records.map{|record| record.key}
end

#get_all_glossary_sourcesObject



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/logaling/glossary_db.rb', line 232

def get_all_glossary_sources
  records = Groonga["glossary_sources"].sort([
    {:key=>"_key", :order=>'ascending'}
  ])
  records = records.collect {|record| record.value }
  source_paths = records.map{|record| [record.key, record.project_type]}
  source_paths.map do |source_path, project_type|
    glossary_name, source_language, target_language = File.basename(source_path).split(/\./)
    project = Logaling.const_get(project_type).new(Logaling::Project.find_path(source_path))
    glossary = Glossary.new(glossary_name, source_language, target_language, project)
    GlossarySource.create(source_path, glossary)
  end
end

#get_bilingual_pair(source_term, target_term, glossary, note = nil) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/logaling/glossary_db.rb', line 202

def get_bilingual_pair(source_term, target_term, glossary, note=nil)
  records = Groonga["translations"].select do |record|
    if note
      [
        record.glossary == glossary,
        record.source_term == source_term,
        record.target_term == target_term,
        record.note == note
      ]
    else
      [
        record.glossary == glossary,
        record.source_term == source_term,
        record.target_term == target_term
      ]
    end
  end
  struct_result(records)
end

#glossary_source_exist?(glossary_source) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
225
226
227
228
229
230
# File 'lib/logaling/glossary_db.rb', line 222

def glossary_source_exist?(glossary_source)
  glossary = Groonga["glossary_sources"].select do |record|
    [
      record.key == glossary_source.source_path,
      record.indexed_at == glossary_source.mtime
    ]
  end
  !glossary.size.zero?
end


246
247
248
249
250
251
# File 'lib/logaling/glossary_db.rb', line 246

def glossary_sources_related_on_glossary(glossary)
  records = Groonga["glossary_sources"].select do |record|
    [record.key =~ glossary.to_s]
  end
  records.map{|record| GlossarySource.create(record.key.key, glossary) }
end

#index_glossary(glossary, glossary_source) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/logaling/glossary_db.rb', line 94

def index_glossary(glossary, glossary_source)
  delete_terms if offline_index?

  deindex_glossary(glossary, glossary_source)

  add_glossary_source(glossary_source)
  add_glossary(glossary)
  glossary_source.load.each do |term|
    source_term = term['source_term']
    target_term = term['target_term']
    note = term['note']
    add_translation(glossary.name, glossary_source.source_path, glossary.source_language, glossary.target_language, source_term, target_term, note)
  end

  create_terms if offline_index?
end

#index_glossary_source(glossary_source) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/logaling/glossary_db.rb', line 77

def index_glossary_source(glossary_source)
  delete_terms if offline_index?
  glossary = glossary_source.glossary

  deindex_glossary_source(glossary_source)

  add_glossary_source(glossary_source)
  glossary_source.load.each do |term|
    source_term = term['source_term']
    target_term = term['target_term']
    note = term['note']
    add_translation(glossary.name, glossary_source.source_path, glossary.source_language, glossary.target_language, source_term, target_term, note)
  end

  create_terms if offline_index?
end

#lookup(source_term, glossary = nil) ⇒ Object



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
# File 'lib/logaling/glossary_db.rb', line 111

def lookup(source_term, glossary=nil)
  records_selected = Groonga["translations"].select do |record|
    conditions = [record.source_term =~ source_term]
    if glossary
      conditions << (record.source_language =~ glossary.source_language) if glossary.source_language
      conditions << (record.target_language =~ glossary.target_language) if glossary.target_language
    end
    conditions
  end
  if glossary
    specified_glossary = records_selected.select do |record|
      record.glossary == glossary.name
    end
    specified_glossary.each do |record|
      record.key._score += 10
    end
  end
  records = records_selected.sort([
    {:key=>"_score", :order=>'descending'},
    {:key=>"glossary", :order=>'ascending'},
    {:key=>"source_term", :order=>'ascending'},
    {:key=>"target_term", :order=>'ascending'}])
  records = records.collect {|record| record.value }

  options = {:width => 5000,
             :html_escape => true,
             :normalize => true}
  snippet = records_selected.expression.snippet(["<snippet>", "</snippet>"], options)
  struct_result(records, snippet)
end

#lookup_dictionary(search_word) ⇒ Object



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
# File 'lib/logaling/glossary_db.rb', line 142

def lookup_dictionary(search_word)
  records_selected_source = Groonga["translations"].select do |record|
    target = record.match_target do |match_record|
      match_record.source_term * 2
    end
    target =~ search_word
  end
  completely_match = records_selected_source.select do |record|
    record.source_term == search_word
  end
  completely_match.each do |record|
    record.key._score += 10
  end

  records_selected_target = Groonga["translations"].select do |record|
    record.target_term =~ search_word
  end

  records_selected = records_selected_target.union!(records_selected_source)
  records = records_selected.sort([
    {:key=>"_score", :order=>'descending'},
    {:key=>"source_term", :order=>'ascending'},
    {:key=>"target_term", :order=>'ascending'}])
  records = records.collect {|record| record.value }

  options = {:width => 5000,
             :html_escape => true,
             :normalize => true}
  snippet = records_selected.expression.snippet(["<snippet>", "</snippet>"], options)

  struct_result(records, snippet)
end

#open(base_path, encoding) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/logaling/glossary_db.rb', line 33

def open(base_path, encoding)
  reset_context(encoding)
  path = File.join(base_path, "logaling.db")
  if File.exist?(path)
    @database = Groonga::Database.open(path)
  else
    FileUtils.mkdir_p(base_path)
    populate(path)
  end
  if block_given?
    begin
      yield(self)
    ensure
      close unless closed?
    end
  end
end

#recreate_tableObject



51
52
53
54
55
56
57
# File 'lib/logaling/glossary_db.rb', line 51

def recreate_table
  unless latest_version?
    remove_schema
    populate_schema
    update_version_to_latest
  end
end

#translation_list(glossary, filter_option, order = 'ascending') ⇒ Object



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
# File 'lib/logaling/glossary_db.rb', line 175

def translation_list(glossary, filter_option, order='ascending')
  records_raw = Groonga["translations"].select do |record|
    if filter_option
      [
        record.glossary == glossary.name,
        record.source_language == glossary.source_language,
        record.target_language == glossary.target_language,
        record.note =~ filter_option
      ]
    else
      [
        record.glossary == glossary.name,
        record.source_language == glossary.source_language,
        record.target_language == glossary.target_language
      ]
    end
  end

  records = records_raw.sort([
    {:key => "source_term", :order => order},
    {:key => "target_term", :order => order}
  ])
  records = records.collect {|record| record.value }

  struct_result(records)
end