Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Ruby检查句子是否有来自数组的二元/三元单词_Arrays_Ruby_String - Fatal编程技术网

Arrays Ruby检查句子是否有来自数组的二元/三元单词

Arrays Ruby检查句子是否有来自数组的二元/三元单词,arrays,ruby,string,Arrays,Ruby,String,我想检查一个句子是否有一个数组项,它是来自wordsarray的二元/三元数组,组成数组项的二元/三元数组一起出现在句子中 words=[“foo”、“bar”、“spooky”、“rick james”] 句子=向里克·詹姆斯问好,但不要向里克和詹姆斯问好 由于rick james是一个数组项,并且一起, 预期产出应为 false #say false #hello false #to true #rick <--- true #james <--- false #but fals

我想检查一个句子是否有一个数组项,它是来自
words
array的二元/三元数组,组成数组项的二元/三元数组一起出现在句子中

words=[“foo”、“bar”、“spooky”、“rick james”]

句子=向里克·詹姆斯问好,但不要向里克和詹姆斯问好

由于
rick james
是一个数组项,并且一起
预期产出应为

false #say
false #hello
false #to
true #rick <---
true #james <---
false #but
false #not
false #rick <---
false #and
false #james <---
false#说
错#你好
错

对#rick也许您可以搜索数组中每个单词的句子(.include?)或(.include_in?)。这样,您将搜索.include?(“rick james”)

也许您可以搜索数组中每个单词的句子(.include?)或(.include_in?)。通过这种方式,您将搜索.include?(“rick james”)

如果您的域是bigrams/trigrams,则应将句子拆分为bigrams/trigrams

可能会对你有帮助(我会选择n=2作为bigrams)

如果bi/三角形作为一个整体包括在内,这意味着它的词汇也包括在内

words = ["foo", "bar", "spooky", "rick james"]   
sentence.split.each_cons(2) do |e| 
  puts "#{e} => #{words.include?(e)||words.include?(e.join(" "))}"
end

# ["say", "hello"] => false
# ["hello", "to"] => false
# ["to", "rick"] => false
# ["rick", "james"] => true
# ["james", "but"] => false
# ["but", "not"] => false
# ["not", "rick"] => false
# ["rick", "and"] => false
# ["and", "james"] => false

然后,您可以获取这些数组元素,并为每个元素返回true/flase

如果您的域是bigrams/trigrams,则应将句子拆分为bigrams/trigrams

可能会对你有帮助(我会选择n=2作为bigrams)

如果bi/三角形作为一个整体包括在内,这意味着它的词汇也包括在内

words = ["foo", "bar", "spooky", "rick james"]   
sentence.split.each_cons(2) do |e| 
  puts "#{e} => #{words.include?(e)||words.include?(e.join(" "))}"
end

# ["say", "hello"] => false
# ["hello", "to"] => false
# ["to", "rick"] => false
# ["rick", "james"] => true
# ["james", "but"] => false
# ["but", "not"] => false
# ["not", "rick"] => false
# ["rick", "and"] => false
# ["and", "james"] => false

然后,您可以获取这些数组元素,并为每个元素返回true/flase

为什么不
单词。每个{w | p句子。包括?(w)}
?拆分
句子的方式将与
“rick james”
不匹配。为什么不
单词。每个{w | p句子。包括?(w)}
?您拆分
句子的方式与
中的“rick james”
不匹配。句子需要按照问题中建议的方式拆分为单格。若句子中有匹配的三词或双词短语,则拆分时每个对应的单字都需要
true
。按照这样的顺序检查tri/bi/uni会太费劲。句子需要按照问题中建议的方式拆分为单格。若句子中有匹配的三词或双词短语,则拆分时每个对应的单字都需要
true
。按那个顺序检查它们的tri/bi/uni会太费钱。
words = ["foo", "bar", "spooky", "rick james"]   
sentence.split.each_cons(2) do |e| 
  puts "#{e} => #{words.include?(e)||words.include?(e.join(" "))}"
end

# ["say", "hello"] => false
# ["hello", "to"] => false
# ["to", "rick"] => false
# ["rick", "james"] => true
# ["james", "but"] => false
# ["but", "not"] => false
# ["not", "rick"] => false
# ["rick", "and"] => false
# ["and", "james"] => false