Пример #1
0
        /// <summary>
        /// https://universaldependencies.org/u/dep/index.html
        /// Stratify a sentence according to the subject-predicate relationship
        /// </summary>
        /// <param name="sentence"></param>
        /// <returns>sentence, subject, predicate, objective</returns>
        public List <(edu.stanford.nlp.util.CoreMap, edu.stanford.nlp.ling.IndexedWord, edu.stanford.nlp.ling.IndexedWord, edu.stanford.nlp.ling.IndexedWord)> StepSamplingDependency(edu.stanford.nlp.util.CoreMap sentence)
        {
            List <(edu.stanford.nlp.util.CoreMap, edu.stanford.nlp.ling.IndexedWord, edu.stanford.nlp.ling.IndexedWord, edu.stanford.nlp.ling.IndexedWord)> samplings = new List <(edu.stanford.nlp.util.CoreMap, edu.stanford.nlp.ling.IndexedWord, edu.stanford.nlp.ling.IndexedWord, edu.stanford.nlp.ling.IndexedWord)>();

            edu.stanford.nlp.semgraph.SemanticGraph dependencies = sentence.get(enhancedPlusPlusDependenciesAnnotationClass) as edu.stanford.nlp.semgraph.SemanticGraph;
            java.util.Collection typedDependencies = dependencies.typedDependencies();
            java.util.Iterator   itr = typedDependencies.iterator();
            while (itr.hasNext())
            {
                edu.stanford.nlp.trees.TypedDependency td = itr.next() as edu.stanford.nlp.trees.TypedDependency;
                string relationType = td.reln().getShortName();
                //Nominals
                if (relationType == "nsubj" || relationType == "nsubjpass")
                {
                    edu.stanford.nlp.ling.IndexedWord subject, predicate, objective;
                    subject = td.dep(); predicate = td.gov();
                    //在谓语位置上,缺不满足谓语角色的词,此种谓语可缺省
                    edu.stanford.nlp.ling.IndexedWord expl = FindIndexedWordByDependencyType(dependencies, predicate, "expl");
                    predicate = expl == null ? predicate : null;
                    //直接宾语,
                    objective = FindIndexedWordByDependencyType(dependencies, predicate, "obj", "dobj");
                    //动词或形容词的补语做宾语, open clausal complement
                    objective = objective ?? FindIndexedWordByDependencyType(dependencies, predicate, "ccomp", "xcomp");
                    //加入层次集合
                    samplings.Add((sentence, subject, predicate, objective));
                }
            }
            return(samplings);
        }
Пример #2
0
        /// <summary>
        /// 因为句子并非要求语法上严格正确,所有经常出现错误的结果,但是结果不会变化
        /// </summary>
        /// <returns></returns>
        private List <edu.stanford.nlp.trees.TypedDependency> FindDeptypeFromSentence(edu.stanford.nlp.util.CoreMap sentence, string depTypeString)
        {
            List <edu.stanford.nlp.trees.TypedDependency> tds = new List <edu.stanford.nlp.trees.TypedDependency>();

            edu.stanford.nlp.semgraph.SemanticGraph dependencies = sentence.get(enhancedPlusPlusDependenciesAnnotationClass) as edu.stanford.nlp.semgraph.SemanticGraph;
            java.util.Collection typedDependencies = dependencies.typedDependencies();
            java.util.Iterator   itr = typedDependencies.iterator();
            while (itr.hasNext())
            {
                edu.stanford.nlp.trees.TypedDependency td = itr.next() as edu.stanford.nlp.trees.TypedDependency;
                if (td.reln().getShortName() == depTypeString)
                {
                    tds.Add(td);
                }
            }
            return(tds);
        }
Пример #3
0
        /// <summary>
        /// 搜索与target token相关的dpendency关系集合
        /// </summary>
        /// <param name="dependencies"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private List <edu.stanford.nlp.trees.TypedDependency> FindRefs(edu.stanford.nlp.semgraph.SemanticGraph dependencies, edu.stanford.nlp.ling.CoreLabel token)
        {
            List <edu.stanford.nlp.trees.TypedDependency> tds = new List <edu.stanford.nlp.trees.TypedDependency>();
            string tokenValue = token.ToString();

            java.util.Collection typedDependencies = dependencies.typedDependencies();
            java.util.Iterator   itr = typedDependencies.iterator();
            while (itr.hasNext())
            {
                edu.stanford.nlp.trees.TypedDependency td = itr.next() as edu.stanford.nlp.trees.TypedDependency;
                string tdValue = td.toString();
                if (tdValue.IndexOf(tokenValue) != -1)
                {
                    tds.Add(td);
                }
            }
            return(tds);
        }
Пример #4
0
        /// <summary>
        /// 搜索token的直接关联关系
        /// </summary>
        /// <param name="sentence"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private List <edu.stanford.nlp.trees.TypedDependency> FindDeirctRefs(edu.stanford.nlp.util.CoreMap sentence, edu.stanford.nlp.ling.CoreLabel token)
        {
            string tokenValue = token.ToString();
            List <edu.stanford.nlp.trees.TypedDependency> tds = new List <edu.stanford.nlp.trees.TypedDependency>();

            edu.stanford.nlp.semgraph.SemanticGraph dependencies = sentence.get(enhancedPlusPlusDependenciesAnnotationClass) as edu.stanford.nlp.semgraph.SemanticGraph;
            java.util.Collection typedDependencies = dependencies.typedDependencies();
            java.util.Iterator   itr = typedDependencies.iterator();
            while (itr.hasNext())
            {
                edu.stanford.nlp.trees.TypedDependency td = itr.next() as edu.stanford.nlp.trees.TypedDependency;
                string tdValue = td.toString();
                if (tdValue.IndexOf(tokenValue) != -1)
                {
                    tds.Add(td);
                }
            }
            return(tds);
        }