Exemplo n.º 1
0
    def analize (cls, text):
        word = []
        tokens = []
        prev_letter = ""

        # Разбивка на токены
        i = 0
        while i < len (text):
            letter = text[i]
            if letter == " ":
                if len (word) > 0:
                    token = Token ()
                    token.text = ''.join (word)
                    tokens.append (token)
                    word = []
            elif letter == "(" or \
                 letter == ")" or \
                 letter == ",":
                 #letter == "_":
                if len (word) > 0:
                    token = Token ()
                    token.text = ''.join (word)
                    tokens.append (token)
                    word = []
                token = Token ()
                token.text = letter
                tokens.append (token)
            elif letter == "." or \
                 letter == "_":
                j = i + 1
                if j == len (text):
                    if len (word) > 0:
                        token = Token ()
                        token.text = ''.join (word)
                        tokens.append (token)
                        word = []
                    token = Token ()
                    token.text = letter
                    tokens.append (token)
                while j < len (text):
                    letter = text[j]

                    if letter == " ":
                        break
                    elif letter == ")" or \
                         letter == "(" or \
                         letter == ",":
                        j = j - 1
                        break
                    if i == (j - 1):
                        if letter == " ":
                            if len (word) > 0:
                                token = Token ()
                                token.text = ''.join (word)
                                tokens.append (token)
                                word = []
                            token = Token ()
                            token.text = letter
                            tokens.append (token)
                            break
                        else:
                            word.append (text[i])
                            prev_letter = letter
                            word.append (letter)
                            j += 1
                    else:
                        prev_letter = letter
                        word.append (letter)
                        j += 1
                i = j
            elif letter == "=":
                if prev_letter == " " or \
                   prev_letter == "(":
                    if len (word) > 0:
                        token = Token ()
                        token.text = ''.join (word)
                        tokens.append (token)
                        word = []
                    token = Token ()
                    token.text = letter
                    tokens.append (token)
                else:
                    word.append (letter)
            elif letter == "?":
                if len (word) > 0:
                    token = Token ()
                    token.text = ''.join (word)
                    tokens.append (token)
                    word = []
                if prev_letter not in [" ", "("]:
                    token = Token ()
                    token.text = letter
                    tokens.append (token)
                else:
                    word.append (letter)
            elif letter == "\"":
                i += 1
                while i < len (text):
                    prev_letter = letter
                    letter = text[i]
                    if letter == "\"" and prev_letter != "\\":
                        break
                    else:
                        word.append (letter)
                    i += 1
                token = Token ()
                token.text = ''.join (word)
                token.type = TokenType.string
                tokens.append (token)
                word = []
            else:
                word.append (letter)
            prev_letter = letter
            i += 1

        # Идентификация токенов
        for token in tokens:
            #print token.text
            if token.type == TokenType.string:
                continue
            if token.text.find ('?') == 0 and len (token.text) > 1:
                s = token.text.replace ('?', '')
                query = "SELECT id FROM qsl_linkage WHERE name = \'" + s + "\';"
                cls.__cursor.execute (query)
                row = cls.__cursor.fetchone ()
                if row != None:
                    token.type = TokenType.linkage
                    token.linkage = TokenLinkage ()
                    token.linkage.id = row[0]
                    token.linkage.name = s
                else:
                    cls.__error_text = ErrorHelper.get_text (102, token.text)
                    return False
            elif token.text.find ('%') == 0 and len (token.text) > 1:
                token.type = TokenType.code_object
            elif token.text.find ('*') == 0:
                # Модификатор
                token.type = TokenType.modifier
            elif token.text == "(":
                token.type = TokenType.opening_bracket
            elif token.text == ")":
                token.type = TokenType.closing_bracket
            elif token.text == ",":
                token.type = TokenType.comma
            elif token.text == "_":
                token.type = TokenType.underscore
            elif token.text == ".":
                token.type = TokenType.point
            elif token.text == "?":
                token.type = TokenType.question_mark
            elif token.text == "=":
                token.type = TokenType.equal_sign
            else:
                query = "SELECT id, type FROM qsl_concept WHERE name = \'" + token.text + "\';"
                cls.__cursor.execute (query)
                row = cls.__cursor.fetchone ()
                if row != None:
                    token.type = TokenType.concept
                    token.concept = TokenConcept ()
                    token.concept.id = row[0]
                    token.concept.type = row[1]
                    token.concept.name = token.text
                else:
                    if token.text.isdigit ():
                        token.type = TokenType.number
                    else:
                        cls.__error_text = ErrorHelper.get_text (103, token.text)
                        return False

        node = cls.build_tree (tokens)
        if node != None:
            cls.proposition_tree = PropositionTree ()
            cls.proposition_tree.root_node = node
        else:
            return False

        return True
Exemplo n.º 2
0
    def analize(cls, text):
        word = []
        tokens = []
        prev_letter = ""

        # Разбивка на токены
        i = 0
        while i < len(text):
            letter = text[i]
            if letter == " ":
                if len(word) > 0:
                    token = Token()
                    token.text = ''.join(word)
                    tokens.append(token)
                    word = []
            elif letter == "(" or \
                 letter == ")" or \
                 letter == ",":
                #letter == "_":
                if len(word) > 0:
                    token = Token()
                    token.text = ''.join(word)
                    tokens.append(token)
                    word = []
                token = Token()
                token.text = letter
                tokens.append(token)
            elif letter == "." or \
                 letter == "_":
                j = i + 1
                if j == len(text):
                    if len(word) > 0:
                        token = Token()
                        token.text = ''.join(word)
                        tokens.append(token)
                        word = []
                    token = Token()
                    token.text = letter
                    tokens.append(token)
                while j < len(text):
                    letter = text[j]

                    if letter == " ":
                        break
                    elif letter == ")" or \
                         letter == "(" or \
                         letter == ",":
                        j = j - 1
                        break
                    if i == (j - 1):
                        if letter == " ":
                            if len(word) > 0:
                                token = Token()
                                token.text = ''.join(word)
                                tokens.append(token)
                                word = []
                            token = Token()
                            token.text = letter
                            tokens.append(token)
                            break
                        else:
                            word.append(text[i])
                            prev_letter = letter
                            word.append(letter)
                            j += 1
                    else:
                        prev_letter = letter
                        word.append(letter)
                        j += 1
                i = j
            elif letter == "=":
                if prev_letter == " " or \
                   prev_letter == "(":
                    if len(word) > 0:
                        token = Token()
                        token.text = ''.join(word)
                        tokens.append(token)
                        word = []
                    token = Token()
                    token.text = letter
                    tokens.append(token)
                else:
                    word.append(letter)
            elif letter == "?":
                if len(word) > 0:
                    token = Token()
                    token.text = ''.join(word)
                    tokens.append(token)
                    word = []
                if prev_letter not in [" ", "("]:
                    token = Token()
                    token.text = letter
                    tokens.append(token)
                else:
                    word.append(letter)
            elif letter == "\"":
                i += 1
                while i < len(text):
                    prev_letter = letter
                    letter = text[i]
                    if letter == "\"" and prev_letter != "\\":
                        break
                    else:
                        word.append(letter)
                    i += 1
                token = Token()
                token.text = ''.join(word)
                token.type = TokenType.string
                tokens.append(token)
                word = []
            else:
                word.append(letter)
            prev_letter = letter
            i += 1

        # Идентификация токенов
        for token in tokens:
            #print token.text
            if token.type == TokenType.string:
                continue
            if token.text.find('?') == 0 and len(token.text) > 1:
                s = token.text.replace('?', '')
                query = "SELECT id FROM qsl_linkage WHERE name = \'" + s + "\';"
                cls.__cursor.execute(query)
                row = cls.__cursor.fetchone()
                if row != None:
                    token.type = TokenType.linkage
                    token.linkage = TokenLinkage()
                    token.linkage.id = row[0]
                    token.linkage.name = s
                else:
                    cls.__error_text = ErrorHelper.get_text(102, token.text)
                    return False
            elif token.text.find('%') == 0 and len(token.text) > 1:
                token.type = TokenType.code_object
            elif token.text.find('*') == 0:
                # Модификатор
                token.type = TokenType.modifier
            elif token.text == "(":
                token.type = TokenType.opening_bracket
            elif token.text == ")":
                token.type = TokenType.closing_bracket
            elif token.text == ",":
                token.type = TokenType.comma
            elif token.text == "_":
                token.type = TokenType.underscore
            elif token.text == ".":
                token.type = TokenType.point
            elif token.text == "?":
                token.type = TokenType.question_mark
            elif token.text == "=":
                token.type = TokenType.equal_sign
            else:
                query = "SELECT id, type FROM qsl_concept WHERE name = \'" + token.text + "\';"
                cls.__cursor.execute(query)
                row = cls.__cursor.fetchone()
                if row != None:
                    token.type = TokenType.concept
                    token.concept = TokenConcept()
                    token.concept.id = row[0]
                    token.concept.type = row[1]
                    token.concept.name = token.text
                else:
                    if token.text.isdigit():
                        token.type = TokenType.number
                    else:
                        cls.__error_text = ErrorHelper.get_text(
                            103, token.text)
                        return False

        node = cls.build_tree(tokens)
        if node != None:
            cls.proposition_tree = PropositionTree()
            cls.proposition_tree.root_node = node
        else:
            return False

        return True