示例#1
0
        /// <summary>This assumes each line is one value and creates index by adding values in the order of the lines in the file</summary>
        /// <param name="file">Which file to load</param>
        /// <returns>An index built out of the lines in the file</returns>
        public static IIndex <string> LoadFromFileWithList(string file)
        {
            IIndex <string> index = new Edu.Stanford.Nlp.Util.HashIndex <string>();
            BufferedReader  br    = null;

            try
            {
                br = new BufferedReader(new FileReader(file));
                for (string line; (line = br.ReadLine()) != null;)
                {
                    index.Add(line.Trim());
                }
                br.Close();
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }
            finally
            {
                if (br != null)
                {
                    try
                    {
                        br.Close();
                    }
                    catch (IOException)
                    {
                    }
                }
            }
            // forget it
            return(index);
        }
示例#2
0
        // give up
        /// <summary>This assumes each line is of the form (number=value) and it adds each value in order of the lines in the file.</summary>
        /// <remarks>
        /// This assumes each line is of the form (number=value) and it adds each value in order of the lines in the file.
        /// Warning: This ignores the value of number, and just indexes each value it encounters in turn!
        /// </remarks>
        /// <param name="file">Which file to load</param>
        /// <returns>An index built out of the lines in the file</returns>
        public static IIndex <string> LoadFromFilename(string file)
        {
            IIndex <string> index = new Edu.Stanford.Nlp.Util.HashIndex <string>();
            BufferedReader  br    = null;

            try
            {
                br = IOUtils.ReaderFromString(file);
                for (string line; (line = br.ReadLine()) != null;)
                {
                    int start = line.IndexOf('=');
                    if (start == -1 || start == line.Length - 1)
                    {
                        continue;
                    }
                    index.Add(Sharpen.Runtime.Substring(line, start + 1));
                }
            }
            catch (IOException e)
            {
                throw new RuntimeIOException(e);
            }
            finally
            {
                IOUtils.CloseIgnoringExceptions(br);
            }
            return(index);
        }
示例#3
0
 public override bool Equals(object o)
 {
     if (this == o)
     {
         return(true);
     }
     // TODO: why not allow equality to non-HashIndex indices?
     if (!(o is Edu.Stanford.Nlp.Util.HashIndex))
     {
         return(false);
     }
     Edu.Stanford.Nlp.Util.HashIndex hashIndex = (Edu.Stanford.Nlp.Util.HashIndex)o;
     return(indexes.Equals(hashIndex.indexes) && objects.Equals(hashIndex.objects));
 }
示例#4
0
        /// <summary>
        /// This is the analogue of
        /// <c>loadFromFilename</c>
        /// , and is intended to be included in a routine
        /// that unpacks a text-serialized form of an object that incorporates an Index.
        /// NOTE: presumes that the next readLine() will read in the first line of the
        /// portion of the text file representing the saved Index.  Currently reads until it
        /// encounters a blank line, consuming that line and returning the Index.
        /// TODO: figure out how best to terminate: currently a blank line is considered to be a terminator.
        /// </summary>
        /// <param name="br">The Reader to read the index from</param>
        /// <returns>An Index read from a file</returns>
        /// <exception cref="System.IO.IOException"/>
        public static IIndex <string> LoadFromReader(BufferedReader br)
        {
            Edu.Stanford.Nlp.Util.HashIndex <string> index = new Edu.Stanford.Nlp.Util.HashIndex <string>();
            string line = br.ReadLine();

            // terminate if EOF reached, or if a blank line is encountered.
            while ((line != null) && (line.Length > 0))
            {
                int start = line.IndexOf('=');
                if (start == -1 || start == line.Length - 1)
                {
                    continue;
                }
                index.Add(Sharpen.Runtime.Substring(line, start + 1));
                line = br.ReadLine();
            }
            return(index);
        }