示例#1
0
        static bool _collapseAnafors(Pullenti.Semantic.SemGraph gr)
        {
            bool ret = false;

            for (int i = 0; i < gr.Objects.Count; i++)
            {
                Pullenti.Semantic.SemObject obj = gr.Objects[i];
                if (obj.Typ == Pullenti.Semantic.SemObjectType.PersonalPronoun || obj.Morph.NormalFull == "КОТОРЫЙ")
                {
                }
                else
                {
                    continue;
                }
                if (obj.Attrs.Count > 0 || obj.Quantity != null)
                {
                    continue;
                }
                if (obj.LinksFrom.Count == 1 && obj.LinksFrom[0].Typ == Pullenti.Semantic.SemLinkType.Anafor)
                {
                }
                else if (obj.LinksFrom.Count == 2 && obj.LinksFrom[0].Typ == Pullenti.Semantic.SemLinkType.Anafor && obj.LinksFrom[0].AltLink == obj.LinksFrom[1])
                {
                }
                else
                {
                    continue;
                }
                Pullenti.Semantic.SemLink alink = obj.LinksFrom[0];
                foreach (Pullenti.Semantic.SemLink li in obj.LinksTo)
                {
                    Pullenti.Semantic.SemLink nli = gr.AddLink(li.Typ, li.Source, alink.Target, li.Question, li.IsOr, li.Preposition);
                    if (alink.AltLink != null)
                    {
                        Pullenti.Semantic.SemLink nli2 = gr.AddLink(li.Typ, li.Source, alink.AltLink.Target, li.Question, li.IsOr, li.Preposition);
                        nli2.AltLink = nli;
                        nli.AltLink  = nli2;
                    }
                }
                gr.RemoveObject(obj);
                i--;
                ret = true;
            }
            return(ret);
        }
示例#2
0
 public static Pullenti.Semantic.SemObject CreateNounGroup(Pullenti.Semantic.SemGraph gr, Pullenti.Ner.Core.NounPhraseToken npt)
 {
     Pullenti.Ner.Token          noun = npt.Noun.BeginToken;
     Pullenti.Semantic.SemObject sem  = new Pullenti.Semantic.SemObject(gr);
     sem.Tokens.Add(npt.Noun);
     sem.Typ = Pullenti.Semantic.SemObjectType.Noun;
     if (npt.Noun.Morph.Class.IsPersonalPronoun)
     {
         sem.Typ = Pullenti.Semantic.SemObjectType.PersonalPronoun;
     }
     else if (npt.Noun.Morph.Class.IsPronoun)
     {
         sem.Typ = Pullenti.Semantic.SemObjectType.Pronoun;
     }
     if (npt.Noun.BeginToken != npt.Noun.EndToken)
     {
         sem.Morph.NormalCase = npt.Noun.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Undefined, Pullenti.Morph.MorphGender.Undefined, false);
         sem.Morph.NormalFull = npt.Noun.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Singular, Pullenti.Morph.MorphGender.Undefined, false);
         sem.Morph.Class      = Pullenti.Morph.MorphClass.Noun;
         sem.Morph.Number     = npt.Morph.Number;
         sem.Morph.Gender     = npt.Morph.Gender;
         sem.Morph.Case       = npt.Morph.Case;
     }
     else if (noun is Pullenti.Ner.TextToken)
     {
         foreach (Pullenti.Morph.MorphBaseInfo wf in noun.Morph.Items)
         {
             if (wf.CheckAccord(npt.Morph, false, false) && (wf is Pullenti.Morph.MorphWordForm))
             {
                 _setMorph(sem, wf as Pullenti.Morph.MorphWordForm);
                 break;
             }
         }
         if (sem.Morph.NormalCase == null)
         {
             sem.Morph.NormalCase = noun.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Undefined, Pullenti.Morph.MorphGender.Undefined, false);
             sem.Morph.NormalFull = noun.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Singular, Pullenti.Morph.MorphGender.Undefined, false);
         }
         List <Pullenti.Semantic.Utils.DerivateGroup> grs = Pullenti.Semantic.Utils.DerivateService.FindDerivates(sem.Morph.NormalFull, true, null);
         if (grs != null && grs.Count > 0)
         {
             sem.Concept = grs[0];
         }
     }
     else if (noun is Pullenti.Ner.ReferentToken)
     {
         Pullenti.Ner.Referent r = (noun as Pullenti.Ner.ReferentToken).Referent;
         if (r == null)
         {
             return(null);
         }
         sem.Morph.NormalFull = (sem.Morph.NormalCase = r.ToString());
         sem.Concept          = r;
     }
     else if (noun is Pullenti.Ner.NumberToken)
     {
         Pullenti.Ner.NumberToken num = noun as Pullenti.Ner.NumberToken;
         sem.Morph.Gender = noun.Morph.Gender;
         sem.Morph.Number = noun.Morph.Number;
         if (num.IntValue != null)
         {
             sem.Morph.NormalCase = Pullenti.Ner.Core.NumberHelper.GetNumberAdjective(num.IntValue.Value, noun.Morph.Gender, noun.Morph.Number);
             sem.Morph.NormalFull = Pullenti.Ner.Core.NumberHelper.GetNumberAdjective(num.IntValue.Value, Pullenti.Morph.MorphGender.Masculine, Pullenti.Morph.MorphNumber.Singular);
         }
         else
         {
             sem.Morph.NormalFull = (sem.Morph.NormalCase = noun.GetSourceText().ToUpper());
         }
     }
     noun.Tag = sem;
     if (npt.Adjectives.Count > 0)
     {
         foreach (Pullenti.Ner.MetaToken a in npt.Adjectives)
         {
             if (npt.MultiNouns && a != npt.Adjectives[0])
             {
                 break;
             }
             Pullenti.Semantic.SemObject asem = CreateNptAdj(gr, npt, a);
             if (asem != null)
             {
                 gr.AddLink(Pullenti.Semantic.SemLinkType.Detail, sem, asem, "какой", false, null);
             }
         }
     }
     if (npt.InternalNoun != null)
     {
         Pullenti.Semantic.SemObject intsem = CreateNounGroup(gr, npt.InternalNoun);
         if (intsem != null)
         {
             gr.AddLink(Pullenti.Semantic.SemLinkType.Detail, sem, intsem, null, false, null);
         }
     }
     gr.Objects.Add(sem);
     return(sem);
 }
示例#3
0
        public static Pullenti.Semantic.SemObject CreateVerbGroup(Pullenti.Semantic.SemGraph gr, Pullenti.Ner.Core.VerbPhraseToken vpt)
        {
            List <Pullenti.Semantic.SemObject>    sems    = new List <Pullenti.Semantic.SemObject>();
            List <Pullenti.Semantic.SemAttribute> attrs   = new List <Pullenti.Semantic.SemAttribute>();
            List <Pullenti.Semantic.SemObject>    adverbs = new List <Pullenti.Semantic.SemObject>();

            for (int i = 0; i < vpt.Items.Count; i++)
            {
                Pullenti.Ner.Core.VerbPhraseItemToken v = vpt.Items[i];
                if (v.IsAdverb)
                {
                    AdverbToken adv = AdverbToken.TryParse(v.BeginToken);
                    if (adv == null)
                    {
                        continue;
                    }
                    if (adv.Typ != Pullenti.Semantic.SemAttributeType.Undefined)
                    {
                        attrs.Add(new Pullenti.Semantic.SemAttribute()
                        {
                            Not = adv.Not, Typ = adv.Typ, Spelling = adv.Spelling
                        });
                        continue;
                    }
                    Pullenti.Semantic.SemObject adverb = CreateAdverb(gr, adv);
                    if (attrs.Count > 0)
                    {
                        adverb.Attrs.AddRange(attrs);
                        attrs.Clear();
                    }
                    adverbs.Add(adverb);
                    continue;
                }
                if (v.Normal == "БЫТЬ")
                {
                    int j;
                    for (j = i + 1; j < vpt.Items.Count; j++)
                    {
                        if (!vpt.Items[j].IsAdverb)
                        {
                            break;
                        }
                    }
                    if (j < vpt.Items.Count)
                    {
                        continue;
                    }
                }
                Pullenti.Semantic.SemObject sem = new Pullenti.Semantic.SemObject(gr);
                gr.Objects.Add(sem);
                sem.Tokens.Add(v);
                v.Tag = sem;
                _setMorph(sem, v.VerbMorph);
                sem.Morph.NormalCase = (sem.Morph.NormalFull = v.Normal);
                if (v.IsParticiple || v.IsDeeParticiple)
                {
                    sem.Typ = Pullenti.Semantic.SemObjectType.Participle;
                    sem.Morph.NormalFull = v.EndToken.GetNormalCaseText(Pullenti.Morph.MorphClass.Verb, Pullenti.Morph.MorphNumber.Undefined, Pullenti.Morph.MorphGender.Undefined, false) ?? sem.Morph.NormalCase;
                    sem.Morph.NormalCase = v.EndToken.GetNormalCaseText(Pullenti.Morph.MorphClass.Adjective, Pullenti.Morph.MorphNumber.Undefined, Pullenti.Morph.MorphGender.Undefined, false);
                    if (sem.Morph.NormalCase == sem.Morph.NormalFull && v.Normal.EndsWith("Й"))
                    {
                        List <Pullenti.Semantic.Utils.DerivateGroup> grs2 = Pullenti.Semantic.Utils.DerivateService.FindDerivates(v.Normal, true, null);
                        if (grs2 != null)
                        {
                            foreach (Pullenti.Semantic.Utils.DerivateGroup g in grs2)
                            {
                                foreach (Pullenti.Semantic.Utils.DerivateWord w in g.Words)
                                {
                                    if (w.Lang == v.EndToken.Morph.Language && w.Class.IsVerb && !w.Class.IsAdjective)
                                    {
                                        sem.Morph.NormalFull = w.Spelling;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else if (sem.Morph.NormalCase == sem.Morph.NormalFull && v.IsParticiple && sem.Morph.NormalFull.EndsWith("Ь"))
                    {
                        foreach (Pullenti.Morph.MorphBaseInfo it in v.EndToken.Morph.Items)
                        {
                            Pullenti.Morph.MorphWordForm wf = it as Pullenti.Morph.MorphWordForm;
                            if (wf == null)
                            {
                                continue;
                            }
                            if (wf.NormalCase.EndsWith("Й") || ((wf.NormalFull != null && wf.NormalFull.EndsWith("Й"))))
                            {
                                sem.Morph.NormalCase = wf.NormalFull ?? wf.NormalCase;
                                break;
                            }
                        }
                        if (sem.Morph.NormalCase == sem.Morph.NormalFull)
                        {
                            List <Pullenti.Semantic.Utils.DerivateGroup> grs2 = Pullenti.Semantic.Utils.DerivateService.FindDerivates(sem.Morph.NormalCase, true, null);
                            if (grs2 != null)
                            {
                                foreach (Pullenti.Semantic.Utils.DerivateGroup g in grs2)
                                {
                                    foreach (Pullenti.Semantic.Utils.DerivateWord w in g.Words)
                                    {
                                        if (w.Lang == v.EndToken.Morph.Language && w.Class.IsVerb && w.Class.IsAdjective)
                                        {
                                            sem.Morph.NormalCase = w.Spelling;
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    sem.Typ = Pullenti.Semantic.SemObjectType.Verb;
                }
                if (v.VerbMorph != null && v.VerbMorph.ContainsAttr("возвр.", null))
                {
                    if (sem.Morph.NormalFull.EndsWith("СЯ") || sem.Morph.NormalFull.EndsWith("СЬ"))
                    {
                        sem.Morph.NormalFull = sem.Morph.NormalFull.Substring(0, sem.Morph.NormalFull.Length - 2);
                    }
                }
                List <Pullenti.Semantic.Utils.DerivateGroup> grs = Pullenti.Semantic.Utils.DerivateService.FindDerivates(sem.Morph.NormalFull, true, null);
                if (grs != null && grs.Count > 0)
                {
                    sem.Concept = grs[0];
                    if (v.VerbMorph != null && v.VerbMorph.Misc.Aspect == Pullenti.Morph.MorphAspect.Imperfective)
                    {
                        foreach (Pullenti.Semantic.Utils.DerivateWord w in grs[0].Words)
                        {
                            if (w.Class.IsVerb && !w.Class.IsAdjective)
                            {
                                if (w.Aspect == Pullenti.Morph.MorphAspect.Perfective)
                                {
                                    sem.Morph.NormalFull = w.Spelling;
                                    break;
                                }
                            }
                        }
                    }
                }
                sem.Not = v.Not;
                sems.Add(sem);
                if (attrs.Count > 0)
                {
                    sem.Attrs.AddRange(attrs);
                    attrs.Clear();
                }
                if (adverbs.Count > 0)
                {
                    foreach (Pullenti.Semantic.SemObject a in adverbs)
                    {
                        gr.AddLink(Pullenti.Semantic.SemLinkType.Detail, sem, a, "как", false, null);
                    }
                }
                adverbs.Clear();
            }
            if (sems.Count == 0)
            {
                return(null);
            }
            if (attrs.Count > 0)
            {
                sems[sems.Count - 1].Attrs.AddRange(attrs);
            }
            if (adverbs.Count > 0)
            {
                Pullenti.Semantic.SemObject sem = sems[sems.Count - 1];
                foreach (Pullenti.Semantic.SemObject a in adverbs)
                {
                    gr.AddLink(Pullenti.Semantic.SemLinkType.Detail, sem, a, "как", false, null);
                }
            }
            for (int i = sems.Count - 1; i > 0; i--)
            {
                gr.AddLink(Pullenti.Semantic.SemLinkType.Detail, sems[i - 1], sems[i], "что делать", false, null);
            }
            return(sems[0]);
        }
示例#4
0
        static bool _processParticiples(Pullenti.Semantic.SemGraph gr)
        {
            bool ret = false;

            for (int i = 0; i < gr.Objects.Count; i++)
            {
                Pullenti.Semantic.SemObject obj = gr.Objects[i];
                if (obj.Typ != Pullenti.Semantic.SemObjectType.Participle)
                {
                    continue;
                }
                Pullenti.Semantic.SemLink own = null;
                bool has = false;
                foreach (Pullenti.Semantic.SemLink li in obj.LinksTo)
                {
                    if (li.Typ == Pullenti.Semantic.SemLinkType.Participle)
                    {
                        own = li;
                    }
                    else
                    {
                        has = true;
                    }
                }
                if (!has)
                {
                    continue;
                }
                if (own == null)
                {
                    Pullenti.Semantic.SemObject dum = new Pullenti.Semantic.SemObject(gr)
                    {
                        Typ = Pullenti.Semantic.SemObjectType.Noun
                    };
                    if (obj.Morph != null)
                    {
                        dum.Morph = new Pullenti.Morph.MorphWordForm()
                        {
                            Class = Pullenti.Morph.MorphClass.Noun, Number = obj.Morph.Number, Gender = obj.Morph.Gender, Case = obj.Morph.Case
                        }
                    }
                    ;
                    gr.Objects.Add(dum);
                    own = gr.AddLink(Pullenti.Semantic.SemLinkType.Participle, dum, obj, "какой", false, null);
                    ret = true;
                }
                for (int j = obj.LinksTo.Count - 1; j >= 0; j--)
                {
                    Pullenti.Semantic.SemLink li = obj.LinksTo[j];
                    if (li.Typ == Pullenti.Semantic.SemLinkType.Participle)
                    {
                        continue;
                    }
                    bool exi = false;
                    foreach (Pullenti.Semantic.SemLink ll in li.Source.LinksFrom)
                    {
                        if (ll.Target == own.Source)
                        {
                            exi = true;
                        }
                    }
                    if (exi)
                    {
                        gr.RemoveLink(li);
                    }
                    else
                    {
                        obj.LinksTo.RemoveAt(j);
                        li.m_Target = own.Source;
                    }
                    ret = true;
                }
            }
            return(ret);
        }