Exemplo n.º 1
0
def trad(request):
    form = Input_trad()
    
    if request.method == 'POST':
        form = Input_trad(request.POST)
        if form.is_valid():
            print 'Isto é um form: ', form
            request.session['id_frase'] = request.POST
            return HttpResponseRedirect('/app/result/')
    else:
        form = Input_trad()
        
    return render(request, 'app/trad.html', {'form': form})
Exemplo n.º 2
0
def trad(request):
    print "cona"
    form = Input_trad()
    #print form
    print request.method
    print 'Cassss'
    if request.method == 'POST':
        print 'cenassssssssssssssssssssss'
        form = Input_trad(request.POST)
        if form.is_valid():
            request.session['id_frase'] = request.POST
            print request.POST['frase']
            #codigo para traduzir
            form = Input_trad(initial={'frase': request.POST['frase'], 'resultado': 'Tradução'})
            #return render(request, 'app/index.html', {'form': form})
    else:
        print 'geeeeet'
        form = Input_trad()

    return render(request, 'app/index.html', {'form': form})
Exemplo n.º 3
0
def trad(request):
    
    form = Input_trad()
            
    best_trans_list = []        #lista com as best translations (em bruto)
    best_list = []              #lista com as best translations (so' frases)
    #print request.method

    if request.method == 'POST':
        #print 'POST!'
        form = Input_trad(request.POST)
        if form.is_valid():
            request.session['id_frase'] = request.POST
            #print request.POST['frase']
            print request.POST
            if 'button' in request.POST:
                print request.POST['button']
                c = request.POST['button']

            #print request.session.get('id_frase').get('frase')
            #codigo para traduzir
            str_input = request.POST['frase']
            working_str = 'working_' + c
            #print str_input
            
            bashCommand = 'echo "' + str_input \
                    + '" | ~/mosesdecoder/bin/moses -f ~/' \
                    + working_str \
                    + '/binarised-model/moses.ini -n-best-list \
                    ~/best_list.txt 10 distinct > log.txt'
        
            process = subprocess.Popen(bashCommand, 
                                       stdout=subprocess.PIPE, shell=True)
        
            output = process.communicate()[0]
        
            #print output
            f = open('/home/nelson/best_list.txt')
        
            line = f.readline()
        
            while line:
        
                best_trans_list.append(line)
                tokens = line.split()
                str = ''
        
                ''' retira os simbolos nao necessarios
                ficando só a frase que interessa
                '''
                for tok in tokens[2:]:
        
                    if tok == '|||':
                        #print "|||"
                        break
                    else:
                        str += tok + " "
        
                best_list.append(str)
                #print str
                line = f.readline()
        
            #print best_list[1]
            f.close()
            
            p = open('frase', 'w')
            p.write(str_input.encode('utf8'))
            f.close()
            
            form = Input_trad(initial={'frase': request.POST['frase'], \
                                       'resultado': best_list[0]})
            
    else:
        #print 'GET!'
        form = Input_trad()

    return render(request, 'app/index.html', {'form': form, \
                                              'best_list': best_list[1:]})