class SSMD::Processors::AnnotationProcessor

Attributes

annotations[R]

Public Class Methods

annotations() click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 9
def self.annotations
  a = SSMD::Annotations

  [
    a::LanguageAnnotation, a::PhonemeAnnotation, a::ProsodyAnnotation,
    a::SubstitutionAnnotation
  ]
    .freeze
end
new(options = {}) click to toggle source
Calls superclass method
# File lib/ssmd/processors/annotation_processor.rb, line 19
def initialize(options = {})
  super

  @annotations = self.class.annotations.dup

  annotations.delete_if do |annotation|
    Array(options[:skip]).any? { |name| annotation.name =~ /\ASSMD::Annotations::#{name}Annotation\Z/i }
  end
end

Public Instance Methods

error?() click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 45
def error?
  !ok?
end
ok?() click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 41
def ok?
  !@text.nil? && Array(@annotations).size > 0
end
result() click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 29
def result
  _, annotations_text = match.captures

  if annotations_text
    @annotations = combine_annotations parse_annotations(annotations_text)

    @annotations.inject(text) do |text, a|
      a.wrap(text)
    end
  end
end

Private Instance Methods

annotations_regex() click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 96
def annotations_regex
  annotations.map(&:regex).join("|")
end
combine_annotations(annotations) click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 69
def combine_annotations(annotations)
  annotations
    .group_by { |a| a.class }
    .values
    .map { |as| as.reduce { |a, b| a.combine b } }
end
find_annotation(text) click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 63
def find_annotation(text)
  self.class.annotations.lazy
    .map { |a| a.try text }
    .find { |a| !a.nil? }
end
parse_annotations(text) click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 51
def parse_annotations(text)
  text.split(/, ?/).flat_map do |annotation_text|
    annotation = find_annotation annotation_text

    if annotation.nil?
      warnings.push "Unknown annotation: #{text}"
    end

    [annotation].compact
  end
end
regex() click to toggle source

Matches explicitly annotated sections. For example:

[Guardians of the Galaxy](en-GB, v: +4dB, p: -3%)
# File lib/ssmd/processors/annotation_processor.rb, line 81
def regex
  @regex ||= %r{
    \[                              # opening text
      ([^\[\]]+)                    # annotated text
    \]                              # closing text
    \(                              # opening annotations
      ((?:
        (?:
          #{annotations_regex}      # annotations
        )(?:,\s?)?
      )+)
    \)                              # closing annotations
  }x
end
warnings() click to toggle source
# File lib/ssmd/processors/annotation_processor.rb, line 100
def warnings
  @warnings ||= []
end