Class: TextAnalyzer
- Inherits:
-
Object
- Object
- TextAnalyzer
- Defined in:
- lib/text_analyzer.rb
Instance Attribute Summary collapse
-
#arr ⇒ Object
readonly
Returns the value of attribute arr.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#text ⇒ Object
Returns the value of attribute text.
-
#textaz ⇒ Object
readonly
Returns the value of attribute textaz.
-
#unique ⇒ Object
readonly
Returns the value of attribute unique.
-
#zipped ⇒ Object
readonly
Returns the value of attribute zipped.
Instance Method Summary collapse
-
#+(obj) ⇒ Object
adds 2 object and create new object.
- #==(ob) ⇒ Object
- #count_occurrences ⇒ Object
- #get_words ⇒ Object
- #init ⇒ Object
-
#initialize(text) ⇒ TextAnalyzer
constructor
A new instance of TextAnalyzer.
- #length ⇒ Object
- #percentage(symbol) ⇒ Object
- #percentages ⇒ Object
- #quantity ⇒ Object
- #slice_to_symbols ⇒ Object
- #sort_words ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #values_from_list(quantity) ⇒ Object
Constructor Details
#initialize(text) ⇒ TextAnalyzer
Returns a new instance of TextAnalyzer.
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/text_analyzer.rb', line 4 def initialize text @text=text @textaz @arr @unique @zipped @count @hash end |
Instance Attribute Details
#arr ⇒ Object (readonly)
Returns the value of attribute arr.
3 4 5 |
# File 'lib/text_analyzer.rb', line 3 def arr @arr end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
3 4 5 |
# File 'lib/text_analyzer.rb', line 3 def count @count end |
#text ⇒ Object
Returns the value of attribute text.
2 3 4 |
# File 'lib/text_analyzer.rb', line 2 def text @text end |
#textaz ⇒ Object (readonly)
Returns the value of attribute textaz.
3 4 5 |
# File 'lib/text_analyzer.rb', line 3 def textaz @textaz end |
#unique ⇒ Object (readonly)
Returns the value of attribute unique.
3 4 5 |
# File 'lib/text_analyzer.rb', line 3 def unique @unique end |
#zipped ⇒ Object (readonly)
Returns the value of attribute zipped.
3 4 5 |
# File 'lib/text_analyzer.rb', line 3 def zipped @zipped end |
Instance Method Details
#+(obj) ⇒ Object
adds 2 object and create new object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/text_analyzer.rb', line 81 def + obj #@text=text #@textaz #@arr #@unique #@zipped #@count #@hash new=TextAnalyzer.new @text+"\n"+obj.text new.instance_variable_set(:@textaz, @textaz+"\n"+obj.textaz) arr=@arr+obj.arr new.instance_variable_set(:@arr, arr) u=arr.uniq c=u.map {|x| arr.count x} zipped = (u.zip c).sort {|x, y| y[1]<=>x[1]} u=c=[] zipped.each{|x| u << x[0];c << x[1] } #zipped.each{|x| c << x[1] } new.instance_variable_set(:@unique, u) new.instance_variable_set(:@count, c) new.instance_variable_set(:@zipped, zipped) new.instance_variable_set(:@hash, Hash[zipped]) return new end |
#==(ob) ⇒ Object
52 53 54 |
# File 'lib/text_analyzer.rb', line 52 def == ob @text==ob.text end |
#count_occurrences ⇒ Object
31 32 33 34 35 36 |
# File 'lib/text_analyzer.rb', line 31 def count_occurrences @unique = @arr.uniq @count=@unique.map {|x| arr.count x} # return @unique, @count return true end |
#get_words ⇒ Object
21 22 23 |
# File 'lib/text_analyzer.rb', line 21 def get_words @textaz=@text.downcase.gsub /[^a-z]/, ' ' #/[\.,\"]/, ' ' end |
#init ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/text_analyzer.rb', line 14 def init get_words slice_to_symbols count_occurrences sort_words to_h end |
#length ⇒ Object
64 65 66 |
# File 'lib/text_analyzer.rb', line 64 def length @arr.length end |
#percentage(symbol) ⇒ Object
68 69 70 71 72 |
# File 'lib/text_analyzer.rb', line 68 def percentage symbol c = length w = @hash[symbol] return w.to_f/c end |
#percentages ⇒ Object
74 75 76 77 78 |
# File 'lib/text_analyzer.rb', line 74 def percentages per = [] @unique.each{|x| per<< (percentage x)} Hash[@unique.zip per] end |
#quantity ⇒ Object
60 61 62 |
# File 'lib/text_analyzer.rb', line 60 def quantity @unique.length end |
#slice_to_symbols ⇒ Object
25 26 27 28 29 |
# File 'lib/text_analyzer.rb', line 25 def slice_to_symbols @textaz||=get_words @arr=@textaz.split(' ').map{|x| x.to_sym} return true end |
#sort_words ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/text_analyzer.rb', line 37 def sort_words @zipped = (@unique.zip @count).sort {|x, y| y[1]<=>x[1]} @count = [] @unique = [] @zipped.each{|x| @unique << x[0] } @zipped.each{|x| @count << x[1] } #return @unique, @count, @zipped return true end |
#to_h ⇒ Object
46 47 48 |
# File 'lib/text_analyzer.rb', line 46 def to_h @hash ||= Hash[@zipped] end |
#to_s ⇒ Object
49 50 51 |
# File 'lib/text_analyzer.rb', line 49 def to_s 'done' end |
#values_from_list(quantity) ⇒ Object
56 57 58 |
# File 'lib/text_analyzer.rb', line 56 def values_from_list quantity @hash.values_at *@unique[0...quantity] end |