Skip to content

Instantly share code, notes, and snippets.

@jpignata
Created January 21, 2012 18:14
Show Gist options
  • Save jpignata/1653476 to your computer and use it in GitHub Desktop.
Save jpignata/1653476 to your computer and use it in GitHub Desktop.
parses phone numbers to their locale via libphonenumber
// Parses a text file of numbers and outputs each number with its locale and
// counts of numbers found per each locale.
//
// Usage:
// scala -classpath ./lib/offline-geocoder-1.9.jar:./lib/libphonenumber-4.5.jar ./locator.scala <input_file>
import java.util.Locale
import scala.io.Source
import scala.collection.mutable.Map
import com.google.i18n.phonenumbers._
import com.google.i18n.phonenumbers.geocoding._
val phoneNumberUtil = PhoneNumberUtil.getInstance()
val geocoder = PhoneNumberOfflineGeocoder.getInstance()
val numbers = Map[String, Int]()
if (args.length > 0) {
for (number <- Source.fromFile(args(0)).getLines()) {
try {
val numberProto = PhoneNumberUtil.getInstance().parse("011" + number, "US")
val locale = geocoder.getDescriptionForNumber(numberProto, Locale.ENGLISH)
val localeOutput = if(locale.length() > 0) locale else "Unknown"
if (numbers.contains(localeOutput))
numbers(localeOutput) += 1
else
numbers += (localeOutput -> 1)
println(number + ": "+ localeOutput)
} catch {
case e: NumberParseException => println(number + ": Invalid Number")
}
}
println(numbers)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment