Esempio n. 1
0
def translate():
    pool = mp.Pool(mp.cpu_count())
    basename = str(int(time.time()))
    current_time = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
    f = request.files['file']
    filepath = os.path.join(
        app.config['UPLOAD_FOLDER'], basename + '.pdf')
    translationProcess = TranslationProcess(
        status=STATUS_PROCESSING, name=f.filename, created_on=current_time, basename=basename)
    translationProcess.save()
    f.save(filepath)
    pool.apply_async(converttoimage, args=(
        filepath, app.config['UPLOAD_FOLDER'], basename, '_hin'), callback=capturetext)
    pool.close()
    pool.join()
    filtertext(app.config['UPLOAD_FOLDER'] + '/' + basename + '_hin.txt',
               app.config['UPLOAD_FOLDER'] + '/' + basename + '_hin_filtered.txt')
    processenglish(app.config['UPLOAD_FOLDER'] +
                   '/' + basename + '_hin_filtered.txt')
    translatewithanuvadaeng(app.config['UPLOAD_FOLDER'] +
                            '/' + basename + '_hin_filtered.txt', app.config['UPLOAD_FOLDER'] +
                            '/' + basename + '_eng_tran.txt')
    f_eng = open(app.config['UPLOAD_FOLDER'] + '/' +
                 basename + '_eng_tran.txt', 'r')
    english_res = []
    hindi_res = []
    for f in f_eng:
        english_res.append(f)
    f_eng.close()
    f_hin = open(app.config['UPLOAD_FOLDER'] + '/' +
                 basename + '_hin_filtered.txt', 'r')
    for f in f_hin:
        hindi_res.append(f)
    f_hin.close()
    data = {'hindi': hindi_res, 'english': english_res}
    translations = []
    for i in range(0, len(hindi_res)):
        translation = Translation(basename=str(
            basename), source=hindi_res[i], target=english_res[i])
        translations.append(translation)
    Translation.objects.insert(translations)
    for f in glob.glob(app.config['UPLOAD_FOLDER'] + '/' + basename + '*'):
        os.remove(f)
    res = CustomResponse(Status.SUCCESS.value, data)
    translationProcess = TranslationProcess.objects(basename=basename)
    translationProcess.update(set__status=STATUS_PROCESSED)
    return res.getres()
    line_count = 0

    for line in f:

        if not line.strip():
            print('file end')
            break

        # print(line)
        surah_number, ayat_number, *line_text = line.strip().split('|')
        text = ' '.join(line_text)
        print('For File ' + edition_id + ' writing line...' + str(line_count))

        trans_id = surah_number + '-' + ayat_number

        translation = Translation()
        translation.id = edition_id + "_" + trans_id
        translation.ayah_id = trans_id
        translation.edition_id = edition_id
        translation.text = text.strip()
        translation.ayah_number = line_count
        translation.save(batch=trans_batch)

        line_count += 1
        count += 1

        if (count >= 400):
            trans_batch.commit()
            count = 0

    print('============Complete=============================')
Esempio n. 3
0
def main():
    args = parse_args()

    # Initialize
    dbmng = DBManager(args.db)

    # Only one session to access to database
    _session = dbmng.get_session()

    # Open the file
    decorder = json.JSONDecoder(object_pairs_hook=collections.OrderedDict)
    input_f = open(args.input_file, 'r')
    input_j = decorder.decode(input_f.read())

    # Load json keys and make it inserted
    nowtime = datetime.datetime.now()

    for key in input_j.keys():
        if key == 'translations':
            for dic in input_j[key]:
                rec = Translation(id=dic['id'],
                                  locale=dic['locale'],
                                  t=dic['t'],
                                  created_at=nowtime,
                                  created_by='rightctl_initializer')
                _session.add(rec)
            else:
                _session.commit()

        elif key == 'rights':
            for dic in input_j[key]:
                rec = Right(action=dic['action'],
                            enable_flag=True,
                            created_at=nowtime,
                            created_by='rightctl_initializer')
                _session.add(rec)
            else:
                _session.commit()

        elif key == 'policies':
            for dic in input_j[key]:
                rec = Policy(id=dic['id'],
                             policy_tid=dic['policy_tid'],
                             created_at=nowtime,
                             created_by='rightctl_initializer')
                _session.add(rec)
                _session.commit()
                for right_s in dic['rights']:
                    right = _session.query(Right).filter(
                        Right.action == right_s).one()
                    child = PolicyHasRight(policy_id=rec.id, right_id=right.id)
                    _session.add(child)
                else:
                    _session.commit()
            else:
                _session.commit()

        elif key == 'roles':
            for dic in input_j[key]:
                rec = Role(id=dic['id'],
                           role_tid=dic['role_tid'],
                           created_at=nowtime,
                           created_by='rightctl_initializer')
                _session.add(rec)
                _session.commit()
                for policy_s in dic['policies']:
                    policy = _session.query(Policy).filter(
                        Policy.id == policy_s).one()
                    child = RoleHasPolicy(role_id=rec.id, policy_id=policy.id)
                    _session.add(child)
                else:
                    _session.commit()
            else:
                _session.commit()

        else:
            None
Esempio n. 4
0
def translateFile():
    pool = mp.Pool(mp.cpu_count())
    basename = str(int(time.time()))
    current_time = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
    f = request.files['file']
    filepath = os.path.join(
        app.config['UPLOAD_FOLDER'], basename + '.pdf')
    translationProcess = TranslationProcess(
        status=STATUS_PROCESSING, name=f.filename, created_on=current_time, basename=basename)
    translationProcess.save()
    f.save(filepath)
    pool.apply_async(converttoimage, args=(
        filepath, app.config['UPLOAD_FOLDER'], basename, '_hin'), callback=capturetext)
    pool.close()
    pool.join()
    filtertext(app.config['UPLOAD_FOLDER'] + '/'+basename+'_hin.txt',
               app.config['UPLOAD_FOLDER'] + '/'+basename+'_hin_filtered.txt')
    processenglish(app.config['UPLOAD_FOLDER'] +
                 '/'+basename+'_hin_filtered.txt')
    translatewithanuvadaeng(app.config['UPLOAD_FOLDER'] +
                         '/'+basename+'_hin_filtered.txt', app.config['UPLOAD_FOLDER'] +
                         '/'+basename+'_eng_tran.txt')
    f_eng = open(app.config['UPLOAD_FOLDER']+'/' +
                 basename + '_eng_tran.txt', 'r')
    english_res = []
    hindi_res = []
    for f in f_eng:
        english_res.append(f)
    f_eng.close()
    f_hin = open(app.config['UPLOAD_FOLDER']+'/' +
                 basename + '_hin_filtered.txt', 'r')
    index = 0
    previousY = 0
    previousX = 0
    previousH = 0
    previousP = ''
    text_y = {}
    text_x = 0
    for f in f_hin:
        hindi_res.append(f)
        print(f)
        point = fetchwordhocrfromsentence(f, basename)
        english = english_res[index]
        words = english.split(' ')
        wordIndex = 0
        
        for word in words:
            try:
                if point['values'] is not None and point['values'][wordIndex] is not None and point['values'][wordIndex]['height'] is not None:
                    previousY = point['values'][wordIndex]['left']
                    previousX = point['values'][wordIndex]['top']
                    previousH = point['values'][wordIndex]['height']
                    try:
                        if text_y[point['values'][wordIndex]['imagepath']] is None:
                            text_y[point['values'][wordIndex]['imagepath']] = 200
                    except Exception as e:
                        text_y[point['values'][wordIndex]['imagepath']] = 200
                    (text_x, vertical) = puttext(point['values'][wordIndex]['height'],200,text_y[point['values'][wordIndex]['imagepath']],english,point['values'][wordIndex]['imagepath'])
                    text_y[point['values'][wordIndex]['imagepath']] = vertical
                    # else:
                    #     (text_x, text_y) = puttext(point['values'][wordIndex]['height'],point['values'][wordIndex]['left'],point['values'][wordIndex]['top'],english,point['values'][wordIndex]['imagepath'])
                    previousP = point['values'][wordIndex]['imagepath']
                    break
            except Exception as e:
                previousY = previousY + 200
                # puttext(previousH,previousY,previousX,word,previousP)
            wordIndex = wordIndex + 1
            # puttext(point['values'][wordIndex]['left'],point['values'][wordIndex]['top'],word,point['values'][wordIndex]['imagepath'])
        index = index + 1
    f_hin.close()
    data = {'hindi': hindi_res, 'english': english_res}
    translations = []
    for i in range(0, len(hindi_res)):
        translation = Translation(basename=str(
            basename), source=hindi_res[i], target=english_res[i])
        translations.append(translation)
    Translation.objects.insert(translations)
    # for f in glob.glob(app.config['UPLOAD_FOLDER']+'/'+basename+'*'):
    #     os.remove(f)
    res = CustomResponse(Status.SUCCESS.value, data)
    translationProcess = TranslationProcess.objects(basename=basename)
    translationProcess.update(set__status=STATUS_PROCESSED)
    return res.getres()