Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
if self.matched == True:
if self.left is not None:
self.left.convert_tree(test_dict)
if self.right is not None:
self.right.convert_tree(test_dict)
else:
test_len = min (self.string_len, test_dict[0])
while test_len != 0:
# Loop through trying successively smaller substrings in the dictionary
for i in range(0, self.string_len - test_len + 1):
if self.string[i:i+test_len] in test_dict[1]:
# Match found.
if i > 0:
# Put everything to the left of the match into the left sub-tree and further process it
self.left = StringTree(self.string[:i])
self.left.convert_tree(test_dict)
if (i+test_len) < self.string_len:
# Put everything to the left of the match into the left sub-tree and further process it
self.right = StringTree(self.string[i+test_len:])
self.right.convert_tree(test_dict)
# Save the dictionary value in this tree
value = test_dict[1][self.string[i:i+test_len]]
if len(value.split(' ')) > 1:
# multiple mapping, use the first one for now
value = value.split(' ')[0]
self.string = value
self.string_len = len(self.string)
self.matched = True
return
test_len -= 1
if self.right is not None:
self.right.convert_tree(test_dict)
else:
test_len = min (self.string_len, test_dict[0])
while test_len != 0:
# Loop through trying successively smaller substrings in the dictionary
for i in range(0, self.string_len - test_len + 1):
if self.string[i:i+test_len] in test_dict[1]:
# Match found.
if i > 0:
# Put everything to the left of the match into the left sub-tree and further process it
self.left = StringTree(self.string[:i])
self.left.convert_tree(test_dict)
if (i+test_len) < self.string_len:
# Put everything to the left of the match into the left sub-tree and further process it
self.right = StringTree(self.string[i+test_len:])
self.right.convert_tree(test_dict)
# Save the dictionary value in this tree
value = test_dict[1][self.string[i:i+test_len]]
if len(value.split(' ')) > 1:
# multiple mapping, use the first one for now
value = value.split(' ')[0]
self.string = value
self.string_len = len(self.string)
self.matched = True
return
test_len -= 1
If a dictionary is part of a group of dictionaries, stop conversion on a word
after the first match is found.
:param string: the input string
:param dictionary: list of dictionaries to be applied against the string
:param is_dict_group: indicates if this is a group of dictionaries in which only
the first match in the dict group should be used
:return: converted string
"""
tree = StringTree(string)
for c_dict in dictionary:
if isinstance(c_dict, tuple):
tree.convert_tree(c_dict)
if not is_dict_group:
# Don't reform the string here if the dictionary list is part of a group
# Recreate the tree for next loop iteration
tree = StringTree("".join(tree.inorder()))
else:
# This is a list of dictionaries. Call back in with the dictionary
# list but specify that this is a group
tree = StringTree(self._convert("".join(tree.inorder()), c_dict, True))
return "".join(tree.inorder())
def _convert(self, string, dictionary = [], is_dict_group = False):
"""
Convert string from Simplified Chinese to Traditional Chinese or vice versa
If a dictionary is part of a group of dictionaries, stop conversion on a word
after the first match is found.
:param string: the input string
:param dictionary: list of dictionaries to be applied against the string
:param is_dict_group: indicates if this is a group of dictionaries in which only
the first match in the dict group should be used
:return: converted string
"""
tree = StringTree(string)
for c_dict in dictionary:
if isinstance(c_dict, tuple):
tree.convert_tree(c_dict)
if not is_dict_group:
# Don't reform the string here if the dictionary list is part of a group
# Recreate the tree for next loop iteration
tree = StringTree("".join(tree.inorder()))
else:
# This is a list of dictionaries. Call back in with the dictionary
# list but specify that this is a group
tree = StringTree(self._convert("".join(tree.inorder()), c_dict, True))
return "".join(tree.inorder())
:param is_dict_group: indicates if this is a group of dictionaries in which only
the first match in the dict group should be used
:return: converted string
"""
tree = StringTree(string)
for c_dict in dictionary:
if isinstance(c_dict, tuple):
tree.convert_tree(c_dict)
if not is_dict_group:
# Don't reform the string here if the dictionary list is part of a group
# Recreate the tree for next loop iteration
tree = StringTree("".join(tree.inorder()))
else:
# This is a list of dictionaries. Call back in with the dictionary
# list but specify that this is a group
tree = StringTree(self._convert("".join(tree.inorder()), c_dict, True))
return "".join(tree.inorder())