当前位置: 首页>>代码示例>>Python>>正文


Python Solver.solveEquations方法代码示例

本文整理汇总了Python中Solver.Solver.solveEquations方法的典型用法代码示例。如果您正苦于以下问题:Python Solver.solveEquations方法的具体用法?Python Solver.solveEquations怎么用?Python Solver.solveEquations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Solver.Solver的用法示例。


在下文中一共展示了Solver.solveEquations方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: mkgoodtrain

# 需要导入模块: from Solver import Solver [as 别名]
# 或者: from Solver.Solver import solveEquations [as 别名]
def mkgoodtrain():
    wps = open("../all_a.txt").readlines()
    answs = open("../all_aa.txt").readlines()
    goodtrain = open("goodp.txt",'w')
    goodtraina = open("gooda.txt",'w')

    for k in range(len(wps)):
        print(k)
        problem = wps[k].lower()
        try:
            story = nlp.parse(problem)
            numbs = setmaker.setmaker(story)
            numlist = [cleannum(v.num) for k,v in numbs if setmaker.floatcheck(v.num)]
            numlist = [x for x in numlist if x!='']
        except:
            continue
        print(numlist)
        signal.signal(signal.SIGALRM, kill)
        signal.alarm(10)
        try:
            ST = Solver(numlist)
            answers = ST.solveEquations(float(answs[k]))

            print(answers)
        except:
            continue
        if answers != []:
            goodtrain.write(problem)
            goodtraina.write(answs[k])
    goodtrain.close();goodtraina.close()
开发者ID:MathProblems,项目名称:april,代码行数:32,代码来源:mktraindata.py

示例2: dotrain

# 需要导入模块: from Solver import Solver [as 别名]
# 或者: from Solver.Solver import solveEquations [as 别名]
def dotrain():
    if len(sys.argv)>1:
        wps = open(sys.argv[1]).readlines()
        answs = open(sys.argv[2]).readlines()
    else:
        wps = open("emnlp_noIrrelev_p.txt").readlines()
        answs = open("emnlp_noIrrelev_a.txt").readlines()
    problematic = open('nogoodtrainproblems','w')

    bigtexamples = {x:([],[]) for x in ["+","*",'/','-','=']}
    replacements = {' two ':' 2 '," three ":' 3 ',' four ':' 4 ',' five ':' 5 ',' six ':' 6 ',' seven ':' 7 ',' eight ':' 8 ',' nine ':' 9 ',' ten ':' 10 ',' eleven ':' 11 ',' week ':' 7 days ',' dozen ':' 12 of ', ' dozens ': ' 12 ', ' twice ':' 2 '}

    for k in range(len(wps)):
        print(k)
        problem = wps[k].lower()
        for r in replacements:
            problem = problem.replace(r,replacements[r])
        #extract numbers:
        #problem = ' '.join([x.replace(",","") for x in problem.split()])
        story = nlp.parse(problem)
        numbs = makesets.makesets(story['sentences'])

        numlist = [(cleannum(v.num),v) for k,v in numbs]
        numlist = [x for x in numlist if x[0]!='']

        allnumbs = {str(k):v for k,v in numlist}
        if 'x' not in allnumbs:
            if 'x*' not in allnumbs:
                problematic.write('no x :'+problem); continue
            

        objs = {k:(0,v) for k,v in numlist}

        print('start solving')
        print(numlist)
        if len(numlist)<2:
            problematic.write("not enough numbers : "+problem);continue
            
        ST = Solver([x[0] for x in numlist if x[0]!='x'])
        answers = ST.solveEquations(float(answs[k]))
        print('done solving')
        #filter out where = in middle if simpler eq exists
        simpleranswers = [x for x in answers if x.split(" ")[1] == '=' or x.split(" ")[-2]=="="]
        if not answers:
            continue
        if simpleranswers:
            answers = simpleranswers
        else:
            print(answers)
            problematic.write("not simple : "+problem);continue

        answervals = [x for x in answers[0].split(" ") if x not in ['+','-','/','=',')','(','*']]
        numvals = [x[0] for x in numlist if x[0] in answervals]
        xidx = numvals.index("x")
        rightidx = [i for i,x in enumerate(answers) if [z for z in x.split(" ") if z not in ['+','-','/','=',')','(','*']].index('x')==xidx]
        xrightanswers = [answers[i] for i in rightidx]
        if xrightanswers:
            answers = xrightanswers

        for j,eq in enumerate(answers):
            trips = []
            print(j,eq)
            l,r = [x.strip().split(' ') for x in eq.split('=')]
            
            compound = l if len(r)==1 else r
            simplex = l if len(l)==1 else r
            target = simplex[0]
            target = (target,objs[target])

            #find innermost parens?
            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
                    #print(p,op,e)
                    p = objs[p]
                    e = objs[e]
                    op = op.strip()
                    trips.append((op,p,e))
                    pute = (0,makesets.combine(p[1],e[1],op))
                    #print("OPERATION SELECTED: ",op)
                    #p.details()
                    #e.details()
                    #print(substr,pute[1].num)
                    objs[substr]=pute
                if pute == -1:
                    exit()
            if simplex == l:
                trips.append(("=",objs[simplex[0]],objs[compound[0]]))
            else:
                trips.append(("=",objs[compound[0]],objs[simplex[0]]))
#.........这里部分代码省略.........
开发者ID:MathProblems,项目名称:may,代码行数:103,代码来源:dev_mktraindata.py

示例3: dotrain

# 需要导入模块: from Solver import Solver [as 别名]
# 或者: from Solver.Solver import solveEquations [as 别名]
def dotrain():
    wps = open("data/trainingp.txt").readlines()
    answs = open("data/traininga.txt").readlines()
    bigtexamples = {x:([],[]) for x in ["+","*",'/','-','=']}
    problematic = open('data/nogoodtrainproblems','w')

    for k in range(len(wps)):
        print(k)
        problem = wps[k].lower()
        story = nlp.parse(problem)
        numbs = setmaker.setmaker(story)
        numlist = [(cleannum(v.num),v) for k,v in numbs if setmaker.floatcheck(v.num) or v.num=='x']
        numlist = [x for x in numlist if x[0]!='']
        allnumbs = {str(v.num):v for k,v in numbs if setmaker.floatcheck(v.num) or v.num=='x'}
        if 'x' not in allnumbs:
            problematic.write('no x :'+problem); continue
            

        objs = {k:(0,v) for k,v in numlist}

        print('start solving')
        print(numlist)
        if len(numlist)<2:
            problematic.write("not enough numbers : "+problem);continue
            
        ST = Solver([x[0] for x in numlist if x[0]!='x'])
        answers = ST.solveEquations(float(answs[k]))
        print('done solving')
        #filter out where = in middle if simpler eq exists
        simpleranswers = [x for x in answers if x.split(" ")[1] == '=' or x.split(" ")[-2]=="="]
        if not answers:
            continue
        if simpleranswers:
            answers = simpleranswers
        else:
            print(answers)
            problematic.write("not simple : "+problem);continue

        answervals = [x for x in answers[0].split(" ") if x not in ['+','-','/','=',')','(','*']]
        numvals = [x[0] for x in numlist if x[0] in answervals]
        xidx = numvals.index("x")
        rightidx = [i for i,x in enumerate(answers) if [z for z in x.split(" ") if z not in ['+','-','/','=',')','(','*']].index('x')==xidx]
        xrightanswers = [answers[i] for i in rightidx]
        if xrightanswers:
            answers = xrightanswers

        for j,eq in enumerate(answers):
            trips = []
            print(j,eq)
            l,r = [x.strip().split(' ') for x in eq.split('=')]
            
            compound = l if len(r)==1 else r
            simplex = l if len(l)==1 else r
            target = simplex[0]
            target = (target,objs[target])

            #find innermost parens?
            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
                    #print(p,op,e)
                    p = objs[p]
                    e = objs[e]
                    op = op.strip()
                    trips.append((op,p,e))
                    pute = (0,setmaker.combine(p[1],e[1],op))
                    #print("OPERATION SELECTED: ",op)
                    #p.details()
                    #e.details()
                    #print(substr,pute[1].num)
                    objs[substr]=pute
                if pute == -1:
                    exit()
            if simplex == l:
                trips.append(("=",objs[simplex[0]],objs[compound[0]]))
            else:
                trips.append(("=",objs[compound[0]],objs[simplex[0]]))
            t = training(trips,problem,target)
            for op in t:
                bigtexamples[op][0].extend(t[op][0])
                bigtexamples[op][1].extend(t[op][1])
            print(op,len(bigtexamples[op][0]))
    pickle.dump(bigtexamples,open('data/training.pickle','wb'))
开发者ID:MathProblems,项目名称:april,代码行数:95,代码来源:mktraindata.py

示例4: make_eq

# 需要导入模块: from Solver import Solver [as 别名]
# 或者: from Solver.Solver import solveEquations [as 别名]

#.........这里部分代码省略.........
        if len(postx)>1:
            # 2 vals to right
            twoToRight = True
        else:
            twoToRight = False
        '''

        



        numlist = [(cleannum(v.num),v) for k,v in sets]
        numlist = [x for x in numlist if x[0]!='']
        if VERBOSE:
            for z,v in numlist:
                v.details()
            input()

        allnumbs = {str(k):v for k,v in numlist}
            

        objs = {k:(0,v) for k,v in numlist}

        print('start solving')
        print(numlist)
        if len(numlist)<2:
            problematic.write("not enough numbers : "+problem);continue
            
        values = [x[0] for x in numlist if x[0]!='x']
        print(values)
        ST = Solver(values)

        answers = []
        answers = ST.solveEquations(float(answs[k]))
        if not answers:
            problematic.write("No answers : " + problem + "\n")
            problematic.write(str([x[0] for x in numlist])+'\n')
            problematic.write(answs[k]+'\n')
            continue
        print('done solving')

        # if target has 2 entities, try eqs with = x op y format
        '''
        simpleranswers = None
        if twoToRight:
            try:
                simpleranswers = [x for x in answers if x.split(" ")[-4]=="=" and x.split(" ")[-3]=='x']
            except:
                pass
        if not simpleranswers:
            simpleranswers = [x for x in answers if x.split(" ")[1] == '=' or x.split(" ")[-2]=="="]
        '''
        simpleranswers = [x for x in answers if x.split(" ")[1] == '=' or x.split(" ")[-2]=="="]

        #filter out where = in middle if simpler eq exists
        if simpleranswers:
            answers = simpleranswers[:]
        else:
            problematic.write("not simple : "+problem+"\n");continue

        values = [x[0] for x in numlist]
        xidx = values.index('x')
        print(xidx)
        for a in simpleranswers:
            aspl = [x for x in a.split(" ") if x not in ["/","-",'+','*','=','(',')']]
            print(a);print(aspl);print(values)
开发者ID:MathProblems,项目名称:may,代码行数:70,代码来源:make_equations.py


注:本文中的Solver.Solver.solveEquations方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。