0

我知道我的问题很简单,但我无法在互联网上找到答案。

我有一个名为 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”的 gem 的帮助下打开一个 docx 文档。之后,它用空格分割文档并创建一个数组。之后,我删除了一些我不想计算的单词(那些包含在禁止列表中的单词)。然后我创建一个哈希,其中键是单词,值是该单词在 docx 中出现的次数。之后,我使用 gem 'hirb. 问题是我只是不知道如何命名创建的表的字段。我希望有一个人可以帮助我。

4

1 回答 1

0

根据hirb docsAutoTable.render()接受一个可以是 array_of_arrays 或 array_of_hashes 的参数。但是因为您的 hash 参数有效,所以我查看了您的表输出,并决定尝试仅添加一个选项来更改您获得的列名:

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
于 2018-01-28T16:37:22.537 回答