Browse Source

CPP: libphonenumber v5.7 changes - small improvement to phone number extraction, updates to metadata after build tool changes.

pull/567/head
Lara Scheidegger 13 years ago
committed by Mihaela Rosca
parent
commit
102e52a58b
12 changed files with 12012 additions and 11416 deletions
  1. +1
    -1
      cpp/CMakeLists.txt
  2. +523
    -506
      cpp/src/phonenumbers/alternate_format.cc
  3. +2
    -0
      cpp/src/phonenumbers/asyoutypeformatter.cc
  4. +10595
    -10167
      cpp/src/phonenumbers/lite_metadata.cc
  5. +37
    -5
      cpp/src/phonenumbers/phonenumbermatcher.cc
  6. +6
    -0
      cpp/src/phonenumbers/phonenumbermatcher.h
  7. +9
    -8
      cpp/src/phonenumbers/phonenumberutil.cc
  8. +6
    -0
      cpp/src/phonenumbers/phonenumberutil.h
  9. +766
    -729
      cpp/src/phonenumbers/test_metadata.cc
  10. +52
    -0
      cpp/test/phonenumbers/phonenumbermatcher_test.cc
  11. +15
    -0
      cpp/test/phonenumbers/phonenumberutil_test.cc
  12. BIN
      tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar

+ 1
- 1
cpp/CMakeLists.txt View File

@ -18,7 +18,7 @@ cmake_minimum_required (VERSION 2.8)
project (libphonenumber) project (libphonenumber)
set (libphonenumber_VERSION_MAJOR 5) set (libphonenumber_VERSION_MAJOR 5)
set (libphonenumber_VERSION_MINOR 6)
set (libphonenumber_VERSION_MINOR 7)
# Helper functions dealing with finding libraries and programs this library # Helper functions dealing with finding libraries and programs this library
# depends on. # depends on.


+ 523
- 506
cpp/src/phonenumbers/alternate_format.cc
File diff suppressed because it is too large
View File


+ 2
- 0
cpp/src/phonenumbers/asyoutypeformatter.cc View File

@ -68,6 +68,8 @@ const char kNationalPrefixSeparatorsPattern[] = "[- ]";
void ReplacePatternDigits(string* pattern) { void ReplacePatternDigits(string* pattern) {
DCHECK(pattern); DCHECK(pattern);
string new_pattern; string new_pattern;
// This is needed since sometimes there is more than one digit in between the
// curly braces.
bool is_in_braces = false; bool is_in_braces = false;
for (string::const_iterator it = pattern->begin(); it != pattern->end(); for (string::const_iterator it = pattern->begin(); it != pattern->end();


+ 10595
- 10167
cpp/src/phonenumbers/lite_metadata.cc
File diff suppressed because it is too large
View File


+ 37
- 5
cpp/src/phonenumbers/phonenumbermatcher.cc View File

@ -26,10 +26,10 @@
#endif // I18N_PHONENUMBERS_USE_ICU_REGEXP #endif // I18N_PHONENUMBERS_USE_ICU_REGEXP
#include <ctype.h> #include <ctype.h>
#include <stddef.h>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
#include <map> #include <map>
#include <stddef.h>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -478,8 +478,8 @@ bool PhoneNumberMatcher::VerifyAccordingToLeniency(
case PhoneNumberMatcher::STRICT_GROUPING: { case PhoneNumberMatcher::STRICT_GROUPING: {
if (!phone_util_.IsValidNumber(number) || if (!phone_util_.IsValidNumber(number) ||
!ContainsOnlyValidXChars(number, candidate, phone_util_) || !ContainsOnlyValidXChars(number, candidate, phone_util_) ||
// Two or more slashes were present.
(FindNth(candidate, '/', 2) != string::npos) ||
ContainsMoreThanOneSlashInNationalNumber(
number, candidate, phone_util_) ||
!IsNationalPrefixPresentIfRequired(number)) { !IsNationalPrefixPresentIfRequired(number)) {
return false; return false;
} }
@ -493,8 +493,8 @@ bool PhoneNumberMatcher::VerifyAccordingToLeniency(
case PhoneNumberMatcher::EXACT_GROUPING: { case PhoneNumberMatcher::EXACT_GROUPING: {
if (!phone_util_.IsValidNumber(number) || if (!phone_util_.IsValidNumber(number) ||
!ContainsOnlyValidXChars(number, candidate, phone_util_) || !ContainsOnlyValidXChars(number, candidate, phone_util_) ||
// Two or more slashes were present.
(FindNth(candidate, '/', 2) != string::npos) ||
ContainsMoreThanOneSlashInNationalNumber(
number, candidate, phone_util_) ||
!IsNationalPrefixPresentIfRequired(number)) { !IsNationalPrefixPresentIfRequired(number)) {
return false; return false;
} }
@ -818,5 +818,37 @@ bool PhoneNumberMatcher::AllNumberGroupsAreExactlyPresent(
formatted_number_groups.at(0))); formatted_number_groups.at(0)));
} }
// static
bool PhoneNumberMatcher::ContainsMoreThanOneSlashInNationalNumber(
const PhoneNumber& number,
const string& candidate,
const PhoneNumberUtil& util) {
size_t first_slash_in_body = candidate.find('/');
if (first_slash_in_body == string::npos) {
// No slashes, this is okay.
return false;
}
// Now look for a second one.
size_t second_slash_in_body = candidate.find('/', first_slash_in_body + 1);
if (second_slash_in_body == string::npos) {
// Only one slash, this is okay.
return false;
}
// If the first slash is after the country calling code, this is permitted.
if (number.country_code_source() == PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN ||
number.country_code_source() ==
PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN) {
string normalized_country_code =
candidate.substr(0, first_slash_in_body);
util.NormalizeDigitsOnly(&normalized_country_code);
if (normalized_country_code == SimpleItoa(number.country_code())) {
// Any more slashes and this is illegal.
return candidate.find('/', second_slash_in_body + 1) != string::npos;
}
}
return true;
}
} // namespace phonenumbers } // namespace phonenumbers
} // namespace i18n } // namespace i18n

+ 6
- 0
cpp/src/phonenumbers/phonenumbermatcher.h View File

@ -149,6 +149,12 @@ class PhoneNumberMatcher {
bool VerifyAccordingToLeniency(Leniency leniency, const PhoneNumber& number, bool VerifyAccordingToLeniency(Leniency leniency, const PhoneNumber& number,
const string& candidate) const; const string& candidate) const;
// In interface for testing purposes.
static bool ContainsMoreThanOneSlashInNationalNumber(
const PhoneNumber& number,
const string& candidate,
const PhoneNumberUtil& util);
// Helper method to determine if a character is a Latin-script letter or not. // Helper method to determine if a character is a Latin-script letter or not.
// For our purposes, combining marks should also return true since we assume // For our purposes, combining marks should also return true since we assume
// they have been added to a preceding Latin character. // they have been added to a preceding Latin character.


+ 9
- 8
cpp/src/phonenumbers/phonenumberutil.cc View File

@ -1093,7 +1093,6 @@ void PhoneNumberUtil::FormatNumberForMobileDialing(
} else { } else {
Format(number_no_extension, NATIONAL, formatted_number); Format(number_no_extension, NATIONAL, formatted_number);
} }
} }
} else if (CanBeInternationallyDialled(number_no_extension)) { } else if (CanBeInternationallyDialled(number_no_extension)) {
with_formatting with_formatting
@ -1102,8 +1101,7 @@ void PhoneNumberUtil::FormatNumberForMobileDialing(
return; return;
} }
if (!with_formatting) { if (!with_formatting) {
NormalizeHelper(reg_exps_->diallable_char_mappings_,
true /* remove non matches */, formatted_number);
NormalizeDiallableCharsOnly(formatted_number);
} }
} }
@ -1285,12 +1283,9 @@ void PhoneNumberUtil::FormatInOriginalFormat(const PhoneNumber& number,
// user entered. // user entered.
if (!formatted_number->empty() && !number.raw_input().empty()) { if (!formatted_number->empty() && !number.raw_input().empty()) {
string normalized_formatted_number(*formatted_number); string normalized_formatted_number(*formatted_number);
NormalizeHelper(reg_exps_->diallable_char_mappings_,
true /* remove non matches */,
&normalized_formatted_number);
NormalizeDiallableCharsOnly(&normalized_formatted_number);
string normalized_raw_input(number.raw_input()); string normalized_raw_input(number.raw_input());
NormalizeHelper(reg_exps_->diallable_char_mappings_,
true /* remove non matches */, &normalized_raw_input);
NormalizeDiallableCharsOnly(&normalized_raw_input);
if (normalized_formatted_number != normalized_raw_input) { if (normalized_formatted_number != normalized_raw_input) {
formatted_number->assign(number.raw_input()); formatted_number->assign(number.raw_input());
} }
@ -2213,6 +2208,12 @@ void PhoneNumberUtil::NormalizeDigitsOnly(string* number) const {
number->assign(NormalizeUTF8::NormalizeDecimalDigits(*number)); number->assign(NormalizeUTF8::NormalizeDecimalDigits(*number));
} }
void PhoneNumberUtil::NormalizeDiallableCharsOnly(string* number) const {
DCHECK(number);
NormalizeHelper(reg_exps_->diallable_char_mappings_,
true /* remove non matches */, number);
}
bool PhoneNumberUtil::IsAlphaNumber(const string& number) const { bool PhoneNumberUtil::IsAlphaNumber(const string& number) const {
if (!IsViablePhoneNumber(number)) { if (!IsViablePhoneNumber(number)) {
// Number is too short, or doesn't match the basic phone number pattern. // Number is too short, or doesn't match the basic phone number pattern.


+ 6
- 0
cpp/src/phonenumbers/phonenumberutil.h View File

@ -188,6 +188,11 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
// strips punctuation and alpha characters. // strips punctuation and alpha characters.
void NormalizeDigitsOnly(string* number) const; void NormalizeDigitsOnly(string* number) const;
// Normalizes a string of characters representing a phone number. This strips
// all characters which are not diallable on a mobile phone keypad (including
// all non-ASCII digits).
void NormalizeDiallableCharsOnly(string* number) const;
// Gets the national significant number of a phone number. Note a national // Gets the national significant number of a phone number. Note a national
// significant number doesn't contain a national prefix or any formatting. // significant number doesn't contain a national prefix or any formatting.
void GetNationalSignificantNumber(const PhoneNumber& number, void GetNationalSignificantNumber(const PhoneNumber& number,
@ -737,6 +742,7 @@ class PhoneNumberUtil : public Singleton<PhoneNumberUtil> {
bool ParsePrefixAsIdd(const RegExp& idd_pattern, string* number) const; bool ParsePrefixAsIdd(const RegExp& idd_pattern, string* number) const;
void Normalize(string* number) const; void Normalize(string* number) const;
PhoneNumber::CountryCodeSource MaybeStripInternationalPrefixAndNormalize( PhoneNumber::CountryCodeSource MaybeStripInternationalPrefixAndNormalize(
const string& possible_idd_prefix, const string& possible_idd_prefix,
string* number) const; string* number) const;


+ 766
- 729
cpp/src/phonenumbers/test_metadata.cc
File diff suppressed because it is too large
View File


+ 52
- 0
cpp/test/phonenumbers/phonenumbermatcher_test.cc View File

@ -85,6 +85,12 @@ class PhoneNumberMatcherTest : public testing::Test {
return PhoneNumberMatcher::IsLatinLetter(letter); return PhoneNumberMatcher::IsLatinLetter(letter);
} }
bool ContainsMoreThanOneSlashInNationalNumber(
const PhoneNumber& phone_number, const string& candidate) {
return PhoneNumberMatcher::ContainsMoreThanOneSlashInNationalNumber(
phone_number, candidate, phone_util_);
}
bool ExtractMatch(const string& text, PhoneNumberMatch* match) { bool ExtractMatch(const string& text, PhoneNumberMatch* match) {
return matcher_.ExtractMatch(text, offset_, match); return matcher_.ExtractMatch(text, offset_, match);
} }
@ -324,6 +330,52 @@ class PhoneNumberMatcherTest : public testing::Test {
int offset_; int offset_;
}; };
TEST_F(PhoneNumberMatcherTest, ContainsMoreThanOneSlashInNationalNumber) {
// A date should return true.
PhoneNumber number;
number.set_country_code(1);
number.set_country_code_source(PhoneNumber::FROM_DEFAULT_COUNTRY);
string candidate = "1/05/2013";
EXPECT_TRUE(ContainsMoreThanOneSlashInNationalNumber(number, candidate));
// Here, the country code source thinks it started with a country calling
// code, but this is not the same as the part before the slash, so it's still
// true.
number.Clear();
number.set_country_code(274);
number.set_country_code_source(PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN);
candidate = "27/4/2013";
EXPECT_TRUE(ContainsMoreThanOneSlashInNationalNumber(number, candidate));
// Now it should be false, because the first slash is after the country
// calling code.
number.Clear();
number.set_country_code(49);
number.set_country_code_source(PhoneNumber::FROM_NUMBER_WITH_PLUS_SIGN);
candidate = "49/69/2013";
EXPECT_FALSE(ContainsMoreThanOneSlashInNationalNumber(number, candidate));
number.Clear();
number.set_country_code(49);
number.set_country_code_source(PhoneNumber::FROM_NUMBER_WITHOUT_PLUS_SIGN);
candidate = "+49/69/2013";
EXPECT_FALSE(ContainsMoreThanOneSlashInNationalNumber(number, candidate));
candidate = "+ 49/69/2013";
EXPECT_FALSE(ContainsMoreThanOneSlashInNationalNumber(number, candidate));
candidate = "+ 49/69/20/13";
EXPECT_TRUE(ContainsMoreThanOneSlashInNationalNumber(number, candidate));
// Here, the first group is not assumed to be the country calling code, even
// though it is the same as it, so this should return true.
number.Clear();
number.set_country_code(49);
number.set_country_code_source(PhoneNumber::FROM_DEFAULT_COUNTRY);
candidate = "49/69/2013";
EXPECT_TRUE(ContainsMoreThanOneSlashInNationalNumber(number, candidate));
}
// See PhoneNumberUtilTest::ParseNationalNumber. // See PhoneNumberUtilTest::ParseNationalNumber.
TEST_F(PhoneNumberMatcherTest, FindNationalNumber) { TEST_F(PhoneNumberMatcherTest, FindNationalNumber) {
// Same cases as in ParseNationalNumber. // Same cases as in ParseNationalNumber.


+ 15
- 0
cpp/test/phonenumbers/phonenumberutil_test.cc View File

@ -92,6 +92,10 @@ class PhoneNumberUtilTest : public testing::Test {
phone_util_.Normalize(number); phone_util_.Normalize(number);
} }
void NormalizeDiallableCharsOnly(string* number) const {
phone_util_.NormalizeDiallableCharsOnly(number);
}
bool IsNumberGeographical(const PhoneNumber& phone_number) const { bool IsNumberGeographical(const PhoneNumber& phone_number) const {
return phone_util_.IsNumberGeographical(phone_number); return phone_util_.IsNumberGeographical(phone_number);
} }
@ -148,6 +152,9 @@ class PhoneNumberUtilTest : public testing::Test {
} }
const PhoneNumberUtil& phone_util_; const PhoneNumberUtil& phone_util_;
private:
DISALLOW_COPY_AND_ASSIGN(PhoneNumberUtilTest);
}; };
TEST_F(PhoneNumberUtilTest, ContainsOnlyValidDigits) { TEST_F(PhoneNumberUtilTest, ContainsOnlyValidDigits) {
@ -2321,6 +2328,14 @@ TEST_F(PhoneNumberUtilTest, NormaliseStripAlphaCharacters) {
<< "Conversion did not correctly remove alpha characters"; << "Conversion did not correctly remove alpha characters";
} }
TEST_F(PhoneNumberUtilTest, NormaliseStripNonDiallableCharacters) {
string input_number("03*4-56&+a#234");
NormalizeDiallableCharsOnly(&input_number);
static const string kExpectedOutput("03*456+234");
EXPECT_EQ(kExpectedOutput, input_number)
<< "Conversion did not correctly remove non-diallable characters";
}
TEST_F(PhoneNumberUtilTest, MaybeStripInternationalPrefix) { TEST_F(PhoneNumberUtilTest, MaybeStripInternationalPrefix) {
string international_prefix("00[39]"); string international_prefix("00[39]");
string number_to_strip("0034567700-3898003"); string number_to_strip("0034567700-3898003");


BIN
tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar View File


Loading…
Cancel
Save