2016-03-28 69 views
-3

我正在嘗試將最高計數存儲到一個變量中。爲什麼我得到分配給變量的錯誤計數?

當我循環訪問數組時,它顯示正確的計數,但對高計數變量的賦值似乎是數組中檢查的最後一項的計數。

def calculate_word_frequency(content, line_number) 
    looper = 0 
    wordCounter = "" 
    #CREATE AN ARRAY FROM EACH LINE 
    myArray = content.split 
    #LOOP THROUGH ARRAY COUNTING INSTANCES OF WORDS 
    while looper < myArray.length 
    p myArray[looper] 
    wordCounter = myArray[looper] 
    puts myArray.count(wordCounter) 
    if highest_wf_count < myArray.count 
     highest_wf_count = myArray.count 
    end 
    looper +=1 
    end 
    puts highest_wf_count 
end 
+1

我真的很難理解這是應該做什麼。你知道你可以使用'array.each'遍歷數組,對吧?使用'while'和一個計數器是非常麻煩的做法。 – tadman

+1

您可以提供樣本輸入和輸出嗎? 你的代碼有點混亂(而不是每個,camelCase和snake_case混合),並且不運行(highest_wf_count是未定義的)。也許你可以把它清理一下? –

+1

什麼是line_number參數? –

回答

1

如何算什麼頻率,並得到最大的價值,是遍佈堆棧溢出。

我會做這樣的:

def word_frequency(content) 
    content 
    .split 
    .each_with_object(
    Hash.new { |h, k| h[k] = 0 } 
) { |w, h| 
    h[w] += 1 
    } 
end 

def max_frequency(content) 
    word_frequency(content) 
    .max_by{ |k, v| v } 
end 

word_frequency('a') # => {"a"=>1} 
word_frequency('a b') # => {"a"=>1, "b"=>1} 
word_frequency('a b a') # => {"a"=>2, "b"=>1} 
word_frequency('a b a c a b') # => {"a"=>3, "b"=>2, "c"=>1} 

max_frequency('a b a c a b') # => ["a", 3] 

我使用的是基本split,只按空白進行分割。

'a b'.split # => ["a", "b"] 
'a. b'.split # => ["a.", "b"] 

這是非常天真的,只會返回在空白,而不是真正的單詞中斷。關於如何改進SO的結果有很多問題。

each_with_objectinject類似,只是更方便。這將是你的朋友。

max_bymax類似,但在處理複雜對象時需要更加方便/更快速,以便獲取要比較的值。

可以做的事:

  • 減少你的代碼成小塊。這對於調試和測試/維護非常重要。
  • 瞭解核心庫,尤其是Enumerable,String,IOFile。如果您進行一般編程,您將比Ruby中的其他類/模塊更多地使用它們。
0

仔細查看這兩條線:

puts myArray.count(wordCounter) 

highest_wf_count = myArray.count 

myArray.count(...)調用一個方法count(something)計數的項目,等於給定的「東西」。
myArray.count是一個屬性,返回myArray中的項目數。

最可能的是,你想叫的第一個,然後檢查它,比較,並從這些數值聚集最多,是這樣的:

countingresult = myArray.count(wordCounter) 
puts countingresult 

if highest_wf_count < countingresult 
    highest_wf_count = countingresult 
end 

當你擁有了它,現在,在比較和gather- max查看數組的恆定長度。

我沒有進一步分析你的算法。請修復,如果您需要更多幫助 - 請努力遵守https://stackoverflow.com/help/mcve - 特別是,描述預期的輸入/輸出

順便說一句。我剛剛注意到wordCounter真的是是。相信我,我花了三次複習才明白。那個變量的名字真的很讓人誤解。當你做一些清理工作,請將其更改爲類似「currentWord」或「nextWordToCheck」等

0

如果你想找到一個字符串出現的字最多,你可以嘗試像

def calculate_word_frequency(content) 
    frequencies = content.split(/\s/).each_with_object(Hash.new(0)) do |word, counts| 
    counts[word] += 1 
    end 
    sorted = frequencies.to_a.sort do |(_, count_a), (_, count_b)| 
    count_b <=> count_a 
    end 
    max_word_and_count = sorted.first 
    max_word_and_count.last 
end 

或(你萬一真的只在最大計數興趣),它不需要排序短路版本:

def calculate_word_frequency(content) 
    max = 0 
    frequencies = content.split(/\s/).each_with_object(Hash.new(0)) do |word, counts| 
    count = counts[word] += 1 
    max = count > max ? count : max 
    end 
    max 
end 
-1

您的問題已回答,因此我想建議一種替代方法,使用Enumerable#group_by,這取決於所需的信息。

str = "Bill thought the other Bill should pay the bill or Sue should pay the bill" 

就在最高頻率

如果你只是想出現的最大次數一個字的頻率,你可以編寫以下。

def calculate_word_frequency(content) 
    content.split. 
      group_by(&:itself). 
      map { |_, arr| arr.size }. 
      max 
end 

calculate_word_frequency str 
    #=> 3 

Object#itself是在Ruby v2.2中引入的。對於早期版本,請將group_by(&:itself)替換爲group_by { |e| e }

請注意,content.split的效果與content.split /\s+/的效果相同。

Word中使用最高頻率,它的頻率

如果,另外,你想知道哪個字有最大的頻率,修改上面如下。

def calculate_word_frequency(content) 
    content.split. 
      group_by(&:itself). 
      map { |word, arr| [word, arr.size] }. 
      max_by(&:last) 
    end 

calculate_word_frequency str 
    # => ["the", 3] 

案例冷漠

如果你想「條例」和「法案」被視爲同一個字,改變content.splitcontent.downcase.split或修改上面如下。

def calculate_word_frequency(content) 
    content.split. 
      group_by { |word| word.downcase }. 
      map { |word, arr| [word, arr.size] }. 
      max_by(&:last) 
    end 

calculate_word_frequency str 
    #=> ["bill", 4] 

否認標點符號

如果你想忽略標點符號,這樣做首先,如下所示。

def calculate_word_frequency(content) 
    content.delete(".,:;'\"?!"). 
      downcase. 
      split. 
      group_by(&:itself). 
      map { |word, arr| [word, arr.size] }. 
      max_by(&:last) 
    end 

str = "Bill said \"Bill, pay the bill!\" Bif said 'Sue' should've payed the bill." 
calculate_word_frequency str 
    #=> ["bill", 4] 
相關問題