Parakeet/parakeet/frontend/cn_normalization/phonecode.py

65 lines
1.9 KiB
Python
Raw Normal View History

2021-08-02 14:28:25 +08:00
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from .num import verbalize_digit
# 规范化固话/手机号码
# 手机
# http://www.jihaoba.com/news/show/13680
# 移动139、138、137、136、135、134、159、158、157、150、151、152、188、187、182、183、184、178、198
# 联通130、131、132、156、155、186、185、176
# 电信133、153、189、180、181、177
RE_MOBILE_PHONE = re.compile(
2021-08-02 14:28:25 +08:00
r"(?<!\d)((\+?86 ?)?1([38]\d|5[0-35-9]|7[678]|9[89])\d{8})(?!\d)")
RE_TELEPHONE = re.compile(
2021-08-05 17:14:43 +08:00
r"(?<!\d)((0(10|2[1-3]|[3-9]\d{2})-?)?[1-9]\d{7,8})(?!\d)")
2021-08-02 14:28:25 +08:00
def phone2str(phone_string: str, mobile=True) -> str:
if mobile:
sp_parts = phone_string.strip('+').split()
result = ''.join(
2021-08-17 15:29:30 +08:00
[verbalize_digit(part, alt_one=True) for part in sp_parts])
2021-08-02 14:28:25 +08:00
return result
else:
sil_parts = phone_string.split('-')
result = ''.join(
2021-08-17 15:29:30 +08:00
[verbalize_digit(part, alt_one=True) for part in sil_parts])
2021-08-02 14:28:25 +08:00
return result
2021-08-25 19:45:45 +08:00
def replace_phone(match) -> str:
"""
Parameters
----------
match : re.Match
Returns
----------
str
"""
2021-08-05 17:14:43 +08:00
return phone2str(match.group(0), mobile=False)
2021-08-25 19:45:45 +08:00
def replace_mobile(match) -> str:
"""
Parameters
----------
match : re.Match
Returns
----------
str
"""
return phone2str(match.group(0))