2016-09-25 17 views
0

我使用的是Python版本的libphonenumbers,但是当我在不同的文本上试用这个库时,有时会发现python函数不会返回任何内容,那里有一个电话号码,有时他们会返回电话号码。请看下面:Python:libphonenumber - 无法获取明显的电话号码

print(x2) 
for match in pnum.PhoneNumberMatcher(x2, "US"): 
print(match) #for the text above, it did not get the number 

输出:

I just read your profile and thought it was really great. I also thought you were cute and loved the fact that you go hiking with your brothers every summer. If you want to know anything more about me, just ask. My num 555-121-5468. 

通过上面这段文字,它不返回我任何电话号码。 但是像下面等的情况下,该功能给了我正确的输入:

x9 = "hay I hate to cut you short, its been fun chatting, but unfortuantely I gotta run. I am gald we became friends though. my number is (323) 2387890" 

for match in pnum.PhoneNumberMatcher(x9, "US"): 
print(match) 

输出:

PhoneNumberMatch [132,145) (323) 2387890 

我不知道是什么问题导致此问题,我是新来的Python和这个图书馆,并真诚感谢洞察力。

回答

1

555-121-5468看起来像一个有效的美国电话号码,但事实上并非如此。

PhoneNumberMatcherconstructor类接受leniency参数,该参数定义类匹配候选电话号码的严格程度(code)。此参数的可能值默认值leniency1,它只会匹配有效的电话号码。将此更改为0将匹配可能的电话号码,如555-121-5468。

>>> for match in pnum.PhoneNumberMatcher(x2, 'US', leniency=0): 
print(match) 
... 
PhoneNumberMatch [220,232) 555-121-5468 

555前缀不是真正的前缀,但用于美国电视和电影中的虚构电话号码。从Wikipedia

带有前缀的电话号码555是为了防止实际的王牌和 好奇来电者打扰广泛用于虚拟 电话号码在北美的电视节目,电影,视频 游戏和其他媒体真实的人和组织通过 打电话给他们在小说作品中看到的数字;一般来说,在美国的北美地区,以555作为前缀的号码将不会连接到真实的 人。