Commit 1fcdf57b by 陈森彬

匹配带"-"号的百分数,kΩ的特殊匹配

parent 5ead7a1a
Showing with 18 additions and 5 deletions
......@@ -86,7 +86,10 @@ unit_map = {
},
'%': {'%': 0.01, },
'%': {'%': 0.01,
'-%': 0.01,
},
}
cast_map = {
......@@ -136,3 +139,5 @@ special_tuple = ("-",)
special_str = "~!@#$%^&*()_+-*/<>,.。,[]\/"
attr_regex = "F|H|K|A|W|KW"
special_attr_regex = "Ω"
......@@ -2,10 +2,10 @@
# -*- coding:utf-8 -*-
import re
from config import unit_map, cast_map, unit_regex, special_tuple, attr_regex, temp_map, special_str, encap_regex, \
r_regex, legal_char, split_char
r_regex, legal_char, split_char, special_attr_regex
unit_pattern = re.compile('([.a-zA-Z\\-]?)(\d+|\d+[\\\./?]\d+)(' + unit_regex + ')([.a-zA-Z\\-]?)')
attr_pattern = re.compile('(\d+|\.\d+)(' + attr_regex + ')')
attr_pattern = re.compile('(\d+|\.\d+)(' + attr_regex + ')('+ special_attr_regex + ')')
r_pattern = re.compile(
'([.a-zA-Z\\-]?)(\d+[' + r_regex + ']\d+|[' + r_regex + ']\d+|\d+[' + r_regex + '])([.a-zA-Z\\-]?)')
encap_pattern = re.compile('(.{0,2})(' + encap_regex + ')(.{0,2})', re.I)
......@@ -76,9 +76,13 @@ def check_param(kw):
else:
new_list = []
for param in check_res:
if "%" not in param:
if not param[0].isalpha() and param[0] not in special_tuple and not param[3].isalpha() and param[
3] not in special_tuple:
new_list.append(param[1] + param[2])
else:
if not param[0].isalpha() and not param[3].isalpha() and param[3] not in special_tuple:
new_list.append(param[0] + param[1] + param[2])
check_res = new_list
return check_res
......@@ -126,10 +130,13 @@ def unit_conversion(unit_res, kw):
"""
unit_str = get_unit(unit_res)
for unit_key, unit_data in unit_map.items():
if unit_str in unit_data:
if "/" not in kw: # 单独处理除号
if "%" in kw: # 百分号的单独处理
if "-" in unit_str:
unit_str = unit_str.replace("-","")
unit_num = int(kw.replace(unit_str, "").replace("-", ""))
else:
unit_num = int(kw.replace(unit_str, ""))
true_unit = str(delete_extra_zero(unit_num * unit_data[unit_str]))
elif "." in kw: # 处理小数点
......@@ -301,11 +308,12 @@ def get_unit(unit_list):
unit_str += u_str
return unit_str
def check_attr(kw_info):
res = attr_pattern.findall(kw_info)
if not res:
return False
else:
if res[0][0] and res[0][1] and "." not in res[0][0]:
if res[0][0] and res[0][1] and "." not in res[0][0] and not res[0][2]:
return True
return False
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment