diff options
author | dbeaumont@google.com <dbeaumont@google.com@ee073f10-1060-11df-b6a4-87a95322a99c> | 2012-07-12 13:14:13 +0000 |
---|---|---|
committer | dbeaumont@google.com <dbeaumont@google.com@ee073f10-1060-11df-b6a4-87a95322a99c> | 2012-07-12 13:14:13 +0000 |
commit | 126c688c23f92d93e5e9e49110ed32112b935be0 (patch) | |
tree | 363d36d2fd8b14c3d46e3a828c1544afdda50c46 /java/libphonenumber | |
parent | a72cc05053361f363f4d7478defc997f7b2053ca (diff) | |
download | android_external_libphonenumbergoogle-126c688c23f92d93e5e9e49110ed32112b935be0.tar.gz android_external_libphonenumbergoogle-126c688c23f92d93e5e9e49110ed32112b935be0.tar.bz2 android_external_libphonenumbergoogle-126c688c23f92d93e5e9e49110ed32112b935be0.zip |
JAVA: Metadata changes and bug fixes; libphonenumber v5.0
git-svn-id: http://libphonenumber.googlecode.com/svn/trunk@491 ee073f10-1060-11df-b6a4-87a95322a99c
Diffstat (limited to 'java/libphonenumber')
6 files changed, 83 insertions, 7 deletions
diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java index da300ca..a7a5e18 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberMatcher.java @@ -433,7 +433,8 @@ final class PhoneNumberMatcher implements Iterator<PhoneNumberMatch> { /** * Small helper interface such that the number groups can be checked according to different - * criteria. + * criteria, both for our default way of performing formatting and for any alternate formats we + * may want to check. */ interface NumberGroupingChecker { /** @@ -553,6 +554,17 @@ final class PhoneNumberMatcher implements Iterator<PhoneNumberMatch> { if (checker.checkGroups(util, number, normalizedCandidate, formattedNumberGroups)) { return true; } + // If this didn't pass, see if there are any alternate formats, and try them instead. + PhoneMetadata alternateFormats = + MetadataManager.getAlternateFormatsForCountry(number.getCountryCode()); + if (alternateFormats != null) { + for (NumberFormat alternateFormat : alternateFormats.numberFormats()) { + formattedNumberGroups = getNationalNumberGroups(util, number, alternateFormat); + if (checker.checkGroups(util, number, normalizedCandidate, formattedNumberGroups)) { + return true; + } + } + } return false; } diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index d984bbe..8bcbc6e 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -265,9 +265,17 @@ public class PhoneNumberUtil { // carrier codes, for example in Brazilian phone numbers. We also allow multiple "+" characters at // the start. // Corresponds to the following: + // [digits]{minLengthNsn}| // plus_sign*(([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])* + // + // The first reg-ex is to allow short numbers (two digits long) to be parsed if they are entered + // as "15" etc, but only if there is no punctuation in them. The second expression restricts the + // number of digits to three or more, but then allows them to be in international form, and to + // have alpha-characters and punctuation. + // // Note VALID_PUNCTUATION starts with a -, so must be the first in the range. private static final String VALID_PHONE_NUMBER = + DIGITS + "{" + MIN_LENGTH_FOR_NSN + "}" + "|" + "[" + PLUS_CHARS + "]*+(?:[" + VALID_PUNCTUATION + STAR_SIGN + "]*" + DIGITS + "){3,}[" + VALID_PUNCTUATION + STAR_SIGN + VALID_ALPHA + DIGITS + "]*"; diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA Binary files differindex fb204d0..1b49771 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GA diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IL b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IL Binary files differindex 9419291..b01451a 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IL +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IL diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java index 1309038..f596989 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberMatcherTest.java @@ -400,7 +400,12 @@ public class PhoneNumberMatcherTest extends TestMetadataTestCase { new NumberTest("\uFF14\uFF11\uFF15\uFF16\uFF16\uFF16\uFF16-\uFF17\uFF17\uFF17", RegionCode.US), new NumberTest("2012-0102 08", RegionCode.US), // Very strange formatting. new NumberTest("2012-01-02 08", RegionCode.US), - new NumberTest("1800-10-10 22", RegionCode.AU), // Breakdown assistance number. + // Breakdown assistance number with unexpected formatting. + new NumberTest("1800-1-0-10 22", RegionCode.AU), + new NumberTest("030-3-2 23 12 34", RegionCode.DE), + new NumberTest("03 0 -3 2 23 12 34", RegionCode.DE), + new NumberTest("(0)3 0 -3 2 23 12 34", RegionCode.DE), + new NumberTest("0 3 0 -3 2 23 12 34", RegionCode.DE), }; /** @@ -413,6 +418,11 @@ public class PhoneNumberMatcherTest extends TestMetadataTestCase { // Should be found by strict grouping but not exact grouping, as the last two groups are // formatted together as a block. new NumberTest("0800-2491234", RegionCode.DE), + // Doesn't match any formatting in the test file, but almost matches an alternate format (the + // last two groups have been squashed together here). + new NumberTest("0900-1 123123", RegionCode.DE), + new NumberTest("(0)900-1 123123", RegionCode.DE), + new NumberTest("0 900-1 123123", RegionCode.DE), }; /** @@ -439,6 +449,11 @@ public class PhoneNumberMatcherTest extends TestMetadataTestCase { new NumberTest("0494949 ext. 49", RegionCode.DE), new NumberTest("01 (33) 3461 2234", RegionCode.MX), // Optional NP present new NumberTest("(33) 3461 2234", RegionCode.MX), // Optional NP omitted + new NumberTest("1800-10-10 22", RegionCode.AU), // Breakdown assistance number. + // Doesn't match any formatting in the test file, but matches an alternate format exactly. + new NumberTest("0900-1 123 123", RegionCode.DE), + new NumberTest("(0)900-1 123 123", RegionCode.DE), + new NumberTest("0 900-1 123 123", RegionCode.DE), }; public void testMatchesWithPossibleLeniency() throws Exception { @@ -863,7 +878,7 @@ public class PhoneNumberMatcherTest extends TestMetadataTestCase { contextPairs.add(new NumberContext("It's cheap! Call ", " before 6:30")); // With a second number later. contextPairs.add(new NumberContext("Call ", " or +1800-123-4567!")); - contextPairs.add(new NumberContext("Call me on June 21 at", "")); // with a Month-Day date + contextPairs.add(new NumberContext("Call me on June 2 at", "")); // with a Month-Day date // With publication pages. contextPairs.add(new NumberContext( "As quoted by Alfonso 12-15 (2009), you may call me at ", "")); diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 2b7f0a3..59f3f70 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -1256,16 +1256,20 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { } public void testIsViablePhoneNumber() { + assertFalse(PhoneNumberUtil.isViablePhoneNumber("1")); // Only one or two digits before strange non-possible punctuation. - assertFalse(PhoneNumberUtil.isViablePhoneNumber("12. March")); assertFalse(PhoneNumberUtil.isViablePhoneNumber("1+1+1")); assertFalse(PhoneNumberUtil.isViablePhoneNumber("80+0")); - assertFalse(PhoneNumberUtil.isViablePhoneNumber("00")); - // Three digits is viable. + // Two digits is viable. + assertTrue(PhoneNumberUtil.isViablePhoneNumber("00")); assertTrue(PhoneNumberUtil.isViablePhoneNumber("111")); // Alpha numbers. assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-pizza")); assertTrue(PhoneNumberUtil.isViablePhoneNumber("0800-4-PIZZA")); + // We need at least three digits before any alpha characters. + assertFalse(PhoneNumberUtil.isViablePhoneNumber("08-PIZZA")); + assertFalse(PhoneNumberUtil.isViablePhoneNumber("8-PIZZA")); + assertFalse(PhoneNumberUtil.isViablePhoneNumber("12. March")); } public void testIsViablePhoneNumberNonAscii() { @@ -1600,6 +1604,10 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { // Test star numbers. Although this is not strictly valid, we would like to make sure we can // parse the output we produce when formatting the number. assertEquals(JP_STAR_NUMBER, phoneUtil.parse("+81 *2345", RegionCode.JP)); + + PhoneNumber shortNumber = new PhoneNumber(); + shortNumber.setCountryCode(64).setNationalNumber(12L); + assertEquals(shortNumber, phoneUtil.parse("12", RegionCode.NZ)); } public void testParseNumberWithAlphaCharacters() throws Exception { @@ -1766,6 +1774,36 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { e.getErrorType()); } try { + String sentencePhoneNumber = "1 Still not a number"; + phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); + fail("This should not parse without throwing an exception " + sentencePhoneNumber); + } catch (NumberParseException e) { + // Expected this exception. + assertEquals("Wrong error type stored in exception.", + NumberParseException.ErrorType.NOT_A_NUMBER, + e.getErrorType()); + } + try { + String sentencePhoneNumber = "1 MICROSOFT"; + phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); + fail("This should not parse without throwing an exception " + sentencePhoneNumber); + } catch (NumberParseException e) { + // Expected this exception. + assertEquals("Wrong error type stored in exception.", + NumberParseException.ErrorType.NOT_A_NUMBER, + e.getErrorType()); + } + try { + String sentencePhoneNumber = "12 MICROSOFT"; + phoneUtil.parse(sentencePhoneNumber, RegionCode.NZ); + fail("This should not parse without throwing an exception " + sentencePhoneNumber); + } catch (NumberParseException e) { + // Expected this exception. + assertEquals("Wrong error type stored in exception.", + NumberParseException.ErrorType.NOT_A_NUMBER, + e.getErrorType()); + } + try { String tooLongPhoneNumber = "01495 72553301873 810104"; phoneUtil.parse(tooLongPhoneNumber, RegionCode.GB); fail("This should not parse without throwing an exception " + tooLongPhoneNumber); @@ -2209,7 +2247,7 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { // Invalid numbers that can't be parsed. assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, - phoneUtil.isNumberMatch("43", "3 331 6043")); + phoneUtil.isNumberMatch("4", "3 331 6043")); assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, phoneUtil.isNumberMatch("+43", "+64 3 331 6005")); assertEquals(PhoneNumberUtil.MatchType.NOT_A_NUMBER, @@ -2321,7 +2359,10 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertTrue(phoneUtil.isAlphaNumber("1800 six-flags")); assertTrue(phoneUtil.isAlphaNumber("1800 six-flags ext. 1234")); assertTrue(phoneUtil.isAlphaNumber("+800 six-flags")); + assertTrue(phoneUtil.isAlphaNumber("180 six-flags")); assertFalse(phoneUtil.isAlphaNumber("1800 123-1234")); + assertFalse(phoneUtil.isAlphaNumber("1 six-flags")); + assertFalse(phoneUtil.isAlphaNumber("18 six-flags")); assertFalse(phoneUtil.isAlphaNumber("1800 123-1234 extension: 1234")); assertFalse(phoneUtil.isAlphaNumber("+800 1234-1234")); } |