我想使用Nokogiri来计算出现在网站上的不同类属性的出现次数。为此,我实现了广度优先搜索,每次遇到新的类属性时,我都希望将其存储在具有唯一ID的哈希中。并且遇到每个相同的类属性,我想找到相同的哈希并增加其出现键。
我正在尝试将所有这些哈希存储在数组中:
hashArray = []
new hash = {
id: uniqueID,
occurrence: 1,
className: node["class"]
}
理想情况下,我在某些时候会遇到这样的事情:
array = [
{id: 1, occurrences: 3, className: 'wrapper'},
{id: 2, occurrences: 5, className: 'media'}
]
如何在每次搜索遇到新类时初始化一个新的哈希以添加到数组中?
我尝试过:
hashArray << {id: uniqueID, occurrence: 1, className: node["class"]}
但是这种方法导致数组一次只保存一个散列。
答案 0 :(得分:0)
我看到id和className都是唯一的,如果您使用classNames
作为标识符,则可能不需要添加其他id
。这是一种应该像您描述的那样工作的方法:
# Initialise a Hash which values default to 0 (instead of nil)
occurrences = Hash.new(0)
# Example:
# occurrences['foo'] # => 0
# occurrences # => {}
# For all relevant nodes, count occurrences of their class names
occurrences[node["class"]] += 1
# Example:
# occurrences['bar'] += 1 # => 1
# occurrences['bar'] += 1 # => 2
# occurrences['foo'] += 1 # => 1
# occurrences # => {"bar"=>2, "foo"=>1}
# Add ids and format array, where ids are the index of the element
occurrences.map.with_index do |list, id|
{id: id, occurence: list.last, className: list.first}
end
# Example:
# => [{:id=>0, :occurence=>2, :className=>"bar"}, {:id=>1, :occurence=>1, :className=>"foo"}]
希望您会有所帮助。