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



我正在学习一个教程,在该教程中我构建了一个文本分析器。在这一点上,它是按预期构建和运行的,我现在应该包括一个总结器,它将找到最相关的句子来总结文本。这也是按预期建造和运行的。按照规定,它在一个单独的文件中。我的问题是让ARVG读取文件。对于summaryze.rb文件是如何包含的,我也有一些困惑。这是第一次这样做,所以请纠正我的逻辑和/或代码错误的地方。

我有3个文件:

1) text.txt->保存原始文本

2) summary.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(/nn/).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文件中的内容-可能值得注意的是,在本课中,要汇总的文本已硬编码在文件中,并附加到一个变量。此外,这些都没有被包装成一种方法。我让它成为一个接受参数(无论文本是什么)的方法,因为它更有意义的是使它与任何文本一起可重用,而不是文件中硬编码的内容。说明书中从未指定要这样做。它只是留下了硬编码的文本。这些更改会有问题吗?

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

以下是summary.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('-')

按照指示。

我的问题是,为什么这不起作用。。。当ARGV.rb和text.txt都没有调用summary.rb时,text.txt文件是如何通过summaryze.rb运行的(我认为这是应该发生的)?这正是我设置的说明(我至少读了五遍,以防我错过了什么…)

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

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

我假设这是一行:

lines = File.readlines(ARVG[0]) 

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

最新更新