File size: 7,759 Bytes
158b61b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
#! /usr/bin/env python
# -*- coding: utf8 -*-

#===============================================================================
# Author: Walapa Muangjeen
#===============================================================================


__version__ = '2.0'

import sys
import os
import codecs
import ConfigParser
from optparse import OptionParser
from copy import deepcopy


class makemteval:

	def __init__(self, config=None):

		if isinstance(config,dict):
			self.config = deepcopy(config)
		else:
			self.config = {
				'filein': None,
				'fileout': None,
				'settype': None,
				'srclang': None,
				'tstlang': None,
				'setid': 'SetID',
				'refid': 'RefID',
				'sysid': 'SysID',
				'docid': 'DocID',
				'genre': 'Genre',
				}
 
 
	def parseini(self, config=None, inifile=None, section='set'):

		if inifile is None:
			inifile = os.path.abspath(os.path.dirname(sys.argv[0])) + os.sep + os.path.splitext(os.path.basename(sys.argv[0]))[0] + '.ini'

		if config is None:
			config = self.config

		cfgparser = ConfigParser.RawConfigParser()

		if not cfgparser.has_section(section):
			cfgparser.add_section(section)

		for option in config:
			cfgparser.set(section, option, config[option])

		cfgparser.read(inifile)

		for option in cfgparser.options(section):
			config[option] = cfgparser.get(section, option)

		return deepcopy(config)


	def writesgm( self, config ):

		try:
			filein = codecs.open(os.path.abspath(os.path.expanduser(config['filein'])), "r", 'utf-8-sig')
		except IOError, ErrorMessage:
			sys.stderr.write("\n: %s\n"%(ErrorMessage))
			sys.stderr.write(": End Program\n")
			return True

		if __name__ == "__main__":
			sys.stderr.write( ": opened \"%s\" for reading\n"%(os.path.basename( config['filein'] )))

		lines = [l.replace('&quot;','\"').replace('&apos;','\'').replace('&gt;','>').replace('&lt;','<').replace('&amp;','&') for l in filein.read().splitlines()]
		filein.close()
		lines = [l.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;').replace('\'','&apos;').replace('\"','&quot;') for l in lines]

		if __name__ == "__main__":
			sys.stderr.write(": closed \"%s\"\n"%(os.path.basename( config['filein'] )))

		try:
			fileout = codecs.open(os.path.abspath(os.path.expanduser(config['fileout'])), "w", 'utf8')
		except IOError, ErrorMessage:
			sys.stderr.write("\n: %s\n"%(ErrorMessage))
			sys.stderr.write(": End Program\n")
			return True

		if __name__ == "__main__":
			sys.stderr.write(": opened \"%s\" for writing\n"%(os.path.basename( config['fileout'] )))

		contents = []
		contents.append('<?xml version=\"1.0\" encoding=\"UTF-8\"?>')
		contents.append('<!DOCTYPE mteval SYSTEM \"ftp://jaguar.ncsl.nist.gov/mt/resources/mteval-xml-v1.3.dtd\">')
		contents.append('<mteval>')

		if config['settype'] == "srcset":
			contents.append("<%(settype)s setid=\"%(setid)s\" srclang=\"%(srclang)s\">"%(config))

		elif config['settype'] == "refset":
			contents.append('<%(settype)s setid=\"%(setid)s\" srclang=\"%(srclang)s\" trglang=\"%(tstlang)s\" refid=\"%(refid)s\">'%(config))

		elif config['settype'] == "tstset":
			contents.append('<%(settype)s setid=\"%(setid)s\" srclang=\"%(srclang)s\" trglang=\"%(tstlang)s\" sysid=\"%(sysid)s\" sysbleu=\"%(sysbleu)s\" language=\"%(language)s\">'%(config))

		else:
			fileout.close()
			os.unlink(os.path.abspath(os.path.expanduser(config['fileout'])))
			sys.stderr.write("\n: Invalid \"settype\" value %s\n"%(config['settype']))
			sys.stderr.write(": End Program\n")
			return True

		contents.append('<DOC %sdocid=\"%s\" genre=\"%s\">'%('' if config['settype'] == "srcset" else 'sysid=\"%s\" '%(config['sysid']),config['docid'],config['genre']))

		if __name__ == "__main__":
			sys.stderr.write(": added header\n")

		for i in range(len(lines)):
			contents.append('<seg id=\"%d\"> %s </seg>'%(i+1,lines[i]))

		if __name__ == "__main__":
			sys.stderr.write(": added %d lines\n"%(i+1))

		contents.append('</DOC>')
		contents.append('</%s>'%(config['settype']))
		contents.append('</mteval>')

		if __name__ == "__main__":
			sys.stderr.write(": added footer\n")

		fileout.write('%s\n'%('\n'.join(contents)))
		ferror = fileout.close()

		if __name__ == "__main__":
			sys.stderr.write(": closed \"" +  os.path.basename( config['fileout'] ) + "\"\n")

		return ferror


def parsecmd( config = {} ):

	optparser = OptionParser()

	optparser.add_option(
		"-i", "--filein", dest = "filein", default = config["filein"],
		help = "UNC path to tokenized input file (required)")

	optparser.add_option(
		"-o", "--fileout", dest = "fileout", default = config["fileout"],
		help = "UNC path of fileout file (required)")

	optparser.add_option(
		"-s", "--srclang", dest = "srclang", default = config["srclang"],
		help = "2-letter code for source language (required)")

	optparser.add_option(
		"-t", "--tstlang", dest = "tstlang", default = config["tstlang"],
		help = "2-letter code for test language (required)")

	optparser.add_option(
		"-T", "--settype", dest = "settype", default = config["settype"],
		help = "Use XML tag: srcset, tstset or refset (required)")

	optparser.add_option(
		"-e", "--setid", dest = "setid", default = config["setid"],
		help = "Test set ID (default \""+config["setid"]+"\")")

	optparser.add_option(
		"-d", "--docid", dest = "docid", default = config["docid"],
		help = "Document ID (default \""+config["docid"]+"\")")

	optparser.add_option(
		"-r", "--refid", dest = "refid", default = config["refid"],
		help = "Reference ID (default \""+config["refid"]+"\")")

	optparser.add_option(
		"-S", "--sysid", dest = "sysid", default = config["sysid"],
		help = "System ID used to make the test set (default \""+config["sysid"]+"\")")

	optparser.add_option(
		"-g", "--genre", dest = "genre", default = config["genre"],
		help = "Genre of the test set and system ID (default \""+config["genre"]+"\")")

	options, commands = optparser.parse_args()

	missing = []
	for k,v in {	"Error: missing --filein" : options.filein, 
			"Error: missing --fileout": options.fileout, 
			"Error: missing --settype": options.settype, 
			"Error: missing --srclang": options.srclang, 
			"Error: missing --tstlang": options.tstlang
								}.items():
		if not v:
			missing.append(k)

	if missing:
		for msg in missing:
			sys.stderr.write('%s\n'%(msg))
		optparser.print_help()
		exit(1)

	config['filein'] = options.filein
	config['fileout'] = options.fileout
	config['settype'] = options.settype
	config['setid'] = options.setid
	config['srclang'] = options.srclang
	config['tstlang'] = options.tstlang
	config['refid'] = options.refid
	config['sysid'] = options.sysid
	config['docid'] = options.docid
	config['genre'] = options.genre

	sys.stderr.write(": Configuration complete\n")

	return


licensetxt=u'''CorpusFiltergraph™
Copyright © 2010-2014 Precision Translation Tools Co., Ltd.

This module is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see http://www.gnu.org/licenses/.

For more information, please contact Precision Translation Tools Pte
at: http://www.precisiontranslationtools.com'''


def main():

	mksgm = makemteval()

	mksgm.parseini(mksgm.config)

	parsecmd(mksgm.config)

	mksgm.writesgm(mksgm.config)

	return 0


if __name__ == "__main__":
	sys.exit(main())