2016-09-25 53 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作爲前綴的號碼將不會連接到真實的 人。