def create_forests(self, filename=None, clear=False): """ This will read sentences to parse. One sentence per line, no periods etc. :param filename: not used :param clear: start with empty """ if clear: treelist = ['hello'] else: treelist = self.load_treelist_from_text_file(self.__class__.default_treeset_file) or \ ['hello'] # Clear this screen before we start creating a mess ctrl.disable_undo() # disable tracking of changes (e.g. undo) if self.forest: self.forest.retire_from_drawing() self.forests = [] grammar = load_grammar(filename=running_environment.plugins_path + '/mgtdbpE/mg0.txt') for line in treelist: sentence = line.strip() if (not sentence) or sentence.startswith('#'): continue syn = classes.get('SyntaxConnection')() syn.sentence = sentence syn.lexicon = grammar forest = Forest(gloss_text=sentence, syntax=syn) self.forests.append(forest) self.current_index = 0 self.forest = self.forests[0] # allow change tracking (undo) again ctrl.resume_undo()
def create_forests(self, treelist=None): """ This will read sentences to parse. One sentence per line, no periods etc. :param treelist: lines of file like above. """ if not treelist: treelist = [] # Clear this screen before we start creating a mess ctrl.disable_undo() # disable tracking of changes (e.g. undo) if self.forest: self.forest.retire_from_drawing() self.forests = [] start = 0 end = 10 for line in treelist: sentence = line.strip() if (not sentence) or sentence.startswith('#'): continue forest = Forest(gloss_text=sentence) self.forests.append(forest) parser = Parser(grammar.g, -0.0001, forest=forest) my_success, my_dnodes = parser.parse(sentence=sentence, start='C') print(my_success) self.current_index = 0 self.forest = self.forests[0] # allow change tracking (undo) again ctrl.resume_undo()
def create_forests(self, filename=None, clear=False): """ This will read example sentences in form used by Ginsburg / Fong parser 1 Tom read a book. (Chomsky 2015:10) ['C','Tpres',['D','n','Tom'],'v*','read','a','n', 'book'] 2 They expected John to win. (Chomsky 2015:10) ['C','Tpast',['D','n','they'],'v*','expect', 'toT',['D','n','John'],'vUnerg','win'] 3 Who do you expect to win. (Chomsky 2015:10) ['C_Q','Tpres',['D','n','you'],'v*','expect', 'toT',['Q','n','who'],'vUnerg','win'] :param treelist: lines of file like above. Lines that don't begin with number are ignored. """ if clear: treelist = [] else: treelist = self.load_treelist_from_text_file( self.__class__.default_treeset_file) or [] # Clear this screen before we start creating a mess ctrl.disable_undo() # disable tracking of changes (e.g. undo) if self.forest: self.forest.retire_from_drawing() self.forests = [] start = 0 end = 10 ug = Generate() for line in treelist: if "Japanese" in line: ug.language = "Japanese" elif "English" in line: ug.language = "English" sentence, lbracket, target_str = line.partition('[') if not (sentence and lbracket and target_str): continue sentence = sentence.strip() sentence_number = sentence[0] if not sentence_number.isdigit(): continue sentence_number = int(sentence_number) if end and sentence_number > end: break elif sentence_number < start: continue sentence = sentence[2:] # remove number and the space after it target_example = eval(lbracket + target_str) ug.out(sentence_number, sentence, target_example) forest = Forest(gloss_text=sentence) self.forests.append(forest) so = ug.generate_derivation(target_example, forest=forest) ug.out("MRGOperations", ug.merge_counter) ug.out("FTInheritanceOp", ug.inheritance_counter) ug.out("FTCheckOp", ug.feature_check_counter) self.current_index = 0 self.forest = self.forests[0] # allow change tracking (undo) again ctrl.resume_undo()
def __init__(self, name=None, filename=None, clear=False): super().__init__() self.name = name or filename or 'New project' self.filename = filename self.forests = [Forest()] self.current_index = 0 self.forest = None self.lexicon = {} self.structures = OrderedDict() self.constituents = OrderedDict() self.features = OrderedDict()
def new_forest(self): """ Add a new forest after the current one. :return: tuple (current_index (int), selected forest (Forest) """ ctrl.undo_pile = set() #ctrl.undo_disabled = True if self.forest: self.forest.retire_from_drawing() forest = Forest() self.current_index += 1 self.poke( 'forests') # <-- announce change in watched list-like attribute self.forests.insert(self.current_index, forest) self.forest = forest # <-- at this point the signal is sent to update UI #ctrl.undo_disabled = False return self.current_index, self.forest
def create_forests(self, filename=None, clear=False): """ This will read list of strings where each line defines a trees or an element of trees. This can be used to reset the KatajaDocument if no treeset or an empty treeset is given. It is common to override this method in plugins to provide custom commands for e.g. running parsers. Example of tree this can read: [.AspP [.Asp\\Ininom] [.vP [.KP [.K\\ng ] [.DP [.D´ [.D ] [.NP\\lola ]] [.KP [.K\\ng] [.DP [.D´ [.D ] [.NP\\alila ] ] [.KP\\{ni Maria} ]]]]] [.v´ [.v ] [.VP [.V ] [.KP\\{ang tubig}]]]]] Ininom = drank ng = NG ng = NG lola = grandma alila = servant ni Maria = NG Maria ang tubig = ANG water 'Maria's grandmother's servant drank the water' :param filename: (optional) file to load from :param clear: (optional) if True, start with an empty treeset and don't attempt to load examples """ print('************* create forests ****************') if clear: treelist = [] else: treelist = self.load_treelist_from_text_file( self.__class__.default_treeset_file) or [] # Clear this screen before we start creating a mess ctrl.disable_undo() # disable tracking of changes (e.g. undo) if self.forest: self.forest.retire_from_drawing() self.forests = [] # buildstring is the bracket trees or trees. buildstring = [] # definitions includes given definitions for constituents of this trees definitions = {} # gloss_text is the gloss for whole trees gloss_text = '' # comments are internal notes about the trees, displayed as help text or something comments = [] started_forest = False syntax_class = classes.get('SyntaxConnection') for line in treelist: line = line.strip() #line.split('=', 1) parts = line.split('=', 1) # comment line if line.startswith('#'): if started_forest: comments.append(line[1:]) # Definition line elif len(parts) > 1 and not line.startswith('['): started_forest = True word = parts[0].strip() values = parts[1] definitions[word] = values # Gloss text: elif line.startswith("'"): if started_forest: if line.endswith("'"): line = line[:-1] gloss_text = line[1:] # empty line: finalize this forest elif started_forest and not line: syn = syntax_class() syn.sentence = buildstring syn.lexicon = definitions forest = Forest(gloss_text=gloss_text, comments=comments, syntax=syn) self.forests.append(forest) started_forest = False # trees definition starts a new forest elif line and not started_forest: started_forest = True buildstring = line definitions = {} gloss_text = '' comments = [] # another trees definition, append to previous elif line: buildstring += '\n' + line if started_forest: # make sure that the last forest is also added syn = syntax_class() syn.sentence = buildstring syn.lexicon = definitions forest = Forest(gloss_text=gloss_text, comments=comments, syntax=syn) self.forests.append(forest) if not self.forests: syn = syntax_class() forest = Forest(gloss_text='', comments=[], syntax=syn) self.forests.append(forest) self.current_index = 0 self.forest = self.forests[0] # allow change tracking (undo) again ctrl.resume_undo()
def __init__(self, kataja_app, no_prefs=False, reset_prefs=False): """ KatajaMain initializes all its children and connects itself to be the main window of the given application. Receives launch arguments: :param no_prefs: bool, don't load or save preferences :param reset_prefs: bool, don't attempt to load preferences, use defaults instead """ QtWidgets.QMainWindow.__init__(self) kataja_app.processEvents() SavedObject.__init__(self) self.use_tooltips = True self.available_plugins = {} self.setDockOptions(QtWidgets.QMainWindow.AnimatedDocks) self.setCorner(QtCore.Qt.TopLeftCorner, QtCore.Qt.LeftDockWidgetArea) self.setCorner(QtCore.Qt.TopRightCorner, QtCore.Qt.RightDockWidgetArea) self.setCorner(QtCore.Qt.BottomLeftCorner, QtCore.Qt.LeftDockWidgetArea) self.setCorner(QtCore.Qt.BottomRightCorner, QtCore.Qt.RightDockWidgetArea) x, y, w, h = (50, 50, 1152, 720) self.setMinimumSize(w, h) self.app = kataja_app self.save_prefs = not no_prefs self.forest = None self.fontdb = QtGui.QFontDatabase() self.color_manager = PaletteManager() self.settings_manager = Settings() self.forest_keepers = [] self.forest_keeper = None ctrl.late_init(self) classes.late_init() prefs.import_node_classes(classes) self.syntax = SyntaxConnection() prefs.load_preferences(disable=reset_prefs or no_prefs) qt_prefs.late_init(running_environment, prefs, self.fontdb, log) self.settings_manager.set_prefs(prefs) self.color_manager.update_custom_colors() self.find_plugins(prefs.plugins_path or running_environment.plugins_path) self.setWindowIcon(qt_prefs.kataja_icon) self.graph_scene = GraphScene(main=self, graph_view=None) self.graph_view = GraphView(main=self, graph_scene=self.graph_scene) self.graph_scene.graph_view = self.graph_view self.ui_manager = UIManager(self) self.settings_manager.set_ui_manager(self.ui_manager) self.ui_manager.populate_ui_elements() # make empty forest and forest keeper so initialisations don't fail because of their absence self.visualizations = VISUALIZATIONS self.init_forest_keepers() self.settings_manager.set_document(self.forest_keeper) kataja_app.setPalette(self.color_manager.get_qt_palette()) self.forest = Forest() self.settings_manager.set_forest(self.forest) self.change_color_theme(prefs.color_theme, force=True) self.update_style_sheet() self.graph_scene.late_init() self.setCentralWidget(self.graph_view) self.setGeometry(x, y, w, h) self.setWindowTitle(self.tr("Kataja")) self.print_started = False self.show() self.raise_() kataja_app.processEvents() self.activateWindow() self.status_bar = self.statusBar() self.install_plugins() self.load_initial_treeset() log.info('Welcome to Kataja! (h) for help') #ctrl.call_watchers(self.forest_keeper, 'forest_changed') # toolbar = QtWidgets.QToolBar() # toolbar.setFixedSize(480, 40) # self.addToolBar(toolbar) gestures = [ QtCore.Qt.TapGesture, QtCore.Qt.TapAndHoldGesture, QtCore.Qt.PanGesture, QtCore.Qt.PinchGesture, QtCore.Qt.SwipeGesture, QtCore.Qt.CustomGesture ] #for gesture in gestures: # self.grabGesture(gesture) self.action_finished(undoable=False) self.forest.undo_manager.flush_pile()