lib/rseg.rb in rseg-0.1.1 vs lib/rseg.rb in rseg-0.1.2

- old
+ new

@@ -1,7 +1,10 @@ $KCODE = 'UTF8' +require 'singleton' +require 'net/http' + require File.join(File.dirname(__FILE__), 'engines/engine') require File.join(File.dirname(__FILE__), 'engines/dict') require File.join(File.dirname(__FILE__), 'engines/english') require File.join(File.dirname(__FILE__), 'engines/number') require File.join(File.dirname(__FILE__), 'engines/name') @@ -9,28 +12,45 @@ require File.join(File.dirname(__FILE__), 'filters/fullwidth') require File.join(File.dirname(__FILE__), 'filters/symbol') require File.join(File.dirname(__FILE__), 'filters/conjunction') class Rseg - @@engines = nil - @@segment = nil - @@filters = nil + include Singleton class << self def segment(input) - @@segment ||= Rseg.new(input) - @@segment.segment + Rseg.instance.input = input + Rseg.instance.segment end + + def load + Rseg.instance + nil + end + + def remote_segment(input) + begin + response = Net::HTTP.post_form(URI.parse('http://127.0.0.1:4100/seg'), :input => input) + response.code == '200' ? response.body.split(' ') : + ["Can't connect to http://localhost:4100\nUse rseg_server to start it"] + rescue + ["Can't connect to http://localhost:4100\nUse rseg_server to start it"] + end + end end - - def initialize(input) - @input = input + + def initialize + @input = '' @words = [] init_engines init_filters end + def input=(input) + @input = input + end + def segment @words.clear @input.chars.each do |origin| char = filter(origin) @@ -42,11 +62,11 @@ end private def filter(char) result = char - @@filters.each do |klass| + @filters.each do |klass| result = klass.filter(result) end result end @@ -91,22 +111,22 @@ engine.run end end def engines=(engines) - @@engines ||= engines + @engines ||= engines end def engines - @@engines + @engines end def init_filters - @@filters = [Fullwidth, Symbol] + @filters = [Fullwidth, Symbol] end def init_engines - @@engines ||= [Dict, English, Number, Name].map do |engine_klass| + @engines ||= [Dict, English, Number, Name].map do |engine_klass| engine_klass.new end end end \ No newline at end of file