Sha256: 54d198d153ba71bdaf407208549f40ab7d23dedf21c3c6d2aaae27f7f300e950

Contents?: true

Size: 1.3 KB

Versions: 23

Compression:

Stored size: 1.3 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Segmentation
    class BreakIterator
      attr_reader :locale, :options

      def initialize(locale = TwitterCldr.locale, options = {})
        @locale = locale
        @options = options
      end

      def each_sentence(str, &block)
        iter = iterator_for('sentence')
        iter.each_segment(str, &block)
      end

      def each_word(str, &block)
        iter = iterator_for('word')
        iter.each_segment(str, &block)
      end

      def each_grapheme_cluster(str, &block)
        iter = iterator_for('grapheme')
        iter.each_segment(str, &block)
      end

      def each_line(str, &block)
        iter = iterator_for('line')
        iter.each_segment(str, &block)
      end

      private

      def iterator_for(boundary_type)
        iterator_cache[boundary_type] ||= begin
          rule_set = RuleSet.create(locale, boundary_type, options)

          case boundary_type
            when 'line'
              LineIterator.new(rule_set)
            when 'word'
              WordIterator.new(rule_set)
            else
              SegmentIterator.new(rule_set)
          end
        end
      end

      def iterator_cache
        @iterator_cache ||= {}
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
twitter_cldr-6.11.5 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.11.4 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.11.3 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.11.2 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.11.1 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.11.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.10.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.9.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.8.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.7.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.6.2 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.6.1 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.6.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.5.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.4.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.3.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.2.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.1.0 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.0.2 lib/twitter_cldr/segmentation/break_iterator.rb
twitter_cldr-6.0.1 lib/twitter_cldr/segmentation/break_iterator.rb