Ruby 解析数组以查找单个单词,大小写是相关的

Ruby 解析数组以查找单个单词,大小写是相关的,ruby,Ruby,下面是我正在做的练习 应该返回每个诅咒词的元音替换为“*”do的句子 示例#1检查员(“该死的,该死的,该死的,开枪的”)=>(“G*SHD*rn it”) 例如#2检查员(“关闭前门”、[“门”)=>(“关闭前门D**R”) 我最困惑的是如何解释大写字母。我最初开始循环遍历每个单词,但无法找到使用诸如。include之类的单词的最佳方法来检查该单词是否在数组中 我从下面开始 def censor(sentence, curse) vowels = "aeiouAEIOU" r

下面是我正在做的练习

应该返回每个诅咒词的元音替换为“*”do的句子

示例#1检查员(“该死的,该死的,该死的,开枪的”)=>(“G*SHD*rn it”)

例如#2检查员(“关闭前门”、[“门”)=>(“关闭前门D**R”)

我最困惑的是如何解释大写字母。我最初开始循环遍历每个单词,但无法找到使用诸如。include之类的单词的最佳方法来检查该单词是否在数组中

我从下面开始

 def censor(sentence, curse)
    vowels = "aeiouAEIOU"
    result = ""
    words = sentence.split(' ')

    new_curse = curse.join(",")
    p new_curse.downcase
    p words

    words.each.with_index do ???
            if word.casecmp?()
                word.each_char do |char|
                    if vowels.include?(char)
                        result << "*"
                    else
                        result << char
                    end
                end
            else 
                 result << word 
            end

     end
    return result
end
def审查(判决、诅咒)
元音=“aeiouAEIOU”
result=“”
单词=句子。拆分(“”)
新诅咒=诅咒。加入(“,”)
新的诅咒
p字
单词。每一个。有索引吗???
如果word.casecmp?()
每个字符都有一个字符|
如果是元音。包括?(字符)

结果这就是我的结局,也许有更好的方法,所以如果是的话,请告诉我。我被小写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母大写字母


def get_stared(word)
  vowels = "aeiou"
  result = ""

  word.each_char do |char|
    if vowels.include?(char.downcase)
      result += "*"
    else
      result += char
    end
  end
  result
end

def censor(sentence, curse_words)

  words = sentence.split(" ")

  new_words = words.map do |word|
    if curse_words.include?(word.downcase)
      get_stared(word)
    else
      word
    end

  end
new_words.join(" ")
end


如果您利用ruby的内置功能,可以更优雅地完成这项工作:

def censor(sentence, curse)
  curse_re = Regexp.new(Regexp.union(curse).source, 'i')
  sentence.split(' ').map do |word|
    curse_re.match(word) ? word.gsub(/[aeiou]/i, '*') : word
  end.join(' ')
end
这适用于您的两个测试用例:

i
标志使元音忽略大小写,
curse\u re
将匹配任何诅咒词,也忽略大小写


作为将来的参考,您将在上对此类问题获得更好的回答。

可能有很多方法可以做到这一点,这就是编码的乐趣所在。如果您想使用正则表达式,这里有一种方法。元音替换不区分大小写,因此它解决了这个问题:

sentence = "Darn, DARN! I forgot my pizza. SHOOT, flip this!" 
bwords = %w(darn shoot flip) #cuss words to censor
censored = [] #array for the result

words = sentence.split(" ")

for word in words
word = word.gsub(/[aeiou]/i, "*") if bwords.include? word.downcase.gsub(/[^a-z0-9\s]/, '')
censored.push(word) #put censored word in new array
end

puts censored.join(' ')
产出:


D*rn,D*rn!我忘了我的比萨饼。SH**T,fl*p这个!

没有必要将字符串拆分成单词,编辑每个单词,然后将它们重新组合在一起形成一个新字符串

def censor(sentence, coarse_words)
   sentence.gsub(/\p{L}+/) { |word|
     coarse_words.include?(word.downcase) ? word.gsub(/[aeiou]/i, '*') : word }
end

对于不熟悉
\p{L}
的读者,请搜索它。它与
\p{Alpha}
[:Alpha:]
相同,都记录在同一文件中

如果要过滤大量文本,则创建一组粗略的单词会更有效:

require 'set'

def censor(sentence, coarse_words)
    coarse_words_set = coarse_words.to_set
    sentence.gsub(/\p{L}+/) { |word|
      coarse_words_set.include?(word.downcase) ? word.gsub(/[aeiou]/i, '*') : word }
end
此外,如果粗词列表是静态的,则可以将粗词集设置为常量:

require 'set'
COARSE_WORDS = ["gosh", "darn", "shoot"].to_set

def censor(sentence)
    sentence.gsub(/\p{L}+/) { |word|
      COARSE_WORDS.include?(word.downcase) ? word.gsub(/[aeiou]/i, '*') : word }
end

你的代码有很多问题。你为什么不先做一个不考虑大写的工作函数呢。从这一点上讲,当你比较句子单词和诅咒词“大写是相关的”时,只需使用大写或小写就可以很容易地添加这一额外的功能“–我认为这无关紧要。似乎取决于你的观点;-)
require 'set'
COARSE_WORDS = ["gosh", "darn", "shoot"].to_set

def censor(sentence)
    sentence.gsub(/\p{L}+/) { |word|
      COARSE_WORDS.include?(word.downcase) ? word.gsub(/[aeiou]/i, '*') : word }
end