Class: Datasets::WikipediaKyotoJapaneseEnglish::ArticleListener

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/datasets/wikipedia-kyoto-japanese-english.rb

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ ArticleListener

Returns a new instance of ArticleListener.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/datasets/wikipedia-kyoto-japanese-english.rb', line 120

def initialize(block)
  @block = block
  @article = nil
  @title = nil
  @section = nil
  @page = nil
  @sentence = nil
  @text_container_stack = []
  @element_stack = []
  @text_stack = [""]
end

Instance Method Details

#tag_end(name) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
201
# File 'lib/datasets/wikipedia-kyoto-japanese-english.rb', line 160

def tag_end(name)
  case name
  when "art"
    @block.call(@article)
    @article = nil
  when "inf"
    @article.source = @text_stack.last
  when "copyright"
    @article.copyright = @text_stack.last
  when "tit"
    @article.contents << @title
    if @section
      @section.title = @title
      @section.contents << @title
    end
    @title = nil
    @text_container_stack.pop
  when "sec"
    @article.sections << @section
    @section = nil
    @text_container_stack.pop
  when "par"
    @paragraph = nil
    @text_container_stack.pop
  when "sen"
    @article.contents << @sentence
    @sentence.section = @section
    @section.contents << @sentence if @section
    @sentence.paragraph = @paragraph
    @paragraph.sentences << @sentence if @paragraph
    @sentence = nil
    @text_container_stack.pop
  when "j"
    @text_container_stack.last.japanese = @text_stack.last
  when "e"
    attributes = @element_stack.last[:attributes]
    if attributes["type"] == "check"
      @text_container_stack.last.english = @text_stack.last
    end
  end
  pop_stacks
end

#tag_start(name, attributes) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/datasets/wikipedia-kyoto-japanese-english.rb', line 132

def tag_start(name, attributes)
  push_stacks(name, attributes)
  case name
  when "art"
    @article = Article.new
    @article.contents = []
    @article.sections = []
  when "tit"
    @title = Title.new
    @title.section = @section
    @text_container_stack.push(@title)
  when "sec"
    @section = Section.new
    @section.id = attributes["id"]
    @section.contents = []
    @text_container_stack.push(@section)
  when "par"
    @paragraph = Paragraph.new
    @paragraph.id = attributes["id"]
    @paragraph.sentences = []
    @text_container_stack.push(@paragraph)
  when "sen"
    @sentence = Sentence.new
    @sentence.id = attributes["id"]
    @text_container_stack.push(@sentence)
  end
end

#text(data) ⇒ Object



203
204
205
# File 'lib/datasets/wikipedia-kyoto-japanese-english.rb', line 203

def text(data)
  @text_stack.last << data
end