def ucto(string): import terminal """Tokenizes a string with Ucto""" open('uctofile','w').write(string) result = terminal.command('ucto -L nl uctofile',True) os.remove('uctofile') return result.replace('<utt>','')
def startserver(self, model, port=None): """ Start a new Timbl server""" if not port: port = self._getnewport() terminal.command(self.cmdstring % (model, port))
def train_model(self, filename): """Train a model on the basis of these ngrams""" terminal.command("timbl -f " + filename + " -I " + filename + ".IGTree +D +vdb -a1 -p 1000000", False) return filename.replace(".training.txt", "")
def start_servers(self, model, look_for_existing): """Starts the necessary servers and connects to them""" if self.approach == "w": foldername = "wordmodels" backupname = "nlsave" elif self.approach == "l": foldername = "lettermodels" backupname = "nlsmall" # Personal model succeeded = False while not succeeded: if look_for_existing: port = get_port_for_timblserver(model + ".training.txt.IGTree") else: port = False if not port: port = get_free_port() terminal.command( "timblserver -i " + model + ".training.txt.IGTree +vdb +D -a1 -G +vcf -S " + str(port) + " -C 1000" ) s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s1.connect(("", port)) s1.recv(1024) succeeded = True except socket.error: pass # General model succeeded = False while not succeeded: if look_for_existing: port = get_port_for_timblserver(foldername + "/" + backupname + ".training.txt.IGTree") else: port = False if not port: port = get_free_port() terminal.command( "timblserver -i " + foldername + "/" + backupname + ".training.txt.IGTree +vdb +D -a1 -G +vcf -S " + str(port) + " -C 1000" ) s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s2.connect(("", port)) s2.recv(1024) succeeded = True except socket.error: pass self.sockets = [s1, s2]
except IOError: print("Model not found. Prepare data to create a new one:") training_file, testfile, lexicon = ss.prepare_training_data(inp) print("Training model") model = ss.train_model(training_file) finally: ss.model = model # If the user has his own testfile, abandon the automatically generated one if testfile_preset: testfile = testfile_preset # Go do the prediction in one of the modes, with the model if settings["mode"] == "d": demo_mode(ss, lexicon) elif settings["mode"] == "s": simulation_mode(model, lexicon, testfile, settings) elif settings["mode"] == "server": server_mode(settings) # Close everything if settings["close_server"]: terminal.command("killall timblserver") # TODO # Server modus # Mogelijk: werkt het beter als je na lang doorgaan stopt met suggereren? # Lettermodus: gaat er ergens iets fout? # Haakje sluiten in simulatiemodus # Backspace: fout als je over de kleurgrenzen heen backspacet gaan de kleuren raar doen