Exemplo n.º 1
0
        /// <summary>
        /// Wykonuje stemming wszystkich s³ów w danym pliku pomijaj¹c tzw. "stop words"
        /// </summary>
        /// <param name="sourceFileName">Pe³na œcie¿ka pliku wejœciowego.</param>
        /// <param name="destFileName">Pe³na œcie¿ka pliku wynikowego zawieraj¹cego s³owa po stemmingu.</param>
        /// <param name="stopWords">S³ownik zawieraj¹cy "stop words" - s³owa (koniecznie zapisane ma³ymi literami!), które s¹ pomijane przy obróbce
        /// i które nie zostan¹ zapisane w wynikowym pliku. Sprawdzenie czy dane s³owo nale¿y do s³ownika
        /// nastêpuej przed jego stemmingiem. </param>
        public static void StemFile(String sourceFileName, String destFileName, Dictionary <int, String> stopWords)
        {
            //odczyt z pliku do stringa
            StreamReader  sr = new StreamReader(sourceFileName);
            String        tmpLine;
            StringBuilder sb = new StringBuilder();

            while ((tmpLine = sr.ReadLine()) != null)
            {
                sb.Append(tmpLine + ' ');
            }
            sr.Close();
            String text = sb.ToString();

            //dzielenie na wyrazy
            //dziele w miejscu, gdzie s¹ znaki specjalne, spacje, lub cyfry
            //uwaga: znak _ nie jest zaliczany do znaków specjalnych
            String[] wordList = Regex.Split(text, @"[\W\s0-9_]+");
            //obróbka s³ów
            Dictionary <String, int> wordsInFile = new Dictionary <String, int>();

            for (int i = 0; i < wordList.Length; i++)
            {
                wordList[i] = wordList[i].ToLower(); //ma³ych liter wymaga stemmer
                //eliminacja stop words, pomijam te¿ wyrazy puste i jednoliterowe
                if (wordList[i].Length <= 1 || (stopWords != null && stopWords.ContainsKey(wordList[i].GetHashCode())))
                {
                    continue;
                }
                //stemming
                Stemmer stemmer = new Stemmer();
                stemmer.add(wordList[i].ToCharArray(), wordList[i].Length);
                stemmer.stem();
                String word = stemmer.ToString();
                if (wordsInFile.ContainsKey(word))
                {
                    wordsInFile[word]++;
                }
                else
                {
                    wordsInFile.Add(word, 1);
                }
                //sw.Write(stemmer.ToString() + ' ');
            }
            //utworzenie pliku wynikowego
            FileStream   fs = new FileStream(destFileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            foreach (String word in wordsInFile.Keys)
            {
                sw.WriteLine(word + ' ' + wordsInFile[word]);
            }
            sw.Close();
            fs.Close();
        }
Exemplo n.º 2
0
        /** Test program for demonstrating the Stemmer.  It reads text from a
         * a list of files, stems each word, and writes the result to standard
         * output. Note that the word stemmed is expected to be in lower case:
         * forcing lower case must be done outside the Stemmer class.
         * Usage: Stemmer file-name file-name ...
         */
        public static void Main(String[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage:  Stemmer <input file>");
                return;
            }
            char[]  w = new char[501];
            Stemmer s = new Stemmer();

            for (int i = 0; i < args.Length; i++)
            {
                try
                {
                    FileStream _in = new FileStream(args[i], FileMode.Open, FileAccess.Read);
                    try
                    {
                        while (true)
                        {
                            int ch = _in.ReadByte();
                            if (Char.IsLetter((char)ch))
                            {
                                int j = 0;
                                while (true)
                                {
                                    ch   = Char.ToLower((char)ch);
                                    w[j] = (char)ch;
                                    if (j < 500)
                                    {
                                        j++;
                                    }
                                    ch = _in.ReadByte();
                                    if (!Char.IsLetter((char)ch))
                                    {
                                        /* to test add(char ch) */
                                        for (int c = 0; c < j; c++)
                                        {
                                            s.add(w[c]);
                                        }
                                        /* or, to test add(char[] w, int j) */
                                        /* s.add(w, j); */
                                        s.stem();

                                        String u;

                                        /* and now, to test toString() : */
                                        u = s.ToString();

                                        /* to test getResultBuffer(), getResultLength() : */
                                        /* u = new String(s.getResultBuffer(), 0, s.getResultLength()); */

                                        Console.Write(u);
                                        break;
                                    }
                                }
                            }
                            if (ch < 0)
                            {
                                break;
                            }
                            Console.Write((char)ch);
                        }
                    }
                    catch (IOException)
                    {
                        Console.WriteLine("error reading " + args[i]);
                        break;
                    }
                }
                catch (FileNotFoundException)
                {
                    Console.WriteLine("file " + args[i] + " not found");
                    break;
                }
            }
        }