textstat 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15a3ff65d3fe588ddc791d9c689ae09d751e0de73d0cf94b83494a48083ac357
4
+ data.tar.gz: 5f66ac2f1d34aafe2f057035f36b7d841fba948c7a77a52746243f4590116f3c
5
+ SHA512:
6
+ metadata.gz: 8dfaf07e741e4ee0acf7e7a682c1c0bff6044326ee35e44a8defa061262e2892f740a43fa4d40d4c96ba7a7013336ca07858f87c29bb9a15c530ee02d371d2fc
7
+ data.tar.gz: ffb268401c6adcf2cba45012d3c5bc5b3d373caa1f15e10e9c2550f70f1883fad41b5d1b2a1665f31d3a34e914004c1e0b536b19ea77ba5b1b85da7fd5b0ba3e
data/lib/counter.rb ADDED
@@ -0,0 +1,37 @@
1
+ class Counter < Hash
2
+ def initialize(other = nil)
3
+ super(0)
4
+ other.each { |e| self[e] += 1 } if other.is_a? Array
5
+ other.each { |k, v| self[k] = v } if other.is_a? Hash
6
+ other.each_char { |e| self[e] += 1 } if other.is_a? String
7
+ end
8
+
9
+ def +(rhs)
10
+ raise TypeError, "cannot add #{rhs.class} to a Counter" unless rhs.is_a? Counter
11
+
12
+ result = Counter.new(self)
13
+ rhs.each { |k, v| result[k] += v }
14
+ result
15
+ end
16
+
17
+ def -(rhs)
18
+ raise TypeError, "cannot subtract #{rhs.class} to a Counter" unless rhs.is_a? Counter
19
+
20
+ result = Counter.new(self)
21
+ rhs.each { |k, v| result[k] -= v }
22
+ result
23
+ end
24
+
25
+ def most_common(n = nil)
26
+ s = sort_by { |_k, v| -v }
27
+ n ? s.take(n) : s
28
+ end
29
+
30
+ def to_s
31
+ "Counter(#{super})"
32
+ end
33
+
34
+ def inspect
35
+ to_s
36
+ end
37
+ end