コード例 #1
0
        /// <summary>
        /// Reads file with synonyms from MOBY format
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="datastructure">The in memory storage structure.</param>
        /// <exception cref="System.IO.FileNotFoundException">Passed filename with keywords not found.</exception>
        /// <exception cref="System.IO.IOException">Failed to read file.</exception>
        public void ReadFile(string filename, ref IinMemoryStorageStructure datastructure)
        {
            if (datastructure == null)
            {
                throw new ArgumentNullException("data structure must me initialized and could not be null");
            }

            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("Passed filename with keywords not found.", filename);
            }

            int stringCounter = 0;

            using (StreamReader reader = new System.IO.StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    stringCounter++;
                    var input = reader.ReadLine();
                    if (input.IndexOf(',') > 0)
                    {
                        var keyword = input.Substring(0, input.IndexOf(","));
                        if (this.validator != null && !this.validator.Validate(keyword))
                        {
                            throw new FormatException(string.Format("Format on the line {0} is not right. Keyword '{1}' contains illegal characters", stringCounter, keyword));
                        }

                        var collection = input.Substring(input.IndexOf(",") + 1).Trim().Split(',').Select(s => s.Trim()).ToArray();
                        List<string> checkedCollection = new List<string>();
                        foreach (var item in collection)
                        {
                            if (this.validator != null && !this.validator.Validate(item))
                            {
                                throw new FormatException(string.Format("Format on the line {0} is not right. Illegal characters value: '{1}'", stringCounter, item));
                            }

                            if (item.Trim().Equals(string.Empty))
                            {
                                // skip silently
                            }
                            else
                            {
                                checkedCollection.Add(item.Trim());
                            }
                        }

                        datastructure.AddSynonyms(keyword.Trim(), checkedCollection.ToArray());
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InMemorySuggester"/> class.
        /// </summary>
        /// <param name="storageStructure">Storage DI</param>
        /// <param name="reader">reader that reads file to memory from specified format</param>
        /// <param name="filename">Filename</param>
        /// <param name="sourceName">Optional name of the source</param>
        public InMemorySuggester(IinMemoryStorageStructure storageStructure, IToMemoryReader reader, string filename, string sourceName = "")
        {
            if (storageStructure == null)
            {
                throw new ArgumentNullException("storageSturcture could not be null");
            }

            if (reader == null)
            {
                throw new ArgumentNullException("reader could not be null");
            }

            this.reader = reader;
            this.storage = storageStructure;
            this.reader.ReadFile(filename, ref this.storage);
            this.id = Guid.NewGuid();
            this.name = sourceName.Equals(string.Empty) ? this.id.ToString() : sourceName;
        }
コード例 #3
0
        /// <summary>
        /// Reads file with synonyms from Ripicts format (word, syn1;raiing1,syn2;raiting2 etc.)
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="datastructure">The in memory storage structure.</param>
        /// <exception cref="System.ArgumentNullException">data structure must me initialized and could not be null</exception>
        /// <exception cref="System.IO.FileNotFoundException">Passed filename with keywords not found.</exception>
        /// <exception cref="System.IO.IOException">Failed to read file.</exception>
        public void ReadFile(string filename, ref IinMemoryStorageStructure datastructure)
        {
            if (datastructure == null)
            {
                throw new ArgumentNullException("datastructure must me initialized and could not be null");
            }

            if (!File.Exists(filename))
            {
                throw new FileNotFoundException("Passed filename with keywords not found.", filename);
            }

            int stringCounter = 0;
            using (StreamReader reader = new System.IO.StreamReader(filename))
            {
                KeywordValidator validator = new KeywordValidator();
                while (!reader.EndOfStream)
                {
                    stringCounter++;
                    var input = reader.ReadLine();

                    // empty string ignore them
                    if (!input.Trim().Equals(string.Empty))
                    {
                        string keyword;
                        var synRelevanceCollection = new List<Tuple<string, int>>();

                        if (input.IndexOf(',') > 0)
                        {
                            keyword = input.Substring(0, input.IndexOf(","));
                            var collection = input.Substring(input.IndexOf(",") + 1).Trim().Split(',').Select(s => s.Trim()).ToArray();

                            for (int i = 0; i < collection.Length; i++)
                            {
                                var element = collection[i];
                                var pair = element.Trim().Split(';');
                                if (pair.Length > 0 && validator.Validate(pair[0]))
                                {
                                    var synvalue = pair[0].Trim();
                                    int relValue = 0;
                                    if (pair.Length > 1)
                                    {
                                        int.TryParse(pair[1], out relValue);
                                    }

                                    if (!synvalue.Equals(string.Empty))
                                    {
                                        synRelevanceCollection.Add(new Tuple<string, int>(synvalue, relValue));
                                    }
                                }
                                else
                                {
                                    throw new FormatException(string.Format("Format on the line {0} is not right. Missing ';' symbol or value '{1}' illegal.", stringCounter, element));
                                }
                            }
                        }
                        else
                        {
                            // means that this is just a word without synonyms;
                            keyword = input.Trim();
                        }

                        try
                        {
                            datastructure.AddSynonyms(keyword, synRelevanceCollection.ToArray());
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Format on the line {0}.", stringCounter), ex);
                        }
                    }
                }
            }
        }