private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            OpusCatMTEngineSettings.Default.MtServicePort     = this.ServicePortBox;
            OpusCatMTEngineSettings.Default.HttpMtServicePort = this.HttpServicePortBox;
            OpusCatMTEngineSettings.Default.StoreOpusCatDataInLocalAppdata = this.StoreDataInAppdata;
            OpusCatMTEngineSettings.Default.DatabaseRemovalInterval        = Int32.Parse(this.DatabaseRemovalInterval);
            if (OpusCatMTEngineSettings.Default.CacheMtInDatabase != this.CacheMtInDatabase)
            {
                OpusCatMTEngineSettings.Default.CacheMtInDatabase = this.CacheMtInDatabase;
                //This checks whether the option can be enabled.
                TranslationDbHelper.SetupTranslationDb();
                this.CacheMtInDatabase = OpusCatMTEngineSettings.Default.CacheMtInDatabase;
            }
            OpusCatMTEngineSettings.Default.UseDatabaseRemoval = this.UseDatabaseRemoval;
            if (OpusCatMTEngineSettings.Default.DisplayOverlay != this.DisplayOverlay)
            {
                OpusCatMTEngineSettings.Default.DisplayOverlay = this.DisplayOverlay;
                if (this.DisplayOverlay)
                {
                    App.OpenOverlay();
                }
                else
                {
                    App.CloseOverlay();
                }
            }

            OpusCatMTEngineSettings.Default.Save();

            NotifyPropertyChanged("SaveButtonEnabled");
        }
Esempio n. 2
0
 private void MainWindow_Loaded(object sender, RoutedEventArgs e)
 {
     this.Loaded -= MainWindow_Loaded;
     if (OpusCatMTEngineSettings.Default.CacheMtInDatabase)
     {
         TranslationDbHelper.SetupTranslationDb();
     }
 }
        private void WriteToTranslationDb(object sender, EventArgs e, IEnumerable <string> input, FileInfo spInput, FileInfo transAndAlign)
        {
            Queue <string> inputQueue
                = new Queue <string>(input);

            if (transAndAlign.Exists)
            {
                using (var reader = transAndAlign.OpenText())
                {
                    while (!reader.EndOfStream)
                    {
                        var line       = reader.ReadLine();
                        var sourceLine = inputQueue.Dequeue();
                        var transPair  = new TranslationPair(sourceLine, line, this.segmentation, this.TargetCode);
                        TranslationDbHelper.WriteTranslationToDb(sourceLine, transPair, this.SystemName, this.segmentation, this.TargetCode);
                    }
                }
            }
        }
Esempio n. 4
0
        private void WriteToTranslationDb(object sender, EventArgs e, IEnumerable <string> input, FileInfo spOutput)
        {
            Queue <string> inputQueue
                = new Queue <string>(input);

            if (spOutput.Exists)
            {
                using (var reader = spOutput.OpenText())
                {
                    while (!reader.EndOfStream)
                    {
                        var line       = reader.ReadLine();
                        var nonSpLine  = (line.Replace(" ", "")).Replace("▁", " ").Trim();
                        var sourceLine = inputQueue.Dequeue();
                        TranslationDbHelper.WriteTranslationToDb(sourceLine, nonSpLine, this.SystemName);
                    }
                }
            }
        }
 internal static void WriteTranslationToDb(
     string sourceText,
     TranslationPair translation,
     string model,
     SegmentationMethod segmentationMethod,
     string targetLanguage)
 {
     TranslationDbHelper.shortTermMtStorage.GetOrAdd(new Tuple <string, string>(sourceText, model), translation);
     if (OpusCatMTEngineSettings.Default.CacheMtInDatabase)
     {
         try
         {
             TranslationDbHelper.WriteTranslationToSqliteDb(sourceText, translation, model, segmentationMethod, targetLanguage);
         }
         catch (Exception ex)
         {
             Log.Error(ex.ToString());
             TranslationDbHelper.SetupTranslationDb();
         }
     }
 }
        private static void WriteTranslationToSqliteDb(
            string sourceText,
            TranslationPair translation,
            string model,
            SegmentationMethod segmentationMethod,
            string targetLanguage)
        {
            var translationDb = new FileInfo(HelperFunctions.GetOpusCatDataPath(OpusCatMTEngineSettings.Default.TranslationDBName));

            if (translationDb.Length == 0)
            {
                translationDb.Delete();
            }

            if (!translationDb.Exists)
            {
                TranslationDbHelper.CreateTranslationDb();
            }

            using (var m_dbConnection = new SQLiteConnection($"Data Source={translationDb};Version=3;"))
            {
                m_dbConnection.Open();

                using (SQLiteCommand insert =
                           new SQLiteCommand(
                               "INSERT or REPLACE INTO translations (sourcetext, translation, segmentedsource, segmentedtranslation, alignment, model, additiondate, segmentationmethod, targetlanguage) VALUES (@sourcetext,@translation,@segmentedsource,@segmentedtranslation,@alignment,@model,CURRENT_TIMESTAMP,@segmentationmethod,@targetlanguage)", m_dbConnection))
                {
                    insert.Parameters.Add(new SQLiteParameter("@sourcetext", sourceText));
                    insert.Parameters.Add(new SQLiteParameter("@translation", translation.Translation));
                    insert.Parameters.Add(new SQLiteParameter("@segmentedsource", String.Join(" ", translation.SegmentedSourceSentence)));
                    insert.Parameters.Add(new SQLiteParameter("@segmentedtranslation", String.Join(" ", translation.SegmentedTranslation)));
                    insert.Parameters.Add(new SQLiteParameter("@alignment", translation.AlignmentString));
                    insert.Parameters.Add(new SQLiteParameter("@model", model));
                    insert.Parameters.Add(new SQLiteParameter("@segmentationmethod", segmentationMethod.ToString()));
                    insert.Parameters.Add(new SQLiteParameter("@targetlanguage", targetLanguage));
                    insert.ExecuteNonQuery();
                }
            }
        }
        internal static void SetupTranslationDb()
        {
            var translationTableColumns =
                new List <string>()
            {
                "model",
                "sourcetext",
                "translation",
                "segmentedsource",
                "segmentedtranslation",
                "alignment",
                "additiondate",
                "segmentationmethod",
                "targetlanguage"
            };

            var translationDb = new FileInfo(HelperFunctions.GetOpusCatDataPath(OpusCatMTEngineSettings.Default.TranslationDBName));

            //If the translation db has a size of 0, it should be deleted (size 0 dbs are caused
            //by db creation bugs).
            if (translationDb.Exists && translationDb.Length == 0)
            {
                translationDb.Delete();
            }

            //Check that db structure is current
            bool tableValid = true;

            if (translationDb.Exists)
            {
                using (var m_dbConnection =
                           new SQLiteConnection($"Data Source={translationDb.FullName};Version=3;"))
                {
                    m_dbConnection.Open();

                    using (SQLiteCommand verify_table =
                               new SQLiteCommand("PRAGMA table_info(translations);", m_dbConnection))
                    {
                        SQLiteDataReader r = verify_table.ExecuteReader();

                        List <string> tableColumnStrikeoutList = new List <string>(translationTableColumns);

                        while (r.Read())
                        {
                            var columnName = Convert.ToString(r["name"]);
                            if (tableColumnStrikeoutList[0] == columnName)
                            {
                                tableColumnStrikeoutList.RemoveAt(0);
                            }
                            else
                            {
                                tableValid = false;
                            }
                        }

                        tableValid = tableValid && !tableColumnStrikeoutList.Any();
                        verify_table.Dispose();
                    }
                    m_dbConnection.Close();
                    m_dbConnection.Dispose();
                }
            }

            if (!tableValid)
            {
                MessageBoxResult result = MessageBox.Show(OpusCatMTEngine.Properties.Resources.App_InvalidDbMessage,
                                                          OpusCatMTEngine.Properties.Resources.App_ConfirmDbCaption,
                                                          MessageBoxButton.OKCancel,
                                                          MessageBoxImage.Question);
                if (result == MessageBoxResult.OK)
                {
                    translationDb.Delete();
                }
                else
                {
                    OpusCatMTEngineSettings.Default.CacheMtInDatabase = false;
                    OpusCatMTEngineSettings.Default.Save();
                    OpusCatMTEngineSettings.Default.Reload();
                }
            }

            translationDb.Refresh();
            if (!translationDb.Exists)
            {
                CreateTranslationDb();
            }

            //Remove old translation from the db (time period can be set in settings)
            if (OpusCatMTEngineSettings.Default.CacheMtInDatabase)
            {
                TranslationDbHelper.RemoveOldTranslations();
            }
        }