我遇到了一个问题,而不是附加 ["02.html", 7]
在值列表中,它为“word”创建了一个全新的条目,并且
将 ["02.html", 7] 放在哈希的末尾。
我没看到:
invindex = {
word1: [
['01.html', 2],
]
}
tokens = %i[
word1
word2
word3
]
doc_name = '02.html'
tokens.each do |token|
if !invindex.include?(token)
invindex[token] = [[doc_name, 1]] #adds the word to the hash with the doc name and occurrence of 1
else
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token].insert([doc_name, 1]) #this SHOULD append the doc name and initial occurrence inside the word's value list since the word is already in the hash
end
end
end
end
p invindex
--output:--
{:word1=>[["01.html", 2]], :word2=>[["02.html", 1]], :word3=>[["02.html", 1]]}
invindex[token].insert([doc_name, 1]) #this SHOULD append the doc name
没有:
invindex = {
word: [
['01.html', 2],
]
}
token = :word
doc_name = '02.html'
invindex[token].insert([doc_name, 7])
p invindex
invindex[token].insert(-1, ["02.html", 7])
p invindex
--output:--
{:word=>[["01.html", 2]]}
{:word=>[["01.html", 2], ["02.html", 7]]}
Array#insert() 要求您将索引指定为第一个参数。通常,当您想在末尾附加一些内容时,您使用<<:
invindex = {
word: [
['01.html', 2],
]
}
token = :word
doc_name = '02.html'
invindex[token] << [doc_name, 7]
p invindex
--output:--
{:word=>[["01.html", 2], ["02.html", 7]]}
for token in tokens
Ruby 爱好者不使用for-in 循环,因为for-in 循环调用each(),所以Ruby 爱好者直接调用each():
tokens.each do |token|
...
end
最后,indenting in ruby 是 2 个空格——不是 3 个空格,不是 1 个空格,也不是 4 个空格。是 2 个空格。
将所有这些应用到您的代码中:
invindex = {
word1: [
['01.html', 2],
]
}
tokens = %i[
word1
word2
word3
]
doc_name = '01.html'
tokens.each do |token|
if !invindex.include?(token)
invindex[token] = [[doc_name, 1]] #adds the word to the hash with the doc name and occurrence of 1
else
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token] << [doc_name, 1] #this SHOULD append the doc name and initial occurrence inside the word's value list since the word is already in the hash
end
end
end
end
p invindex
--output:--
{:word1=>[["01.html", 3]], :word2=>[["01.html", 1]], :word3=>[["01.html", 1]]}
但是,仍然存在一个问题,这是因为您正在更改一个正在逐步执行的数组——这是计算机编程中的一大禁忌:
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token] << [doc_name, 1] #***PROBLEM***
看看会发生什么:
invindex = {
word1: [
['01.html', 2],
]
}
tokens = %i[
word1
word2
word3
]
%w[ 01.html 02.html].each do |doc_name|
tokens.each do |token|
if !invindex.include?(token)
invindex[token] = [[doc_name, 1]] #adds the word to the hash with the doc name and occurrence of 1
else
invindex[token].each do |list|
if list[0] == doc_name
list[1] += 1 #adds one to the occurrence with the same doc_name
else
invindex[token] << [doc_name, 1] #this SHOULD append the doc name and initial occurrence inside the word's value list since the word is already in the hash
end
end
end
end
end
p invindex
--output:--
{:word1=>[["01.html", 3], ["02.html", 2]], :word2=>[["01.html", 1], ["02.html", 2]], :word3=>[["01.html", 1], ["02.html", 2]]}
问题 1: 您不想在每次检查的子数组不包含doc_name 时插入[doc_name, 1]——您只想在之后插入[doc_name, 1]已检查所有子阵列,但未找到 doc_name。如果您使用起始哈希运行上面的示例:
invindex = {
word1: [
['01.html', 2],
['02.html', 7],
]
}
...你会看到输出更糟。
问题 2: 在遍历数组时将[doc_name, 1] 附加到数组意味着当循环到达数组末尾时,[doc-name, 1] 也将被检查——然后您的循环会将其计数增加到 2。规则是:不要更改您正在单步执行的数组,因为会发生不好的事情。