Sha256: 331c4d454802c42f4dfe80b3a86fc9d37c79903653463f3aea0fdb75f2daa088

Contents?: true

Size: 939 Bytes

Versions: 11

Compression:

Stored size: 939 Bytes

Contents

require_relative 'annotation'

require 'pathname'

module SSMD::Annotations
  class PhonemeAnnotation < Annotation
    attr_reader :x_sampa, :ipa

    def self.regex
      /ph: ?(.+)/
    end

    def initialize(x_sampa)
      @x_sampa = x_sampa
      @ipa = x_sampa_to_ipa x_sampa
    end

    def wrap(text)
      "<phoneme alphabet=\"ipa\" ph=\"#{ipa}\">#{text}</phoneme>"
    end

    def combine(annotation)
      self # discard further phoneme annotations
    end

    def x_sampa_to_ipa(input)
      x_sampa_to_ipa_table.inject(input) do |text, (x_sampa, ipa)|
        text.gsub x_sampa, ipa
      end
    end

    def x_sampa_to_ipa_table
      @table ||= begin
        lines = File.read(x_sampa_to_ipa_table_file_path).lines

        lines.map { |line| line.split(" ") }
      end
    end

    def x_sampa_to_ipa_table_file_path
      Pathname(SSMD.root_dir).join("lib/ssmd/annotations/xsampa_to_ipa_table.txt")
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ssmd-0.6.1 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.6.0 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.5.0 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.4.0 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.3.1 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.3.0 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.2.3 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.2.2 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.2.1 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.2.0 lib/ssmd/annotations/phoneme_annotation.rb
ssmd-0.1.0 lib/ssmd/annotations/phoneme_annotation.rb