public bool WriteEntitiesNames(ViewsManager views, EntityLabelResult entityLabels) { if (entityLabels.Labels == null) { return(false); } var connection = views.MainForm.adapterEntities.Connection; if (connection.State != ConnectionState.Open) { connection.Open(); } //Delete unnecessary labels var query = "DELETE * FROM Entities "; if (entityLabels.Labels.Any()) { query += "WHERE Entity NOT IN ("; bool first = true; foreach (var label in entityLabels.Labels) { if (first) { query += "\"" + label + "\""; first = false; } else { query += ", " + "\"" + label + "\""; } } query += ")"; } var cmd = new OleDbCommand(query, connection); cmd.ExecuteNonQuery(); var insertQuery = "INSERT INTO [Entities] (Entity) VALUES (@Entity)"; var checkQuery = "SELECT COUNT (Id) FROM [Entities] WHERE Entity = @Entity"; foreach (var label in entityLabels.Labels) { var cmdCheck = new OleDbCommand(checkQuery, connection); cmdCheck.Parameters.AddWithValue("@Entity", label); int count = (int)cmdCheck.ExecuteScalar(); if (count != 1) { var cmdInsert = new OleDbCommand(insertQuery, connection); cmdInsert.Parameters.AddWithValue("@Entity", label); cmdInsert.ExecuteNonQuery(); } } return(true); }
public EntityLabelResult GetEntityLabelsFromSqlite(string sqliteFilePath) { EntityLabelResult result = new EntityLabelResult(); result.Labels = new List <string>(); using (var test = new SQLiteConnection(sqliteFilePath)) { var query = test.Table <EntityNames>(); foreach (var row in query) { result.Labels.Add(row.label); } } return(result); }
public bool RunAction(ViewsManager views) { try { /////////////////////////////////////////////////////////////////////////////// ReportProgress(0); /////////////////////////////////////////////////////////////////////////////// if (!StartEntitiesPlumbingOperation(_parm.GetFullPath(_paramsFileName))) { return(false); } /////////////////////////////////////////////////////////////////////////////// WaitForExit(); /////////////////////////////////////////////////////////////////////////////// if (_parm.Command == "Calculate") { ReportProgress(70); CalculatedEntitiesResult result = null; if (File.Exists(_parm.GetFullPath(_parm.OutputFileName))) { var json = File.ReadAllText(_parm.GetFullPath(_parm.OutputFileName)); result = JsonConvert.DeserializeObject <CalculatedEntitiesResult>(json); } if (result == null) { return(false); } if (!WriteEntitiesNames(views, result.EntityLabels)) { return(false); } if (!WriteEntitiesValues(views, result.Documents)) { return(false); } if (!CalculateStatistics(views, result)) { return(false); } } else if (_parm.Command == "GetEntityNames") { ReportProgress(50); // write to database EntityLabelResult result = null; if (File.Exists(_parm.GetFullPath(_parm.OutputFileName))) { var json = File.ReadAllText(_parm.GetFullPath(_parm.OutputFileName)); result = JsonConvert.DeserializeObject <EntityLabelResult>(json); } if (result == null) { return(false); } if (!WriteEntitiesNames(views, result)) { return(false); } } return(true); } catch (Exception e) { HandleException(e); } return(false); }
public EntityLabelResult GetEntityNames(string pythonFile) { EntityLabelResult results = new EntityLabelResult(); results.Labels = new List <string>(); string commandPath; commandPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly() .Location), "spaCyEntities"); Directory.SetCurrentDirectory(commandPath); var redirectConsole = !Properties.Settings.Default.ShowPythonWindow; _process = new Process { StartInfo = new ProcessStartInfo("cmd.exe") { UseShellExecute = false, RedirectStandardOutput = redirectConsole, RedirectStandardError = redirectConsole, RedirectStandardInput = true, CreateNoWindow = redirectConsole }, EnableRaisingEvents = true }; if (redirectConsole) { //_process.OutputDataReceived += process_OutputDataReceived; _process.ErrorDataReceived += process_ErrorDataReceived; } _process.Exited += process_Exited; _process.Start(); if (!String.IsNullOrEmpty(_param.AnacondaPath)) { string activateEnv; activateEnv = "\"" + Path.Combine(_param.AnacondaPath, "Scripts", "activate.bat") + "\"" + " " + _param.VirtualEnv; _process.StandardInput.WriteLine(activateEnv); } var pythonarguments = String.Format(@"""{0}""", pythonFile); _process.StandardInput.WriteLine("python " + pythonarguments); _process.StandardInput.WriteLine("exit"); var output = _process.StandardOutput.ReadToEnd(); bool start = false; foreach (var line in output.Replace("\r", "").Split('\n')) { if (line.Contains("--------------------------")) { start = !start; continue; } if (start) { results.Labels.Add(line); } } _process.WaitForExit(); return(results); }