Skip to content

Commit

Permalink
v0.5.11 feat(cn2an, transform): cn2an normal mode support 元/圆; transf…
Browse files Browse the repository at this point in the history
…orm support 半/两.
  • Loading branch information
Ailln committed Apr 25, 2021
1 parent 8afb6c6 commit c2054c0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

🔗[点击访问 DEMO](https://www.dovolopor.com/cn2an)

> 🎈 `v0.5.9 update`: fix "拾壹" => "壹拾壹"
> 🎈 `v0.5.11 update`: cn2an normal mode support 元/圆; transform support 半/两.
>
> 🎈 [`en2an`](https://github.com/Ailln/en2an): 「英文数字」和「阿拉伯数字」互转正在收集需求中! [详情](https://github.com/Ailln/en2an)
>
Expand Down Expand Up @@ -92,7 +92,7 @@ import cn2an

# 查看当前版本号
print(cn2an.__version__)
# 0.5.9
# 0.5.11
```

### 3.1 `中文数字` => `阿拉伯数字`
Expand Down
2 changes: 1 addition & 1 deletion cn2an/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .an2cn import An2Cn
from .transform import Transform

__version__ = "0.5.10"
__version__ = "0.5.11"

cn2an = Cn2An().cn2an
an2cn = An2Cn().an2cn
Expand Down
7 changes: 7 additions & 0 deletions cn2an/cn2an.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ def __check_input_data_is_valid(self, check_data: str, mode: str) -> (int, str,
if check_data[-2:] == word:
check_data = check_data[:-2]

# 去除 元、圆
if mode != "strict":
normal_stop_words = ["圆", "元"]
for word in normal_stop_words:
if check_data[-1] == word:
check_data = check_data[:-1]

# 处理元角分
yjf_pattern = re.compile(fr"^.*?[元圆][{self.all_num}]角([{self.all_num}]分)?$")
result = yjf_pattern.search(check_data)
Expand Down
4 changes: 4 additions & 0 deletions cn2an/cn2an_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def setUp(self) -> None:
"三万五": 35000,
"两千六": 2600,
"一百二": 120,
# 大写
"壹拾壹元": 11,
"壹佰壹拾壹圆": 111,
"壹拾壹圆": 11,
}

self.smart_data_dict = {
Expand Down
4 changes: 3 additions & 1 deletion cn2an/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def __init__(self) -> None:
self.smart_cn_pattern = f"-?([0-9]+.)?[0-9]+[{self.all_unit}]+"

def transform(self, inputs: str, method: str = "cn2an") -> str:
inputs = inputs.replace("廿", "二十")
inputs = inputs.replace("廿", "二十").replace("半", "0.5").replace("两", "2")

if method == "cn2an":
# date
inputs = re.sub(fr"((({self.smart_cn_pattern})|({self.cn_pattern}))年)?([{self.all_num}十]+月)?([{self.all_num}十]+日)?",
Expand All @@ -33,6 +34,7 @@ def transform(self, inputs: str, method: str = "cn2an") -> str:
# number
output = re.sub(self.cn_pattern,
lambda x: self.__sub_util(x.group(), "cn2an", "number"), inputs)

elif method == "an2cn":
# date
inputs = re.sub(r"(\d{2,4}年)?(\d{1,2}月)?(\d{1,2}日)?",
Expand Down
4 changes: 3 additions & 1 deletion cn2an/transform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def setUp(self) -> None:
self.smart_data_dict = {
"约2.5亿年~6500万年": "约250000000年~65000000年",
"廿二日,日出东方": "22日,日出东方",
"大陆": "大陆"
"大陆": "大陆",
"半斤": "0.5斤",
"两个": "2个",
}

self.t = Transform()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="cn2an",
version="0.5.10",
version="0.5.11",
author="Ailln",
author_email="[email protected]",
url="https://github.com/Ailln/cn2an",
Expand Down

0 comments on commit c2054c0

Please sign in to comment.