text_analyzer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/text_analyzer.rb +112 -0
  3. metadata +44 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTMxNzI2NzkxZmI1ZjhlZWFmNjlhYjQ5ZDgwZjA0YTQyMDdlYTUzZA==
5
+ data.tar.gz: !binary |-
6
+ MjYwODdmODZhYjNhNGJjY2I2YmI4ZDA1NzBhYTA0MGQ5OWY1ZDdiNA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NGQ0OWM0NDM4MWQ3MGM5MjQyNjVhYjU2NmI4NmI1ZjZhNzUwM2MyODFiMzRh
10
+ MWE0NjQ3MjJlMzI0ZTZhZDk0ZDg3YjcwMGRmYmQwM2I0YzQxNmUwNzdiNjA4
11
+ MDYwNzdjMTg1NjA3NGU0YjU2ZTE5NWE3OTFlYTM0NDQ2YmY0MGE=
12
+ data.tar.gz: !binary |-
13
+ MzAxOWEzZDgxZjVhMWJkOTYzM2MyZjE1MTc4ZTFjODhhNTc5ZTM1MjllNGVh
14
+ MmUyODAyODg0NDRlZjQ1ODA3ZDYyNGIyMjQ1NzgzNDJlYTNlMTFjNzk3NDYx
15
+ YTZiYTZmNDM3YTFkMTNkMTAwNWY1NWQ5ZTE2YWRjOTliZTYzYzU=
@@ -0,0 +1,112 @@
1
+ class TextAnalyzer
2
+ attr_accessor :text
3
+ attr_reader :arr, :unique,:textaz, :zipped, :count
4
+ def initialize text
5
+ @text=text
6
+ @textaz
7
+ @arr
8
+ @unique
9
+ @zipped
10
+ @count
11
+ @hash
12
+
13
+ end
14
+ def init
15
+ get_words
16
+ slice_to_symbols
17
+ count_occurrences
18
+ sort_words
19
+ to_h
20
+ end
21
+ def get_words
22
+ @textaz=@text.downcase.gsub /[^a-z]/, ' ' #/[\.,\"]/, ' '
23
+ end
24
+
25
+ def slice_to_symbols
26
+ @textaz||=get_words
27
+ @arr=@textaz.split(' ').map{|x| x.to_sym}
28
+ return true
29
+ end
30
+
31
+ def count_occurrences
32
+ @unique = @arr.uniq
33
+ @count=@unique.map {|x| arr.count x}
34
+ # return @unique, @count
35
+ return true
36
+ end
37
+ def sort_words
38
+ @zipped = (@unique.zip @count).sort {|x, y| y[1]<=>x[1]}
39
+ @count = []
40
+ @unique = []
41
+ @zipped.each{|x| @unique << x[0] }
42
+ @zipped.each{|x| @count << x[1] }
43
+ #return @unique, @count, @zipped
44
+ return true
45
+ end
46
+ def to_h
47
+ @hash ||= Hash[@zipped]
48
+ end
49
+ def to_s
50
+ 'done'
51
+ end
52
+ def == ob
53
+ @text==ob.text
54
+ end
55
+
56
+ def values_from_list quantity
57
+ @hash.values_at *@unique[0...quantity]
58
+ end
59
+
60
+ def quantity
61
+ @unique.length
62
+ end
63
+
64
+ def length
65
+ @arr.length
66
+ end
67
+
68
+ def percentage symbol
69
+ c = length
70
+ w = @hash[symbol]
71
+ return w.to_f/c
72
+ end
73
+
74
+ def percentages
75
+ per = []
76
+ @unique.each{|x| per<< (percentage x)}
77
+ Hash[@unique.zip per]
78
+ end
79
+
80
+ #adds 2 object and create new object
81
+ def + obj
82
+
83
+ #@text=text
84
+ #@textaz
85
+ #@arr
86
+ #@unique
87
+ #@zipped
88
+ #@count
89
+ #@hash
90
+
91
+ new=TextAnalyzer.new @text+"\n"+obj.text
92
+ new.instance_variable_set(:@textaz, @textaz+"\n"+obj.textaz)
93
+
94
+ arr=@arr+obj.arr
95
+ new.instance_variable_set(:@arr, arr)
96
+
97
+
98
+ u=arr.uniq
99
+ c=u.map {|x| arr.count x}
100
+ zipped = (u.zip c).sort {|x, y| y[1]<=>x[1]}
101
+ u=c=[]
102
+ zipped.each{|x| u << x[0];c << x[1] }
103
+ #zipped.each{|x| c << x[1] }
104
+ new.instance_variable_set(:@unique, u)
105
+ new.instance_variable_set(:@count, c)
106
+ new.instance_variable_set(:@zipped, zipped)
107
+ new.instance_variable_set(:@hash, Hash[zipped])
108
+
109
+
110
+ return new
111
+ end
112
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: text_analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Darek Nędza
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-07-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This simple text analyzer. It aimed at simplicity and not too much dependencies.
14
+ email: nedzadarek@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/text_analyzer.rb
20
+ homepage:
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.0.6
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A simple text analyzer
43
+ test_files: []
44
+ has_rdoc: