2015-08-20 20:12:18 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import json
|
|
|
|
import urllib2
|
|
|
|
import sys
|
2015-10-20 20:16:00 +02:00
|
|
|
import host
|
2015-08-20 20:12:18 +02:00
|
|
|
import time
|
|
|
|
|
2015-08-21 08:37:54 +02:00
|
|
|
BUFFER_SIZE = 500
|
|
|
|
|
2015-10-20 20:16:00 +02:00
|
|
|
address = 'http://'+host.concordia_host
|
|
|
|
if len(host.concordia_port) > 0:
|
|
|
|
address += ':'+host.concordia_port
|
|
|
|
|
|
|
|
|
2015-08-20 20:12:18 +02:00
|
|
|
def file_len(fname):
|
|
|
|
with open(fname) as f:
|
|
|
|
for i, l in enumerate(f):
|
|
|
|
pass
|
|
|
|
return i + 1
|
|
|
|
|
|
|
|
def add_data(data):
|
2015-10-20 20:16:00 +02:00
|
|
|
req = urllib2.Request(address)
|
2015-08-20 20:12:18 +02:00
|
|
|
req.add_header('Content-Type', 'application/json')
|
|
|
|
urllib2.urlopen(req, json.dumps(data)).read()
|
|
|
|
|
|
|
|
sourceFile = sys.argv[1]
|
2015-10-20 20:16:00 +02:00
|
|
|
sourceLangId = int(sys.argv[2])
|
|
|
|
targetFile = sys.argv[3]
|
|
|
|
targetLangId = int(sys.argv[4])
|
|
|
|
name = sys.argv[5]
|
2015-08-20 20:12:18 +02:00
|
|
|
|
|
|
|
totalLines = file_len(sourceFile)
|
|
|
|
if file_len(targetFile) != totalLines:
|
|
|
|
print "File lengths do not match"
|
|
|
|
sys.exit(1)
|
2015-10-20 20:16:00 +02:00
|
|
|
|
|
|
|
data = {
|
|
|
|
'operation': 'addTm',
|
|
|
|
'sourceLangId':sourceLangId,
|
|
|
|
'targetLangId':targetLangId,
|
|
|
|
'name':name
|
|
|
|
}
|
|
|
|
|
|
|
|
req = urllib2.Request(address)
|
|
|
|
req.add_header('Content-Type', 'application/json')
|
|
|
|
response = json.loads(urllib2.urlopen(req, json.dumps(data)).read())
|
|
|
|
tmId = int(response['newTmId'])
|
|
|
|
print "Added new tm: %d" % tmId
|
|
|
|
|
2015-08-20 20:12:18 +02:00
|
|
|
|
|
|
|
data = {
|
2015-10-20 20:16:00 +02:00
|
|
|
'operation': 'addSentences',
|
|
|
|
'tmId':tmId
|
2015-08-20 20:12:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sentences = []
|
|
|
|
start = time.time()
|
2015-10-20 20:16:00 +02:00
|
|
|
with open(sourceFile) as sourceSentences:
|
|
|
|
with open(targetFile) as targetSentences:
|
2015-08-20 20:12:18 +02:00
|
|
|
lineNumber = 0
|
|
|
|
for sourceSentence in sourceSentences:
|
|
|
|
lineNumber += 1
|
|
|
|
targetSentence = targetSentences.readline()
|
2015-10-20 20:16:00 +02:00
|
|
|
sentences.append([sourceSentence, targetSentence])
|
2015-08-20 20:12:18 +02:00
|
|
|
if lineNumber % BUFFER_SIZE == 0:
|
|
|
|
data['sentences'] = sentences
|
|
|
|
sentences = []
|
|
|
|
add_data(data)
|
|
|
|
mark = time.time()
|
|
|
|
print "Added %d of %d sentences. Time elapsed: %.4f s, current speed: %.4f sentences/second" % (lineNumber, totalLines, mark-start, lineNumber/(mark-start))
|
|
|
|
|
|
|
|
|
|
|
|
if len(sentences) > 0:
|
|
|
|
data['sentences'] = sentences
|
|
|
|
add_data(data)
|
|
|
|
|
|
|
|
end = time.time()
|
|
|
|
print "Added all %d sentences. Time elapsed: %.4f s, overall speed: %.4f sentences/second" % (lineNumber, end-start, lineNumber/(end-start))
|
|
|
|
|
|
|
|
print "Generating index..."
|
|
|
|
start = time.time()
|
|
|
|
data = {
|
2015-10-20 20:16:00 +02:00
|
|
|
'operation': 'refreshIndex',
|
|
|
|
'tmId' : tmId
|
2015-08-20 20:12:18 +02:00
|
|
|
}
|
2015-10-20 20:16:00 +02:00
|
|
|
req = urllib2.Request(address)
|
2015-08-20 20:12:18 +02:00
|
|
|
req.add_header('Content-Type', 'application/json')
|
|
|
|
urllib2.urlopen(req, json.dumps(data)).read()
|
|
|
|
|
|
|
|
end = time.time()
|
|
|
|
print "Index regeneration complete. The operation took %.4f s" % (end - start)
|
|
|
|
|
|
|
|
|
|
|
|
|