Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

build_sym_alignment.py 3.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the LICENSE file in
  5. # the root directory of this source tree. An additional grant of patent rights
  6. # can be found in the PATENTS file in the same directory.
  7. #
  8. """
  9. Use this script in order to build symmetric alignments for your translation
  10. dataset.
  11. This script depends on fast_align and mosesdecoder tools. You will need to
  12. build those before running the script.
  13. fast_align:
  14. github: http://github.com/clab/fast_align
  15. instructions: follow the instructions in README.md
  16. mosesdecoder:
  17. github: http://github.com/moses-smt/mosesdecoder
  18. instructions: http://www.statmt.org/moses/?n=Development.GetStarted
  19. The script produces the following files under --output_dir:
  20. text.joined - concatenation of lines from the source_file and the
  21. target_file.
  22. align.forward - forward pass of fast_align.
  23. align.backward - backward pass of fast_align.
  24. aligned.sym_heuristic - symmetrized alignment.
  25. """
  26. import argparse
  27. import os
  28. from itertools import zip_longest
  29. def main():
  30. parser = argparse.ArgumentParser(description='symmetric alignment builer')
  31. parser.add_argument('--fast_align_dir',
  32. help='path to fast_align build directory')
  33. parser.add_argument('--mosesdecoder_dir',
  34. help='path to mosesdecoder root directory')
  35. parser.add_argument('--sym_heuristic',
  36. help='heuristic to use for symmetrization',
  37. default='grow-diag-final-and')
  38. parser.add_argument('--source_file',
  39. help='path to a file with sentences '
  40. 'in the source language')
  41. parser.add_argument('--target_file',
  42. help='path to a file with sentences '
  43. 'in the target language')
  44. parser.add_argument('--output_dir',
  45. help='output directory')
  46. args = parser.parse_args()
  47. fast_align_bin = os.path.join(args.fast_align_dir, 'fast_align')
  48. symal_bin = os.path.join(args.mosesdecoder_dir, 'bin', 'symal')
  49. sym_fast_align_bin = os.path.join(
  50. args.mosesdecoder_dir, 'scripts', 'ems',
  51. 'support', 'symmetrize-fast-align.perl')
  52. # create joined file
  53. joined_file = os.path.join(args.output_dir, 'text.joined')
  54. with open(args.source_file, 'r') as src, open(args.target_file, 'r') as tgt:
  55. with open(joined_file, 'w') as joined:
  56. for s, t in zip_longest(src, tgt):
  57. print('{} ||| {}'.format(s.strip(), t.strip()), file=joined)
  58. bwd_align_file = os.path.join(args.output_dir, 'align.backward')
  59. # run forward alignment
  60. fwd_align_file = os.path.join(args.output_dir, 'align.forward')
  61. fwd_fast_align_cmd = '{FASTALIGN} -i {JOINED} -d -o -v > {FWD}'.format(
  62. FASTALIGN=fast_align_bin,
  63. JOINED=joined_file,
  64. FWD=fwd_align_file)
  65. assert os.system(fwd_fast_align_cmd) == 0
  66. # run backward alignment
  67. bwd_align_file = os.path.join(args.output_dir, 'align.backward')
  68. bwd_fast_align_cmd = '{FASTALIGN} -i {JOINED} -d -o -v -r > {BWD}'.format(
  69. FASTALIGN=fast_align_bin,
  70. JOINED=joined_file,
  71. BWD=bwd_align_file)
  72. assert os.system(bwd_fast_align_cmd) == 0
  73. # run symmetrization
  74. sym_out_file = os.path.join(args.output_dir, 'aligned')
  75. sym_cmd = '{SYMFASTALIGN} {FWD} {BWD} {SRC} {TGT} {OUT} {HEURISTIC} {SYMAL}'.format(
  76. SYMFASTALIGN=sym_fast_align_bin,
  77. FWD=fwd_align_file,
  78. BWD=bwd_align_file,
  79. SRC=args.source_file,
  80. TGT=args.target_file,
  81. OUT=sym_out_file,
  82. HEURISTIC=args.sym_heuristic,
  83. SYMAL=symal_bin
  84. )
  85. assert os.system(sym_cmd) == 0
  86. if __name__ == '__main__':
  87. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...