统计单词出现的次数

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#Counts the number of times a 'word' appears in a 'phrase'
def CountWordOccurrence(word, phrase, matchCase)
  # Gets the length of the phrase
  index = phrase.length
  # Splits the string into an array getting all of the words from the phrase
  array = phrase.split(' ')
  # Placeholder for how many words that we find a match to
  count = 0
  # Loop through the array
  array.each do |item|
    # If we care about the word CAsINg
    if matchCase == true
      # If the current item is the word we are trying to find and has same CAsINg
      if item == word
        # Add 1 to the count
        count = count + 1
        # Exit the if statement
      end
      # If we do not care about the word CAsINg
    else
      # If the current item is the word we are trying to find CAsINg doesn't matter
      if item.downcase == word.downcase
        # Add 1 to the count
        count = count + 1
        # Exit the if statement
      end
      # Exit the if statement
    end
      # Exit the loop
    end
  # Return the number of words matched
  return count
  # Exit the method
end