Ruby 在数组中的数组内动态创建哈希
我正在创建一个数组: 所有单词的哈希 在所有段落数组中 用于文件夹中所有文件的数组 我相信我已经得到了所有单词的哈希值,在所有段落的数组中。但是对每个文件都这样做,并为每个文件创建一个特定的密钥,这是一个太远的桥梁 这是到目前为止我的代码。为文件夹中的所有文件创建唯一数组,并将该文件的所有段落数组放入文件数组时出错Ruby 在数组中的数组内动态创建哈希,ruby,arrays,loops,hash,Ruby,Arrays,Loops,Hash,我正在创建一个数组: 所有单词的哈希 在所有段落数组中 用于文件夹中所有文件的数组 我相信我已经得到了所有单词的哈希值,在所有段落的数组中。但是对每个文件都这样做,并为每个文件创建一个特定的密钥,这是一个太远的桥梁 这是到目前为止我的代码。为文件夹中的所有文件创建唯一数组,并将该文件的所有段落数组放入文件数组时出错 numberfiles = Dir.glob(File.join('**', '*')).select { |file| File.file?(file) }.count c
numberfiles = Dir.glob(File.join('**', '*')).select { |file| File.file?(file) }.count
countfiles+1
# HERE I MAKE THE ARRAY FOR ALL FILES
filesArray = Array.new(numberfiles.to_i, Hash.new)
for j in 0...numberfiles.to_i do
filesArray[j] = Hash.new
end
#now to open all textfiles..
Dir.glob("*.txt").each do |textfile|
lines = File.readlines(textfile)
text = lines.join
paragraph_count = text.split("\.\r").length
#create array with key for every paragraph
testArray = Array.new(paragraph_count.to_i, Hash.new)
for $i in 0...paragraph_count.to_i do
testArray[$i] = Hash.new
end
words_in_each_paragraph = Array.new
i = 0
files = []
Dir.glob("*.txt").each do |textfile|
paragraphs = []
File.foreach(textfile, "\n\n") do |paragraph|
words = Hash.new(0)
paragraph.split(/\W+/).each {|word| words[word] += 1}
paragraphs << words
end
files << paragraphs
end
p files
在这里,我想将所有TestArray保存到filearray中。这是行不通的:
File.foreach(textfile, "\.\r") do |paragraph|
word_hash = {}
paragraph.split(/\W+/).each_with_object(word_hash) { |w, h|
h[w] = []
}
words_in_each_paragraph << word_hash
testArray[i][:value] = word_hash
filesArray[j][:file] = testArray # HERE IT GOES WRONG
i += 1
end
puts filesArray[1]
end
File.foreach(textfile,\.\r)do|段落|
单词_hash={}
段落.拆分(/\W+/)。每个带有_对象的_(单词_散列){| W,h|
h[w]=[]
}
每个段落中的单词我不完全确定您想做什么,但我知道您实际上不必在Ruby中预先分配数组的大小。下面的代码将遍历每个.txt文件,将其拆分为段落,并将这些段落中的每个单词放入散列中。该单词散列被附加到段落数组,而段落数组又被附加到文件数组
numberfiles = Dir.glob(File.join('**', '*')).select { |file| File.file?(file) }.count
countfiles+1
# HERE I MAKE THE ARRAY FOR ALL FILES
filesArray = Array.new(numberfiles.to_i, Hash.new)
for j in 0...numberfiles.to_i do
filesArray[j] = Hash.new
end
#now to open all textfiles..
Dir.glob("*.txt").each do |textfile|
lines = File.readlines(textfile)
text = lines.join
paragraph_count = text.split("\.\r").length
#create array with key for every paragraph
testArray = Array.new(paragraph_count.to_i, Hash.new)
for $i in 0...paragraph_count.to_i do
testArray[$i] = Hash.new
end
words_in_each_paragraph = Array.new
i = 0
files = []
Dir.glob("*.txt").each do |textfile|
paragraphs = []
File.foreach(textfile, "\n\n") do |paragraph|
words = Hash.new(0)
paragraph.split(/\W+/).each {|word| words[word] += 1}
paragraphs << words
end
files << paragraphs
end
p files
文件=[]
Dir.glob(“*.txt”)。每个do |文本文件|
段落=[]
foreach(textfile,“\n\n”)do |段落|
words=Hash.new(0)
段落.拆分(/\W+/)。每个{字{字[字]+=1}
段落当您想对可枚举元素的每个元素执行操作并将结果存储在数组中时,请考虑map
result = Dir.glob("*.txt").map do |textfile|
File.read(textfile).split("\n\n").map do |paragraph| #again!
words = Hash.new(0)
paragraph.split(/\W+/).each {|word| words[word] += 1} #copied from @Jonas Elfström
end
end
p result
你为什么要粘贴所有的代码。。只给出疼痛区域。为什么要使用进行
循环?使用each
Arup,我正在粘贴大部分代码,以便获得清晰的上下文:它是在[Current not working and not dynamic]数组中动态创建的数组中动态创建的哈希。这就是方法。现在我想更改我的答案。:)@JonasElfströmsteenslag和steenslag:很棒的工作。这就是我需要的(我必须承认:从50行到7行是纯粹的美)。现在,在这之后,所有内容是如何映射/索引的?第二份文件第二段的参考内容是什么如何引用循环中所有文件的所有段落?files[1][1]
包含第二个文件的第二段。如果要循环所有段落,请先将文件展平。也要考虑使用<代码> map < /C> >更合适。