def iftopf(expression, order):
    #Infix to postfix conversion

    s = Stack()
    pf = []
    query_terms = expression.split(' ')
    for term in query_terms:
        if term not in (["OR", "AND", "NOT", "(", ")"]):
            term = stemmer.stem(term, 0, len(term) - 1)
            id1 = get_id(term)
            pf.append(id1)

        elif term == '(':
            s.push(term)
        elif term == ')':
            topterm = s.pop()
            while topterm != '(':
                pf.append(topterm)
                topterm = s.pop()
        else:
            if (not s.isEmpty() and s.peek() == "NOT"):
                pf.append(s.pop())

            while (not s.isEmpty()) and (order[s.peek()] >= order[term]
                                         and term != "NOT"):
                pf.append(s.pop())
            s.push(term)

    while not s.isEmpty():
        pf.append(s.pop())

    return pf
Example #2
0
def infix_to_postfix(token_list):
    stack = Stack()
    postfix_list = []

    for token in token_list:
        if token.isalpha() or token.isdecimal():
            postfix_list.append(token)

        elif token == '(':
            stack.push(token)

        elif token == ')':
            top_token = stack.pop()

            while top_token != '(':
                postfix_list.append(top_token)
                top_token = stack.pop()

        else:
            while not stack.isEmpty() and OPERATORS_PRECEDENCE[
                    stack.peek()] >= OPERATORS_PRECEDENCE[token]:
                postfix_list.append(stack.pop())

            stack.push(token)

    while not stack.isEmpty():
        postfix_list.append(stack.pop())

    return postfix_list
Example #3
0
def infixToPostfix(infixexpr):
    prec = {
        '*': 3,
        '/': 3,
        '+': 2,
        '-': 2,
        '(': 1,
    }

    opStack = Stack()
    postfixList = []
    tokenList = infixexpr.split()

    for token in tokenList:
        if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and \
                (prec[opStack.peek()] >= prec[token]):
                postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return ''.join(postfixList)
Example #4
0
def toPostfix(infixString):
    s = Stack()
    output = []
    infixString = infixString.split()

    for i in infixString:
        if i.isalnum():
            output.append(i)
        elif i == '(':
           s.push(i)
        elif i == ')':
            top = s.pop()
            while top != '(':
                output.append(top)
                top = s.pop()
        else:
            while (not s.isEmpty()) and (prec[s.peek()] > prec[i]):
                output.append(s.pop())
            s.push(i)
    
    while not s.isEmpty():
        output.append(s.pop())
    return " ".join(output)