Commit 1fcdf57b by 陈森彬

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

parent 5ead7a1a
Showing with 23 additions and 11 deletions
...@@ -86,7 +86,10 @@ unit_map = { ...@@ -86,7 +86,10 @@ unit_map = {
}, },
'%': {'%': 0.01, }, '%': {'%': 0.01,
'-%': 0.01,
},
} }
cast_map = { cast_map = {
...@@ -136,3 +139,5 @@ special_tuple = ("-",) ...@@ -136,3 +139,5 @@ special_tuple = ("-",)
special_str = "~!@#$%^&*()_+-*/<>,.。,[]\/" special_str = "~!@#$%^&*()_+-*/<>,.。,[]\/"
attr_regex = "F|H|K|A|W|KW" attr_regex = "F|H|K|A|W|KW"
special_attr_regex = "Ω"
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
import re import re
from config import unit_map, cast_map, unit_regex, special_tuple, attr_regex, temp_map, special_str, encap_regex, \ 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\\-]?)') 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( r_pattern = re.compile(
'([.a-zA-Z\\-]?)(\d+[' + r_regex + ']\d+|[' + r_regex + ']\d+|\d+[' + r_regex + '])([.a-zA-Z\\-]?)') '([.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) encap_pattern = re.compile('(.{0,2})(' + encap_regex + ')(.{0,2})', re.I)
...@@ -76,9 +76,13 @@ def check_param(kw): ...@@ -76,9 +76,13 @@ def check_param(kw):
else: else:
new_list = [] new_list = []
for param in check_res: for param in check_res:
if not param[0].isalpha() and param[0] not in special_tuple and not param[3].isalpha() and param[ if "%" not in param:
3] not in special_tuple: if not param[0].isalpha() and param[0] not in special_tuple and not param[3].isalpha() and param[
new_list.append(param[1] + param[2]) 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 check_res = new_list
return check_res return check_res
...@@ -126,11 +130,14 @@ def unit_conversion(unit_res, kw): ...@@ -126,11 +130,14 @@ def unit_conversion(unit_res, kw):
""" """
unit_str = get_unit(unit_res) unit_str = get_unit(unit_res)
for unit_key, unit_data in unit_map.items(): for unit_key, unit_data in unit_map.items():
if unit_str in unit_data: if unit_str in unit_data:
if "/" not in kw: # 单独处理除号 if "/" not in kw: # 单独处理除号
if "%" in kw: # 百分号的单独处理 if "%" in kw: # 百分号的单独处理
unit_num = int(kw.replace(unit_str, "")) 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])) true_unit = str(delete_extra_zero(unit_num * unit_data[unit_str]))
elif "." in kw: # 处理小数点 elif "." in kw: # 处理小数点
unit_num = float(kw.replace(unit_str, "")) unit_num = float(kw.replace(unit_str, ""))
...@@ -301,11 +308,12 @@ def get_unit(unit_list): ...@@ -301,11 +308,12 @@ def get_unit(unit_list):
unit_str += u_str unit_str += u_str
return unit_str return unit_str
def check_attr(kw_info): def check_attr(kw_info):
res = attr_pattern.findall(kw_info) res = attr_pattern.findall(kw_info)
if not res: if not res:
return False return False
else: 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 True
return False return False
\ No newline at end of file
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