我正在编写一个教程,我已经构建了一个文本分析器。在这一点上,它是按照预期构建和工作的,现在我应该包括一个摘要器,它将找到最相关的句子来总结文本。这也是按照预期建立和运作的。它位于单独的文件中,如指定的那样。我的问题是让ARVG读取文件。关于如何包含summarize.rb文件,我也有些困惑。这是第一次这样做,请纠正我的逻辑和/或代码错误。
我有3个文件:
1)text.txt - >其中包含原始文本
2)总结.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文件中的内容 - 值得注意的是,在课程中,要汇总的文本已在文件中硬编码并附加到变量。也没有一个被包裹作为一种方法。我使它成为一个接受参数(无论文本)的方法,因为它使任何文本可以重复使用更有意义,而不是文件中的硬编码。指令从未指定过这样做。它只是留下了硬编码的文本。这些更改是否有问题?
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
以下是在我从课程指定的内容改变它之前总结的.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('-')
按照指示。
我的问题是,为什么这不起作用...当从未调用summarize.rb时,text.txt文件是如何通过summarize.rb(我认为应该发生的那样)运行的。 ARGV.rb还是text.txt?而这正是说明书中我指出的原因(我至少读了五次,以防错过了什么......)
答案 0 :(得分:3)
analyzer.rb:5:in `<main>': uninitialized constant ARVG (NameError)
此消息告诉您程序的第5行存在问题。
我假设的是这一行:
lines = File.readlines(ARVG[0])
它告诉你,你还没有初始化一个名为ARVG
的常量......这是正确的 - 你正在使用一个名为ARVG
的常量......但我&# 39; m假设你意味着使用的是常数ARGV
(注意拼写错误)?