Sha256: 5086d56aad052599ea70456cc7770123954c6334b26067a89aa1527bd88ff57a

Contents?: true

Size: 703 Bytes

Versions: 2

Compression:

Stored size: 703 Bytes

Contents

require 'rubygems'
require 'json'
require 'cgi'
require 'open-uri'

class GlitchIME
  def initialize(text)
    @text = text
  end

  def generate
    result = ''
    patterns.each{|pair|
      key, values = *pair
      result += choice(values)
    }
    result
  end

  private
  def patterns
    @json ||= begin
      url = "http://www.google.com/transliterate?langpair=ja-Hira%7Cja&text=" + CGI.escape(@text)
      src =  open(url).read.gsub(/\n/, '').gsub(/,\]/, ']')
      JSON.parse(src)
    end
  end

  def choice(array)
    if array.respond_to? :choice
      array.choice
    elsif array.respond_to? :sample
      array.sample
    else
      array[(rand * array.length).to_i ]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
glitch-ime-0.2.0 lib/glitch-ime.rb
glitch-ime-0.1.0 lib/glitch-ime.rb