Skip to content

Commit

Permalink
Handle some bugs (#20)
Browse files Browse the repository at this point in the history
* handle codacy issue

* remove blank space docstring

* fix bugs

* pump version
  • Loading branch information
patrickphat committed Sep 22, 2020
1 parent d86e345 commit 1a55543
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='urbamt',
version='0.0.1-b3',
version='0.0.1-b4',
author="Patrick Phat Nguyen",
author_email="me@patrickphat.com",
description="URBaMT: Universal Rule-based Machine Translation Toolkit",
Expand Down
6 changes: 4 additions & 2 deletions urbamt/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ def translate(self, sentences: List[str] or str, allow_multiple_translation = Fa
sentence = self.__process_text_input(sentence)
trees = self.parser.parse(sentence.split())
list_trees = [tree for tree in trees]

if len(list_trees) == 0:
failed_sentences.append(sentence)
continue
trans_sentence = translate_trees_grammar(list_trees, self.src_to_tgt_grammar, self.src_to_tgt_dictionary)
translated_sentences.append(trans_sentence)

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

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

return translated_sentences
33 changes: 19 additions & 14 deletions urbamt/utils/tree_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,25 @@ def translate_tree_grammar(tree: nltk.Tree, grammar_substitutions: dict):
num_subs = 0
# Convert tree to ParentedTree
ptree = tree_to_ptree(tree)
# Traverse through subtrees
for sub in ptree.subtrees():
# Create grammar string from left-most node. E.g: NP -> JJ NP,
# in this case, JJ is left-most node
grammar_str = build_grammar_str_from_left_most(sub)
for src_grammar, tgt_grammar in grammar_substitutions.items():
if grammar_str == src_grammar:
# Increment number of substitutions
num_subs += 1
# Calculate displacement between 2 grammar strings
disp, new_words = calculate_displacement(src_grammar,tgt_grammar)
# Change tree nodes positions thanks to new displacement
swap_tree_given_left(sub, disp, new_words)
old_num_subs = -1

# Loops until there no substitution left
while num_subs != old_num_subs:
old_num_subs = num_subs
# Traverse through subtrees
for sub in ptree.subtrees():
# Create grammar string from left-most node. E.g: NP -> JJ NP,
# in this case, JJ is left-most node
grammar_str = build_grammar_str_from_left_most(sub)
for src_grammar, tgt_grammar in grammar_substitutions.items():
if grammar_str == src_grammar:
# Increment number of substitutions
num_subs += 1
# Calculate displacement between 2 grammar strings
disp, new_words = calculate_displacement(src_grammar,tgt_grammar)
# Change tree nodes positions thanks to new displacement
swap_tree_given_left(sub, disp, new_words)


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

for tree in list_trees:

# Translate grammar
trans_gram_sentence, num_subs = translate_tree_grammar(tree, src_to_tgt_grammar)

Expand Down

0 comments on commit 1a55543

Please sign in to comment.