Class: PROIEL::Source

Inherits:
TreebankObject show all
Defined in:
lib/proiel/source.rb

Overview

A source object in a treebank.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from TreebankObject

#inspect

Constructor Details

#initialize(parent, id, export_time, language, dialect, metadata, alignment_id, &block) ⇒ Source

Creates a new source object.

Raises:

  • (ArgumentError)

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/proiel/source.rb', line 32

def initialize(parent, id, export_time, language, dialect, , alignment_id, &block)
  @treebank = parent
  @id = id.freeze

  raise ArgumentError, 'string or nil expected' unless export_time.nil? or export_time.is_a?(String)
  @export_time = export_time.nil? ? nil : DateTime.parse(export_time).freeze

  @language = language.freeze
  @dialect = dialect ? dialect.freeze : nil
  @metadata = .freeze

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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Accesses metadata fields.


68
69
70
71
72
73
74
# File 'lib/proiel/source.rb', line 68

def method_missing(method_name, *args, &block)
  if @metadata.key?(method_name) and args.empty?
    @metadata[method_name]
  else
    super
  end
end

Instance Attribute Details

#alignment_idnil, String (readonly)

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

Returns:

  • (nil, String)

    ID of the source that this source is aligned to


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

def alignment_id
  @alignment_id
end

#dialectString (readonly)

Returns dialect of the source.

Returns:

  • (String)

    dialect of the source


19
20
21
# File 'lib/proiel/source.rb', line 19

def dialect
  @dialect
end

#export_timeDateTime (readonly)

Returns export time for the source.

Returns:

  • (DateTime)

    export time for the source


22
23
24
# File 'lib/proiel/source.rb', line 22

def export_time
  @export_time
end

#idString (readonly)

Returns ID of the source.

Returns:

  • (String)

    ID of the source


10
11
12
# File 'lib/proiel/source.rb', line 10

def id
  @id
end

#languageString (readonly)

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

Returns:

  • (String)

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


16
17
18
# File 'lib/proiel/source.rb', line 16

def language
  @language
end

#metadataHash{Symbol, String} (readonly)

Returns metadata fields for the source.

Returns:

  • (Hash{Symbol, String})

    metadata fields for the source

See Also:


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

def 
  @metadata
end

#treebankTreebank (readonly)

Returns treebank that this source belongs to.

Returns:

  • (Treebank)

    treebank that this source belongs to


13
14
15
# File 'lib/proiel/source.rb', line 13

def treebank
  @treebank
end

Instance Method Details

#citationString

Returns a complete citation for the source.

Returns:

  • (String)

    a complete citation for the source


50
51
52
# File 'lib/proiel/source.rb', line 50

def citation
  citation_part
end

#divsEnumerator

Finds all divs in the source.

Returns:

  • (Enumerator)

    divs in the source


79
80
81
# File 'lib/proiel/source.rb', line 79

def divs
  @children.to_enum
end

#printable_form(custom_token_formatter: nil) ⇒ String

Returns the printable form of the source 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 source


61
62
63
# File 'lib/proiel/source.rb', line 61

def printable_form(custom_token_formatter: nil)
  @children.map { |d| d.printable_form(custom_token_formatter: custom_token_formatter) }.compact.join
end

#sentencesEnumerator

Finds all sentences in the source.

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 source


96
97
98
99
100
101
102
103
104
# File 'lib/proiel/source.rb', line 96

def sentences
  Enumerator.new do |y|
    @children.each do |div|
      div.sentences.each do |sentence|
        y << sentence
      end
    end
  end
end

#tokensEnumerator

Finds all tokens in the source.

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 source


119
120
121
122
123
124
125
126
127
128
129
# File 'lib/proiel/source.rb', line 119

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