Example #1
0
def main():
    data = pd.read_csv(data_file_path)
    data = data.drop(columns=["Unnamed: 0"])

    (sentence, sentence_tokens) = readsentence()  # take input from user and save text, tokenized text

    if os.path.exists('mytagger.pkl'):
        # try to open a previously saved tagger
        input = open('mytagger.pkl', 'rb')
        mytagger = load(input)
        input.close()
    else:
        # no such tagger is found so train/save it
        mytagger = traintagger()
        output = open('mytagger.pkl', 'wb')
        dump(mytagger, output, -1)
        output.close()
    tagged_tokens = mytagger.tag(sentence_tokens)
    print(tagged_tokens)

    if os.path.exists('mypcfg.pickle'):
        # try to open a previously saved PCFG
        input = open('mypcfg.pickle', 'rb')
        mypcfg = load(input)
        input.close()
    else:
        # no such PCFG exists, so induce/save it
        mypcfg = buildpcfg()
        output = open('mypcfg.pickle', 'wb')
        dump(mypcfg, output)
        output.close()
    try:
        tree = sequence_matching(tagged_tokens)
        print("Sequence matching was used")

    except:
        parser = ViterbiParser(mypcfg)
        tree = parser.parse(tagged_tokens)
        print("Vitberi parser was used")
    finally:
        if not isinstance(tree, Tree):
            Tree.pretty_print(next(tree))  # do something to print it out, or print error message if input couldn't be parsed
        else: print(tree)

    df2 = {'sentence': sentence, 'sentence tokens': sentence_tokens, 'tagged tokens': tagged_tokens}
    data = data.append(df2, ignore_index=True)
    data.to_csv(data_file_path)

    print("Previous data:")
    print(data)
            productionrhs.append(node['type'])
            if(node['type']=='num'):
                val.append(node['num'])
            elif(node['type'] in ['id','rop']):
                val.append(node['buf'])

    for prod in grammardict[productionlhs]:
        print(prod['rhs'],productionrhs)
        if(prod['rhs']==productionrhs):
            print(prod['query'])
            eval(prod['query'])

tree=[]
genAST(parse_tree)
tr=Tree('main',[])
tr.pretty_print()
root=stack['program_node'][0]
def draw(root,tree):
	for i in root['children']:
		if(i is not None):
			if(root['name']=='if'):
				print(i['name'])
			if(i['leaf']):
				tree.append(Tree(i['name'],[i['value']]))
			else:
				tree.append(Tree(i['name'],[]))
		if(root['name']=='if'):
				tree.pretty_print()
	tr.pretty_print()
	j=0
	for i in root['children']: