Last active
November 21, 2018 16:47
-
-
Save LKI/4ba84e39b00d8666dce3a172a74e9ea3 to your computer and use it in GitHub Desktop.
demo relation translation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
MAPPING = {} | |
MODEL_COUNT = 500 | |
FIELD_COUNT = 20 | |
CUSTOM_COUNT = 1000 | |
CUSTOM_LENGTH = (2, 10) | |
def mock(): | |
# 先把原始 model 加载到内存里 | |
# 翻译数据就直接用 base10 -> base16 造假了 | |
MAPPING.update({ | |
str(i): hex(i)[2:].upper() for i in range(MODEL_COUNT) | |
}) | |
# 再把 model_set 也加载到内存里 | |
MAPPING.update({ | |
f'{i}_set': str(int(str(i), 16)) for i in range(MODEL_COUNT) | |
}) | |
# 自定义字段就随机组合 | |
custom_mapping = {} | |
for _ in range(CUSTOM_COUNT): | |
length = random.randint(*CUSTOM_LENGTH) | |
fields = tuple(random.sample(MAPPING.keys(), length)) | |
custom_mapping[fields] = ''.join(MAPPING[f] for f in fields) | |
# 20 个字段就用 小写字母 -> 大写字母 来代替吧 | |
MAPPING.update({ | |
chr(ord('a') + i): chr(ord('A') + i) for i in range(FIELD_COUNT) | |
}) | |
MAPPING.update({('1', '10'): 'test'}) | |
def translate(string): | |
# 动态规划: | |
# f(n) = f(n-1) | |
# f(n) = max(f(n), f(i) + (n-i+1)) if is_custom(s[i:n]) | |
fields = string.split('__') | |
steps = [ | |
{ | |
'length': 1, | |
'keys': (fields[0],), | |
}, | |
] | |
for i in range(1, len(fields)): | |
last_step = steps[-1] | |
max_length = last_step['length'] | |
max_keys = (*last_step['keys'], fields[i]) | |
for j in range(0, i): | |
custom_word = tuple(fields[j:i + 1]) | |
if custom_word in MAPPING: | |
length = i - j + 1 | |
keys = (custom_word,) | |
if j > 0: | |
length += steps[j - 1]['length'] | |
keys = (*steps[j - 1]['keys'], *keys) | |
if length > max_length: | |
max_length = length | |
max_keys = keys | |
steps.append({'length': max_length, 'keys': max_keys}) | |
return '-'.join(MAPPING.get(w, 'unknown') for w in steps[-1]['keys']) | |
def main(): | |
mock() | |
print(translate('1__10__23_set__453__34__65_set__4__59__40_set__354_set__b')) | |
# %timeit translate('1__10__23_set__453__34__65_set__4__59__40_set__354_set__b') | |
# 42.6 µs ± 4.42 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each) | |
def test(): | |
MAPPING.update({ | |
'A': 'a', | |
'B': 'b', | |
'C': 'c', | |
'D': 'd', | |
'E': 'e', | |
}) | |
MAPPING.update({ | |
('A', 'B'): 'ab', | |
('B', 'C'): 'bc', | |
# ('A', 'B', 'C'): 'abc', | |
('D', 'E'): 'de', | |
# ('A', 'B', 'C', 'D'): 'abcd', | |
# ('A', 'B', 'C', 'D', 'E'): 'abcde', | |
}) | |
print(translate('A__B__C__D__E')) | |
if __name__ == '__main__': | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment