예제 #1
0
        /*Stem the given word with, return the stemmed word
         */
        public List <string> Stem(string word)
        {
            if (pipeline == null)
            {
                Initial();
            }
            var lemmas = new List <String>();
            // create an empty Annotation just with the given text
            var document = new Annotation(word);

            // run all Annotators on this text
            try
            {
                pipeline.annotate(document);
            }
            catch (Exception)
            {
                return(null);
            }
            // Iterate over all of the sentences found
            var senObj    = new edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation();
            var obj       = document.get(senObj.getClass());
            var tokenObj  = new edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation();
            var lemmaObj  = new edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation();
            var sentences = (ArrayList)obj;

            foreach (CoreMap sentence in sentences)
            {
                // Iterate over all tokens in a sentence
                lemmas.AddRange(from CoreLabel token in (ArrayList)sentence.get(tokenObj.getClass())
                                select(string) token.get(lemmaObj.getClass()));
            }
            return(lemmas);
        }