Class: Epos::EntryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/epos/entry-parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(text) ⇒ Object



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
# File 'lib/epos/entry-parser.rb', line 6

def parse(text)

  entry = {
    :headword      => nil,
    :n             => nil,
    :trademark     => nil,
    :translation   => nil,
    :pronunciation => nil,
    :language      => nil,
    :first_use     => nil,
    :orthoepy      => nil,
    :variants      => [],
    :old_spelling  => nil,
    :see           => false,
    :tabs          => [],
    :divs          => [],
  }

  div    = nil
  defin  = nil
  either = nil

  text.lines.each do |line|
    code   = line[0]
    suffix = line[1..-1].strip

    case code

    when "*"
      entry[:headword] = suffix
    when ">"
      entry[:see] = true
    when "t"
      entry[:translation] = suffix
    when "p"
      entry[:pronunciation] = suffix
    when "n"
      entry[:n] = suffix.to_i
    when "L"
      entry[:language] = suffix.split("|")
    when "®"
      entry[:trademark] = true
    when "d"
      entry[:first_use] = suffix
    when "o"
      entry[:orthoepy] = suffix
    when "M"
      entry[:variants] = suffix.split("|")
    when "$"
      entry[:old_spelling] = suffix
    when "0"
      entry[:tabs] << {:type => :grammar, :text => suffix}
    when "1"
      entry[:tabs] << {:type => :grammar_usage, :text => suffix}
    when "2"
      entry[:tabs] << {:type => :usage, :text => suffix}
    when "3"
      entry[:tabs] << {:type => :etymology, :text => suffix}
    when "4"
      entry[:tabs] << {:type => :synonyms, :text => suffix}
    when "5"
      entry[:tabs] << {:type => :antonyms, :text => suffix}
    when "6"
      entry[:tabs] << {:type => :collective, :text => suffix}
    when "7"
      entry[:tabs] << {:type => :homonyms, :text => suffix}
    when "8"
      entry[:tabs] << {:type => :paronyms, :text => suffix}
    when "9"
      entry[:tabs] << {:type => :animals, :text => suffix}

    when "B"
      div = {
        :pos        => nil,
        :idiom      => nil,
        :field      => nil,
        :regional   => nil,
        :register   => nil,
        :temporal   => nil,
        :freq       => nil,
        :plural     => nil,
        :symbols    => nil,
        :derivation => nil,
        :defins     => [],
      }
      entry[:divs] << div
      either = div
      defin  = nil
    when "C"
      div[:pos] = suffix.split("|")
    when "#"
      div[:idiom] = suffix
    when "P"
      div[:plural] = suffix
    when "s"
      div[:symbols] = true

    when "A"
      defin = {
        :num        => nil,
        :text       => nil,
        :transty     => nil,
        :examples   => [],
        :note       => nil,
        :derivation => nil,
        :field      => nil,
        :register   => nil,
        :freq       => nil,
        :regional   => nil,
        :temporal   => nil,
        :see        => entry[:see],
      }
      either = defin
      div[:defins] << defin
    when "-"
      defin[:num] = suffix
    when ":"
      defin[:text] = suffix
    when "r"
      defin[:transty] = suffix.split("|")
    when "<"
      defin[:examples] = suffix.split("|")
    when "!"
      defin[:note] = suffix
    when "D"
      either[:derivation] = suffix.split("|")
    when "T"
      either[:field] = suffix.split("|")
    when "U"
      either[:register] = suffix.split("|")
    when "E"
      either[:freq] = suffix.split("|")
    when "R"
      either[:regional] = suffix.split("|")
    when "I"
      either[:temporal] = suffix.split("|")

    when "c"  # hidden, apparently
    when "S"  # unknown
    when "v"  # unknown
    when "\\" # coding error
    else
      raise "#{code}#{suffix}"
    end
  end

  return entry
end