Esempio n. 1
0
    def simplify_name(self, name):
        simple = name
        year = Group(ZeroOrMore("[" + Word(nums, exact=4) +
                                "-" + Word(nums, exact=2) + "]"))

        dash = Group(ZeroOrMore(White() + "-" + White()))
        no_squared_brackets = Word(
            alphanums+alphas8bit,
            ",;.:-_@#°§+*{}^'?=)(/&%$£!\\|\""
        )
        bracketed = Group("[" + OneOrMore(no_squared_brackets) + "]")
        middle = ~bracketed + OneOrMore(Word(alphanums+alphas8bit+"'"))
        try:
            grammar = year.suppress() + dash.suppress() + middle
            simple = " ".join(grammar.parseString(name))
        except ParseException:
            commonlogger.error(f'Failed to simplify course name {name}',
                               exc_info=True)
        return simple.title()
Esempio n. 2
0
    def simplify_name(self, name):
        simple = name
        year = Group(
            ZeroOrMore("[" + Word(nums, exact=4) + "-" + Word(nums, exact=2) +
                       "]"))
        dash_with_student_code = Group(ZeroOrMore("-" + Word(nums)))
        personal_names = OneOrMore(Word(alphas + ',.;:/'))
        course_name = Group(
            OneOrMore(Word(alphanums + alphas8bit +
                           ',.;:/|\'"'))).setResultsName('course_name')
        course_extra_specs = Group(
            ZeroOrMore('(' + ZeroOrMore(Word(alphanums + ',.:;/\'')) + ')'))
        # Some courses specifies the professor name like this
        # [2019/20] COURSE MEMEOLOGY - George Miller
        # Most of the courses like this
        # [2019/20] COURSE MEMEOLOGY [ George Miller ]
        prof_name = Group(
            Suppress('[') + personal_names + Suppress(']') ^
            Suppress('-') + personal_names) \
            .setResultsName('prof_name')

        try:
            grammar = year.suppress() + dash_with_student_code.suppress() + \
                    Suppress(ZeroOrMore('-')) + course_name +\
                    course_extra_specs.suppress() + prof_name
            parsed_name_tokens = grammar.parseString(name)

            simple = f"{' '.join(parsed_name_tokens['course_name'])} "
            # Append professor names only if present
            if parsed_name_tokens['prof_name']:
                # Replace `/` with `;`
                safe_names = ' '.join(parsed_name_tokens['prof_name'])\
                    .replace(' /', ';')
                simple += f"[{safe_names}]"

        except ParseException:
            commonlogger.error(f'Failed to simplify course name {name}',
                               exc_info=True)
        return simple.title()
Esempio n. 3
0
 def simplify_name(self, name):
     simple = name
     year = Group(
         "[" + OneOrMore(Word(nums, exact=4) + "-" + Word(nums, exact=2)) +
         "]")
     no_squared_brackets = Word(alphanums,
                                ",;.:-_@#°§+*{}^'?=)(/&%$£!\\|\"")
     bracketed = Group("[" + OneOrMore(no_squared_brackets) + "]")
     middle = ~bracketed + OneOrMore(Word(alphas))
     try:
         grammar = year.suppress() + Literal("-").suppress() + middle
         simple = " ".join(grammar.parseString(name))
     except ParseException:
         logger.error('Failed to simplify course name {}'.format(name),
                      exc_info=True)
     return simple.title()
Esempio n. 4
0
string.setParseAction(lambda toks: ["'%s'" % toks[0]])

columnName = delimitedList( ident, ".",combine=True)
tableName = delimitedList( ident, ".",combine=True)
dataType = Word(alphas) + Combine(Optional(Literal("(") + (Word(nums) ^ delimitedList(string,combine=True)) + Literal(")"))) + ZeroOrMore(nnToken ^ autoincrementToken ^ (defaultToken + (string ^ nullToken)) ^ unsignedToken.suppress() )
dataType.setParseAction(convert_datatypes)

columnDescription = Group(ident + dataType)
keyDescription = Optional(primaryToken ^ uniqueToken) + keyToken + Optional(ident) + Literal("(") + delimitedList(ident + Optional(Literal("(") + Word(nums) + Literal(")"))) + Literal(")")

createTableStmt = Group(createToken + tableToken + ifneToken + ident + Literal("(")) + delimitedList(columnDescription ^ keyDescription.suppress()) + Group(Literal(")")) + Optional(autoincrementToken + Literal("=") + Word(nums)).suppress()
createTableStmt.setParseAction(rebuild_createtable)


createDataBaseStmt = Group(createToken + databaseToken + ident +  dcsToken + Word(alphanums)+ collateToken + ident)

useStmt = Group(useToken + ident)

comment = LineStart() + CharsNotIn("\n") + LineEnd()

value = Group(Literal("(") + delimitedList(Word(nums) ^ string) + Literal(")"))

insertPrefix = Group(insertToken + intoToken + ident + Literal("(") + delimitedList(ident) + Literal(")") + valuesToken)

insertStmt = insertPrefix + delimitedList(value)
insertStmt.setParseAction(rebuild_insert)

statement = ((createTableStmt ^ createDataBaseStmt.suppress() ^ useStmt.suppress() ^ insertStmt)  + Literal(";").setParseAction(lambda: [";\n"])) ^ comment.suppress() ^ White().suppress()

sql = ZeroOrMore(statement)