<commit_before>require 'java'

# TODO Where should this REALLY go?
# TODO How to load cue.language.jar when you're a gem?
require '../bin/cue.language.jar'


module Cue

  def self.each_word(string, &block)
    iter = CueEnumerator.new Java::CueLang::WordIterator.new(string)

    if block_given?
      iter.each &block
    else
      iter
    end
  end

  def self.each_sentence(string, &block)
    iter = CueEnumerator.new Java::CueLang::SentenceIterator.new(string)

    if block_given?
      iter.each &block
    else
      iter
    end
  end

  # TODO allow for Locale, and custom StopWords
  def self.each_ngram(string, n, &block)
    iter = CueEnumerator.new Java::CueLang::NGramIterator.new(n, string)

    if block_given?
      iter.each &block
    else
      iter
    end
  end

  class CueEnumerator
    include Enumerable

    def initialize(java_iter)
      @java_iter = java_iter
    end

    def each
      while @java_iter.has_next
        yield @java_iter.next
      end
    end

  end

  # TODO add StopWords.guess()

end<commit_msg>DRYed up the Cue.each_* method implementations<commit_after>require 'java'

# TODO Where should this REALLY go?
# TODO How to load cue.language.jar when you're a gem?
require '../bin/cue.language.jar'


module Cue

  def self.each_word(string, &block)
    iterate Java::CueLang::WordIterator.new(string), &block
  end

  def self.each_sentence(string, &block)
    iterate Java::CueLang::SentenceIterator.new(string), &block
  end

  # TODO allow for Locale, and custom StopWords
  def self.each_ngram(string, n, &block)
    iterate Java::CueLang::NGramIterator.new(n, string), &block
  end

  private
  def self.iterate(java_iter, &block)
    CueEnumerator.new(java_iter).tap do |enum|
      if block_given?
        enum.each &block
      else
        enum
      end
    end
  end

  class CueEnumerator
    include Enumerable

    def initialize(java_iter)
      @java_iter = java_iter
    end

    def each
      while @java_iter.has_next
        yield @java_iter.next
      end
    end

  end

  # TODO add StopWords.guess()

end
{'fraction_non_alphanumeric': 0.08689390302448392, 'fraction_numerical': 0.0, 'mean_word_length': 2.795992714025501, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6366429', 'n_tokens_mistral': 732, 'n_tokens_neox': 687, 'n_words': 187}