Created
September 5, 2012 16:51
-
-
Save jpignata/3639800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "java" | |
require "lib/vendor/libphonenumber-4.5.jar" | |
require "lib/vendor/offline-geocoder-1.9.jar" | |
class PhoneNumber | |
US_DIALING_PREFIX = "011" | |
US_COUNTRY_NAME = "US" | |
UNKNOWN = "Unknown" | |
import import java.util.Locale | |
import com.google.i18n.phonenumbers.PhoneNumberUtil | |
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder | |
attr_reader :number | |
def initialize(number) | |
@number = number | |
end | |
def locale | |
locale = geocoder.get_description_for_number(parsed, Locale::ENGLISH) | |
locale.size > 0 ? locale : UNKNOWN | |
rescue com.google.i18n.phonenumbers.NumberParseException | |
UNKNOWN | |
end | |
private | |
def parsed | |
@parsed ||= phone_number_util.parse(US_DIALING_PREFIX + number, US_COUNTRY_NAME) | |
end | |
def phone_number_util | |
PhoneNumberUtil.get_instance | |
end | |
def geocoder | |
PhoneNumberOfflineGeocoder.get_instance | |
end | |
end | |
class PhoneNumberLocaleCounter | |
attr_reader :counts | |
def initialize(file_name) | |
@file_name = file_name | |
@numbers = [] | |
@counts = {} | |
end | |
def run | |
read_file | |
count | |
counts.sort_by | |
end | |
private | |
attr_reader :file_name, :numbers | |
def read_file | |
File.open(file_name) do |file| | |
file.each_line do |line| | |
add_number(line.strip[/[0-9]{7,}/]) | |
end | |
end | |
end | |
def add_number(number) | |
numbers << PhoneNumber.new(number) | |
end | |
def count | |
numbers.each do |number| | |
counts[number.locale] = counts[number.locale].to_i + 1 | |
end | |
end | |
end | |
counter = PhoneNumberLocaleCounter.new("numbers.txt") | |
counter.run | |
counter.counts.sort_by { |count| -count.last }.each do |locale, count| | |
puts "#{locale},#{count}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment