Ejemplo n.º 1
0
        public void AddCorpus(string corpus_caption, string txt_files, CorpusFormat.CorpusFormatDecriptor corpus_format, bool do_index)
        {
            if (string.IsNullOrEmpty(corpus_caption))
            {
                throw new ArgumentException("corpus_caption");
            }

            if (string.IsNullOrEmpty(txt_files))
            {
                throw new ArgumentException("txt_files");
            }

            if (corpus_format == null)
            {
                throw new ArgumentNullException("corpus_format");
            }

            log4net.ILog log = log4net.LogManager.GetLogger(typeof(Corpora_ViewModel));
            log.InfoFormat("{0} corpus_caption={1} txt_files={1} format={2} do_index={3}", nameof(AddCorpus), corpus_caption, txt_files, corpus_format, do_index);

            //db_session.Clear();

            try
            {
                using (var db_session = session_factory.OpenSession())
                {
                    using (var tx = db_session.BeginTransaction())
                    {
                        DbObjectMappings.CorpusInfo new_corpus = new DbObjectMappings.CorpusInfo();
                        new_corpus.Caption      = corpus_caption;
                        new_corpus.TxtFilesPath = txt_files;
                        new_corpus.CorpusFormat = corpus_format.ToJSON();

                        db_session.Save(new_corpus);

                        tx.Commit();

                        log.Info("tx.Commit OK");

                        corpus_infos.Add(new_corpus);
                        SelectedCorpus = new_corpus;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }

            return;
        }
Ejemplo n.º 2
0
        private void btAddCorpora_Click(object sender, RoutedEventArgs e)
        {
            var  w   = new AddCorpusWindow();
            bool?res = w.ShowDialog();

            if (res.HasValue && res.Value)
            {
                string corpus_caption = w.tbCaption.Text.Trim();
                string txt_files      = w.tbtxtFile.Text.Trim();
                bool   do_index       = w.chbIndex.IsChecked.Value;

                CorpusFormat.CorpusFormatDecriptor format = new CorpusFormat.CorpusFormatDecriptor();
                if (w.rbSingleFileEOL.IsChecked.Value)
                {
                    format.files   = CorpusFormat.CorpusFormat_Files.SINGLE_FILE;
                    format.content = CorpusFormat.CorpusFormat_Content.LINES;
                }
                else
                {
                    throw new NotImplementedException();
                }

                if (((KeyValuePair <string, string>)w.cbxEncoding.SelectedValue).Key == "utf-8")
                {
                    format.encoding = CorpusFormat.CorpusFormat_Encoding.UTF8;
                }
                else
                {
                    throw new NotImplementedException();
                }


                ((Corpora_ViewModel)DataContext).AddCorpus(corpus_caption, txt_files, format, do_index);

                if (do_index)
                {
                    btReindexCorpus_Click(sender, e);
                }
            }
        }
Ejemplo n.º 3
0
        public void BuildIndex(string index_folder,
                               string corpus_file_path,
                               CorpusFormat.CorpusFormatDecriptor corpus_format,
                               ICancelIndexation cancellation, IShowIndexationProgress progress)
        {
            if (string.IsNullOrEmpty(index_folder))
            {
                throw new ArgumentException("index_folder");
            }

            if (string.IsNullOrEmpty(corpus_file_path))
            {
                throw new ArgumentException("corpus_file_path");
            }

            // todo: добавить учет параметров corpus_format, вероятно через фабрику.


            if (!System.IO.File.Exists(corpus_file_path))
            {
                throw new ApplicationException($"File {corpus_file_path} does not exists");
            }

            // Очистим папку с индексной информацией от предыдущего индексирования.
            if (System.IO.Directory.Exists(index_folder))
            {
                System.IO.Directory.Delete(index_folder, true);
            }

            // Для оценки прогресса индексирования большого файла нам нужно заранее получить число строк в нем,
            // чем мы сейчас и займемся в лоб.
            // TODO: для оптимизации можно читать байты блоками и искать \n
            int total_lines = 0;

            using (System.IO.StreamReader rdr0 = new System.IO.StreamReader(corpus_file_path))
            {
                while (!rdr0.EndOfStream)
                {
                    rdr0.ReadLine();
                    total_lines += 1;
                }
            }


            using (Lucene.Net.Store.Directory luceneIndexDirectory = Lucene.Net.Store.FSDirectory.Open(index_folder))
            {
                Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Ru.RussianAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT);
                //Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT);

                using (Lucene.Net.Index.IndexWriter writer = new Lucene.Net.Index.IndexWriter(luceneIndexDirectory, analyzer, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED))
                {
                    CorpusFileReader rdr = new CorpusFileReader();

                    int line_counter = 0;
                    foreach (var sampleDataFileRow in rdr.ReadAllLines(corpus_file_path))
                    {
                        line_counter++;

                        if (cancellation.GetCancellationPending())
                        {
                            cancellation.Cancelled = true;
                            break;
                        }

                        if ((line_counter % 100000) == 0)
                        {
                            int percentage = (int)Math.Round((100.0 * line_counter) / total_lines);
                            progress.ShowProgress(percentage);
                        }

                        Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();

                        doc.Add(new Lucene.Net.Documents.Field(IndexModel.LineNumber,
                                                               sampleDataFileRow.LineNumber.ToString(),
                                                               Lucene.Net.Documents.Field.Store.YES,
                                                               Lucene.Net.Documents.Field.Index.NO));

                        doc.Add(new Lucene.Net.Documents.Field(IndexModel.LineText,
                                                               sampleDataFileRow.LineText,
                                                               Lucene.Net.Documents.Field.Store.YES,
                                                               Lucene.Net.Documents.Field.Index.ANALYZED));

                        writer.AddDocument(doc);
                    }

                    writer.Optimize();
                    writer.Flush(true, true, true);
                }
            }
        }