Esempio n. 1
0
def add_new_rule(grammar: Grammar):
    '''Пополнить грамматику правилом A → aN , где A ∈ V N , a ∈ V T и
    N - новый нетерминал, для каждого правила вида A → a , если в грамматике нет
    соответствующего ему правила A → aB , где B ∈ V N .'''
    rules_with_terms = {
        rule
        for rule in grammar.rules if rule.right_part != EMPTY_SEQUENCE
        and set(rule.right_part) <= grammar.terminals
    }
    rules_without_other_nonterms = {
        rule
        for rule in grammar.rules
        if len(rule.right_part) == 2 and rule.left_part in rule.right_part
    }
    rules = rules_with_terms - {
        Rule(rule.left_part,
             get_term_nonterm(rule, grammar.terminals)[0])
        for rule in rules_without_other_nonterms
    }
    if rules:
        new_nonterm = ('N'
                       if 'N' not in grammar.nonterminals else random.choice(
                           list(set(ascii_uppercase) - grammar.nonterminals)))
        grammar.nonterminals.add(new_nonterm)

        new_rules = {
            Rule(rule.left_part, rule.right_part + new_nonterm)
            for rule in rules
        }
        grammar.rules.update(new_rules)
Esempio n. 2
0
def get_rules(sets, acceleration_values, consumption_values):
    new_rules = []

    # 1. if consumption is low => efficiency high
    antecedent = Antecedent(sets['consumption']['low'], consumption_values)
    consequence = Consequence(sets['efficiency']['high'])
    new_rules.append(Rule(antecedent, consequence))

    # 2. if consumption is high => efficiency low
    antecedent = Antecedent(sets['consumption']['high'], consumption_values)
    consequence = Consequence(sets['efficiency']['low'])
    new_rules.append(Rule(antecedent, consequence))

    # 3. if acceleration is negative and consumption low => efficiency high
    antecedent_acceleration = Antecedent(sets['acceleration']['negative'],
                                         acceleration_values)
    antecedent_consumption = Antecedent(sets['consumption']['low'],
                                        consumption_values)
    consequence = Consequence(sets['efficiency']['high'])
    new_rules.append(
        Rule(And(antecedent_acceleration, antecedent_consumption),
             consequence))

    # 4. if acceleration is positive or consumption high => efficiency low
    antecedent_acceleration = Antecedent(sets['acceleration']['positive'],
                                         acceleration_values)
    antecedent_consumption = Antecedent(sets['consumption']['high'],
                                        consumption_values)
    consequence = Consequence(sets['efficiency']['low'])
    new_rules.append(
        Rule(Or(antecedent_acceleration, antecedent_consumption), consequence))

    return new_rules
Esempio n. 3
0
 def on_packet_in(self, packet, switch, inport):
     print("\n>> ", inport, "switch", switch.id, packet)
     # the magic oracle function knows what to do with the packet...
     use_port = get_port_by_ip(switch, packet.ip_dst)
     if use_port >= 0:
         r = Rule()
         r.MATCH('IP_DST', packet.ip_dst)
         r.ACTION('OUTPUT', use_port)
         send_rule(r, switch)
         # important to not loose any packets!
         send_packet(packet, switch, use_port)
Esempio n. 4
0
    def toCNF(self):
        """
        each rule must be of format
        A → BC   (A,B,C ∈ N)
        A → a    (A ∈ N, a ∈ Σ)
        S → ε    if S is not on the right side of any rule
        """
        import CFG

        G = self.toOwn()
        N = G.N.copy()
        P = CFG.P()

        i = 0
        while i < len(N):
            A = N[i]
            for rule in G.P.get(A, P[A]):
                A = N[i]
                if len(rule) >= 2:
                    # change rule A -> abcd... to A -> a<bcd...>
                    B, *C = rule

                    B1 = Rule(B if B.isupper() else f"{B}̄")
                    C1 = Rule((f"<{''.join(C)}>" if len(C) > 1 else
                               C[0] if C[0].isupper() else f"{C[0]}̄"))

                    P[A].add(Rule(f"{B1}{C1}"))

                    A = Rule(f"{C1[0]}")

                    # continue generating <bcd...> -> b<cd...>
                    while len(A[0]) >= 2 and A[0] != "<>":
                        rule = Rule(f"{C1[0][1]}<{C1[0][2:-1]}>")
                        B, *C = rule

                        B1 = Rule(B if B.isupper() else f"{B}̄")
                        C1 = Rule((f"{''.join(C[0])}" if len(C[0]) > 1 else
                                   C[0] if C[0].isupper() else f"{C[0]}̄"))

                        P[A].add(Rule(f"{B1}{C1}"))

                        A = Rule(f"{C1[0]}")

                else:
                    P[A].add(Rule(rule))

            i += 1

        return CFG(N, self.Σ, P, self.S)
Esempio n. 5
0
        def resolve(A, B):
            for _ in range(len(P[A])):
                rule = list(P[A].pop(0))
                if rule[0].islower():
                    for i, c in enumerate(rule[1:]):
                        if c.islower() and len(c) == 1:
                            rule[i + 1] = f"{c}̄"

                    rule = "".join(rule)
                    P[A].add(Rule(rule))

                elif rule[0] == B:
                    rules = Set()
                    for rule1 in P[B]:
                        rules.add(Rule(rule1 + "".join(rule[1:])))
                    P[A] |= rules

                else:
                    P[A].add(Rule(rule))
Esempio n. 6
0
    def remove_left_recursion(self):
        def calc_potentials():
            potentials = {}
            for A in N:
                potentials.setdefault(A, Set())
                for rule in P[A]:
                    if rule[0] in N:
                        potentials[A].add(rule[0])
            return potentials

        N = self.N.copy()
        P = self.P.copy()

        for i, A in enumerate(N):
            for B in N[:i + 1]:
                if not B in calc_potentials()[A]:
                    continue

                if A == B:
                    α = [rule for rule in P[A] if rule.startswith(B)]
                    β = [rule for rule in P[A] if not rule.startswith(B)]
                    N = Set(f"{A}'").union(N)
                    P[f"{A}'"] |= Set(Rule(rule[1:]) for rule in α) | Set(
                        Rule(rule[1:] + f"{A}'") for rule in α)
                    P[A] = Set(Rule(rule) for rule in β) | Set(
                        Rule(rule + f"{A}'") for rule in β)

                else:
                    α = [rule for rule in P[A] if rule.startswith(B)]
                    β = [rule for rule in P[A] if not rule.startswith(B)]
                    P[A] = Set(Rule(rule) for rule in β) | Set(
                        Rule(ruleB + rule[1:]) for rule in α for ruleB in P[B])

        return CFG(N, self.Σ.copy(), P, self.S)
Esempio n. 7
0
    def Earley(self):
        for i in range(0, len(self.word) + 1):
            self.Scan(i)
            old_length = len(self.D[i])
            self.Predict(i)
            self.Complete(i)
            new_length = len(self.D[i])
            while new_length != old_length:
                old_length = new_length
                self.Predict(i)
                self.Complete(i)
                new_length = len(self.D[i])

        if Situation(Rule('S1', 'S'), 0, 1) in self.D[len(self.word)]:
            return 'YES'
        else:
            return 'NO'
Esempio n. 8
0
def make_finite_state_machine(grammar: Grammar):
    '''Преобразовать регулярную грамматику в конечный автомат'''
    add_new_rule(grammar)

    # Начальный символ грамматики S принять за начальное состояние КА H.
    start_state = grammar.start_symbol
    # Из нетерминалов образовать множество состояний автомата Q = V N ∪ {N },
    states = grammar.nonterminals
    # а из терминалов – множество символов входного алфавита T = V T .
    input_symbols = grammar.terminals

    transitions = transform_rules_to_transitions(grammar)

    # Если в грамматике имеется правило S → ε , где S - начальный символ грамматики,
    # то поместить S во множество заключительных состояний.
    final_states = {grammar.start_symbol} if Rule(
        grammar.start_symbol, EMPTY_SEQUENCE) in grammar.rules else set()

    fsm = FSM(states, input_symbols, transitions, start_state, final_states)

    # Если получен НКА, то преобразовать его в ДКА
    if not is_dfsm(fsm):
        fsm = transform_nfsm_to_dfsm(fsm)
    return fsm
Esempio n. 9
0
 def __init__(self, grammar, word):
     self.word = word
     self.grammar = grammar
     self.D = [set() for i in range(len(self.word) + 1)]
     self.D[0].add(Situation(Rule('S1', 'S'), 0, 0))
Esempio n. 10
0
    def on_connect(self, switch):
        print "switch connected", switch.id

        if switch.id == 1:
            r = Rule()
            r.MATCH('IP_DST', '20.0.0.0/16')
            r.ACTION('OUTPUT', 5)
            send_rule(r, switch)

        if switch.id == 2:
            r = Rule()
            r.MATCH('IP_DST', '20.0.1.0/24')
            r.ACTION('OUTPUT', 2)
            send_rule(r, switch)

            r = Rule()
            r.MATCH('IP_DST', '20.0.2.0/24')
            r.ACTION('OUTPUT', 3)
            send_rule(r, switch)

            r = Rule()
            r.MATCH('IP_DST', '20.0.3.0/24')
            r.ACTION('OUTPUT', 4)
            send_rule(r, switch)

            r = Rule()
            r.MATCH('IP_DST', '20.0.4.0/24')
            r.ACTION('OUTPUT', 5)
            send_rule(r, switch)
Esempio n. 11
0
def get_mongo(mongo_address="mongodb://localhost:27017/", mongo_db="python", mongo_collection="tokens", logfile=None):
    try:
        client = pymongo.MongoClient(mongo_address)
        db = client[mongo_db]
        collection = db[mongo_collection]

        current = []

        if mongo_collection == "tokens":
            for x in collection.find():
                left = None
                right = None
                if 'leftborder' in x and x['leftborder']:
                    if 'allow_symbols' in x['leftborder']:
                        left = Border(x['leftborder']['allow_symbols'])
                    if 'their_dict' in x['leftborder']:
                        left.their_dict = x['leftborder']['their_dict']
                if 'rightborder' in x and x['rightborder']:
                    if 'allow_symbols' in x['rightborder']:
                        right = Border(x['rightborder']['allow_symbols'])
                    if 'their_dict' in x['rightborder']:
                        right.their_dict = x['rightborder']['their_dict']

                current.append(Token(x['prior'], x['reg'], x['name'], left, right))
        elif mongo_collection == "rules":
            for x in collection.find():
                role = []
                ignore_symbols = {}
                visa_verse = []
                visa_verse_tokens = []
                use_parent_final_words = False
                inner = False
                could_be_end = None
                check_could_be_end = None
                if 'role' in x:
                    role = x['role']
                if 'ignore_symbols' in x:
                    ignore_symbols = x['ignore_symbols']
                if 'inner' in x:
                    inner = x['inner']
                if 'visa_verse' in x:
                    visa_verse = x['visa_verse']
                if 'visa_verse_tokens' in x:
                    visa_verse_tokens = x['visa_verse_tokens']
                if 'use_parent_final_words' in x:
                    use_parent_final_words = x['use_parent_final_words']
                if 'check_could_be_end' in x:
                    check_could_be_end = x['check_could_be_end']
                if 'could_be_end' in x:
                    could_be_end = x['could_be_end']
                current.append(
                    Rule(x['keyword'], x['allow_tokens'], x['final_words'], role, ignore_symbols, inner, visa_verse,
                         visa_verse_tokens, use_parent_final_words, could_be_end, check_could_be_end))
        elif mongo_collection == "bugs":
            for x in collection.find():
                id = 0
                object = ""
                line = 0
                column = 0
                description = ""
                priority = 1
                important = 1
                type = ""
                date = ""
                author = ""
                program = ""
                version = "1.0.0"
                state = "Open"
                manage_by = ""
                possible_decision = ""
                if 'id' in x:
                    id = x['id']
                if 'object' in x:
                    object = x['object']
                if 'line' in x:
                    line = x['line']
                if 'column' in x:
                    column = x['column']
                if 'description' in x:
                    description = x['description']
                if 'priority' in x:
                    priority = x['priority']
                if 'important' in x:
                    important = x['important']
                if 'type' in x:
                    type = x['type']

                if 'date' in x:
                    date = x['date']
                if 'author' in x:
                    author = x['author']
                if 'program' in x:
                    program = x['program']
                if 'version' in x:
                    version = x['version']
                if 'state' in x:
                    state = x['state']
                if 'manage_by' in x:
                    manage_by = x['manage_by']
                if 'possible_decision' in x:
                    possible_decision = x['possible_decision']
                current.append(
                    Bug(id, object, line, column, description, priority, important, type, date, author, program,
                        version, state, manage_by, possible_decision))

    except Exception as ex:
        print('The error has occured')
        print(ex)

    finally:
        if client:
            client.close()
        if current:
            return current
Esempio n. 12
0
def test():
    print(Rule("SaSaS").chars)
    print(Rule("aX'b").chars)
    print(Rule("ab̄c").chars)
    print(Rule("ab̂c").chars)
    print(Rule("a<bc<d>f>e").chars)
Esempio n. 13
0
def scores(s, best_rules, f=None):
    #scores = {}
    bestscore = 0
    bestrule = []
    a = 0
    top_rules = {}
    for atag in s.keys():
        if len(atag) > 4:
            stat = s[atag]
            vtags = atag.split()
            if len(vtags) < 2:
                continue
            for y in vtags:
                try:
                    freq = s[y]
                except KeyError:
                    s[y] = ({0: {}}, {0: {}}, {0: {}}, 0)
                    continue
                for i, cont_type in enumerate(s[y][:3]):
                    for distance in cont_type:
                        for context in cont_type[distance]:
                            fr = -sys.maxint
                            for z in vtags:
                                if y != z:
                                # relative frequency
                                    try:
                                        incontext_z = s[z][i][distance][context]
                                    except:
                                        incontext_z = 0.0
                                    try:
                                        freq_z = s[z][3]
                                        relf = float(s[y][3]) / float(freq_z) * float(incontext_z)
                                    except:
                                        freq_z = 0.0
                                        relf = 0.0
                                    if relf >= fr:
                                        fr = relf
                                    try:
                                        w = incontext_z
                                    except:
                                        w = 0
                                #print vtags
                            x = s[y][i][distance][context] - float(fr)
                            curr_rule = Rule(*[atag, y, (distance, context), i])
                            curr_rule.id = feature_type(context)
                            if x > bestscore and s[y][i][distance][context] != w:
                                try:
                                    a = stat[i][distance][context]
                                except KeyError:
                                    continue
                                bestscore = x
                                bestrule = curr_rule
                                top_rules = {}
                                top_rules[bestrule] = a
                            elif x == bestscore and s[y][i][distance][context] != w:
                                try:
                                    a = stat[i][distance][context]
                                except KeyError:
                                    continue
                                bestrule = curr_rule
                                top_rules[bestrule] = a
    return top_rules, bestscore, a
Esempio n. 14
0
                                try:
                                    a = stat[i][distance][context]
                                except KeyError:
                                    continue
                                bestrule = curr_rule
                                top_rules[bestrule] = a
    return top_rules, bestscore, a


def random_choice(corpus):
    for s in corpus:
        for token in s:
            try:
                if token.has_ambig():
                    token.disambiguate(random.choice(token.getPOStags().split('_')))
            except:
                pass


if __name__ == '__main__':
    inc = read_corpus(sys.stdin)
    #s = context_stats(inc, join_context=True)
    #print s
    #rs = scores(s, [])
    #pprint(rs[1:3])
    '''for x in rs[1].keys():
        print x.display()
        print rs[1][x]
    print rs[2]'''
    r = Rule('ADVB NOUN', 'NOUN', (1, 'PNCT'), 0)
Esempio n. 15
0
from utils import Rule
from algorithm import Earley


if __name__ == '__main__':
    n = int(input())
    grammar = []
    for i in range(n):
        s = input().split('->')
        grammar.append(Rule(s[0],s[1]))
    word = input()
    algo = Earley(grammar, word)
    print(algo.Earley())