Sha256: 735b7b725e9dcfa5b68d6a54562ba49a524daad165cc6e74ae195e91241f3ed7

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

require File.expand_path('../examples_helper', File.dirname(__FILE__))

# "Telegram Problem"
#
# The "Telegram Problem", originally described by Peter Naur:
# * accepts lines of text
# * generates output lines that are shortern than a given length (or contain only one word)
# * without splitting any of the words in the text (if a word is longer than the line width, emit it on its own line).

Wukong.processor :recompose do
  field :break_length, Integer
  attr_accessor :line

  def initialize(*) super; @line = "" ; end

  def process(word)
    if word == "" then flush! ; emit("") ; return ; end
    flush! if "#{line} #{word}".lstrip.length > break_length
    if word.length >= break_length
      emit word
    else
      line << " " << word
    end
  end

  def flush!
    emit line[1..-1] unless line.blank?
    self.line = ""
  end

  def stop
    flush!
  end
end

ExampleUniverse.dataflow(:telegram) do
  input   :default, file_source(Pathname.path_to(:data, 'text/rectification_of_names.txt'))
  output  :dump,    file_sink(  Pathname.path_to(:tmp,  'output/dataflow/telegram/names.txt'))

  input(:default) >
    map{|line| line.blank? ? [""] : line.strip.split(/\s+/m) } >
    flatten >
    recompose(:break_length => 80) >
    output(:dump)
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
ul-wukong-4.1.1 examples/dsl/dataflow/telegram.rb
ul-wukong-4.1.0 examples/dsl/dataflow/telegram.rb
wukong-4.0.0 examples/dsl/dataflow/telegram.rb
wukong-3.0.1 examples/dsl/dataflow/telegram.rb
wukong-3.0.0 examples/dsl/dataflow/telegram.rb
wukong-3.0.0.pre3 examples/dsl/dataflow/telegram.rb
wukong-3.0.0.pre2 examples/dataflow/telegram.rb
wukong-3.0.0.pre examples/dataflow/telegram.rb