Esempio n. 1
0
        private bool _Reload()
        {
            this.BytesProcessed  = 0;
            this.PercentComplete = 0.0;
            this.LinesProcessed  = 0;

            lock (this._syncRoot)
            {
                this.Clear();
            }

            using (StreamReader reader = File.OpenText(Path.Combine(this.StaticDbFolder, IndexFile.PosToFileName(this.PartOfSpeech))))
            {
                string currentLine = "";
                while (!reader.EndOfStream && String.IsNullOrWhiteSpace(currentLine) && IndexFile._commentRegex.IsMatch(currentLine))
                {
                    currentLine          = reader.ReadLine();
                    this.BytesProcessed  = reader.BaseStream.Position;
                    this.PercentComplete = Math.Round((Convert.ToDouble(reader.BaseStream.Position) / Convert.ToDouble(reader.BaseStream.Length)) * 100.0, 2);
                    this.LinesProcessed++;
                }

                if (!reader.EndOfStream)
                {
                    do
                    {
                        if (String.IsNullOrWhiteSpace(currentLine))
                        {
                            continue;
                        }

                        int   position = 0;
                        Match m        = IndexFile._firstFourRegex.Match(currentLine);
                        if (!m.Success)
                        {
                            throw new WordNetParseException("Error parsing first four index file fields", IndexFile._firstFourRegex, currentLine, position);
                        }
                        IndexItem item = new IndexItem
                        {
                            Word           = m.Groups["lemma"].Value.Replace('_', ' '),
                            PartOfSpeech   = Common.SymbolAttribute.GetEnum <Common.PartOfSpeech>(m.Groups["pos"].Value),
                            SynsetCount    = Convert.ToInt32(m.Groups["synset_cnt"].Value),
                            PointerSymbols = new Collection <Common.PointerSymbol>(),
                            SynsetOffsets  = new Collection <long>()
                        };

                        int p_cnt = Convert.ToInt32(m.Groups["p_cnt"].Value);

                        for (int i = 0; i < p_cnt; i++)
                        {
                            position += m.Groups["r"].Index;
                            m         = IndexFile._pointerSymbolRegex.Match(m.Groups["r"].Value);
                            if (!m.Success)
                            {
                                throw new WordNetParseException(String.Format("Error parsing pointer symbol {0}", i + 1), IndexFile._pointerSymbolRegex, currentLine, position);
                            }
                            item.PointerSymbols.Add(Common.PosAndSymbolAttribute.GetEnum <Common.PointerSymbol>(m.Groups["ptr_symbol"].Value, item.PartOfSpeech));
                        }

                        position += m.Groups["r"].Index;
                        m         = IndexFile._countsRegex.Match(m.Groups["r"].Value);
                        if (!m.Success)
                        {
                            throw new WordNetParseException("Error parsing counts", IndexFile._pointerSymbolRegex, currentLine, position);
                        }
                        int sense_cnt = Convert.ToInt32(m.Groups["sense_cnt"].Value);

                        for (int i = 0; i < sense_cnt; i++)
                        {
                            position += m.Groups["r"].Index;
                            m         = IndexFile._offsetRegex.Match(m.Groups["r"].Value);
                            if (!m.Success)
                            {
                                throw new WordNetParseException(String.Format("Error parsing offset {0}", i + 1), IndexFile._offsetRegex, currentLine, position);
                            }
                            item.SynsetOffsets.Add(Convert.ToInt64(m.Groups["synset_offset"].Value));
                        }

                        lock (this._syncRoot)
                        {
                            this.Add(item);
                        }

                        currentLine = reader.ReadLine();
                        this.LinesProcessed++;
                        this.BytesProcessed  = reader.BaseStream.Position;
                        this.PercentComplete = Math.Round((Convert.ToDouble(reader.BaseStream.Position) / Convert.ToDouble(reader.BaseStream.Length)) * 100.0, 2);
                    } while (!reader.EndOfStream);

                    this.BytesProcessed = reader.BaseStream.Length;
                }
            }

            return(this.LoadError == null);
        }
Esempio n. 2
0
        private TResult AccessFile <TResult>(Func <TResult> handler)
        {
            TResult result;

            lock (this._fileAcessSync)
            {
                lock (this._syncRoot)
                {
                    this._closeFileAfter   = null;
                    this._lastFileActivity = null;
                    if (this._inputStream == null)
                    {
                        this._inputStream = File.OpenRead(Path.Combine(this.StaticDbFolder, IndexFile.PosToFileName(this.PartOfSpeech)));
                    }
                }

                result = handler();

                lock (this._syncRoot)
                {
                    if (this._inputStream != null)
                    {
                        if (this.FileOpenTimeout.Equals(TimeSpan.Zero))
                        {
                            this._inputStream.Close();
                            this._inputStream.Dispose();
                            this._inputStream = null;
                        }
                        else
                        {
                            this._lastFileActivity = DateTime.Now;
                            this._closeFileAfter   = this._lastFileActivity.Value.Add(this.FileOpenTimeout);
                        }
                    }
                }
            }

            return(result);
        }