argv 的问题:未初始化的常量 ARVG (NameError)

Troubles with argv :uninitialized constant ARVG (NameError)

我正在学习构建文本分析器的教程。此时它已按预期构建和工作,我现在应该包括一个摘要器,它将找到最相关的句子来总结文本。这也按预期构建和工作。按照指定,它位于单独的文件中。我的问题是让 ARVG 读取文件。我也对如何包含 summarize.rb 文件感到困惑。这是第一次这样做所以请纠正我的逻辑 and/or 代码错误的地方。

我有 3 个文件:

1) text.txt -> 保存原文

2) summarize.rb -> 保存程序分析text.txt

中的文本

3) ARGV.rb -> 到运行 正文通过总结??

我应该在命令行中编写说明状态

ruby analyzer.rb text.txt

我收到一个错误:

analyzer.rb:5:in `<main>': uninitialized constant ARVG (NameError)

说明没有说明需要初始化任何与 运行 arvg 文件相关的内容。

这是我的 analyzer.rb 文件中的内容

File.open("text.txt") 

lines = File.readlines(ARVG[0]) 
line_count = lines.size #this counts the lines in the new array
text = lines.join 
total_characters = text.length
total_characters_nonspace = text.gsub(/\s/, '').length
word_count = text.split.length
total_sentences = text.split(/\.|\?|!/).length
paragraphs = text.split(/\n\n/).count 
words_per_sentence = word_count / total_sentences
sentences_per_paragraph = total_sentences / paragraphs

stop_words = %w(the by a on the for of are with just but and to the my I has some in)


#puts text
puts
puts "stats:"
puts "#{line_count} lines"
puts "#{total_characters} total characters"
puts "#{total_characters_nonspace} total characters not including spaces"
puts "#{word_count} is the word count"
puts "#{total_sentences} is the number of sentences"
puts "#{paragraphs} total paragraphs"
puts
puts "Averages:"
puts "#{words_per_sentence} average words per sentence"
puts "#{sentences_per_paragraph} average sentences per paragraph"
puts 

这是我的 summerize.rb 文件中的内容 - 可能值得注意的是,在课程中,要总结的文本已硬编码在文件中并附加到一个变量。 none 也被包装为一种方法。我使它成为一个接受参数(无论文本)的方法,因为让它可与任何文本重用而不是文件中硬编码的内容更有意义。说明从未指定这样做。它只是留下了硬编码文本。这些更改会不会有问题?

def summarizer (str)
  sentences = str.gsub(/\s+/, ' ').strip.split(/\.|\?|!/) 
  sentences_sorted = sentences.sort_by{|sentence| sentence.length} 
  one_third = sentences_sorted.length / 3 
  ideal_sentences = sentences_sorted.slice(one_third, one_third + 1) 
  ideal_sentences = ideal_sentences.select{|sentence| sentence =~ /is|are/} 
  puts ideal_sentences.join('. ')
end

这是 summarize.rb 在我根据课程指定的内容进行更改之前的样子。

text = %q{Ruby is a great programming language. It is object oriented and has many groovey features. Some people don't like it but that's not our problem! It's easy to learn.It's great. To learn more about Ruby visit the official Ruby website today}

  sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|!/) #getting rid of white space and splitting @ end of sentence
  sentences_sorted = sentences.sort_by{|sentence| sentence.length} #sorts sentences by length
  one_third = sentences_sorted.length / 3 #calculates what a third of sentences are since we don't want too many.
  ideal_sentences = sentences_sorted.slice(one_third, one_third + 1) #slices out the first and last third
  ideal_sentences = ideal_sentences.select{|sentence| sentence =~ /is|are/} #<= defines words we are looking for
  puts ideal_sentences.join('. ')

这是我的 ARGV.rb 文件中的内容

puts ARGV.join('-')

按照指示。

我的问题是,为什么这不起作用...text.txt 文件如何从 运行 到 summarize.rb(我认为应该是发生)当 summarize.rb 从未被 ARGV.rb 或 text.txt 调用时?这正是说明指示我设置它的方式(我至少阅读了五遍,以防我错过了什么......)

analyzer.rb:5:in `<main>': uninitialized constant ARVG (NameError)

此消息告诉您程序的第 5 行有问题。

我假设是这一行:

lines = File.readlines(ARVG[0]) 

它告诉你你还没有初始化一个名为 ARVG 的常量......这是正确的 - 你正在使用一个名为 ARVG 的常量......但我假设你意思是使用常量ARGV(注意拼写错误)?