我知道我的问题非常简单,但我无法在互联网上找到答案。
我有一个名为sorted_frequency的哈希。我想使用gem hirb将其输出为表格。那时我刚刚能够在默认字段名称(0,1)下打印哈希。所以它看起来像是:
0 1
wordA ntimes
wordB mtimes
wordC rtimes
我想重命名字段名称,所以它会是这样的:
words number of times
wordA ntimes
wordB mtimes
wordC rtimes
我的实际代码是:
#needs to install 'docx' and 'hirb' gems
require 'docx'
require 'hirb'
doc = Docx::Document.open('monografia.docx')
text_listed = doc.to_s.downcase.split(" ")
forbidden_list = ["o", "si", "em", "ha", "no", "és", "amb", "i", "/","el",
"la", "els","les", "l'", "lo", "los", "en", "n'", "na", "es", "ets", "s'",
"sa", "so", "ses", "sos", "un", "una", "unes", "uns", "a", "que", "s'",
"al", "de","del", "per", "ens", "als", "com"]
clean_text= text_listed - forbidden_list
frequency = Hash.new 0
clean_text.each { |word| frequency[word] += 1 }
sorted_frequency = Hash[frequency.sort_by{ | word, times | -times }[0..20]]
puts Hirb::Helpers::AutoTable.render(sorted_frequency)
再次,如果这是一个新手问题,我很抱歉
编辑:
当我的所有代码都被问到时,我会解释它。它在一个名为' docx'的宝石的帮助下打开一个docx文档。之后,它按空格分割doc并创建一个数组。之后,我删除了一些我不想计算的单词(包括在forbidden_list中的单词)。然后我创建一个哈希,其中键是单词,值是该单词出现在docx中的次数。之后,我使用gem' hirb对哈希和输出进行排序。问题是我不知道如何命名创建的表的字段。我希望有人可以帮助我。
答案 0 :(得分:0)
根据hirb docs,AutoTable.render()
接受一个可以是array_of_arrays或array_of_hashes的参数。但是因为你的哈希参数有效,我查看了你的表输出,我决定只尝试添加一个选项来更改你得到的列名:
require 'hirb'
freqs = {
'go' => 3,
'no' => 4,
'to' => 1
}
puts Hirb::Helpers::AutoTable.render(
freqs,
fields: [1, 0], #Specify which fields to include in the table and their order.
#For a row that is an array, the field names are the integers 0, 1, 2, etc.
#For a row that is a hash, the field names are the keys.
headers: {0 => 'Word', 1 => 'Frequency'}, #Convert the field names to something more desirable for the column headers
description: false #Get rid of "3 rows in set" following the table
)
--output:--
+-----------+------+
| Frequency | Word |
+-----------+------+
| 3 | go |
| 4 | no |
| 1 | to |
+-----------+------+
有效。必须发生的是:AutoTable.render()
需要一个数组 - array_of_arrays或array_of_hashes - 如果该方法没有将数组作为参数,它会在参数上调用to_a()
。看看你的哈希会发生什么:
~/ruby_programs$ irb
2.4.0 :001 > freqs = {'go' => 3, 'no' => 4, 'to' => 1}
=> {"go"=>3, "no"=>4, "to"=>1}
2.4.0 :002 > freqs.to_a
=> [["go", 3], ["no", 4], ["to", 1]]
AutoTable.render()
需要的array_of_arrays。重新排列一下,array_of_arrays看起来像这样:
index 0 index 1
| |
[ V V
["go", 3], #row array
["no", 4],
["to", 1]
]
对于array_of_arrays,表中的列标题是每个行数组中的索引位置。 AutoTable.render()
的选项允许您指定要包含在表中的列/索引位置及其顺序,并且这些选项允许您将列标题转换为更合意的列。
这是一个更一般的例子:
require 'hirb'
require 'pp'
data = [
['go', 1, '1/12/18'],
['to', 4, '1/24/18'],
['at', 2, '1/28/18']
]
puts Hirb::Helpers::AutoTable.render(
data,
fields: [2, 0], #Specify the index positions in each row array to include in the table and their column order in the table
headers: {0 => 'Word', 2 => 'Date'}, #Convert the column headers to something more desirable
description: false #Get rid of "3 rows in set" following the table
)
--output:--
+---------+------+
| Date | Word |
+---------+------+
| 1/12/18 | go |
| 1/24/18 | to |
| 1/28/18 | at |
+---------+------+
====
require 'hirb'
require 'pp'
freqs = {
'go' => 3,
'no' => 4,
'to' => 1
}
col_names = %w[word count]
new_freqs = freqs.map do |key, val|
{col_names[0] => key, col_names[1] => val}
end
pp new_freqs
puts Hirb::Helpers::AutoTable.render(
new_freqs,
fields: ['word', 'count'], #Specify which keys to include in table and their column order.
headers: {'word' => 'Good Word', 'count' => 'Frequency'}, #Convert keys to more desirable headers.
description: false #Get rid of "3 rows in set" following the table
)
--output:--
[{"word"=>"go", "count"=>3},
{"word"=>"no", "count"=>4},
{"word"=>"to", "count"=>1}]
+-----------+-----------+
| Good Word | Frequency |
+-----------+-----------+
| go | 3 |
| no | 4 |
| to | 1 |
+-----------+-----------+
====
require 'hirb'
require 'pp'
freqs = {
'go' => 3,
'no' => 4,
'to' => 1
}
col_names = %i[word count]
new_freqs = freqs.map do |key, val|
{col_names[0] => key, col_names[1] => val}
end
pp new_freqs
puts Hirb::Helpers::AutoTable.render(new_freqs)
--output:--
[{:word=>"go", :count=>3}, {:word=>"no", :count=>4}, {:word=>"to", :count=>1}]
+-------+------+
| count | word |
+-------+------+
| 3 | go |
| 4 | no |
| 1 | to |
+-------+------+
3 rows in set
===
require 'hirb'
require 'pp'
data = {
'first' => 'second',
'third' => 'fourth',
'fifth' => 'sixth'
}
col_names = %i[field1 field2]
new_data = data.map do |key, val|
{col_names[0] => key, col_names[1] => val}
end
pp new_data
puts Hirb::Helpers::AutoTable.render(new_data)
--output:--
[{:field1=>"first", :field2=>"second"},
{:field1=>"third", :field2=>"fourth"},
{:field1=>"fifth", :field2=>"sixth"}]
+--------+--------+
| field1 | field2 |
+--------+--------+
| first | second |
| third | fourth |
| fifth | sixth |
+--------+--------+
3 rows in set
=====
require 'hirb'
data = [
{field1: 'first', field2: 'second'},
{field1: 'third', field2: 'fourth'},
{field1: 'fifth', field2: 'sixth'}
]
puts Hirb::Helpers::AutoTable.render(data)
--output:--
+--------+--------+
| field1 | field2 |
+--------+--------+
| first | second |
| third | fourth |
| fifth | sixth |
+--------+--------+
3 rows in set