示例#1
0
文件: app.py 项目: ag-gipp/MathQA
def my_form_post():
    """
        Get formula from the user input and process it
        Return response
    """
    try:
        if request.form['formula']:
            global formula
            formula = request.form['formula']
            global processedformula
            processedformula = latexformlaidentifiers.prepformula(formula)

            if formula is not None:
                return makeresponse(processedformula, None, None)

    # except:
    #     response= jsonify("System is not able to find the result.")
    #     response.status_code = 202
    #     return response

    except Exception as ex:
        print(''.join(
            traceback.format_exception(etype=type(ex),
                                       value=ex,
                                       tb=ex.__traceback__)))
def my_form_post():
    """
        Get formula from the user input and process it
        Return response
    """
    try:
        if request.form['formula']:
            global formula
            formula = request.form['formula']
            global processedformula
            processedformula = latexformlaidentifiers.prepformula(formula)

            if formula is not None:
                return makeresponse(processedformula)

    except:
        response = jsonify("System is not able to find the result.")
        response.status_code = 202
        return response
示例#3
0
文件: app.py 项目: ag-gipp/MathQA
def get_hindiformula():
    """
        Get Hindi question from the user and apply regex to get subject and predicate
        Parse subject and predicate to HindiRequestHandler to get formula
        Return response
    """

    try:
        question = request.form['formula']
        matchObj = re.match(r'(.*)की (.*?) .*', question, re.M | re.I)
        matchObj1 = re.match(r'(.*)के लिए (.*?) क्या है *', question,
                             re.M | re.I)
        matchObj2 = re.match(r'(.*)और (.*?) के बीच *', question, re.M | re.I)
        if matchObj:
            subject = matchObj.group(1)
            predicate = matchObj.group(2)

        if matchObj1:
            subject = matchObj1.group(1)
            predicate = matchObj1.group(2)

        if matchObj2:
            subject = matchObj2.group(1)
            predicate = matchObj2.group(2)

        reques = HindiRequestHandler("hi", subject, predicate)
        global formula
        formula = reques.answer()
        global processedformula
        processedformula = latexformlaidentifiers.prepformula(formula)
        print(processedformula)
        if not (formula.startswith("System")):
            return makeresponse(processedformula, None, None)
        else:
            response = jsonify(formula)
            response.status_code = 202
            return response
    except Exception:
        response = jsonify("System is not able to find the result.")
        response.status_code = 202
        return response
def get_formula():
    """
        Get English question from the user parse it to Questionparsing module to get Triple
        Parse Triple (Subject, predicate, ?) to FormulaRequestHandler to get Formula from Wikidata
        Return response 
    """

    try:
        question = request.form['formula']

        meas = {'accuracy': 0.5, 'relevance': 0.5}
        q = RequestHandler(
            Request(language="en",
                    id=1,
                    tree=Sentence(question),
                    measures=meas))
        query = q.answer()

        reques = FormulaRequestHandler(query)
        global formula
        formula = reques.answer()

        global processedformula
        processedformula = latexformlaidentifiers.prepformula(formula)
        if not (formula.startswith("System")):
            return makeresponse(processedformula)
        else:

            response = jsonify(formula)
            response.status_code = 202
            print(response)
            return response
    except Exception:
        response = jsonify("System is not able to find the result.")
        response.status_code = 202
        return response
示例#5
0
文件: app.py 项目: ag-gipp/MathQA
def get_formula():
    """
        Get English question from the user parse it to Questionparsing module to get Triple
        Parse Triple (Subject, predicate, ?) to FormulaRequestHandler to get Formula from Wikidata
        Return response
    """

    # parse question
    try:
        question = request.form['formula']
        # lowercase first letter of question
        question = question[:1].lower() + question[1:]
        print("Question: " + question)

        # Determine question type

        # identifier symbol question
        if "symbol" in question:
            mode = 'symbol_question'
            print("Symbol question")

            exclude = ["what", "is", "the", "symbol", "for", "?"]
            subject = get_input(question, exclude)

            formula = get_identifier_symbol(identifier_name=subject)

        # relationship question (semantic search)
        elif "relationship" in question or "relation" in question:
            mode = 'relationship_question'
            print("Relationship question")

            exclude = [
                "what", "is", "the", "relationship", "relation", "between",
                "and", "?"
            ]
            input = get_input(question, exclude)

            # check if input is identifier names or symbols
            # symbols if all characters
            #
            #symbols = True
            #for element in input:
            #    if len(element) > 1:
            #        symbols = False
            # names if at least one is word
            #if symbols:
            #    mode_number = 1
            #else:
            #    mode_number = 2
            #
            # results = search_formulae_by_identifier_names\
            #     (identifier_names=identifier_names,catalog="NTCIR-12_arXiv_astro-ph"\
            #      ,inverse=True,multiple=False)
            #results = search_formulae_by_identifiers(input=input,
            #                                                mode_number=mode_number)
            results, subject = search_formulae_by_identifiers_Wikidata(
                identifiers=input)

            formula = list(results.items())[0][0].split(" (")[0]

        # Formula question
        elif "formula" in question:
            mode = 'formula_question'
            print("Formula question")

            exclude = ["what ", "is ", "the ", "formula ", "for ", "?"]
            for word in exclude:
                question = question.replace(word, "")
            # strip whitespace (at beginning) and end
            #subject = question[1:].strip()
            subject = question.strip()

            formula = search_formulae_by_concept_name_Wikidata(subject)

        # General or geometry question
        else:
            mode = 'general_geometry'
            print("General or geometry question")
            meas = {'accuracy': 0.5, 'relevance': 0.5}
            q = RequestHandler(
                Request(language="en",
                        id=1,
                        tree=Sentence(question),
                        measures=meas))
            try:
                query = q.answer()
            except:
                print("Stanford CoreNLP was unable to parse the question")
            reques = FormulaRequestHandler(query)

            subject = reques.request[0]._attributes['tree']._attributes[
                'subject']._attributes['value']

            formula = reques.answer()
            formula = latexformlaidentifiers.prepformula(formula)

        # generate response
        if not (formula.startswith("System")):
            return makeresponse(formula, subject, mode)
        else:
            response = jsonify(formula)
            response.status_code = 202
            print(response)
            return response

    except Exception:
        response = jsonify("System is not able to find the result.")
        response.status_code = 202
        return response