コード例 #1
0
        static private DicResult getTranslationData(string filename)
        {
            StreamReader SR  = new StreamReader(filename);
            string       str = SR.ReadToEnd();

            XmlSerializer xml = new XmlSerializer(typeof(DicResult));
            DicResult     res = (DicResult)xml.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(str)));

            return(res);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: Stio001/translator
        public void FillToRichTextBox(string strTranslation, DicResult dicResult)
        {
            toRichTextBox.Text += strTranslation;

            if (dicResult.Definitions.Count > 0)
            {
                toRichTextBox.Text += "\n____________________________\nВозможные переводы:";

                foreach (var tr in dicResult.Definitions[0].Translations)
                {
                    toRichTextBox.Text += "\n\t" + tr.Text;
                }
            }
        }
コード例 #3
0
        static public string GetPos(string word)
        {
            string filename = getDataFile(word);

            if (filename == null)
            {
                Console.WriteLine(" no trans for " + word);
                return(null);
            }


            DicResult trans = getTranslationData(filename);

            if ((trans.def == null) || (trans.def.tr.Count == 0))
            {
                Console.WriteLine(" no trans for " + word);
                return(null);
            }

            return(trans.def.pos);
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: Stio001/translator
        private void TranslateButton_Click(object sender, EventArgs e)
        {
            toRichTextBox.Clear();

            using (SQLiteConnection connection = new SQLiteConnection("DataSource=saved_translations.db"))
            {
                connection.Open();

                LangPair langPair = new LangPair(translator.DetectLang(fromRichTextBox.Text), GetLang(toLangComboBox.SelectedItem.ToString()));

                SQLiteCommand   sqlCommand   = new SQLiteCommand("SELECT EXISTS (SELECT * FROM translation_cache WHERE from_str = @from_str and lang_pair = @lang_pair)", connection);
                SQLiteParameter fromStrParam = new SQLiteParameter("@from_str", fromRichTextBox.Text);
                sqlCommand.Parameters.Add(fromStrParam);
                SQLiteParameter langPairParam = new SQLiteParameter("@lang_pair", langPair.ToString());
                sqlCommand.Parameters.Add(langPairParam);

                try
                {
                    string isTranslationExists = sqlCommand.ExecuteScalar().ToString();

                    if (isTranslationExists == "0")
                    {
                        Translation translation;

                        switch (fromLangComboBox.SelectedItem.ToString())
                        {
                        case "Автоопределение":
                            translation = translator.Translate(fromRichTextBox.Text, langPair);
                            break;

                        default:
                            translation = translator.Translate(fromRichTextBox.Text, langPair);
                            break;
                        }

                        DicResult dicResult = dictionary.Lookup(langPair, fromRichTextBox.Text);

                        FillToRichTextBox(translation.Text, dicResult);

                        sqlCommand.CommandText = "INSERT INTO translation_cache (from_str, to_str, lang_pair, dic_result)" +
                                                 "VALUES (@from_str, @to_str, @lang_pair, @dic_result)";
                        SQLiteParameter toStrParam = new SQLiteParameter("@to_str", translation.Text);
                        sqlCommand.Parameters.Add(toStrParam);
                        SQLiteParameter dicResultParam = new SQLiteParameter("@dic_result", JsonConvert.SerializeObject(dicResult));
                        sqlCommand.Parameters.Add(dicResultParam);
                        sqlCommand.ExecuteNonQuery();
                    }
                    else
                    {
                        sqlCommand.CommandText = "SELECT to_str FROM translation_cache WHERE from_str = @from_str and lang_pair = @lang_pair";
                        string strTranslaton = sqlCommand.ExecuteScalar().ToString();

                        sqlCommand.CommandText = "SELECT dic_result FROM translation_cache WHERE from_str = @from_str and lang_pair = @lang_pair";

                        DicResult dicResult = JsonConvert.DeserializeObject <DicResult>(sqlCommand.ExecuteScalar().ToString());

                        FillToRichTextBox(strTranslaton, dicResult);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }