def oracle_moves(gold_heads): dependents = defaultdict(int) for head in gold_heads: dependents[head] += 1 m = None config = Parser.initial_config(len(gold_heads)) i, stack, heads = config while not Parser.is_final_config(config): valid_moves = Parser.valid_moves(config) second_topmost = stack[-2] if len(stack) >= 2 else None topmost = stack[-1] if len(stack) >= 1 else None if Parser.LA in valid_moves and gold_heads[ second_topmost] == topmost and dependents[second_topmost] == 0: m = Parser.LA dependents[topmost] -= 1 elif Parser.RA in valid_moves and gold_heads[ topmost] == second_topmost and dependents[topmost] == 0: m = Parser.RA dependents[second_topmost] -= 1 elif Parser.SH in valid_moves: m = Parser.SH else: raise "Oracle failed, no moves valid" yield config, m config = Parser.next_config(config, m)
def predict(self, words, tags): pred_heads = [] config = Parser.initial_config(len(words)) while not Parser.is_final_config(config): valid_moves = Parser.valid_moves(config) features = self.featurize(words, tags, config) pred_moves = self.model.forward(features) best_m_s = [valid_moves[0], pred_moves[valid_moves[0]]] for m in valid_moves: if pred_moves[m] > best_m_s[1]: best_m_s = [m, pred_moves[m]] config = Parser.next_config(config, best_m_s[0]) return config[2]
def predict(self, words, tags): parser = Parser() # 1. Start in the initial configuration for the input sentence. config = parser.initial_config(len(words)) # 2. As long as there are valid moves, ask the averaged perceptron for the next move to take. while len(self.valid_moves(config)) != 0: features = self.featurize(words, tags, config) output_vector = self.model.forward(features) move = max(output_vector, key=output_vector.get) config = self.next_config(config, move) # 3. Return the list of heads associated with the final configuration. return config[2]
from dolfin import * from lib.MeshCreation import generate_footing_square, generate_boundary_measure from lib.Poromechanics import Poromechanics from lib.Parser import Parser from time import time import numpy as np parameters["mesh_partitioner"] = "ParMETIS" initial_time = time() parser = Parser() Nelements = 10 refinements = 0 if parser.options.N: Nelements = parser.options.N if parser.options.refinements: refinements = parser.options.refinements length = 64 mesh, markers, LEFT, RIGHT, TOP, BOTTOM, NONE = generate_footing_square( Nelements, length, refinements) neumann_solid_markers = [TOP] # All others get weakly 0'd. neumann_fluid_markers = [] dsNs = generate_boundary_measure(mesh, markers, neumann_solid_markers) dsNf = generate_boundary_measure(mesh, markers, neumann_fluid_markers) # Set up load terms fs_vol = ff_vol = fs_sur = lambda t: Constant((0., 0.)) def p_source(t): return Constant(0)
from lib.Contants import Constants from lib.Exporter import Exporter from lib.Parser import Parser if __name__ == '__main__': Constants.init() Parser.parseConfigJSON() Exporter.exportInitFile()
neg = [] for l in raw_input('\nplease input formula(or "END"): ').split('+'): words = l.split('-') pos.append(words[0]) for i in range(1, len(words)): neg.append(words[i]) return pos, neg if __name__ == '__main__': args = parser.parse_args() menu = args.menu in_path = args.in_path out_path = args.out_path if menu == 'parse': p = Parser('-Owakati') p.parse_file(in_path, out_path) elif menu == 'vectorize': v = Vectorizer(min_count=10) v.build_from_file(in_path) v.store(out_path) elif menu == 'calc': v = Vectorizer() v.load(in_path) while True: pos, neg = get_input() if pos[0] == 'END': break; else: v.calc(pos, neg)
def writeInitFile(self, categories): p = Parser(Constants.QTCREATOR_CONFIG_FILE) for cat in categories: self.f.write(p.parseIniFileCategorie(cat) + '\n')