Exemple #1
0
def make_eq(q, a, equations):
    wps = q  #open(q).readlines()
    answs = a  #open(a).readlines()
    right = 0
    wrong = 0

    for k in range(len(wps)):
        answers = get_k_eqs(equations[k], g=True, a=True)
        seeneq = []
        seen = []
        for x in answers:
            if x[1] not in seeneq:
                seen.append(x)
                seeneq.append(x[1])
        answers = seen
        answers = list(set(answers))

        #First preprocessing, tokenize slightly
        problem = wps[k]  #.lower()
        problem = problem.strip().split(" ")
        for i, x in enumerate(problem):
            if len(x) == 0: continue
            if x[-1] in [',', '.', '?']:
                problem[i] = x[:-1] + " " + x[-1]
        problem = ' '.join(problem)
        problem = " " + problem + " "
        #print(equations[k])
        #print(problem)
        if len(answers) == 0:
            print("0 Answers \n" + str(equations[k]) + " INCORRECT")
            wrong += 1
            continue

        #make story
        story = read_parse(int(equations[k]))
        #sets = makesets.makesets(story['sentences'])
        sets = read_sets(int(equations[k]))
        i = 0

        for x in sets:
            x[1].details()

        xidx = [i for i, x in enumerate(sets) if x[1].num == 'x']
        if not xidx:
            print(str(equations[k]) + " INCORRECT NO X WHY")
            wrong += 1
            continue

        xidx = xidx[0]

        numlist = [(cleannum(v.num), v) for k, v in sets]
        numlist = [x for x in numlist if x[0] != '']
        allnumbs = {str(k): v for k, v in numlist}
        objs = {k: (0, v) for k, v in numlist}
        #print(objs.items())
        consts = [
            x for x in answers[0][1].split(" ") if x not in [
                '(',
                ')',
                '+',
                '-',
                '/',
                '*',
                '=',
            ]
        ]
        present = [x for x in consts if x in objs]
        if consts != present:
            print(present, consts)
            print(str(equations[k]) + " INCORRECT missing thing")
            wrong += 1
            continue
        if len([x for x in objs if x not in consts]) > 0:
            print(str(equations[k]) + " INCORRECT missing thing")
            wrong += 1
            continue
        scores = []

        for j, eq, cons, guess in answers:
            consts = [
                x for x in eq.split(" ") if x not in [
                    '(',
                    ')',
                    '+',
                    '-',
                    '/',
                    '*',
                    '=',
                ]
            ]
            order = int(consts == [x[0] for x in numlist])
            if order == 0: continue
            #j = randint(0,len(answers)-1)
            #eq = answers[j]
            trips = []
            #print(j,eq)
            l, r = [x.strip().split(' ') for x in eq.split('=')]
            consts = " ".join([
                x for x in answers[0][1].split(" ") if x not in [
                    '(',
                    ')',
                    '+',
                    '-',
                    '/',
                    '*',
                ]
            ])
            consts = consts.split(" = ")
            sp = (objs[consts[0].split(" ")[-1]],
                  objs[consts[1].split(" ")[0]])

            target = 'x'
            target = (target, objs[target])

            #find innermost parens?
            #print(eq)
            sides = []
            thisscore = []
            for i, compound in enumerate([l, r]):
                while len(compound) > 1:
                    if "(" in compound:
                        rpidx = (len(compound) - 1) - compound[::-1].index('(')
                        lpidx = rpidx + compound[rpidx:].index(")")
                        subeq = compound[rpidx + 1:lpidx]
                        substr = "(" + ''.join(subeq) + ")"
                        compound = compound[:rpidx] + [substr
                                                       ] + compound[lpidx + 1:]
                    else:
                        subeq = compound[0:3]
                        substr = "(" + ''.join(subeq) + ")"
                        compound = [substr] + compound[3:]
                    p, op, e = subeq
                    p = objs[p]
                    e = objs[e]
                    op = op.strip()
                    pute = compute(p, op, e, target, problem, story, order)
                    objs[substr] = pute
                    if pute == -1:
                        exit()
                    score, c, vals = pute
                    thisscore.append(score)
                    #print(subeq,score)
                sides.append(objs[compound[0]])
            p = sides[0]
            e = sides[1]
            score = 1
            for s in thisscore:
                score *= s
            gscore = compute(p, '=', e, target, problem, story, order, score,
                             cons)[0]
            #print("gscore ",gscore)
            score *= gscore
            scores.append((score, j, eq, guess))
        scores = sorted(scores, reverse=True)
        righties = [x for x in scores if x[1] == 1]
        #print(scores[:3])
        if not righties:
            wrong += 1
            print("TOP SCORING NO CORRECT SOLUTION \n," + str(equations[k]) +
                  " INCORRECT")
            continue
        else:
            corr = righties[0][3]

        if len(scores) > 0:
            if scores[0][1] == 1:
                right += 1
                print(str(equations[k]) + " CORRECT")
            else:
                wrong += 1
                print(str(equations[k]) + " INCORRECT")
        else:
            wrong += 1
            print(str(equations[k]) + " INCORRECT")

    return (right, wrong)
Exemple #2
0
def make_eq(q, a, equations):
    tdata = []
    wps = q  #open(q).readlines()
    answs = a  #open(a).readlines()

    for k in range(len(wps)):
        print(k, equations[k])
        answers = get_k_eqs(equations[k], g=True)
        good = list(set([x for x in answers if x[0] == 1]))
        bad = list(set([x for x in answers if x[0] == 0]))[:len(good)]
        '''
        if len(bad)>len(good):
            bad = sample(bad,len(good))
        '''
        answers = good + bad
        if answers == []: continue
        answers = list(set(answers))

        #First preprocessing, tokenize slightly
        problem = wps[k]  #.lower()
        problem = problem.strip().split(" ")
        for i, x in enumerate(problem):
            if len(x) == 0: continue
            if x[-1] in [',', '.', '?']:
                problem[i] = x[:-1] + " " + x[-1]
        problem = ' '.join(problem)
        problem = " " + problem + " "
        print(problem)

        #make story
        story = nlp.parse(problem)
        sets = makesets.makesets(story['sentences'])
        i = 0

        xidx = [i for i, x in enumerate(sets) if x[1].num == 'x']
        if not xidx:
            print("NO X WHY")
            continue

        #TODO look for 2 xes
        xidx = xidx[0]

        numlist = [(cleannum(v.num), v) for k, v in sets]
        numlist = [x for x in numlist if x[0] != '']
        allnumbs = {str(k): v for k, v in numlist}
        objs = {k: (0, v) for k, v in numlist}
        print(numlist)
        consts = [
            x for x in answers[0][1].split(" ") if x not in [
                '(',
                ')',
                '+',
                '-',
                '/',
                '*',
                '=',
            ]
        ]
        print(consts)
        present = [x for x in consts if x in objs]
        if present != consts:
            print(present, consts)
            print("missing thing")
            continue

        scores = []
        print(answers)

        for j, eq, cons in answers:
            consts = [
                x for x in eq.split(" ") if x not in [
                    '(',
                    ')',
                    '+',
                    '-',
                    '/',
                    '*',
                    '=',
                ]
            ]
            order = int(consts == [x[0] for x in numlist])
            #if order == 0:continue
            trips = []
            print(j, eq)
            l, r = [x.strip().split(' ') for x in eq.split('=')]
            consts = " ".join([
                x for x in answers[0][1].split(" ") if x not in [
                    '(',
                    ')',
                    '+',
                    '-',
                    '/',
                    '*',
                ]
            ])
            consts = consts.split(" = ")

            target = 'x'
            target = (target, objs[target])

            #find innermost parens?
            sides = []
            thisscore = []
            for i, compound in enumerate([l, r]):
                while len(compound) > 1:
                    if "(" in compound:
                        rpidx = (len(compound) - 1) - compound[::-1].index('(')
                        lpidx = rpidx + compound[rpidx:].index(")")
                        subeq = compound[rpidx + 1:lpidx]
                        substr = "(" + ''.join(subeq) + ")"
                        compound = compound[:rpidx] + [substr
                                                       ] + compound[lpidx + 1:]
                    else:
                        subeq = compound[0:3]
                        substr = "(" + ''.join(subeq) + ")"
                        compound = [substr] + compound[3:]
                    p, op, e = subeq
                    p = objs[p]
                    e = objs[e]
                    op = op.strip()
                    pute = compute(p, op, e, target, problem, story, order)
                    objs[substr] = pute
                    if pute == -1:
                        exit()
                    score, c, vals = pute
                    thisscore.append(score)
                sides.append(objs[compound[0]])
            p = sides[0]
            e = sides[1]
            #thisscore.append(compute(p,'=',e,target,problem,story,order,sp)[0])
            score = 1
            for s in thisscore:
                score *= s
            #scores.append((score,j,eq))
            tdata.append(
                training(sides[0], sides[1], problem, story, target, j, order,
                         score, cons))

    f = open("data/" + sys.argv[1][-1] + ".global.data", 'w')
    for v in tdata:
        f.write(str(v[0]) + " ")
        for i, j in enumerate(v[1:]):
            f.write(str(i + 1) + ":" + str(j) + " ")
        f.write("\n")
Exemple #3
0
def make_eq(q,a,equations):
    wps = q #open(q).readlines()
    answs = a #open(a).readlines()
    right = 0
    wrong = 0

    for k in range(len(wps)):
        answers = get_k_eqs(equations[k],g=True,a=True)
        seeneq = []
        seen = []
        for x in answers:
            if x[1] not in seeneq:
                seen.append(x)
                seeneq.append(x[1])
        answers = seen
        answers = list(set(answers))
        


        #First preprocessing, tokenize slightly
        problem = wps[k]#.lower()
        problem = problem.strip().split(" ")
        for i,x in enumerate(problem):
            if len(x)==0:continue
            if x[-1] in [',','.','?']:
                problem[i] = x[:-1]+" "+x[-1]
        problem = ' '.join(problem)
        problem = " " + problem + " "
        #print(equations[k])
        #print(problem)
        if len(answers)==0:print("0 Answers \n"+str(equations[k])+" INCORRECT"); wrong += 1; continue


        #make story
        story = read_parse(int(equations[k]))
        #sets = makesets.makesets(story['sentences'])
        sets = read_sets(int(equations[k]))
        i = 0

        for x in sets:
            x[1].details()

        xidx = [i for i,x in enumerate(sets) if x[1].num=='x']
        if not xidx:
            print(str(equations[k])+" INCORRECT NO X WHY");wrong += 1; continue

        xidx = xidx[0]


        numlist = [(cleannum(v.num),v) for k,v in sets]
        numlist = [x for x in numlist if x[0]!='']
        allnumbs = {str(k):v for k,v in numlist}
        objs = {k:(0,v) for k,v in numlist}
        #print(objs.items())
        consts = [x for x in answers[0][1].split(" ") if x not in ['(',')','+','-','/','*','=',]]
        present = [x for x in consts if x in objs]
        if consts!=present: print(present,consts);print(str(equations[k])+" INCORRECT missing thing");wrong += 1; continue
        if len([x for x in objs if x not in consts])>0: print(str(equations[k])+" INCORRECT missing thing");wrong +=1;continue
        scores = []


        for j,eq,cons,guess in answers:
            consts = [x for x in eq.split(" ") if x not in ['(',')','+','-','/','*','=',]]
            order = int(consts==[x[0] for x in numlist])
            if order == 0: continue
            #j = randint(0,len(answers)-1)
            #eq = answers[j]
            trips = []
            #print(j,eq)
            l,r = [x.strip().split(' ') for x in eq.split('=')]
            consts = " ".join([x for x in answers[0][1].split(" ") if x not in ['(',')','+','-','/','*',]])
            consts = consts.split(" = ")
            sp = (objs[consts[0].split(" ")[-1]],objs[consts[1].split(" ")[0]])
             
            target = 'x'
            target = (target,objs[target])

            #find innermost parens?
            #print(eq)
            sides = []
            thisscore = []
            for i,compound in enumerate([l,r]):
                while len(compound)>1:
                    if "(" in compound:
                        rpidx = (len(compound) - 1) - compound[::-1].index('(')
                        lpidx = rpidx+compound[rpidx:].index(")")
                        subeq = compound[rpidx+1:lpidx]
                        substr = "("+''.join(subeq)+")"
                        compound = compound[:rpidx]+[substr]+compound[lpidx+1:]
                    else:
                        subeq = compound[0:3]
                        substr = "("+''.join(subeq)+")"
                        compound = [substr]+compound[3:]
                    p,op,e = subeq
                    p = objs[p]
                    e = objs[e]
                    op = op.strip()
                    pute = compute(p,op,e,target,problem,story,order)
                    objs[substr]=pute
                    if pute == -1:
                        exit()
                    score,c,vals = pute
                    thisscore.append(score)
                    #print(subeq,score)
                sides.append(objs[compound[0]])
            p = sides[0]; e = sides[1]
            score = 1
            for s in thisscore: score *= s
            gscore = compute(p,'=',e,target,problem,story,order,score,cons)[0]
            #print("gscore ",gscore)
            score *= gscore
            scores.append((score,j,eq,guess))
        scores = sorted(scores,reverse=True)
        righties = [x for x in scores if x[1]==1]
        #print(scores[:3])
        if not righties:
            wrong+=1
            print("TOP SCORING NO CORRECT SOLUTION \n,"+str(equations[k])+" INCORRECT")
            continue
        else:
            corr = righties[0][3]


        if len(scores)>0:
            if scores[0][1]==1:
                right += 1
                print(str(equations[k])+" CORRECT")
            else:
                wrong += 1
                print(str(equations[k])+" INCORRECT")
        else:
            wrong += 1
            print(str(equations[k])+" INCORRECT")

    return (right,wrong)
Exemple #4
0
def make_eq(q, a, equations):
    tdata = []
    wps = q  #open(q).readlines()
    answs = a  #open(a).readlines()

    for k in range(len(wps)):
        answers = get_k_eqs(equations[k])
        if answers == []: continue
        answers = list(set(answers))

        #First preprocessing, tokenize slightly
        problem = wps[k]  #.lower()
        problem = problem.strip().split(" ")
        for i, x in enumerate(problem):
            if len(x) == 0: continue
            if x[-1] in [',', '.', '?']:
                problem[i] = x[:-1] + " " + x[-1]
        problem = ' '.join(problem)
        problem = " " + problem + " "
        print(problem)

        #make story
        story = nlp.parse(problem)
        sets = makesets.makesets(story['sentences'])
        i = 0

        xidx = [i for i, x in enumerate(sets) if x[1].num == 'x']
        if not xidx:
            print("NO X WHY")
            continue

        #TODO look for 2 xes
        xidx = xidx[0]

        numlist = [(cleannum(v.num), v) for k, v in sets]
        numlist = [x for x in numlist if x[0] != '']
        allnumbs = {str(k): v for k, v in numlist}
        objs = {k: (0, v) for k, v in numlist}
        print(objs.items())
        consts = [
            x for x in answers[0][1].split(" ") if x not in [
                '(',
                ')',
                '+',
                '-',
                '/',
                '*',
                '=',
            ]
        ]
        present = [x for x in consts if x in objs]
        if consts != present:
            print(present, consts)
            print("missing thing")
            continue
        order = int(consts == [x[0] for x in numlist])

        for j, eq in answers:
            #j = randint(0,len(answers)-1)
            #eq = answers[j]
            trips = []
            print(j, eq)
            l, r = [x.strip().split(' ') for x in eq.split('=')]
            consts = " ".join([
                x for x in answers[0][1].split(" ") if x not in [
                    '(',
                    ')',
                    '+',
                    '-',
                    '/',
                    '*',
                ]
            ])
            consts = consts.split(" = ")
            sp = (objs[consts[0].split(" ")[-1]][1],
                  objs[consts[1].split(" ")[0]][1])

            target = 'x'
            target = (target, objs[target])

            #find innermost parens?
            sides = []
            for i, compound in enumerate([l, r]):
                while len(compound) > 1:
                    if "(" in compound:
                        rpidx = (len(compound) - 1) - compound[::-1].index('(')
                        lpidx = rpidx + compound[rpidx:].index(")")
                        subeq = compound[rpidx + 1:lpidx]
                        substr = "(" + ''.join(subeq) + ")"
                        compound = compound[:rpidx] + [substr
                                                       ] + compound[lpidx + 1:]
                    else:
                        subeq = compound[0:3]
                        substr = "(" + ''.join(subeq) + ")"
                        compound = [substr] + compound[3:]
                    if True:
                        p, op, e = subeq
                        p = objs[p]
                        e = objs[e]
                        op = op.strip()
                        #trips.append((op,p,e))
                        pute = (0, makesets.combine(p[1], e[1], op))
                        objs[substr] = pute
                    if pute == -1:
                        exit()
                sides.append(objs[compound[0]])
            tdata.append(
                training(sides[0], sides[1], problem, story, target, j, order,
                         sp))

    f = open("data/" + sys.argv[1][-1] + ".global.data", 'w')
    for v in tdata:
        f.write(str(v[0]) + " ")
        for i, j in enumerate(v[1:]):
            f.write(str(i + 1) + ":" + str(j) + " ")
        f.write("\n")