class String

  def mbsubstr1(*range)

      split(//)[*range].join("")

  end

 

   def mbsubstr2(idx, len)

       slice(/\A.{#{idx}}(.{0,#{len}}/m, 1)

   end

end

p "一大串字一大串字".mbsubstr1(2,3)

p "一大串字一大串字".mbsubstr1(2..3)

p "一大串字一大串字".mbsubstr2(2,2)

Maximum Matching Segmentation (MMSeg)

#str 一段文字, term 字典的中的詞

def mmseg(str,term)
strEnd = str.length-1
  loop do
    tmpStr=str.substr(0..strEnd)
    strEnd -= 1
    sleep 0.1
    puts tmpStr

    if tmpStr == term
      print "I found #{tmpStr}"
      break
    end
    if (strEnd == -1)
      str=str.substr(1..-1)
      strEnd = str.length-1
      if str.length == 1
        puts "no match"
        break
      end
    end
  end
end

 

 

arrow
arrow
    全站熱搜

    igogo 發表在 痞客邦 留言(0) 人氣()