コード例 #1
0
def submit(partId):
    print '==\n== [automata-class] Submitting Solutions  | Programming Exercise %s\n==' % homework_id(
    )
    """Test on Sample"""
    sampleoutput = CYK.Start('sampleCYK.in')
    print '**********************\nTest on sample input (sampleCYK.in)......'
    if (cmp(sampleoutput,
            "1 \n02 1 \n1 02 1 \n02 1 02 1 \n13 23 13 23 13 \n") == 0):
        print '\nYou output is correct for sample input. Now start submitting to servers.....\n**********************\n\n'
    else:
        print 'Your output on sampleCYK.in is not correct'
        print 'Your generated table X is: %s' % sampleoutput
        print 'Correct table X is: 1 \n02 1 \n1 02 1 \n02 1 02 1 \n13 23 13 23 13 '
        print '0: S, 1: A, 2: B, 3: C'
        print 'You cannot submit until your output on samples is correct'
        return
    """Submission"""
    # Setup submit list
    partId = promptPart()
    partNames = validParts()
    if (not isValidPartId(partId)):
        print '!! Invalid homework part selected.'
        print '!! Expected an integer from 1 to %d.' % len(partNames) + 1
        print '!! Submission Cancelled'
        return

    (login, password) = loginPrompt()
    if not login:
        print '!! Submission Cancelled Because of Failure in Logging in'
        return

    print '\n== Connecting to automata-class ... \n'

    if partId == len(partNames) + 1:
        submitParts = range(1, len(partNames) + 1)
    else:
        submitParts = [partId]

    for partId in submitParts:
        # Get Challenge
        (login, ch, state, ch_aux) = getChallenge(login, partId)
        if ((not login) or (not ch) or (not state)):
            # Some error occured, error string in first return element.
            print '\n!! Error: %s\n' % login
            return

        # Attempt Submission with Challenge
        ch_resp = challengeResponse(login, password, ch)
        result = submitSolution(login, ch_resp, partId, output(partId, ch_aux), \
                                        source(partId), state, ch_aux)
        if (not result):
            result = 'NULL RESPONSE'

        print '\n== [automata-class] Submitted Homework %s - Part %d - %s' % \
                  (homework_id(), partId, partNames[partId - 1])
        print '== %s' % result.strip()
コード例 #2
0
ファイル: viterbi.py プロジェクト: macabot/ELPL_RC
def mostProbableTree(string, grammar):
    """Creates a parse forest an creates most probable
    parse tree.
    Arguments:
    string      - contains words separated by single whitespace
    grammar     - dictionary mapping rhs to [(P(rule_1), lhs_1), ..., (P(rule_n), lhs_n)]
    Return:
    nltk.Tree() -  most probable parse tree"""
    parseForest, probs = CYK.makeForest(string, grammar)
    j = len(string.split())
    if not (0,j) in parseForest:
        return None
    bestTree = viterbi(parseForest, probs, 0, j)
    unbinarizeAndReunarize(bestTree)
    return bestTree
コード例 #3
0
ファイル: viterbi.py プロジェクト: macabot/ELPL_RC
def mostProbableTree(string, grammar):
    """Creates a parse forest an creates most probable
    parse tree.
    Arguments:
    string      - contains words separated by single whitespace
    grammar     - dictionary mapping rhs to [(P(rule_1), lhs_1), ..., (P(rule_n), lhs_n)]
    Return:
    nltk.Tree() -  most probable parse tree"""
    parseForest, probs = CYK.makeForest(string, grammar)
    j = len(string.split())
    if not (0, j) in parseForest:
        return None
    bestTree = viterbi(parseForest, probs, 0, j)
    unbinarizeAndReunarize(bestTree)
    return bestTree
コード例 #4
0
ファイル: test.py プロジェクト: GioJS/parserNLP
    return False

signal.signal(signal.SIGINT, signal_handler)

prob=true_answer(raw_input('probabilistico? '))
start = time.clock()


s="she eats a fish with a fork"
if not prob:
	G=Grammar('S')
	G.add_rules_from_file('BigGrammar')
	#print G.get_unit_productions()
	#s=raw_input('write a phrase: ')
	
	parser=CYK(G,s)
	parser.parse()
	print parser.C
	print parser.getTrees()
	#print parser.derivations()
	#pre-t
	#print parser.D

	#print parser.getTrees()
else:
	from StochasticGrammar import *
	from CYKProb import *
	
	G=StochasticGrammar('S')
	G.add_rules_from_file('gramm_test')
	G.init_chances()
コード例 #5
0
def output(partId, ch_aux):
    """Uses the student code to compute the output for test cases."""

    res = CYK.Start("testCYK.in")
    return res
コード例 #6
0
PoSgood = defaultdict(float)
PoStot = defaultdict(float)
parsable, senttot = 0, 0
start = time()
textfile = open('bail.txt', 'w')
for sentence in testSet:
    st = SentenceTree(sentence)
    st.extractStructure()
    st.cleanHyphen()
    truePoSlist = st.root.PoSlist()
    s = st.root.printSentence()
    # print('Affecting a PoS to every token...')
    nodeList = sentence2PoS(s, pcfg.lexicon, words, total_embeddings)
    st.root.addGrammar(pcfg)
    # print('Executing CYK...')
    c = CYK(pcfg, nodeList)
    # print('Undoing the CNF...')
    if c is not None:
        affectedPoSlist = c.PoSlist()
        assert len(truePoSlist) == len(affectedPoSlist)
        for i in range(len(affectedPoSlist)):
            PoStot[truePoSlist[i]] += 1
            if affectedPoSlist[i] == truePoSlist[i]:
                PoSgood[truePoSlist[i]] += 1
                ngood += 1
            ntot += 1
        parsable += 1
        senttot += 1
        c.remove_BIN()
        c.remove_TERM()
        # print(c.sentence2bracketed())
コード例 #7
0
import CYK

if __name__ == "__main__":

    input_files = ["Input_Files\\grammar.txt", "Input_Files\\sentence.txt"]

    CFG_grammer = CYK.Grammar(input_files)

    print("_" * 150)
    print("Main CFG Grammar")
    for item in CFG_grammer:
        print(item[0], "-->", item[1:])

    CNF_grammar = CYK.CFG_to_CNF(CFG_grammer)

    print("_" * 150)
    print("Main CFG Grammar converted to Chumskey Normal Form(CNF):")
    for item in CNF_grammar[2]:
        print(item[0], "-->", item[1:])

    print("_" * 150)
    print("List of non-terminal symbols")
    print(CNF_grammar[0])

    print("_" * 150)
    print("List of terminal symbols")
    print(CNF_grammar[1])

    sentence = CYK.Sentence(input_files)
    print("_" * 150)
    print("Sentence to parse")
コード例 #8
0
    # print(syntax)
    return syntax

Terminals, CNF = CFG2CNF.CFG2CNF("model.txt")
languages = open(sys.argv[1]).read()

# print(languages)
for token in tokens:
    read = len(token)
    for i in range(len(languages)-read+1):
        CC = languages[i:i+read]
        if CC == token:
            # print(CC)
            if not (languages[i-1] == " "):
                # print("[i:]", languages[i:])
                languages = languages[:i] + " " + languages[i:]
            elif not ( i == len(languages)-2):
                if not (languages[i+read+1] == " "):
                    languages = languages[:i+read] + " " + languages[i+read:]
# print(languages)
languages = readSyntax(Terminals, languages)
# print(languages)
# remove endl in end of syntax
if languages[-1] == "ENDL":
    languages = languages[:-1]

print(languages)
if ( CYK.CYK(CNF, 'S0', languages) ):
    print("Compile success .... ")
else :
    print("Syntax error!")
コード例 #9
0
ファイル: main.py プロジェクト: MuslimMujahid/Tubes-2-TBFO
varContainer += ['A12', 'B12', 'C12', 'D12', 'E12', 'F12', 'G12', 'H12', 'I12', 'J12', 'K12', 'L12', 'M12', 'N12', 'O12', 'P12', 'Q12', 'R12', 'S12', 'T12', 'U12', 'V12', 'W12', 'X12', 'Y12', 'Z12']

def readSyntax(Terminals, languages):
    syntax = languages
    syntax = syntax.replace("\t", "") 
    syntax = syntax.replace("\n", " ENDL ")
    syntax = syntax.split(" ")
    for x in range (syntax.count('')):
        syntax.remove('')
    for i in range(len(syntax)):
        if syntax[i] not in Terminals and len(syntax[i]) > 0:
            syntax[i] = "NAME"
    return syntax


Terminals, V, Productions = CFG.loadModel("model.txt")
varContainer = CFG.getNotUsedVariables(V, varContainer)

Productions, V, varContainer = CFG2CNF.START(Productions, V, varContainer)
Productions, V, varContainer = CFG2CNF.REMOVE_NULL_PRODUCTIONS(Productions, V, varContainer)
Productions, V, varContainer = CFG2CNF.REMOVE_UNIT_PRODUCTIONS(Productions, V, varContainer)
Productions, V, varContainer = CFG2CNF.REMOVE_MORE_THAN_2_VARIABLES_PRODUCTION(Productions, V, varContainer)
Productions, V, varContainer = CFG2CNF.REMOVE_TERM_PRODUCTION(Productions, V, varContainer) 

languages = open("syntax.txt").read()
languages = readSyntax(Terminals, languages)
for x in Productions:
    print(x)
print(languages)
CYK.CYK(Productions, 'S0', languages)
コード例 #10
0
def showMenu():
    print("Leitor de Grámatica, escolha uma opção para exibir:\n")
    print("1  - Terminais")
    print("2  - Variaveis")
    print("3  - Simbolo Inicial")
    print("4  - Regras de Producao")
    print("5  - Imprimir toda a gramática")
    print("6  - Resultado da Remoção de Símbolos Inúteis")
    print("7  - Resultado da Remoção de Produções Vazias")
    print("8  - Resultado das Produções que Substituem Variáveis")
    print("9  - Resultado da Forma Normal de Chomsky")
    print("10 - Executar algoritmo de Cocke-Younger-Kasami")
    print("X  - Sair")

    option = input("-> ")
    option = str(option)
    print("\n")

    if option == "1":
        print("Terminais: ")
        printList(terminals)

    elif option == "2":
        print("Variaveis:")
        printList(variables)

    elif option == "3":
        print("Simbolo Inicial:")
        print("".join(initial))

    elif option == "4":
        print("Regras:")
        print("\n".join(map(lambda x: x[0] + " -> " + " | ".join(x[1]),
                            rules)))

    elif option == "5":
        printGrammar(terminals, variables, initial, rules)

    elif option == "6":
        G2 = RemoveUselessSymbols.removeUselessSymbols(terminals, variables,
                                                       initial, rules)
        printGrammar(G2[0], G2[1], G2[2], G2[3])

    elif option == "7":
        G2 = RemoveEmptyProductions.removeEmptyProductions(
            terminals, variables, initial, rules)
        printGrammar(G2[0], G2[1], G2[2], G2[3])

    elif option == "8":
        G2 = ProductionsSubstituteVariables.generate(terminals, variables,
                                                     initial, rules)
        printGrammar(G2[0], G2[1], G2[2], G2[3])

    elif option == "9":
        print("Gramática simplificada:")
        GS = NormalFormOfChomsky.firstStep(terminals, variables, initial,
                                           rules)
        printGrammar(GS[0], GS[1], GS[2], GS[3])
        print("\nGramática na FNC:")
        G2 = NormalFormOfChomsky.generate(terminals, variables, initial, rules)
        printGrammar(G2[0], G2[1], G2[2], G2[3])

    elif option == "10":
        word = input("Palavra a ser verificada: ")
        if CYK.run((terminals, variables, initial, rules), word):
            print("Palavra aceita!")
        else:
            print("Palavra rejeitada!")

    else:
        return -1

    input("\nPressione uma tecla para continuar.")

    return showMenu()