reverting to python2

This commit is contained in:
rjawor 2019-05-21 15:02:27 +02:00
parent 3163720fef
commit e1fbd87b51

View File

@ -1,12 +1,11 @@
#!/usr/bin/python3 #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import json import json
import requests import urllib2
import sys import sys
import host import host
import time import time
import codecs
BUFFER_SIZE = 500 BUFFER_SIZE = 500
LEAVE_OUT = 1 # that does not leave out anything LEAVE_OUT = 1 # that does not leave out anything
@ -14,8 +13,6 @@ LEAVE_OUT = 1 # that does not leave out anything
address = 'http://'+host.concordia_host address = 'http://'+host.concordia_host
if len(host.concordia_port) > 0: if len(host.concordia_port) > 0:
address += ':'+host.concordia_port address += ':'+host.concordia_port
headers = {"content-type" : "application/json;charset=UTF-8" }
def file_len(fname): def file_len(fname):
@ -25,7 +22,10 @@ def file_len(fname):
return i + 1 return i + 1
def add_examples(examplesData): def add_examples(examplesData):
response = requests.post(address, data=json.dumps(examplesData, ensure_ascii=False).encode('utf-8'), headers=headers).json() req = urllib2.Request(address)
req.add_header('Content-Type', 'application/json')
response = json.loads(urllib2.urlopen(req, json.dumps(examplesData), timeout = 3600).read())
print(response)
if response['status'] == 'error': if response['status'] == 'error':
raise Exception(response['message']) raise Exception(response['message'])
@ -62,10 +62,12 @@ data = {
'tmLemmatized':True 'tmLemmatized':True
} }
response = requests.post(address, json=data, headers=headers).json() req = urllib2.Request(address)
req.add_header('Content-Type', 'application/json')
response = json.loads(urllib2.urlopen(req, json.dumps(data), timeout = 3600).read())
print(response) print(response)
tmId = int(response['newTmId']) tmId = int(response['newTmId'])
print("Added new tm: %d" % tmId) print "Added new tm: %d" % tmId
data = { data = {
'operation': 'addSentences', 'operation': 'addSentences',
@ -74,7 +76,7 @@ data = {
examples = [] examples = []
start = time.time() start = time.time()
with codecs.open(sourceFile, "r", "utf-8", errors='replace') as source_file, codecs.open(lemmatizedSourceFile, "r", "utf-8", errors='replace') as lemmatized_source_file, codecs.open(targetFile, "r", "utf-8", errors='replace') as target_file, open(alignmentsFile) as alignments_file, open(sourceIdsFile) as source_ids_file: with open(sourceFile) as source_file, open(lemmatizedSourceFile) as lemmatized_source_file, open(targetFile) as target_file, open(alignmentsFile) as alignments_file, open(sourceIdsFile) as source_ids_file:
addedCount = 0 addedCount = 0
for lineNumber in range(sourceFileLength): for lineNumber in range(sourceFileLength):
if lineNumber % LEAVE_OUT == 0: if lineNumber % LEAVE_OUT == 0:
@ -90,7 +92,7 @@ with codecs.open(sourceFile, "r", "utf-8", errors='replace') as source_file, cod
data['examples'] = examples data['examples'] = examples
add_examples(data) add_examples(data)
mark = time.time() mark = time.time()
print("Added %d of %d lemmatized examples. Time elapsed: %.4f s, current speed: %.4f examples/second" % (addedCount, totalExamples, mark-start, addedCount/(mark-start))) print "Added %d of %d lemmatized examples. Time elapsed: %.4f s, current speed: %.4f examples/second" % (addedCount, totalExamples, mark-start, addedCount/(mark-start))
examples = [] examples = []
@ -99,15 +101,17 @@ if len(examples) > 0:
add_examples(data) add_examples(data)
end = time.time() end = time.time()
print("Added all %d lemmatized sentences. Time elapsed: %.4f s, overall speed: %.4f sentences/second" % (addedCount, end-start, addedCount/(end-start))) print "Added all %d lemmatized sentences. Time elapsed: %.4f s, overall speed: %.4f sentences/second" % (addedCount, end-start, addedCount/(end-start))
print("Generating index...") print "Generating index..."
start = time.time() start = time.time()
data = { data = {
'operation': 'refreshIndex', 'operation': 'refreshIndex',
'tmId' : tmId 'tmId' : tmId
} }
requests.post(address, json=data, headers=headers) req = urllib2.Request(address)
req.add_header('Content-Type', 'application/json')
urllib2.urlopen(req, json.dumps(data), timeout = 3600).read()
end = time.time() end = time.time()
print("Index regeneration complete. The operation took %.4f s" % (end - start)) print "Index regeneration complete. The operation took %.4f s" % (end - start)