Class: Dphil::Lemma

Inherits:
Object
  • Object
show all
Defined in:
lib/dphil/lemma.rb

Overview

Public: A storage object for words and groups of words from TEI XML data. Also contains information about the source/location of the words. Immutable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = "", index = nil) ⇒ Lemma

Public: Initialize a lemma object.

source - XML data to initialize the lemma from



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dphil/lemma.rb', line 15

def initialize(source = "", index = nil)
  @source = source.strip
  @index = index

  xml = Nokogiri::XML("<lemma>#{source}</lemma>") { |config| config.strict.noent }
  xml.encoding = "UTF-8"

  @text = xml.text.strip.gsub(/\-+\s*\-*/, "")
  @page = xml.css("pb").map { |el| el.attr("n") }.join(",")
  @facs = xml.css("pb").map { |el| el.attr("facs") }.join(",")
  @line = xml.css("lb").map { |el| el.attr("n") }.join(",")
rescue Nokogiri::XML::SyntaxError => e
  $stderr.puts "Error in Lemma.new(`#{source}`, ...): #{e}"
  abort
end

Instance Attribute Details

#facsObject (readonly)

Public: Returns the raw source data for the lemma.



10
11
12
# File 'lib/dphil/lemma.rb', line 10

def facs
  @facs
end

#indexObject (readonly)

Public: Returns the raw source data for the lemma.



10
11
12
# File 'lib/dphil/lemma.rb', line 10

def index
  @index
end

#lineObject (readonly)

Public: Returns the raw source data for the lemma.



10
11
12
# File 'lib/dphil/lemma.rb', line 10

def line
  @line
end

#pageObject (readonly)

Public: Returns the raw source data for the lemma.



10
11
12
# File 'lib/dphil/lemma.rb', line 10

def page
  @page
end

#sourceObject (readonly)

Public: Returns the raw source data for the lemma.



10
11
12
# File 'lib/dphil/lemma.rb', line 10

def source
  @source
end

#textObject (readonly)

Public: Returns the raw source data for the lemma.



10
11
12
# File 'lib/dphil/lemma.rb', line 10

def text
  @text
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
42
# File 'lib/dphil/lemma.rb', line 39

def ==(other)
  return false unless other.is_a?(Dphil::Lemma)
  source == other.source
end

#to_sObject



31
32
33
# File 'lib/dphil/lemma.rb', line 31

def to_s
  "(#{index}|#{page}:#{line}) #{text}"
end

#to_symObject



35
36
37
# File 'lib/dphil/lemma.rb', line 35

def to_sym
  "<Lemma>#{self}".to_sym
end