Update models/rule_based_mt.py
Browse files- models/rule_based_mt.py +471 -470
models/rule_based_mt.py
CHANGED
|
@@ -1,470 +1,471 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
|
| 4 |
-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 5 |
-
|
| 6 |
-
import re
|
| 7 |
-
import nltk
|
| 8 |
-
from nltk.tokenize import word_tokenize
|
| 9 |
-
from nltk.tag import pos_tag
|
| 10 |
-
from nltk.parse import ChartParser, ViterbiParser
|
| 11 |
-
from nltk.grammar import CFG, PCFG, Nonterminal, ProbabilisticProduction
|
| 12 |
-
from nltk.tree import Tree
|
| 13 |
-
import contractions
|
| 14 |
-
import string
|
| 15 |
-
from collections import defaultdict
|
| 16 |
-
import spacy
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
for
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
tree = self.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
words.
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
#
|
| 297 |
-
#
|
| 298 |
-
#
|
| 299 |
-
#
|
| 300 |
-
#
|
| 301 |
-
#
|
| 302 |
-
#
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
words
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
words
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
words
|
| 356 |
-
words.insert(0, "
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
words
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
words
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
words =
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
words
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
"
|
| 429 |
-
"
|
| 430 |
-
"
|
| 431 |
-
"
|
| 432 |
-
"
|
| 433 |
-
"
|
| 434 |
-
"
|
| 435 |
-
"
|
| 436 |
-
"
|
| 437 |
-
"
|
| 438 |
-
"
|
| 439 |
-
"
|
| 440 |
-
"
|
| 441 |
-
"
|
| 442 |
-
"
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
"
|
| 449 |
-
"
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
"
|
| 453 |
-
"
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
"
|
| 457 |
-
"
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
"
|
| 461 |
-
"
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
print("
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
print()
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 5 |
+
|
| 6 |
+
import re
|
| 7 |
+
import nltk
|
| 8 |
+
from nltk.tokenize import word_tokenize
|
| 9 |
+
from nltk.tag import pos_tag
|
| 10 |
+
from nltk.parse import ChartParser, ViterbiParser
|
| 11 |
+
from nltk.grammar import CFG, PCFG, Nonterminal, ProbabilisticProduction
|
| 12 |
+
from nltk.tree import Tree
|
| 13 |
+
import contractions
|
| 14 |
+
import string
|
| 15 |
+
from collections import defaultdict
|
| 16 |
+
import spacy
|
| 17 |
+
|
| 18 |
+
spacy.cli.download("en_core_web_sm")
|
| 19 |
+
nlp = spacy.load("en_core_web_sm")
|
| 20 |
+
|
| 21 |
+
import json
|
| 22 |
+
|
| 23 |
+
with open("data/en_vi_dictionary.json", "r", encoding='utf-8') as json_file:
|
| 24 |
+
dictionary = json.load(json_file)
|
| 25 |
+
|
| 26 |
+
with open('grammar.txt', 'r', encoding='utf-8') as text_file:
|
| 27 |
+
grammar = text_file.read()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class TransferBasedMT:
|
| 31 |
+
|
| 32 |
+
def __init__(self) -> None:
|
| 33 |
+
# English - Vietnamese dictionary
|
| 34 |
+
self.dictionary = dictionary
|
| 35 |
+
|
| 36 |
+
# Define the CFG grammar for English sentence structure
|
| 37 |
+
self.grammar = grammar
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
################################################ STAGE 1: PREPROCESSING SOURCE SENTENCE ###################################################
|
| 41 |
+
|
| 42 |
+
def preprocessing(self, sentence: str) -> str:
|
| 43 |
+
"""Preprocess the input sentence: handle named entities, lowercase, expand contractions, and tokenize and regroup."""
|
| 44 |
+
# Handle named entities, e.g. New York -> New_York
|
| 45 |
+
doc = nlp(sentence)
|
| 46 |
+
entities = {ent.text: ent.label_ for ent in doc.ents}
|
| 47 |
+
for ent_text in sorted(entities.keys(), key=len,reverse=True):
|
| 48 |
+
ent_joined = ent_text.replace(" ", "_")
|
| 49 |
+
sentence = sentence.replace(ent_text, ent_joined)
|
| 50 |
+
|
| 51 |
+
# Lowercase and strip redundant space
|
| 52 |
+
sentence = sentence.lower().strip()
|
| 53 |
+
|
| 54 |
+
# Expand contractions, e.g. don't -> do not
|
| 55 |
+
sentence = contractions.fix(sentence) #type: ignore
|
| 56 |
+
|
| 57 |
+
# Tokenize and regroup tokens
|
| 58 |
+
sentence = " ".join(word_tokenize(sentence))
|
| 59 |
+
|
| 60 |
+
return sentence
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def safe_tag(self, tag):
|
| 64 |
+
"""Convert tags with special characters to safe nonterminal symbols."""
|
| 65 |
+
return tag.replace("$", "S")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
################################################ STAGE 2: ANALYZE SOURCE SENTENCE #########################################################
|
| 69 |
+
|
| 70 |
+
def analyze_source(self, sentence: str):
|
| 71 |
+
"""Analyze the source sentence: tokenize, POS tag, and parse into a syntax tree."""
|
| 72 |
+
doc = nlp(sentence)
|
| 73 |
+
filtered_pos_tagged = []
|
| 74 |
+
punctuation_marks = []
|
| 75 |
+
|
| 76 |
+
for i, token in enumerate(doc):
|
| 77 |
+
word = token.text
|
| 78 |
+
tag = token.tag_
|
| 79 |
+
if all(char in string.punctuation for char in word):
|
| 80 |
+
punctuation_marks.append((i, word, tag))
|
| 81 |
+
else:
|
| 82 |
+
filtered_pos_tagged.append((token.lemma_.lower(), tag))
|
| 83 |
+
|
| 84 |
+
grammar_str = self.grammar
|
| 85 |
+
|
| 86 |
+
# Add terminal rule grammars
|
| 87 |
+
for word, tag in filtered_pos_tagged:
|
| 88 |
+
safe_tag = self.safe_tag(tag)
|
| 89 |
+
escaped_word = word.replace('"', '\\"')
|
| 90 |
+
grammar_str += f'\n{safe_tag} -> "{escaped_word}"'
|
| 91 |
+
|
| 92 |
+
try:
|
| 93 |
+
grammar = CFG.fromstring(grammar_str)
|
| 94 |
+
parser = ChartParser(grammar)
|
| 95 |
+
tagged_tokens_only = [word for word, _ in filtered_pos_tagged]
|
| 96 |
+
|
| 97 |
+
parses = list(parser.parse(tagged_tokens_only)) # Generate parse trees
|
| 98 |
+
|
| 99 |
+
tree = (parses[0] if parses else self._create_fallback_tree(filtered_pos_tagged)) # Use first parse or fallback
|
| 100 |
+
tree = self._add_punctuation_to_tree(tree, punctuation_marks) # Reattach punctuation
|
| 101 |
+
|
| 102 |
+
return tree
|
| 103 |
+
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"Grammar creation error: {e}")
|
| 106 |
+
return self._create_fallback_tree(filtered_pos_tagged) # Fallback on error
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def _create_fallback_tree(self, pos_tagged):
|
| 110 |
+
"""Create a simple fallback tree when parsing fails."""
|
| 111 |
+
children = [Tree(self.safe_tag(tag), [word]) for word, tag in pos_tagged] # Create leaf nodes for each token
|
| 112 |
+
return Tree("S", children) # Wrap in a sentence node
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def _add_punctuation_to_tree(self, tree, punctuation_marks):
|
| 116 |
+
"""Add punctuation marks back to the syntax tree."""
|
| 117 |
+
if not punctuation_marks:
|
| 118 |
+
return tree
|
| 119 |
+
if tree.label() == "S": # Only add to root sentence node
|
| 120 |
+
for _, word, tag in sorted(punctuation_marks):
|
| 121 |
+
tree.append(Tree(self.safe_tag(tag), [word]))
|
| 122 |
+
return tree
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
#################################################### STAGE 3: TRANSFER GRAMMAR ############################################################
|
| 126 |
+
|
| 127 |
+
def transfer_grammar(self, tree):
|
| 128 |
+
"""Transfer the English parse tree to Vietnamese structure."""
|
| 129 |
+
if not isinstance(tree, nltk.Tree):
|
| 130 |
+
return tree
|
| 131 |
+
|
| 132 |
+
# Sentence level: recurse through children
|
| 133 |
+
if tree.label() == "S":
|
| 134 |
+
return Tree("S", [self.transfer_grammar(child) for child in tree])
|
| 135 |
+
|
| 136 |
+
# Verb Phrase: adjust word order
|
| 137 |
+
elif tree.label() == "VP":
|
| 138 |
+
children = [self.transfer_grammar(child) for child in tree]
|
| 139 |
+
child_labels = [child.label() if isinstance(child, Tree) else child for child in children]
|
| 140 |
+
|
| 141 |
+
if (len(children) >= 3 and "V" in child_labels and "To" in child_labels and "VP" in child_labels): # Remove TO from V TO VP
|
| 142 |
+
return Tree("VP", [children[0], children[2]])
|
| 143 |
+
|
| 144 |
+
return Tree("VP", children) # Default: preserve order
|
| 145 |
+
|
| 146 |
+
# Noun Phrase: adjust word order
|
| 147 |
+
elif tree.label() == "NP":
|
| 148 |
+
children = [self.transfer_grammar(child) for child in tree]
|
| 149 |
+
child_labels = [child.label() if isinstance(child, Tree) else child for child in children]
|
| 150 |
+
|
| 151 |
+
if (len(children) >= 3 and 'Det' in child_labels and 'AdjP' in child_labels and 'N' in child_labels): # Reorder Det Adj N -> Det N Adj
|
| 152 |
+
return Tree("NP", [children[0], children[2], children[1]])
|
| 153 |
+
|
| 154 |
+
elif (len(children) >= 2 and 'PRPS' in child_labels and 'N' in child_labels): # Reorder PRPS N -> N PRPS
|
| 155 |
+
return Tree("NP", [children[1], children[0]])
|
| 156 |
+
|
| 157 |
+
elif (len(children) >= 2 and 'Det' in child_labels and 'N' in child_labels): # Remove Det from Det N
|
| 158 |
+
return Tree("NP", [children[1]])
|
| 159 |
+
|
| 160 |
+
return Tree("NP", children) # Default: preserve order
|
| 161 |
+
|
| 162 |
+
# Prepositional Phrase: adjust word order
|
| 163 |
+
elif tree.label() == "PP":
|
| 164 |
+
children = [self.transfer_grammar(child) for child in tree]
|
| 165 |
+
return Tree("PP", children) # Default: preserve order
|
| 166 |
+
|
| 167 |
+
# Adverbial Phrase: adjust word order
|
| 168 |
+
elif tree.label() == 'AdvP':
|
| 169 |
+
children = [self.transfer_grammar(child) for child in tree]
|
| 170 |
+
return Tree("AdvP", children) # Default: preserve order
|
| 171 |
+
|
| 172 |
+
# Adjective Phrase: adjust word order
|
| 173 |
+
elif tree.label() == 'AdjP':
|
| 174 |
+
children = [self.transfer_grammar(child) for child in tree]
|
| 175 |
+
return Tree("AdjP", children) # Default: preserve order
|
| 176 |
+
|
| 177 |
+
# Wh-Question: adjust word order
|
| 178 |
+
elif tree.label() == "WhQ":
|
| 179 |
+
children = [self.transfer_grammar(child) for child in tree]
|
| 180 |
+
child_labels = [child.label() if isinstance(child, Tree) else child for child in children]
|
| 181 |
+
|
| 182 |
+
if len(children) >= 4 and "WH_Word" in child_labels and "AUX" in child_labels and "NP" in child_labels and "VP" in child_labels:
|
| 183 |
+
return Tree("WhQ", [children[2], children[3], children[0]]) # Remove AUX from WH_Word AUX NP VP
|
| 184 |
+
|
| 185 |
+
elif len(children) >= 3 and "WH_Word" in child_labels and "NP" in child_labels and "VP" in child_labels and "AUX" not in child_labels:
|
| 186 |
+
return Tree("WhQ", [children[1], children[2], children[0]])
|
| 187 |
+
|
| 188 |
+
elif len(children) >= 2 and "WH_Word" in child_labels and "VP" in child_labels:
|
| 189 |
+
if len(children[1]) >= 2:
|
| 190 |
+
return Tree("WhQ", [children[1][1], children[1][0], children[0]]) # WH_Word VP -> WH_Word V NP
|
| 191 |
+
|
| 192 |
+
else:
|
| 193 |
+
return Tree("WhQ", children) # Default: preserve order
|
| 194 |
+
|
| 195 |
+
# Yes/No-Question: adjust word order
|
| 196 |
+
elif tree.label() == "YNQ":
|
| 197 |
+
children = [self.transfer_grammar(child) for child in tree]
|
| 198 |
+
child_labels = [child.label() if isinstance(child, Tree) else child for child in children]
|
| 199 |
+
|
| 200 |
+
if len(children) >= 3 and "AUX" in child_labels and "NP" in child_labels and "VP" in child_labels:
|
| 201 |
+
return Tree("YNQ", [children[1], children[2]])
|
| 202 |
+
|
| 203 |
+
elif len(children) >= 3 and "DO" in child_labels and "NP" in child_labels and "VP" in child_labels:
|
| 204 |
+
return Tree("YNQ", [children[1], children[2]])
|
| 205 |
+
|
| 206 |
+
elif len(children) >= 3 and "MD" in child_labels and "NP" in child_labels and "VP" in child_labels:
|
| 207 |
+
return Tree("YNQ", [children[1], children[2]])
|
| 208 |
+
|
| 209 |
+
return Tree("YNQ", children)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
# Other labels: recurse through children
|
| 213 |
+
else:
|
| 214 |
+
return Tree(tree.label(), [self.transfer_grammar(child) for child in tree])
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
#################################################### STAGE 4: GENERATION STAGE ############################################################
|
| 218 |
+
|
| 219 |
+
def generate(self, tree):
|
| 220 |
+
"""Generate Vietnamese output from the transformed tree."""
|
| 221 |
+
if not isinstance(tree, nltk.Tree):
|
| 222 |
+
return self._lexical_transfer(tree) # Translate leaf nodes
|
| 223 |
+
|
| 224 |
+
words = [self.generate(child) for child in tree if self.generate(child)] # Recurse
|
| 225 |
+
|
| 226 |
+
# Handle questions specifically
|
| 227 |
+
if tree.label() == "WhQ":
|
| 228 |
+
words = self._process_wh_question(tree, words)
|
| 229 |
+
elif tree.label() == "YNQ":
|
| 230 |
+
words = self._process_yn_question(tree, words)
|
| 231 |
+
elif tree.label() == "NP": # Add classifiers for nouns
|
| 232 |
+
words = self._add_classifiers(tree, words)
|
| 233 |
+
elif tree.label() == "VP": # Apply tense/aspect/mood markers
|
| 234 |
+
words = self._apply_tam_mapping(tree, words)
|
| 235 |
+
|
| 236 |
+
words = self._apply_agreement(tree, words) # Handle agreement (e.g., plurals)
|
| 237 |
+
result = " ".join(words) # Join words into a string
|
| 238 |
+
|
| 239 |
+
return result
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def _process_wh_question(self, tree, words):
|
| 243 |
+
"""Process a Wh-question structure for Vietnamese."""
|
| 244 |
+
words = [w for w in words if w]
|
| 245 |
+
|
| 246 |
+
wh_word = None
|
| 247 |
+
for word in words:
|
| 248 |
+
if word in ["cái gì", "ai", "ở đâu", "khi nào", "tại sao", "như thế nào", "cái nào", "của ai"]:
|
| 249 |
+
wh_word = word
|
| 250 |
+
break
|
| 251 |
+
|
| 252 |
+
if wh_word == "tại sao":
|
| 253 |
+
if words and words[0] != "tại sao":
|
| 254 |
+
words.remove("tại sao")
|
| 255 |
+
words.insert(0, "tại sao")
|
| 256 |
+
elif wh_word == "như thế nào":
|
| 257 |
+
if "vậy" not in words:
|
| 258 |
+
words.append("vậy")
|
| 259 |
+
|
| 260 |
+
question_particles = ["vậy", "thế", "à", "hả"]
|
| 261 |
+
has_particle = any(particle in words for particle in question_particles)
|
| 262 |
+
|
| 263 |
+
if not has_particle and wh_word != "tại sao":
|
| 264 |
+
words.append("vậy")
|
| 265 |
+
|
| 266 |
+
return words
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def _process_yn_question(self, tree, words):
|
| 270 |
+
"""Process a Yes/No question structure for Vietnamese."""
|
| 271 |
+
|
| 272 |
+
words = [w for w in words if w not in ["", "do_vn", "does_vn", "did_vn"]]
|
| 273 |
+
|
| 274 |
+
has_question_particle = any(w in ["không", "à", "hả", "nhỉ", "chứ"] or
|
| 275 |
+
w in ["không_vn", "à_vn", "hả_vn", "nhỉ_vn", "chứ_vn"]
|
| 276 |
+
for w in words)
|
| 277 |
+
|
| 278 |
+
if not has_question_particle:
|
| 279 |
+
if "đã" in words or "đã_vn" in words:
|
| 280 |
+
words.append("phải không")
|
| 281 |
+
else:
|
| 282 |
+
words.append("không")
|
| 283 |
+
|
| 284 |
+
return words
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def _lexical_transfer(self, word):
|
| 288 |
+
"""Translate English words to Vietnamese using the dictionary."""
|
| 289 |
+
if word in self.dictionary:
|
| 290 |
+
return self.dictionary[word] # Return translation if in dictionary
|
| 291 |
+
return f"{word}_vn" # Mark untranslated words with _vn suffix
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
def _add_classifiers(self, np_tree, words):
|
| 295 |
+
"""Add Vietnamese classifiers based on nouns."""
|
| 296 |
+
# noun_indices = [
|
| 297 |
+
# i for i, child in enumerate(np_tree) if isinstance(child, Tree)
|
| 298 |
+
# and child.label() in ["N", "NN", "NNS", "NNP", "NNPS"]
|
| 299 |
+
# ] # Find noun positions
|
| 300 |
+
# for i in noun_indices:
|
| 301 |
+
# if len(words) > i and not any(words[i].startswith(prefix) for prefix in ["một_vn", "những_vn", "các_vn"]): # Check if classifier is needed
|
| 302 |
+
# if words[i].endswith("_vn"): # Add default classifier for untranslated nouns
|
| 303 |
+
# words.insert(i, "cái_vn")
|
| 304 |
+
return words
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
def _apply_tam_mapping(self, vp_tree, words):
|
| 308 |
+
"""Apply Vietnamese TAM (Tense, Aspect, Mood) markers to the word list.
|
| 309 |
+
|
| 310 |
+
Args:
|
| 311 |
+
vp_tree: A parse tree node representing the verb phrase.
|
| 312 |
+
words: List of words to be modified with TAM markers.
|
| 313 |
+
|
| 314 |
+
Returns:
|
| 315 |
+
List of words with appropriate Vietnamese TAM markers inserted.
|
| 316 |
+
"""
|
| 317 |
+
verb_tense = None
|
| 318 |
+
mood = None
|
| 319 |
+
|
| 320 |
+
# Identify verb tense and mood from the verb phrase tree
|
| 321 |
+
for child in vp_tree:
|
| 322 |
+
if isinstance(child, Tree):
|
| 323 |
+
if child.label() in ["V", "VB", "VBD", "VBG", "VBN", "VBP", "VBZ"]:
|
| 324 |
+
verb_tense = child.label()
|
| 325 |
+
if child.label() == "MD": # Modal verbs indicating mood
|
| 326 |
+
mood = "indicative"
|
| 327 |
+
elif child.label() == "TO": # Infinitive marker, often subjunctive
|
| 328 |
+
mood = "subjunctive"
|
| 329 |
+
|
| 330 |
+
if not verb_tense:
|
| 331 |
+
print("Warning: No verb tense identified in the verb phrase tree.")
|
| 332 |
+
return words
|
| 333 |
+
|
| 334 |
+
# Apply TAM markers based on verb tense
|
| 335 |
+
if verb_tense == "VBD":
|
| 336 |
+
words.insert(0, "đã_vn")
|
| 337 |
+
elif verb_tense == "VB":
|
| 338 |
+
if "will_vn" in words:
|
| 339 |
+
words = [w for w in words if w != "will_vn"]
|
| 340 |
+
words.insert(0, "sẽ_vn")
|
| 341 |
+
elif "going_to_vn" in words:
|
| 342 |
+
words = [w for w in words if w != "going_to_vn"]
|
| 343 |
+
words.insert(0, "sẽ_vn")
|
| 344 |
+
elif verb_tense == "VBG":
|
| 345 |
+
words.insert(0, "đang_vn")
|
| 346 |
+
if "đã_vn" in words:
|
| 347 |
+
words.insert(0, "đã_vn")
|
| 348 |
+
elif verb_tense == "VBN":
|
| 349 |
+
words.insert(0, "đã_vn")
|
| 350 |
+
elif verb_tense == "VBP" or verb_tense == "VBZ":
|
| 351 |
+
pass
|
| 352 |
+
|
| 353 |
+
# Handle future continuous (e.g., "will be running" -> "sẽ đang")
|
| 354 |
+
if verb_tense == "VBG" and "will_vn" in words:
|
| 355 |
+
words = [w for w in words if w != "will_vn"]
|
| 356 |
+
words.insert(0, "đang_vn") # Continuous marker
|
| 357 |
+
words.insert(0, "sẽ_vn") # Future marker
|
| 358 |
+
|
| 359 |
+
# Apply mood markers if applicable
|
| 360 |
+
if mood == "subjunctive":
|
| 361 |
+
words.insert(0, "nếu_vn") # Subjunctive marker (e.g., "if" clause)
|
| 362 |
+
elif mood == "indicative" and "must_vn" in words:
|
| 363 |
+
words = [w for w in words if w != "must_vn"]
|
| 364 |
+
words.insert(0, "phải_vn") # Necessity marker
|
| 365 |
+
|
| 366 |
+
return words
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
def _apply_agreement(self, tree, words):
|
| 370 |
+
"""Apply agreement rules for Vietnamese (e.g., pluralization)."""
|
| 371 |
+
if tree.label() == "NP":
|
| 372 |
+
for i, word in enumerate(words):
|
| 373 |
+
if "_vn" in word and word.replace("_vn", "").endswith("s"): # Handle English plurals
|
| 374 |
+
base_word = word.replace("_vn", "")[:-1] + "_vn" # Remove 's'
|
| 375 |
+
words[i] = base_word
|
| 376 |
+
words.insert(i, "các_vn") # Add plural marker
|
| 377 |
+
return words
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def _post_process_vietnamese(self, text):
|
| 381 |
+
"""Post-process the Vietnamese output: remove _vn, fix punctuation, capitalize."""
|
| 382 |
+
text = text.replace("_vn", "") # Remove untranslated markers
|
| 383 |
+
|
| 384 |
+
def fix_entities(word):
|
| 385 |
+
if "_" in word:
|
| 386 |
+
word = " ".join([w for w in word.split("_")])
|
| 387 |
+
return word.title()
|
| 388 |
+
return word.lower() # Lowercase non-entity words
|
| 389 |
+
|
| 390 |
+
words = text.split()
|
| 391 |
+
words = [fix_entities(word) for word in words]
|
| 392 |
+
|
| 393 |
+
text = " ".join(words)
|
| 394 |
+
for punct in [".", ",", "!", "?", ":", ";"]: # Attach punctuation directly
|
| 395 |
+
text = text.replace(f" {punct}", punct)
|
| 396 |
+
|
| 397 |
+
if text:
|
| 398 |
+
words = text.split()
|
| 399 |
+
words[0] = words[0].capitalize() # Capitalize first word
|
| 400 |
+
text = ' '.join(words)
|
| 401 |
+
return text
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
def translate(self, english_sentence):
|
| 405 |
+
"""Main translation function that applies all stages of the process."""
|
| 406 |
+
# Step 1: Preprocess input
|
| 407 |
+
preprocessed = self.preprocessing(english_sentence)
|
| 408 |
+
|
| 409 |
+
# Step 2: Parse English sentence
|
| 410 |
+
source_tree = self.analyze_source(preprocessed)
|
| 411 |
+
print("English parse tree:")
|
| 412 |
+
source_tree.pretty_print() # Display English parse tree
|
| 413 |
+
|
| 414 |
+
# Step 3: Transform to Vietnamese structure
|
| 415 |
+
target_tree = self.transfer_grammar(source_tree)
|
| 416 |
+
print("Vietnamese structure tree:")
|
| 417 |
+
target_tree.pretty_print() # Display Vietnamese parse tree
|
| 418 |
+
|
| 419 |
+
# Step 4: Generate final translation
|
| 420 |
+
raw_output = self.generate(target_tree)
|
| 421 |
+
vietnamese_output = self._post_process_vietnamese(raw_output)
|
| 422 |
+
return vietnamese_output
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
if __name__ == "__main__":
|
| 426 |
+
translator = TransferBasedMT()
|
| 427 |
+
test_sentences = [
|
| 428 |
+
"I read books.", "The student studies at school.",
|
| 429 |
+
"She has a beautiful house.", "They want to buy a new car.",
|
| 430 |
+
"This is a good computer.", "Are you ready to listen?",
|
| 431 |
+
"I want to eat.", "This is my book.","What is your name?",
|
| 432 |
+
"Do you like books?",
|
| 433 |
+
"Is she at school?",
|
| 434 |
+
"Are you ready to listen?",
|
| 435 |
+
"Can they buy a new car?",
|
| 436 |
+
"Did he read the book yesterday?",
|
| 437 |
+
"What is your name?",
|
| 438 |
+
"Where do you live?",
|
| 439 |
+
"Who is your teacher?",
|
| 440 |
+
"When will you go to school?",
|
| 441 |
+
"Why did he leave early?",
|
| 442 |
+
"How do you feel today?",
|
| 443 |
+
"I live in New York"
|
| 444 |
+
]
|
| 445 |
+
|
| 446 |
+
test_sentences_2 = [
|
| 447 |
+
# YNQ -> BE NP
|
| 448 |
+
"Is the renowned astrophysicist still available for the conference?",
|
| 449 |
+
"Are those adventurous explorers currently in the remote jungle?",
|
| 450 |
+
"Was the mysterious stranger already gone by midnight?",
|
| 451 |
+
# YNQ -> BE NP Adj
|
| 452 |
+
"Is the vibrant annual festival exceptionally spectacular this season?",
|
| 453 |
+
"Are the newly discovered species remarkably resilient to harsh climates?",
|
| 454 |
+
"Were the ancient ruins surprisingly well-preserved after centuries?",
|
| 455 |
+
# YNQ -> BE NP NP
|
| 456 |
+
"Is she the brilliant leader of the innovative research team?",
|
| 457 |
+
"Are they the enthusiastic organizers of the grand charity event?",
|
| 458 |
+
"Was he the sole survivor of the perilous expedition?",
|
| 459 |
+
# YNQ -> BE NP PP
|
| 460 |
+
"Is the priceless artifact still hidden in the ancient underground chamber?",
|
| 461 |
+
"Are the colorful tropical birds nesting high above the lush rainforest canopy?",
|
| 462 |
+
"Was the historic manuscript carefully stored within the fortified library vault?"
|
| 463 |
+
]
|
| 464 |
+
|
| 465 |
+
print("English to Vietnamese Translation Examples:")
|
| 466 |
+
print("-" * 50)
|
| 467 |
+
for sentence in test_sentences_2:
|
| 468 |
+
print(f"English: {sentence}")
|
| 469 |
+
translation = translator.translate(sentence)
|
| 470 |
+
print(f"Vietnamese: {translation}")
|
| 471 |
+
print()
|