number_normalizer 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6d41f5736c2b795bf67f8accbb6ea4f7a8e0548
4
+ data.tar.gz: ffad646d8150cbb7a33a23286d90f2e3b7e6dfd9
5
+ SHA512:
6
+ metadata.gz: 20b75ba6963bd2392bcffb625c4519d3f10612d800490d766fa69b9ad5747c562f2a1701821fd82f51416eda1e8d6092a4592fc1ac3414123ef329b17ef84a98
7
+ data.tar.gz: 813b80035cbb5670c9be080a531c644556e8ed37e90b1c16603fd68c446811b554705668b76559c1aee44e9a2146e82a1ec198aebb2d47d1079f4aca7ae76983
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .project
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at TODO: Write your email address. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in number_normalizer.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,63 @@
1
+ # NumberNormalizer
2
+
3
+ This gem can help you to extract and normalize numbers from a piece of free text.
4
+
5
+ __Extraction__
6
+
7
+ - Numbers expressed as digits in forms of eg.
8
+ * 1000000
9
+ * 1 000 000
10
+ * 1 120 123.12
11
+ - Numbers expressed as plain words, eg.
12
+ * two hundred and four million one hundred ninety-five thousand --> 204195000
13
+ * __Note__: currently it supports English only.
14
+
15
+ __Normalization__
16
+
17
+ - Currently it will normalize all numbers to digits (Integer or Float).
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'number_normalizer'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install number_normalizer
34
+
35
+ ## Usage
36
+
37
+ ```ruby
38
+ t = "The area is 10 123 456 square meters. The population is 1 300 000 000."
39
+ n = NumberNormalizer.new t
40
+ puts n.digit_numbers # [10123456, 1300000000]
41
+ ```
42
+
43
+ ```ruby
44
+ t = "There are two hundred million five thousand people."
45
+ n = NumberNormalizer.new t
46
+ puts n.digit_numbers # [200005000]
47
+ ```
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dandanwei/number_normalizer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
63
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "number_normalizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,226 @@
1
+ require "number_normalizer/version"
2
+ require "set"
3
+
4
+ class NumberNormalizer
5
+
6
+ attr_reader :text
7
+ attr_reader :matches
8
+
9
+ NUM_WORD = {
10
+ 'zero' => 0 ,
11
+ 'one' => 1 ,
12
+ 'two' => 2 ,
13
+ 'three' => 3 ,
14
+ 'four' => 4 ,
15
+ 'five' => 5 ,
16
+ 'six' => 6 ,
17
+ 'seven' => 7 ,
18
+ 'eight' => 8 ,
19
+ 'nine' => 9 ,
20
+ 'ten' => 10,
21
+ 'eleven' => 11,
22
+ 'twelve' => 12,
23
+ 'thirteen' => 13,
24
+ 'fourteen' => 14,
25
+ 'fifteen' => 15,
26
+ 'sixteen' => 16,
27
+ 'seventeen' => 17,
28
+ 'eighteen' => 18,
29
+ 'nineteen' => 19,
30
+ 'twenty' => 20,
31
+ 'thirty' => 30,
32
+ 'forty' => 40,
33
+ 'fifty' => 50,
34
+ 'sixty' => 60,
35
+ 'seventy' => 70,
36
+ 'eighty' => 80,
37
+ 'ninety' => 90,
38
+ 'a' => 1,
39
+ 'an' => 1,
40
+ }
41
+
42
+ MULT_WORD = {
43
+ 'half' => 0.5,
44
+ 'dozen' => 12,
45
+ 'hundred' => 100,
46
+ 'thousand' => 1000,
47
+ 'million' => 1000000,
48
+ 'billion' => 1000000000,
49
+ 'trillion' => 1000000000000,
50
+ }
51
+
52
+ ADJ_WORD = {
53
+ '-' => 0,
54
+ 'and' => 0,
55
+ }
56
+
57
+ def initialize text
58
+ @text = text
59
+ @token = ' '
60
+ @text_array = []
61
+ # parse all numbers and store it in an array
62
+ # [ {original_format => "1 thousand",
63
+ # digit_format => "1000",
64
+ # word_format => "one thousand",
65
+ # occurance => 1}, ... ]
66
+ @p_digits_only = '\d+'
67
+ @p_floating_digits = '\d+\.\d+'
68
+ @p_floating_digits_2 = '\.\d+'
69
+ @p_space_sperated_digits = '\d{,3}(\s\d{3}){1,}(\.\d{1,3}){,1}(\s\d{3})*(\s\d{,3})?'
70
+ @p_comma_sperated_digits = '\d{,3}(,\d{3}){1,}(\.\d{1,3}){,1}(,\d{3})*(,\d{,3})?'
71
+ @p_begin = ''
72
+ @p_end = '\W?(\s|$)'
73
+ @matches = {}
74
+
75
+ find_all_digits
76
+
77
+ find_all_words
78
+ end
79
+
80
+ def digit_numbers
81
+
82
+ convert_digit_string_to_numbers
83
+ digits = Set.new
84
+
85
+ #puts @matches
86
+
87
+ @matches.each_value do |v|
88
+ if v.has_key?:digit_form
89
+ digits.add(v[:digit_form])
90
+ end
91
+ end
92
+
93
+ return digits.to_a
94
+ end
95
+
96
+ protected
97
+
98
+ def tokenlize
99
+ @text_array = @text.split(@token)
100
+ end
101
+
102
+ def find_all_digits
103
+ digits_pattern_str = '(' + @p_space_sperated_digits + '|' \
104
+ + @p_comma_sperated_digits + '|' \
105
+ + @p_floating_digits + '|' \
106
+ + @p_floating_digits_2 + '|' \
107
+ + @p_digits_only + ')' \
108
+ + @p_end
109
+
110
+ digits_pattern = Regexp.new(digits_pattern_str)
111
+
112
+ @text.enum_for(:scan, digits_pattern).each do |m|
113
+ mstr = m[0]
114
+ position = Regexp.last_match.begin(0)
115
+ if @matches.has_key?mstr
116
+ @matches[mstr][:pos].push(position)
117
+ else
118
+ @matches[mstr] = {:pos => [position],
119
+ :type => :digits}
120
+ end
121
+ end
122
+ end
123
+
124
+ def find_all_words
125
+ text_arr = preprocess(@text)
126
+
127
+ num_words = ''
128
+ temp_words = ''
129
+ temp_num = 0
130
+ temp_mode = 0
131
+ hundred_num = 0
132
+ final_num = 0
133
+ flag = false
134
+ is_prev_mult = false
135
+ is_prev_num = false
136
+
137
+ text_arr.each do |str|
138
+ num = NUM_WORD[str]
139
+ mult = MULT_WORD[str]
140
+ adj = ADJ_WORD[str]
141
+ #puts "#{str}==#{num}==#{mult}==#{adj}=="
142
+
143
+ if num != nil
144
+ if temp_mode > 100
145
+ temp_num += num
146
+ temp_words = temp_words + ' ' + str
147
+ else
148
+ hundred_num += num
149
+ num_words = num_words + ' ' + str
150
+ temp_mode = 0
151
+ end
152
+ flag = true
153
+ is_prev_mult = false
154
+ is_prev_num = true
155
+ elsif flag && adj != nil && ((str == '-') || (is_prev_mult && (str == 'and')))
156
+ if temp_mode >= 100 && temp_mode % 10 == 0 && str == 'and'
157
+ temp_mode += 1
158
+ end
159
+ num_words = num_words + ' ' + str if str == 'and'
160
+ num_words += str if str == '-'
161
+ is_prev_mult = false
162
+ is_prev_num = false
163
+ elsif flag && mult != nil
164
+ if (temp_mode > 1000 && temp_mode % 10 == 1) || (temp_mode > 100 && temp_mode < 1000 && mult < 1000)
165
+ # 2 numbers
166
+ # -- save previous number first
167
+ final_num += hundred_num
168
+ save_num_words(num_words.strip, final_num)
169
+ # -- start counting current number
170
+ num_words = temp_words
171
+ hundred_num = temp_num
172
+ temp_words = ''
173
+ temp_num = 0
174
+ final_num = 0
175
+ else
176
+ # 1 number
177
+ hundred_num += temp_num
178
+ temp_num = 0
179
+ end
180
+ hundred_num *= mult
181
+ if hundred_num >= 1000
182
+ final_num += hundred_num
183
+ hundred_num = 0
184
+ end
185
+ num_words = num_words + ' ' + str
186
+ is_prev_mult = true
187
+ is_prev_num = false
188
+ temp_mode = mult
189
+ elsif flag
190
+ final_num += hundred_num + temp_num
191
+ save_num_words(num_words.strip, final_num)
192
+ num_words = ''
193
+ final_num = 0
194
+ hundred_num = 0
195
+ temp_num = 0
196
+ temp_words = ''
197
+ temp_mode = 0
198
+ flag = false
199
+ is_prev_mult = false
200
+ is_prev_num = false
201
+ end
202
+ end
203
+
204
+ save_num_words(num_words.strip, final_num + hundred_num) if flag
205
+ end
206
+
207
+ def save_num_words(num_words, number)
208
+ @matches[num_words] = {:pos => [1],
209
+ :type => :words,
210
+ :digit_form => number}
211
+ end
212
+
213
+ def convert_digit_string_to_numbers
214
+ @matches.each_pair do |key, value|
215
+ if value[:type] == :digits
216
+ s = key.gsub(/[\s,]/, '')
217
+ value[:digit_form] = if s =~ /\./ then s.to_f else s.to_i end
218
+ end
219
+ end
220
+ end
221
+
222
+ def preprocess(str)
223
+ str.downcase.gsub(/([^a-zA-Z0-9\s])/, ' \1 ').split()
224
+ end
225
+
226
+ end
@@ -0,0 +1,3 @@
1
+ class NumberNormalizer
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'number_normalizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "number_normalizer"
8
+ spec.version = NumberNormalizer::VERSION
9
+ spec.authors = ["Dandan Wei"]
10
+ spec.email = ["dandan.wei@ymail.com"]
11
+
12
+ spec.summary = %q{Normalize and extract numbers writen as words or digits or mixed in a text}
13
+ spec.description = "In free texts, numbers maybe writen as digits (eg. 123.4) or words (eg. one hundred fifty) " \
14
+ "or a mixed way (eg. 1 million). This gem will help you to normalize or extract the numbers " \
15
+ "presented in the free text."
16
+ spec.homepage = "https://github.com/dandanwei/number_normalizer"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: number_normalizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dandan Wei
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: In free texts, numbers maybe writen as digits (eg. 123.4) or words (eg.
56
+ one hundred fifty) or a mixed way (eg. 1 million). This gem will help you to normalize
57
+ or extract the numbers presented in the free text.
58
+ email:
59
+ - dandan.wei@ymail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/number_normalizer.rb
75
+ - lib/number_normalizer/version.rb
76
+ - number_normalizer.gemspec
77
+ homepage: https://github.com/dandanwei/number_normalizer
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Normalize and extract numbers writen as words or digits or mixed in a text
101
+ test_files: []