Class: Mmseg

Inherits:
Object
  • Object
show all
Defined in:
lib/mmseg.rb

Instance Method Summary collapse

Constructor Details

#initialize(dic_path = File.dirname(__FILE__)+"/dict.txt") ⇒ Mmseg

Returns a new instance of Mmseg.



3
4
5
6
7
8
9
10
11
12
# File 'lib/mmseg.rb', line 3

def initialize(dic_path=File.dirname(__FILE__)+"/dict.txt")
	@dict=Hash.new
	@result=""
	s=File.open(dic_path)
	@dict=Hash.new(0)
	while str=s.gets
 			str=str.chomp
 			@dict[str]=1
	end
end

Instance Method Details

#segment(str) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mmseg.rb', line 14

def segment(str)
 		str=str.chomp
 		left=str
   while left.length>0
   	if @dict[str]==1 or str.length==1
     		@result+=str+"  "
     		len=str.length
     		left=left[len..-1]
     		str=left         
	else
    		str=str[0..-2]     
	end
end
	return @result
end