Skip to content

Commit 1a55543

Browse files
authored
Handle some bugs (#20)
* handle codacy issue * remove blank space docstring * fix bugs * pump version
1 parent d86e345 commit 1a55543

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='urbamt',
8-
version='0.0.1-b3',
8+
version='0.0.1-b4',
99
author="Patrick Phat Nguyen",
1010
author_email="me@patrickphat.com",
1111
description="URBaMT: Universal Rule-based Machine Translation Toolkit",

urbamt/translator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ def translate(self, sentences: List[str] or str, allow_multiple_translation = Fa
6868
sentence = self.__process_text_input(sentence)
6969
trees = self.parser.parse(sentence.split())
7070
list_trees = [tree for tree in trees]
71-
71+
if len(list_trees) == 0:
72+
failed_sentences.append(sentence)
73+
continue
7274
trans_sentence = translate_trees_grammar(list_trees, self.src_to_tgt_grammar, self.src_to_tgt_dictionary)
7375
translated_sentences.append(trans_sentence)
7476

7577
# String to display failed sentence
7678
failed_sentences = '\n'.join(failed_sentences)
7779

7880
if len(failed_sentences) > 0:
79-
raise ValueError(f"Please check your grammar again, failed to translated these sentence \n {failed_sentences}")
81+
raise ValueError(f"Please check your grammar again, failed to parse these sentences: \n{failed_sentences}")
8082

8183
return translated_sentences

urbamt/utils/tree_manipulation.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,25 @@ def translate_tree_grammar(tree: nltk.Tree, grammar_substitutions: dict):
6565
num_subs = 0
6666
# Convert tree to ParentedTree
6767
ptree = tree_to_ptree(tree)
68-
# Traverse through subtrees
69-
for sub in ptree.subtrees():
70-
# Create grammar string from left-most node. E.g: NP -> JJ NP,
71-
# in this case, JJ is left-most node
72-
grammar_str = build_grammar_str_from_left_most(sub)
73-
for src_grammar, tgt_grammar in grammar_substitutions.items():
74-
if grammar_str == src_grammar:
75-
# Increment number of substitutions
76-
num_subs += 1
77-
# Calculate displacement between 2 grammar strings
78-
disp, new_words = calculate_displacement(src_grammar,tgt_grammar)
79-
# Change tree nodes positions thanks to new displacement
80-
swap_tree_given_left(sub, disp, new_words)
68+
old_num_subs = -1
69+
70+
# Loops until there no substitution left
71+
while num_subs != old_num_subs:
72+
old_num_subs = num_subs
73+
# Traverse through subtrees
74+
for sub in ptree.subtrees():
75+
# Create grammar string from left-most node. E.g: NP -> JJ NP,
76+
# in this case, JJ is left-most node
77+
grammar_str = build_grammar_str_from_left_most(sub)
78+
for src_grammar, tgt_grammar in grammar_substitutions.items():
79+
if grammar_str == src_grammar:
80+
# Increment number of substitutions
81+
num_subs += 1
82+
# Calculate displacement between 2 grammar strings
83+
disp, new_words = calculate_displacement(src_grammar,tgt_grammar)
84+
# Change tree nodes positions thanks to new displacement
85+
swap_tree_given_left(sub, disp, new_words)
86+
8187

8288
translated_grammar_sentence = " ".join(ptree.leaves())
8389
return translated_grammar_sentence, num_subs
@@ -101,7 +107,6 @@ def translate_trees_grammar(list_trees: List[nltk.Tree], src_to_tgt_grammar, src
101107
trans_map = {}
102108

103109
for tree in list_trees:
104-
105110
# Translate grammar
106111
trans_gram_sentence, num_subs = translate_tree_grammar(tree, src_to_tgt_grammar)
107112

0 commit comments

Comments
 (0)