Guest User

Untitled

a guest
Dec 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1.  
  2. #!/usr/bin/ruby
  3. #CS132A - Ruby - Lab 3 - Text Analyzer
  4. #Author: Zach
  5. #Purpose: Utilize Ruby File.class to analyze Text files
  6. # and generate statitical information therein.
  7.  
  8.  
  9.  
  10. STOPWORDS = IO.readlines('stop_words.txt').map{|i| i.chomp}
  11.  
  12. Dir.glob("*.txt").each do |text_file|
  13.  
  14. # Local Variables / Text Files
  15. line_count = text_file.size
  16. text = File.read(text_file)
  17.  
  18. # Count the characters
  19. char_stats = count_chars(text)
  20.  
  21. # Count the words, sentences and paragraphs
  22. chunk_stats = count_chunks(text)
  23.  
  24. # Stop Words Processing
  25. puts "Meaningful Words"
  26. puts
  27. keywords = text.split(/\s+/).select {|word| !STOPWORDS.include?(word)}
  28. puts keywords.join(' ')
  29. good_percent = ((keywords.length.to_f / word_count.to_f) * 100).to_i
  30.  
  31. # Ideal Sentences
  32. puts "Ideal sentences from text"
  33. sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|!/)
  34. sentences_sorted = sentences.sort_by { |sentence| sentence.length }
  35. foo = sentences_sorted.length / 7
  36. ideal_sentences = sentences_sorted.slice(foo, foo + 1)
  37. ideal_sentences = ideal_sentences.select { |sentence| sentence =~/\sis\W|\sare\W/ }
  38. puts
  39.  
  40. # Common Words
  41. puts "10 Most common words from text"
  42. words_less_stop = (keywords - STOPWORDS).group_by{ |x| x}.sort_by{ |word, hits| -hits.length}[0..9].map(&:first)
  43. puts words_less_stop
  44. puts
  45.  
  46. # Output Statements
  47. puts "Innagural Speech Statistics"
  48. puts "The total number of lines in the inaugural speech is #{line_count}\."
  49. puts "The total number of characters in the first part is #{tot_chars}\."
  50. puts "The total number of characters less whitespace is #{tot_chars_no_space}\."
  51. puts "The total number of words is #{word_count}\."
  52. puts "The total number of sentences is #{sent_count}\."
  53. puts "The total number of paragraphs is #{para_count}\."
  54. puts "The average sentences per paragraph is #{sent_count/para_count}\."
  55. puts "The average words per sentence is #{word_count/ sent_count}\."
  56. puts "#{good_percent}% of all words in the text are non-fluff words."
  57. puts
  58. puts "The ideal sentences are:\n\n" + ideal_sentences.join(". ")
  59. puts
  60.  
  61. end
  62.  
  63. def count_chars(text)
  64. # Count the characters; return a hash with stats
  65. {
  66. :tot_chars => text.length
  67. :tot_chars_no_space => text.gsub(/\s+/, '').length
  68. }
  69. end
  70.  
  71. def count_chunks(text)
  72. # Count the words, sentences and paragraphs; return a hash with stats
  73. {
  74. :word_count => text.split.length
  75. :sent_count => text.split(/\.|\?|!/).length
  76. :para_count => text.split(/\n\n/).length
  77. }
  78. end
Add Comment
Please, Sign In to add comment