Class: PROIEL::Div

Inherits:
TreebankObject show all
Extended by:
Memoist
Defined in:
lib/proiel/div.rb

Overview

A div object in a treebank.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TreebankObject

#inspect

Constructor Details

#initialize(parent, id, title, presentation_before, presentation_after, alignment_id, &block) ⇒ Div

Creates a new div object.

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/proiel/div.rb', line 35

def initialize(parent, id, title, presentation_before, presentation_after, alignment_id, &block)
  @source = parent

  raise ArgumentError, 'integer expected' unless id.is_a?(Integer)
  @id = id

  raise ArgumentError, 'string or nil expected' unless title.nil? or title.is_a?(String)
  @title = title.freeze

  raise ArgumentError, 'string or nil expected' unless presentation_before.nil? or presentation_before.is_a?(String)
  @presentation_before = presentation_before.freeze

  raise ArgumentError, 'string or nil expected' unless presentation_after.nil? or presentation_after.is_a?(String)
  @presentation_after = presentation_after.freeze

  raise ArgumentError, 'integer or nil expected' unless alignment_id.nil? or alignment_id.is_a?(Integer)
  @alignment_id = alignment_id

  @children = block.call(self) if block_given?
end

Instance Attribute Details

#alignment_idnil, String (readonly)

Returns ID of the div that this div is aligned to.

Returns:

  • (nil, String)

    ID of the div that this div is aligned to



32
33
34
# File 'lib/proiel/div.rb', line 32

def alignment_id
  @alignment_id
end

#idFixnum (readonly)

Returns the ID of the div.

PROIEL XML 2.0 lacks IDs for divs while later versions require them. For PROIEL XML 2.0 unique IDs are generated by PROIEL::Treebank.

Returns:

  • (Fixnum)

    ID of the div



17
18
19
# File 'lib/proiel/div.rb', line 17

def id
  @id
end

#presentation_afternil, String (readonly)

Returns presentation material after form.

Returns:

  • (nil, String)

    presentation material after form



29
30
31
# File 'lib/proiel/div.rb', line 29

def presentation_after
  @presentation_after
end

#presentation_beforenil, String (readonly)

Returns presentation material before form.

Returns:

  • (nil, String)

    presentation material before form



26
27
28
# File 'lib/proiel/div.rb', line 26

def presentation_before
  @presentation_before
end

#sourceSource (readonly)

Returns source that the div belongs to.

Returns:

  • (Source)

    source that the div belongs to



20
21
22
# File 'lib/proiel/div.rb', line 20

def source
  @source
end

#titlenil, String (readonly)

Returns title of the div.

Returns:

  • (nil, String)

    title of the div



23
24
25
# File 'lib/proiel/div.rb', line 23

def title
  @title
end

Instance Method Details

#alignment(aligned_source) ⇒ Div, NilClass

Returns the aligned div if any.

Returns:

  • (Div, NilClass)

    aligned div



145
146
147
# File 'lib/proiel/div.rb', line 145

def alignment(aligned_source)
  alignment_id ? aligned_source.treebank.find_div(alignment_id) : nil
end

#citationString

Returns a complete citation for the div.

Returns:

  • (String)

    a complete citation for the div



69
70
71
# File 'lib/proiel/div.rb', line 69

def citation
  [source.citation_part, citation_part].join(' ')
end

#citation_partString

Computes an appropriate citation component for the div.

The computed citation component must be concatenated with the citation component provided by the source to produce a complete citation.

Returns:

  • (String)

    the citation component

See Also:



81
82
83
84
85
86
87
# File 'lib/proiel/div.rb', line 81

def citation_part
  tc = tokens.select(&:has_citation?)
  x = tc.first ? tc.first.citation_part : nil
  y = tc.last ? tc.last.citation_part : nil

  Citations.citation_make_range(x, y)
end

#inferred_alignment(aligned_source) ⇒ Array<Div>

Returns inferred aligned divs if any.

Returns:

  • (Array<Div>)

    inferred aligned divs



152
153
154
155
156
# File 'lib/proiel/div.rb', line 152

def inferred_alignment(aligned_source)
  sentences.map do |sentence|
    sentence.inferred_alignment(aligned_source)
  end.flatten.compact.map(&:div).uniq
end

#languageString

Returns language of the div as an ISO 639-3 language tag.

Returns:

  • (String)

    language of the div as an ISO 639-3 language tag



62
63
64
# File 'lib/proiel/div.rb', line 62

def language
  source.language
end

#printable_form(custom_token_formatter: nil) ⇒ String

Returns the printable form of the div with all token forms and any presentation data.

which is passed the token as its sole argument

Parameters:

  • custom_token_formatter (Lambda) (defaults to: nil)

    formatting function for tokens

Returns:

  • (String)

    the printable form of the div



96
97
98
99
100
# File 'lib/proiel/div.rb', line 96

def printable_form(custom_token_formatter: nil)
  [presentation_before,
   @children.map { |s| s.printable_form(custom_token_formatter: custom_token_formatter) },
   presentation_after].compact.join
end

#sentencesEnumerator

Finds all sentences in the div.

Examples:

Iterating sentences

sentences.each { |s| puts s.id }

Create an array with only reviewed sentences

sentences.select(&:reviewed?)

Counting sentences

sentences.count #=> 200

Returns:

  • (Enumerator)

    sentences in the div



115
116
117
# File 'lib/proiel/div.rb', line 115

def sentences
  @children.to_enum
end

#tokensEnumerator

Finds all tokens in the div.

Examples:

Iterating tokens

tokens.each { |t| puts t.id }

Create an array with only empty tokens

tokens.select(&:is_empty?)

Counting tokens

puts tokens.count #=> 200

Returns:

  • (Enumerator)

    tokens in the div



132
133
134
135
136
137
138
139
140
# File 'lib/proiel/div.rb', line 132

def tokens
  Enumerator.new do |y|
    @children.each do |sentence|
      sentence.tokens.each do |token|
        y << token
      end
    end
  end
end

#treebankTreebank

Returns parent treebank object.

Returns:



57
58
59
# File 'lib/proiel/div.rb', line 57

def treebank
  @source.treebank
end