コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jarRootPath">Path to the folder with models extracted from 'stanford-corenlp-3.5.2-models.jar'</param>
        public StanfordLemmatizer(string jarRootPath)
        {
            if (!Directory.Exists(jarRootPath))
            {
                string fullPath = Path.GetFullPath(jarRootPath);
                throw new DirectoryNotFoundException("Folder(s) extracted from 'stanford-corenlp-3.5.2-models.jar' was not found in path: . " +
                                                     "-->" + fullPath + "<--. " +
                                                     "Please make sure correct path is listed in .config file.");
            }

            // Set properties required for lemma
            java.util.Properties props = new java.util.Properties();
            props.setProperty("annotators", "tokenize, ssplit, pos, lemma");
            props.setProperty("ner.useSUTime", "0");

            // We should change current directory, so StanfordCoreNLP could find all the model files automatically
            string curDir = Environment.CurrentDirectory;

            Directory.SetCurrentDirectory(jarRootPath);
            _pipeLine = new StanfordCoreNLP(props);
            Directory.SetCurrentDirectory(curDir);

            // Instantiate annotation
            _sentencesAnnotation = new CoreAnnotations.SentencesAnnotation();
            _tokensAnnotation    = new CoreAnnotations.TokensAnnotation();
            _lemmaAnnotation     = new CoreAnnotations.LemmaAnnotation();
        }
コード例 #2
0
ファイル: Pipeline.cs プロジェクト: 1412520/TextMining
        public static void Execute(string option, string text, bool disableLogging = true)
        {
            if (disableLogging)
            {
                RedwoodConfiguration.current().clear().apply();
            }
            var jarRoot = @"../../../data/paket-files/stanford-corenlp-3.9.1-models/";
            var props   = new Properties();

            props.setProperty("annotators", option);
            props.setProperty("ner.useSUTime", "0");

            // We should change current directory, so StanfordCoreNLP could find all the model files automatically
            var curDir = Environment.CurrentDirectory;

            Directory.SetCurrentDirectory(jarRoot);
            var pipeline = new StanfordNLP.StanfordCoreNLP(props);

            Directory.SetCurrentDirectory(curDir);

            // Annotation
            var annotation = new StanfordNLP.Annotation(text);

            pipeline.annotate(annotation);

            //get sentencesAnnotation to get sentences
            var sentencesAnnotation = new CoreAnnotations.SentencesAnnotation().getClass();

            //get tokensAnnotaion to get tokens in each sentence
            var tokensAnnotaion = new CoreAnnotations.TokensAnnotation().getClass();

            //get posAnnotation to get POS result of each token
            var posAnnotation = new CoreAnnotations.PartOfSpeechAnnotation().getClass();

            //get nerAnnotation to get NER result of each token
            var nerAnnotaion      = new CoreAnnotations.NamedEntityTagAnnotation().getClass();
            var deparseAnnotation = new TreeCoreAnnotations.TreeAnnotation().getClass();
            //deparseAnnotation = new TypedDependency().getClass();
            var sentences = annotation.get(sentencesAnnotation) as ArrayList;

            foreach (CoreMap sentence in sentences.toArray())
            {
                var tokens = (ArrayList)sentence.get(tokensAnnotaion);
                Console.WriteLine("Token-POS-NER: ");
                foreach (CoreLabel token in tokens)
                {
                    Console.Write($"{token.value()}-{token.get(posAnnotation)}-{token.get(nerAnnotaion)} ");
                }
                Console.WriteLine("\n\n\n");
                var parsedText = (Tree)sentence.get(deparseAnnotation);
                if (parsedText != null)
                {
                    Console.WriteLine("Parsed Text: ");
                    new TreePrint("penn,typedDependenciesCollapsed").printTree(parsedText);
                }
            }
        }
コード例 #3
0
ファイル: EmotionAnalysis.cs プロジェクト: rikace/devConf2017
        public EmotionAnalysis(string modelsFolder)
        {
            var properties = new Properties();

            properties.setProperty("annotators",
                                   "tokenize,ssplit,pos,parse,sentiment");

            var temp = Environment.CurrentDirectory;

            Environment.CurrentDirectory = modelsFolder;
            stanfordNLP = new StanfordCoreNLP(properties);
            Environment.CurrentDirectory = temp;

            emotionAnnotationTreeClassName =
                new SentimentCoreAnnotations.SentimentAnnotatedTree().getClass();
            sentencesAnnotationClassName =
                new CoreAnnotations.SentencesAnnotation().getClass();
        }
コード例 #4
0
        // Sample from https://stanfordnlp.github.io/CoreNLP/coref.html
        static void Main()
        {
            var jarRoot = @"..\..\..\..\data\paket-files\nlp.stanford.edu\stanford-corenlp-full-2018-02-27\models";

            Annotation document = new Annotation("Barack Obama was born in Hawaii.  He is the president. Obama was elected in 2008.");
            Properties props    = new Properties();

            props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref");
            props.setProperty("ner.useSUTime", "0");

            var curDir = Environment.CurrentDirectory;

            Directory.SetCurrentDirectory(jarRoot);
            var pipeline = new StanfordCoreNLP(props);

            Directory.SetCurrentDirectory(curDir);

            pipeline.annotate(document);

            var corefChainAnnotation    = new CorefCoreAnnotations.CorefChainAnnotation().getClass();
            var sentencesAnnotation     = new CoreAnnotations.SentencesAnnotation().getClass();
            var corefMentionsAnnotation = new CorefCoreAnnotations.CorefMentionsAnnotation().getClass();

            Console.WriteLine("---");
            Console.WriteLine("coref chains");
            var corefChain = document.get(corefChainAnnotation) as Map;

            foreach (CorefChain cc in corefChain.values().toArray())
            {
                Console.WriteLine($"\t{cc}");
            }
            var sentences = document.get(sentencesAnnotation) as ArrayList;

            foreach (CoreMap sentence in sentences.toArray())
            {
                Console.WriteLine("---");
                Console.WriteLine("mentions");
                var corefMentions = sentence.get(corefMentionsAnnotation) as ArrayList;
                foreach (Mention m in corefMentions)
                {
                    Console.WriteLine("\t" + m);
                }
            }
        }
コード例 #5
0
        public void CorefTest()
        {
            Annotation document = new Annotation("Barack Obama was born in Hawaii.  He is the president. Obama was elected in 2008.");
            Properties props    = new Properties();

            props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,coref");
            props.setProperty("ner.useSUTime", "false");

            var curDir = Environment.CurrentDirectory;

            Directory.SetCurrentDirectory(Files.CoreNlp.JarRoot);
            var pipeline = new StanfordCoreNLP(props);

            Directory.SetCurrentDirectory(curDir);

            pipeline.annotate(document);

            var corefChainAnnotation    = new CorefCoreAnnotations.CorefChainAnnotation().getClass();
            var sentencesAnnotation     = new CoreAnnotations.SentencesAnnotation().getClass();
            var corefMentionsAnnotation = new CorefCoreAnnotations.CorefMentionsAnnotation().getClass();

            Console.WriteLine("---");
            Console.WriteLine("coref chains");
            var corefChain = (Map)document.get(corefChainAnnotation);

            foreach (CorefChain cc in corefChain.values().toArray())
            {
                Console.WriteLine($"\t{cc}");
            }
            var sentences = (ArrayList)document.get(sentencesAnnotation);

            foreach (CoreMap sentence in sentences.toArray())
            {
                Console.WriteLine("---");
                Console.WriteLine("mentions");
                var corefMentions = (ArrayList)sentence.get(corefMentionsAnnotation);
                foreach (Mention m in corefMentions)
                {
                    Console.WriteLine("\t" + m);
                }
            }
        }