Ruby 我怎样才能阻止台词重复?

Ruby 我怎样才能阻止台词重复?,ruby,Ruby,看看这个代码。我得到了期望的结果,那就是扫描一个人的输入,看它是否匹配一个内部数组 sentence = [] compare = [] database_array = ["Mouse", "killer", "Blood", "Vampires", "True Blood", "Immortal" ] def parser_sentence compare database_array = ["Mouse", "killer", "Blood", "Vampires", "True

看看这个代码。我得到了期望的结果,那就是扫描一个人的输入,看它是否匹配一个内部数组

sentence = []
compare = []
database_array = ["Mouse", "killer", "Blood", "Vampires", "True Blood", "Immortal" ]

def parser_sentence compare
    database_array = ["Mouse", "killer", "Blood", "Vampires", "True Blood", "Immortal"]
    initial_index = 0
while compare.count > initial_index      
          compare.each do |item| 
         if item == database_array[initial_index]
            puts "You found the key word, it was #{item}"
          else
            puts "Sorry the key word was not inside your sentence"

          end
         end 
  initial_index = initial_index + 1
 end
end

puts "Please enter in your sentences of words and i will parse it for the key word."
sentence = gets.chomp
compare = sentence.split (" ")

因为每个循环都告诉它要重复,所以它会重复,但是我如何才能停止呢?

一个不涉及循环的可能解决方案是将
比较
数据库数组
数组相交,如下所示:

matching_words = compare & database_array
这将比较两个数组并创建一个新数组,该数组只包含两个数组共用的元素。例如:

# If the user input the sentence "The Mouse is Immortal", then...
compare = ["The", "Mouse", "is", "Immortal"]
# matching_words will contain an array with elements ["Mouse", "Immortal"]
matching_words = compare & database_array
然后可以检查数组的长度并显示消息。我相信这可以取代您的整个功能,如下所示:

def parser_sentence compare
    matching_words = compare & database_array
    if matching_works.length > 0
        puts "You found the key word, it was #{matching_words.join(" ")}"
    else
        puts "Sorry the key word was not inside your sentence"
    end
end

请注意
join
的用法,如果您不熟悉它,它基本上会使用数组中由传入的分隔符字符串分隔的每个元素创建一个字符串,在我的示例中,分隔符字符串只是一个空格;当然可以用你自己的单独数组代替,或者用你想用它做的任何事情。

一个不涉及循环的可能解决方案是将你的
compare
database\u array
数组相交,如下所示:

matching_words = compare & database_array
这将比较两个数组并创建一个新数组,该数组只包含两个数组共用的元素。例如:

# If the user input the sentence "The Mouse is Immortal", then...
compare = ["The", "Mouse", "is", "Immortal"]
# matching_words will contain an array with elements ["Mouse", "Immortal"]
matching_words = compare & database_array
然后可以检查数组的长度并显示消息。我相信这可以取代您的整个功能,如下所示:

def parser_sentence compare
    matching_words = compare & database_array
    if matching_works.length > 0
        puts "You found the key word, it was #{matching_words.join(" ")}"
    else
        puts "Sorry the key word was not inside your sentence"
    end
end

请注意
join
的用法,如果您不熟悉它,它基本上会使用数组中由传入的分隔符字符串分隔的每个元素创建一个字符串,在我的示例中,分隔符字符串只是一个空格;当然可以用你自己的separate代替,或者你想用它做的任何事情。

在这种情况下,正则表达式将比拆分输入字符串更有效,更不容易出错,特别是因为关键字列表中有两个单词的短语

def parser_sentence(sentence)
  matching_words = sentence.scan(Regexp.union(database_array))
  if matching_words.empty?
    puts "Sorry the key word was not inside your sentence"
  else
    puts "You found the key word, it was #{matching_words.join(" ")}"
  end
end

稍加修改即可使其区分大小写(如果需要),或在关键字中添加单词边界,以避免与部分单词匹配。

在这种情况下,正则表达式将比拆分输入字符串更有效,更不容易出错,特别是因为关键字列表中有两个单词的短语

def parser_sentence(sentence)
  matching_words = sentence.scan(Regexp.union(database_array))
  if matching_words.empty?
    puts "Sorry the key word was not inside your sentence"
  else
    puts "You found the key word, it was #{matching_words.join(" ")}"
  end
end

稍加修改即可使其区分大小写(如果需要的话),或在关键字中添加单词边界,以避免与部分单词不匹配。

请根据输入添加输入和输出。是否有任何原因使
数据库数组在此处重复?@tadman我猜可能是错误的复制粘贴:)初始索引不是一个好名字,因为您正在更改它。你也有一些松散的结尾。在空格中拆分句子的一个问题是,你永远找不到像“真血”这样的短语。请在输入的基础上添加输入和输出。是否有任何原因使
数据库数组在这里重复出现?@tadman,我想可能是错误的复制粘贴:)初始索引不是一个好名字,因为您正在更改它。你也有松散的结尾。在空格处拆分句子的问题是你永远找不到像“真爱如血”这样的短语