Sha256: 2cc041f4054f01487f117e5a9c0b72bd4600b3b325d5575da92f25f1fe9679af

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require "forwardable"

require "dphil/syllables/syllable"

module Dphil
  class Syllables
    using ::Ragabash::Refinements
    include Enumerable
    extend Forwardable
    def_delegators :@syllables, :[], :each, :first, :last, :length

    attr_reader :source, :source_script, :weights, :syllables

    def initialize(source, source_script: nil)
      @source = source.to_str.safe_copy.freeze
      @source_script = source_script || Transliterate.detect(@source) || Transliterate.default_script
      slp1_syllables = VerseAnalysis.syllables(@source, from: @source_script, to: :slp1)
      @weights = VerseAnalysis.syllables_weights(slp1_syllables, from: :slp1, contextual: true).freeze
      @syllables = (slp1_syllables.map.with_index do |syl, i|
        source = @source_script == :slp1 ? syl : Transliterate.t(syl, :slp1, @source_script)
        Syllables::Syllable.new(source, @weights[i], parent: self, index: i, slp1: syl)
      end).freeze
    end

    def inspect
      "<Syllables \"#{@source}\":#{@source_script} (#{@weights}) (#{@syllables.count}) => #{@syllables.inspect}>"
    end

    def to_a
      @syllables.map { |syl| Transliterate.t(syl.source, :slp1, @source_script) }
    end

    def to_s
      @source.dup
    end

    def simple_weights
      @simple_weights ||= @weights.upcase.freeze
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dphil-0.1.4 lib/dphil/syllables.rb