Class: Epos::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/epos/dictionary.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Dictionary

Returns a new instance of Dictionary.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/epos/dictionary.rb', line 9

def initialize(path)
  @idf1 = Epos::IndexedDataFile.new(File.join(path, "deah002.dhn"), File.join(path, "deah001.dhn"))
  @idf2 = Epos::IndexedDataFile.new(File.join(path, "deah008.dhn"), File.join(path, "deah007.dhn"))

  @fmt1 = Epos::HtmlFormatter.new(unmarked: false)
  @fmt2 = Epos::HtmlFormatter.new(unmarked: true)

  @entry_parser = Epos::EntryParser.new

  @fuzzy = {}

  (@idf1.keys + @idf2.keys).each do |key|
    simple = simplify(key)
    if @fuzzy.has_key?(simple)
      @fuzzy[simple] << key
    else
      @fuzzy[simple] = [key]
    end
  end

  @fuzzy.each_value do |value|
    value.sort!.uniq!
  end
end

Instance Method Details

#look_up(word, level: 1, fragment: false) ⇒ Object



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
# File 'lib/epos/dictionary.rb', line 34

def look_up(word, level: 1, fragment: false)
  html        = ""
  simple_word = simplify(word)
  used        = {}

  if level >= 0
    html << look_up_and_format(word)
    used[word] = true
  end

  if level >= 1
    (@fuzzy[simple_word] || []).each do |actual|
      if !used[actual]
        html << look_up_and_format(actual)
        used[actual] = true
      end
    end
  end

  if level >= 2
    @fuzzy.each do |simple, words|
      if simple.include?(simple_word)
        words.each do |actual|
          if !used[actual]
            html << look_up_and_format(actual)
            used[actual] = true
          end
        end
      end
    end
  end

  if !fragment
    if html.length == 0
      html = "<i>No results were found for your query.</i>"
    end
    html = %Q{
<!DOCTYPE html>
<html>
  <head>
<meta charset='utf-8'>
<title>Epos</title>
<style type='text/css'>#{self.style}</style>
  </head>
  <body>
#{html}
  </body>
</html>
}
  end

  return html
end

#look_up_and_format(word) ⇒ Object



92
93
94
95
96
97
# File 'lib/epos/dictionary.rb', line 92

def look_up_and_format(word)
  s = ""
  s << @idf1.look_up(word).map{|text| @fmt1.format(@entry_parser.parse(text))}.join
  s << @idf2.look_up(word).map{|text| @fmt2.format(@entry_parser.parse(text))}.join
  return s
end

#simplify(s) ⇒ Object



99
100
101
# File 'lib/epos/dictionary.rb', line 99

def simplify(s)
  s.tr("áéíóúâêôãõàüçÁÉÍÓÚÂÊÔÃÕÀÜÇ", "aeiouaeoaoaucaeiouaeoaoauc").downcase.gsub(/[ \-\\(\\)]/, "")
end

#styleObject



88
89
90
# File 'lib/epos/dictionary.rb', line 88

def style
  @fmt1.style
end