Fix Python 2 syntax to be Python 3 compatible.
(file_path)
| 10 | |
| 11 | # Python 2 to Python 3 compatibility fixes |
| 12 | def fix_python2_syntax(file_path): |
| 13 | """Fix Python 2 syntax to be Python 3 compatible.""" |
| 14 | try: |
| 15 | with open(file_path, 'r', encoding='utf-8') as f: |
| 16 | content = f.read() |
| 17 | |
| 18 | # Fix xrange -> range |
| 19 | content = re.sub(r'\bxrange\b', 'range', content) |
| 20 | |
| 21 | # Fix .iteritems() -> .items() |
| 22 | content = re.sub(r'\.iteritems\(\)', '.items()', content) |
| 23 | |
| 24 | # Fix tuple unpacking in function parameters: def func(x, (a, b)) -> def func(x, tuple_arg) |
| 25 | # This is a more complex fix, need to find function definition and fix calls |
| 26 | # For cook_test function in bleu_scorer.py |
| 27 | if 'bleu_scorer.py' in str(file_path): |
| 28 | # Fix function definition |
| 29 | content = re.sub( |
| 30 | r'def cook_test\(test, \(reflen, refmaxcounts\), eff=None, n=4\):', |
| 31 | 'def cook_test(test, ref_tuple, eff=None, n=4):\n reflen, refmaxcounts = ref_tuple', |
| 32 | content |
| 33 | ) |
| 34 | |
| 35 | # Write fixed content to temporary file or execute directly |
| 36 | return content |
| 37 | except Exception as e: |
| 38 | print(f"Warning: Cannot fix file {file_path}: {e}") |
| 39 | return None |
| 40 | |
| 41 | # Add CommonGen evaluation script path |
| 42 | # Try multiple possible paths |