0

I keep getting the following error every time I run my code

undefined method '[]' for nilClass: NoMethodError

There are two classes, LineAnalyzer and Solution class and the problem seems to be in the LineAnalyzer calculate_word_frequency method but I couldn't find the error.

The code is as shown below:

class LineAnalyzer
  attr_accessor :highest_wf_count, :highest_wf_words, :linesCount, :content , :line_number

  @highest_wf_count = Hash.new(0)
  @highest_wf_words = Array.new(0)
  @content 
  @line_number

  def initialize(line, line_num)
    @content = line
    @line_number = line_num
    calculate_word_frequency()
  end

  def calculate_word_frequency()
    @content.split.each do |word|
      @highest_wf_count[word.downcase] += 1
    end
    max = 0
    @highest_wf_count.each_pair do |key, value| 
      if value > max 
        max = value
      end
    end
    word_frequency.each_pair do |key, value| 
      if value == max 
        @highest_wf_words << key
      end
    end 
  end
end

class Solution
  attr_accessor :highest_count_across_lines, :highest_count_words_across_lines, :line_analyzers

  @highest_count_across_lines = Hash.new()
  @highest_count_words_across_lines = Hash.new()
  @line_analyzers = Array.new()

  def analyze_file
    line_count = 0
    x = File.foreach('C:\x\test.txt')
    x.each do |line|
      line_count += 1
      @line_analyzers << LineAnalyzer.new(line, line_count)
    end
  end
end

solution = Solution.new
# Expect errors until you implement these methods
solution.analyze_file

Any help would be appreciated.

oxfist
  • 749
  • 6
  • 22
HamsterRoll
  • 107
  • 1
  • 16
  • Where are you declaring `word_frequency` in `word_frequency.each_pair do |key, value|` – patrickh003 Sep 25 '15 at 18:20
  • word_frequency is supposed to be @highest_wf_count ... mistake in the code – HamsterRoll Sep 25 '15 at 18:42
  • 1
    I think you are mixing class instance variables with instance variables, I hope this link can help you http://stackoverflow.com/questions/15773552/ruby-class-instance-variable-vs-class-variable. Basically, you're declaring @highest_wf_words as a class instance variable, and you're trying to use it as an instance variable, that's why you get that error, because in the line @highest_wf_count[word.downcase]+=1, that variable was declared as a class instance variable. – fanta Sep 25 '15 at 18:56
  • try `File.open('C:\x\test.txt')` – patrickh003 Sep 25 '15 at 19:13

3 Answers3

0

Try this:

def calculate_word_frequency()
  @content.split.each do |word|
    next if word.blank?
    @highest_wf_count[word.downcase]+=1
  end
  max =0 
  @highest_wf_count.each_pair do |key, value| 
   if value > max 
     max = value
   end
  end
  word_frequency.each_pair do |key, value| 
   if value == max 
    @highest_wf_words << key
   end
  end 
end
Prashant4224
  • 1,551
  • 1
  • 14
  • 21
0

It looks like your issue is that when you are trying to increment the word_count the first time a word is seen, the hash you are using doesn't have that key, so it can't increment it. The first time the key is seen, you should set it to 1, and all occurrences should increment it:

class LineAnalyzer
    attr_accessor :highest_wf_count, :highest_wf_words, :linesCount, :content , :line_number

    def initialize(line,line_num)
        @highest_wf_count = Hash.new(0)
        @highest_wf_words = Array.new(0)
        @content = line
        @line_number = line_num
        calculate_word_frequency()
    end

   def calculate_word_frequency()
       @content.split.each do |word|
           highest_wf_count.key?(word.downcase) ? @highest_wf_count[word.downcase]+=1 : @highest_wf_count[word.downcase]=1
       end
       max =0 
       @highest_wf_count.each_pair do |key, value| 
           if value > max 
              max = value
           end
        end
       highest_wf_count.each_pair do |key, value| 
          if value == max 
             @highest_wf_words << key
          end
       end 
    end
end

class Solution
    attr_accessor :highest_count_across_lines, :highest_count_words_across_lines, :line_analyzers
    @highest_count_across_lines = Hash.new()
    @highest_count_words_across_lines = Hash.new()

    def analyze_file
      line_count = 0
      x = File.foreach('C:\x\test.txt')
      x.each{ |line|
        line_count +=1
        line_analyzer << LineAnalyzer.new(line,line_count)
      }
      puts "line_analyzer: #{line_analyzer.inspect}"
   end

    def line_analyzer
        @line_analyzers ||= Array.new
    end
end
Hawkeye001
  • 791
  • 2
  • 11
  • 25
  • did not make a difference , the error when i try to add value to lineAnalyzers array => @LineAnalyzers << LineAnalyzer.new(line , line_count) – HamsterRoll Sep 25 '15 at 21:04
  • 1
    @Yolanda, the issue with the @ LineAnalyzers << LineAnalyzer.new(...) line is because of the issue fanta mentioned in a previous comment; mixing instance variables with class instance variables. I have edited the answer to include the Solution class. Until you resolve the usage with the variables, you are still going to have a problem with the highest_count_words_across_lines attribute, depending on how you end up using it. – Hawkeye001 Sep 25 '15 at 22:00
  • @Hawkey001 the code fix you made was a life saver... I now understand what meant now. Thank you all – HamsterRoll Sep 26 '15 at 05:59
0

Yolanda, Hi! I found an error concerning highest_wf_count in LineAnalyzer Class. I mean "highest_wf_count value is 3". See below.

class LineAnalyzer

    attr_accessor :highest_wf_count, :highest_wf_hash, :highest_wf_words, :linesCount, :content , :line_number

    def initialize(line,line_num) #<--- taking a line of text (content) and a line number
        @highest_wf_hash = Hash.new(0) 
        @highest_wf_count = highest_wf_count #<--- a number with maximum number of occurrences for a single word (calculated)
        @highest_wf_words = Array.new(0) #<--- an array of words with the maximum number of occurrences (calculated)
        @content = line #<--- the string analyzed (provided)
        @line_number = line_num #<--- the line number analyzed (provided)
        calculate_word_frequency()
    end

   def calculate_word_frequency()
       @content.split.each do |word|
           highest_wf_hash.key?(word.downcase) ? @highest_wf_hash[word.downcase]+=1 : @highest_wf_hash[word.downcase]=1
       end
       @highest_wf_count = 0
       @highest_wf_hash.each_pair do |key, value| 
           if value > @highest_wf_count 
              @highest_wf_count = value

           end
        end
       highest_wf_hash.each_pair do |key, value| 
          if value == @highest_wf_count 
             @highest_wf_words << key
             @highest_wf_count == value
          end
       end 
    end
end

But still there is an issue with class Solution

  • line_analyzer
  • calculate_line_with_highest_frequency
  • print_highest_word_frequency_across_lines

Do you know how to solve it?%)

  • please refer to this link : someone helped me solve it in few lines and then you can try to figure out how to do it on the class level : http://pastie.org/10445014 – HamsterRoll Oct 16 '15 at 10:11