代码之家  ›  专栏  ›  技术社区  ›  Konstantin

用Ruby将单词分解成字母

  •  2
  • Konstantin  · 技术社区  · 7 年前

    abc=[*%w{tty ccs lly ggy ssz nny dzs zzs sz zs cs gy ny dz ty ly q w r t z p l k j h g f d s x c v b n m y}.map{|z| [z,"c"]},*"eéuioöüóőúűáía".split(//).map{|z| [z,"v"]}].to_h
    

    spell("csobolyó") => [ "cs", "o", "b", "o", "ly", "ó" ]
    spell("nyirettyű") => [ "ny", "i", "r", "e", "tty", "ű" ]
    spell("dzsesszmuzsikus") => [ "dzs", "e", "ssz", "m", "u", "zs", "i", "k", "u", "s" ]
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Simple Lime    7 年前

    你可以开始看 String#scan ,这似乎为您的示例提供了不错的结果:

    "csobolyó".scan(Regexp.union(abc.keys))
    # => ["cs", "o", "b", "o", "ly", "ó"]
    "nyirettyű".scan(Regexp.union(abc.keys))
    # => ["ny", "i", "r", "e", "tty", "ű"]
    "dzsesszmuzsikus".scan(Regexp.union(abc.keys))
    # => ["dzs", "e", "ssz", "m", "u", "zs", "i", "k", "u", "s"]
    

    your statement in the comments

    我对字母表中的字母进行了排序:如果一个字母出现得更早,那么它应该被识别出来,而不是它的简单字母。当一个单词包含“dzs”时,应将其视为“dzs”,而不是“d”和“zs”

        2
  •  1
  •   Siva Praveen    7 年前

    我没有使用排序时的首选项,而是使用了较高字符的单词比较低字符的单词具有更高的首选项。

    def spell word
      abc=[*%w{tty ccs lly ggy ssz nny dzs zzs sz zs cs gy ny dz ty ly q w r t z p l k j h g f d s x c v b n m y}.map{|z| [z,"c"]},*"eéuioöüóőúűáía".split(//).map{|z| [z,"v"]}].to_h
      current_position = 0
      maximum_current_position = 2
      maximum_possible_position = word.length
      split_word = []
      while current_position < maximum_possible_position do 
        current_word = set_current_word word, current_position, maximum_current_position
        if abc[current_word] != nil
          current_position, maximum_current_position = update_current_position_and_max_current_position current_position, maximum_current_position
          split_word.push(current_word)
        else
          maximum_current_position = update_max_current_position maximum_current_position
          current_word = set_current_word word, current_position, maximum_current_position
          if abc[current_word] != nil
            current_position, maximum_current_position = update_current_position_and_max_current_position current_position, maximum_current_position
            split_word.push(current_word)
          else
            maximum_current_position = update_max_current_position maximum_current_position
            current_word = set_current_word word, current_position, maximum_current_position
            if abc[current_word] != nil
              current_position, maximum_current_position = update_current_position_and_max_current_position current_position, maximum_current_position          
              split_word.push(current_word)
            else
              puts 'This word cannot be formed in the current language'
              break
            end
          end
        end
      end
      split_word
    end
    
    def update_max_current_position max_current_position
        max_current_position = max_current_position - 1
    end
    
    def update_current_position_and_max_current_position current_position,max_current_position
        current_position = max_current_position + 1
        max_current_position = current_position + 2
        return current_position, max_current_position
    end
    
    def set_current_word word, current_position, max_current_position
      word[current_position..max_current_position]
    end
    
    puts "csobolyó => #{spell("csobolyó")}"
    puts "nyirettyű => #{spell("nyirettyű")}"
    puts "dzsesszmuzsikus => #{spell("dzsesszmuzsikus")}"
    

    输出

    csobolyó => ["cs", "o", "b", "o", "ly", "ó"]
    nyirettyű => ["ny", "i", "r", "e", "tty", "ű"]
    dzsesszmuzsikus => ["dzs", "e", "ssz", "m", "u", "zs", "i", "k", "u", "s"]
    
        3
  •  0
  •   Konstantin    7 年前

    abc=[*%w{tty ccs lly ggy ssz nny dzs zzs sz zs cs gy ny dz ty ly q w r t z p l k j h g f d s x c v b n m y}.map{|z| [z,"c"]},*"eéuioöüóőúűáía".split(//).map{|z| [z,"v"]}].to_h
    
    def spell(w,abc)
    
    
        s=w.split(//)
        p=""
        t=[]
    
        for i in 0..s.size-1 do
          p << s[i]
          if i>=s.size-2 then
    
           if abc[p]!=nil then
              t.push p
              p=""
    
           elsif abc[p[0..-2]]!=nil then
              t.push p[0..-2]
              p=p[-1]
    
           elsif abc[p[0]]!=nil then
              t.push p[0]
              p=p[1..-1]
    
           end 
    
          elsif p.size==3 then
           if abc[p]!=nil then
              t.push p
              p=""
    
           elsif abc[p[0..-2]]!=nil then
              t.push p[0..-2]
              p=p[-1]
    
           elsif abc[p[0]]!=nil then
              t.push p[0]
              p=p[1..-1]
           end
          end
        end
    
        if p.size>0 then
            if abc[p]!=nil then
              t.push p
              p=""
    
           elsif abc[p[0..-2]]!=nil then
              t.push p[0..-2]
              p=p[-1]
          end
        end
    
        if p.size>0 then
          t.push p
        end
        return t
    end
    
    推荐文章