Ejemplo n.º 1
0
 internal void Deserialize(Stream str, bool lazyLoad)
 {
     Pullenti.Morph.Internal.ByteArrayWrapper wr = null;
     using (MemoryStream tmp = new MemoryStream())
     {
         Pullenti.Morph.Internal.MorphDeserializer.DeflateGzip(str, tmp);
         wr = new Pullenti.Morph.Internal.ByteArrayWrapper(tmp.ToArray());
         int pos = 0;
         int cou = wr.DeserializeInt(ref pos);
         for (; cou > 0; cou--)
         {
             int p1 = wr.DeserializeInt(ref pos);
             Pullenti.Semantic.Utils.DerivateGroup ew = new Pullenti.Semantic.Utils.DerivateGroup();
             if (lazyLoad)
             {
                 ew.LazyPos = pos;
                 pos        = p1;
             }
             else
             {
                 ew.Deserialize(wr, ref pos);
             }
             ew.Id = m_AllGroups.Count + 1;
             m_AllGroups.Add(ew);
         }
         m_Root = new ExplanTreeNode();
         m_Root.Deserialize(wr, this, lazyLoad, ref pos);
     }
     m_Buf = wr;
 }
Ejemplo n.º 2
0
        static int _calcRank(Pullenti.Semantic.Utils.DerivateGroup gr)
        {
            if (gr.IsDummy)
            {
                return(0);
            }
            int res = 0;

            foreach (Pullenti.Semantic.Utils.DerivateWord w in gr.Words)
            {
                if (w.Lang.IsRu && w.Class != null)
                {
                    if (w.Class.IsVerb && w.Class.IsAdjective)
                    {
                    }
                    else
                    {
                        res++;
                    }
                }
            }
            if (gr.Prefix == null)
            {
                res += 3;
            }
            return(res);
        }
Ejemplo n.º 3
0
 static void _addNormals(KeywordReferent kref, List <Pullenti.Semantic.Utils.DerivateGroup> grs, string norm)
 {
     if (grs == null || grs.Count == 0)
     {
         return;
     }
     for (int k = 0; k < grs.Count; k++)
     {
         bool ch = false;
         for (int i = 0; i < (grs.Count - 1); i++)
         {
             if (_calcRank(grs[i]) < _calcRank(grs[i + 1]))
             {
                 Pullenti.Semantic.Utils.DerivateGroup gr = grs[i];
                 grs[i]     = grs[i + 1];
                 grs[i + 1] = gr;
                 ch         = true;
             }
         }
         if (!ch)
         {
             break;
         }
     }
     for (int i = 0; (i < 3) && (i < grs.Count); i++)
     {
         if (!grs[i].IsDummy && grs[i].Words.Count > 0)
         {
             if (grs[i].Words[0].Spelling != norm)
             {
                 kref.AddSlot(KeywordReferent.ATTR_NORMAL, grs[i].Words[0].Spelling, false, 0);
             }
         }
     }
 }
Ejemplo n.º 4
0
        internal void Deserialize(Pullenti.Morph.Internal.ByteArrayWrapper str, DerivateDictionary dic, bool lazyLoad, ref int pos)
        {
            int        cou = str.DeserializeShort(ref pos);
            List <int> li  = (cou > 0 ? new List <int>() : null);

            for (; cou > 0; cou--)
            {
                int id = str.DeserializeInt(ref pos);
                if (id > 0 && id <= dic.m_AllGroups.Count)
                {
                    Pullenti.Semantic.Utils.DerivateGroup gr = dic.m_AllGroups[id - 1];
                    if (gr.LazyPos > 0)
                    {
                        int p0 = pos;
                        pos = gr.LazyPos;
                        gr.Deserialize(str, ref pos);
                        gr.LazyPos = 0;
                        pos        = p0;
                    }
                }
                li.Add(id);
            }
            if (li != null)
            {
                Groups = li;
            }
            cou = str.DeserializeShort(ref pos);
            if (cou == 0)
            {
                return;
            }
            for (; cou > 0; cou--)
            {
                int            ke  = str.DeserializeShort(ref pos);
                int            p1  = str.DeserializeInt(ref pos);
                ExplanTreeNode tn1 = new ExplanTreeNode();
                if (Nodes == null)
                {
                    Nodes = new Dictionary <short, ExplanTreeNode>();
                }
                short sh = (short)ke;
                if (lazyLoad)
                {
                    tn1.LazyPos = pos;
                    pos         = p1;
                }
                else
                {
                    tn1.Deserialize(str, dic, false, ref pos);
                }
                if (!Nodes.ContainsKey(sh))
                {
                    Nodes.Add(sh, tn1);
                }
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Проверка значения
 /// </summary>
 public bool IsValue(string word, SemObjectType typ = SemObjectType.Undefined)
 {
     if (typ != SemObjectType.Undefined)
     {
         if (typ != Typ)
         {
             return(false);
         }
     }
     if (Morph.NormalFull == word || Morph.NormalCase == word)
     {
         return(true);
     }
     Pullenti.Semantic.Utils.DerivateGroup gr = Concept as Pullenti.Semantic.Utils.DerivateGroup;
     if (gr != null)
     {
         if (gr.Words[0].Spelling == word)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 6
0
 public static Pullenti.Semantic.Utils.DerivateWord FindWordInGroup(Pullenti.Ner.MetaToken mt, Pullenti.Semantic.Utils.DerivateGroup gr)
 {
     if (gr == null || mt == null)
     {
         return(null);
     }
     Pullenti.Ner.Token t = null;
     if (mt is Pullenti.Ner.Core.NounPhraseToken)
     {
         t = (mt as Pullenti.Ner.Core.NounPhraseToken).Noun.EndToken;
     }
     else if ((mt is SemanticAbstractSlave) && ((mt as SemanticAbstractSlave).Source is Pullenti.Ner.Core.NounPhraseToken))
     {
         t = ((mt as SemanticAbstractSlave).Source as Pullenti.Ner.Core.NounPhraseToken).Noun.EndToken;
     }
     else
     {
         t = mt.EndToken;
     }
     foreach (Pullenti.Semantic.Utils.DerivateWord w in gr.Words)
     {
         if (w.Class != null && w.Class.IsNoun && w.Lang.IsRu)
         {
             if (t.IsValue(w.Spelling, null))
             {
                 return(w);
             }
         }
     }
     return(null);
 }
Ejemplo n.º 7
0
        static List <SemanticLink> _tryCreateVerb(Pullenti.Ner.Core.VerbPhraseToken vpt1, Pullenti.Ner.MetaToken slave, Pullenti.Semantic.Utils.DerivateGroup gr)
        {
            if (slave is Pullenti.Ner.Core.VerbPhraseToken)
            {
                return(_tryCreateInf(vpt1, slave as Pullenti.Ner.Core.VerbPhraseToken, gr));
            }
            SemanticAbstractSlave sla2 = slave as SemanticAbstractSlave;
            List <SemanticLink>   res  = new List <SemanticLink>();

            if (sla2 == null)
            {
                return(res);
            }
            Pullenti.Semantic.Utils.ControlModelItem cit = FindControlItem(vpt1.LastVerb, gr);
            string prep = sla2.Preposition;

            Pullenti.Morph.MorphBaseInfo morph = (Pullenti.Morph.MorphBaseInfo)sla2.Morph;
            bool isRev1  = vpt1.LastVerb.IsVerbReversive || vpt1.LastVerb.IsVerbPassive;
            bool noNomin = false;
            bool noInstr = false;

            if (prep == null && morph.Case.IsNominative && !vpt1.FirstVerb.IsParticiple)
            {
                bool ok  = true;
                bool err = false;
                Pullenti.Morph.MorphWordForm vm = vpt1.FirstVerb.VerbMorph;
                if (vm == null)
                {
                    return(res);
                }
                if (vm.Number == Pullenti.Morph.MorphNumber.Singular)
                {
                    if (morph.Number == Pullenti.Morph.MorphNumber.Plural)
                    {
                        if (!vpt1.FirstVerb.IsVerbInfinitive)
                        {
                            ok = false;
                        }
                    }
                }
                if (!CheckMorphAccord(morph, false, vm, false))
                {
                    if (!err && !vpt1.FirstVerb.IsVerbInfinitive)
                    {
                        ok = false;
                    }
                }
                else if (vm.Misc.Person != Pullenti.Morph.MorphPerson.Undefined)
                {
                    if (((vm.Misc.Person & Pullenti.Morph.MorphPerson.Third)) == Pullenti.Morph.MorphPerson.Undefined)
                    {
                        if (((vm.Misc.Person & Pullenti.Morph.MorphPerson.First)) == Pullenti.Morph.MorphPerson.First)
                        {
                            if (!morph.ContainsAttr("1 л.", null))
                            {
                                ok = false;
                            }
                        }
                        if (((vm.Misc.Person & Pullenti.Morph.MorphPerson.Second)) == Pullenti.Morph.MorphPerson.Second)
                        {
                            if (!morph.ContainsAttr("2 л.", null))
                            {
                                ok = false;
                            }
                        }
                    }
                }
                noNomin = true;
                if (ok)
                {
                    Pullenti.Semantic.Utils.ControlModelItem cit00 = cit;
                    bool isRev0 = isRev1;
                    if (vpt1.FirstVerb != vpt1.LastVerb && ((vpt1.FirstVerb.IsVerbReversive || vpt1.FirstVerb.IsVerbPassive || vpt1.FirstVerb.Normal == "ИМЕТЬ")))
                    {
                        cit00  = null;
                        isRev0 = true;
                        List <Pullenti.Semantic.Utils.DerivateGroup> grs = FindDerivates(vpt1.FirstVerb);
                        if (grs != null)
                        {
                            foreach (Pullenti.Semantic.Utils.DerivateGroup gg in grs)
                            {
                                if ((((cit00 = FindControlItem(vpt1.FirstVerb, gg)))) != null)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    SemanticLink sl       = null;
                    bool         addagent = false;
                    if (cit00 == null)
                    {
                        sl = new SemanticLink()
                        {
                            Modelled = true, Role = (isRev0 ? SemanticRole.Pacient : SemanticRole.Agent), Rank = 1, Question = Pullenti.Semantic.Utils.ControlModelQuestion.BaseNominative, IsPassive = isRev0
                        }
                    }
                    ;
                    else
                    {
                        foreach (KeyValuePair <Pullenti.Semantic.Utils.ControlModelQuestion, SemanticRole> kp in cit00.Links)
                        {
                            Pullenti.Semantic.Utils.ControlModelQuestion q = kp.Key;
                            if (q.Check(null, Pullenti.Morph.MorphCase.Nominative))
                            {
                                sl = new SemanticLink()
                                {
                                    Role = kp.Value, Rank = 2, Question = q, IsPassive = isRev0
                                };
                                if (sl.Role == SemanticRole.Agent)
                                {
                                    sl.IsPassive = false;
                                }
                                else if (sl.Role == SemanticRole.Pacient && cit00.NominativeCanBeAgentAndPacient && vpt1.LastVerb.IsVerbReversive)
                                {
                                    addagent = true;
                                }
                                break;
                            }
                        }
                    }
                    if (sl != null)
                    {
                        if (cit00 == null && morph.Case.IsInstrumental && isRev0)
                        {
                            sl.Rank -= 0.5;
                        }
                        if (morph.Case.IsAccusative)
                        {
                            sl.Rank -= 0.5;
                        }
                        if (sla2.BeginChar > vpt1.BeginChar)
                        {
                            sl.Rank -= 0.5;
                        }
                        if (err)
                        {
                            sl.Rank -= 0.5;
                        }
                        res.Add(sl);
                        if (addagent)
                        {
                            res.Add(new SemanticLink()
                            {
                                Role = SemanticRole.Agent, Rank = sl.Rank, Question = sl.Question
                            });
                        }
                    }
                }
            }
            if (prep == null && isRev1 && morph.Case.IsInstrumental)
            {
                noInstr = true;
                Pullenti.Semantic.Utils.ControlModelItem cit00 = cit;
                SemanticLink sl = null;
                if (cit00 == null)
                {
                    sl = new SemanticLink()
                    {
                        Modelled = true, Role = SemanticRole.Agent, Rank = 1, Question = Pullenti.Semantic.Utils.ControlModelQuestion.BaseInstrumental, IsPassive = true
                    }
                }
                ;
                else
                {
                    foreach (KeyValuePair <Pullenti.Semantic.Utils.ControlModelQuestion, SemanticRole> kp in cit00.Links)
                    {
                        Pullenti.Semantic.Utils.ControlModelQuestion q = kp.Key;
                        if (q.Check(null, Pullenti.Morph.MorphCase.Instrumental))
                        {
                            sl = new SemanticLink()
                            {
                                Role = kp.Value, Rank = 2, Question = q
                            };
                            if (sl.Role == SemanticRole.Agent)
                            {
                                sl.IsPassive = true;
                            }
                            break;
                        }
                    }
                }
                if (sl != null)
                {
                    if (cit00 == null && morph.Case.IsNominative)
                    {
                        sl.Rank -= 0.5;
                    }
                    if (morph.Case.IsAccusative)
                    {
                        sl.Rank -= 0.5;
                    }
                    if (sla2.BeginChar < vpt1.BeginChar)
                    {
                        sl.Rank -= 0.5;
                    }
                    res.Add(sl);
                    if ((gr != null && gr.Model.Items.Count > 0 && gr.Model.Items[0].Typ == Pullenti.Semantic.Utils.ControlModelItemType.Verb) && gr.Model.Items[0].Links.ContainsKey(Pullenti.Semantic.Utils.ControlModelQuestion.BaseInstrumental))
                    {
                        sl.Rank = 0;
                        SemanticLink sl0 = new SemanticLink()
                        {
                            Question = sl.Question, Rank = 1, Role = gr.Model.Items[0].Links[Pullenti.Semantic.Utils.ControlModelQuestion.BaseInstrumental]
                        };
                        res.Insert(0, sl0);
                    }
                }
            }
            if (prep == null && morph.Case.IsDative && ((cit == null || !cit.Links.ContainsKey(Pullenti.Semantic.Utils.ControlModelQuestion.BaseDative))))
            {
                SemanticLink sl = new SemanticLink()
                {
                    Modelled = cit == null, Role = SemanticRole.Strong, Rank = 1, Question = Pullenti.Semantic.Utils.ControlModelQuestion.BaseDative
                };
                if (morph.Case.IsAccusative || morph.Case.IsNominative)
                {
                    sl.Rank -= 0.5;
                }
                if (vpt1.EndToken.Next != sla2.BeginToken)
                {
                    sl.Rank -= 0.5;
                }
                if (cit != null)
                {
                    sl.Rank -= 0.5;
                }
                res.Add(sl);
            }
            _createRoles(cit, prep, morph.Case, res, noNomin, noInstr);
            if (gr != null && gr.Model.Pacients.Count > 0)
            {
                bool ok = false;
                foreach (string n in gr.Model.Pacients)
                {
                    if (sla2.Source != null)
                    {
                        if (sla2.Source.EndToken.IsValue(n, null))
                        {
                            ok = true;
                            break;
                        }
                    }
                    else if (sla2.EndToken.IsValue(n, null))
                    {
                        ok = true;
                        break;
                    }
                }
                if (ok)
                {
                    if (res.Count == 0)
                    {
                        ok = false;
                        if (prep == null && isRev1 && morph.Case.IsNominative)
                        {
                            ok = true;
                        }
                        else if (prep == null && !isRev1 && morph.Case.IsAccusative)
                        {
                            ok = true;
                        }
                        if (ok)
                        {
                            res.Add(new SemanticLink()
                            {
                                Role = SemanticRole.Pacient, Question = (isRev1 ? Pullenti.Semantic.Utils.ControlModelQuestion.BaseNominative : Pullenti.Semantic.Utils.ControlModelQuestion.BaseAccusative), Idiom = true
                            });
                        }
                    }
                    else
                    {
                        foreach (SemanticLink r in res)
                        {
                            r.Rank += 4;
                            if (r.Role == SemanticRole.Common)
                            {
                                r.Role = SemanticRole.Strong;
                            }
                            if (vpt1.EndToken.Next == sla2.BeginToken)
                            {
                                r.Rank += 2;
                            }
                            r.Idiom = true;
                        }
                    }
                }
            }
            return(res);
        }
Ejemplo n.º 8
0
        static List <SemanticLink> _tryCreateNoun(Pullenti.Ner.Core.NounPhraseToken npt1, Pullenti.Ner.MetaToken slave, Pullenti.Semantic.Utils.DerivateGroup gr)
        {
            if (npt1 == null || slave == null)
            {
                return(null);
            }
            if (slave is Pullenti.Ner.Core.VerbPhraseToken)
            {
                return(_tryCreateInf(npt1, slave as Pullenti.Ner.Core.VerbPhraseToken, gr));
            }
            SemanticAbstractSlave sla2 = slave as SemanticAbstractSlave;
            List <SemanticLink>   res  = new List <SemanticLink>();

            if (sla2 == null)
            {
                return(res);
            }
            Pullenti.Semantic.Utils.ControlModelItem cit = FindControlItem(npt1, gr);
            _createRoles(cit, sla2.Preposition, sla2.Morph.Case, res, false, false);
            if (res.Count == 1 && res[0].Role == SemanticRole.Agent && res[0].Question == Pullenti.Semantic.Utils.ControlModelQuestion.BaseInstrumental)
            {
                if (gr.Model.Items.Count > 0 && gr.Model.Items[0].Typ == Pullenti.Semantic.Utils.ControlModelItemType.Verb && gr.Model.Items[0].Links.ContainsKey(Pullenti.Semantic.Utils.ControlModelQuestion.BaseInstrumental))
                {
                    res[0].Role = gr.Model.Items[0].Links[Pullenti.Semantic.Utils.ControlModelQuestion.BaseInstrumental];
                }
            }
            bool ok = false;

            Pullenti.Semantic.Utils.DerivateWord w = FindWordInGroup(npt1, gr);
            if (w != null && w.NextWords != null && w.NextWords.Count > 0)
            {
                foreach (string n in w.NextWords)
                {
                    if (sla2.Source != null)
                    {
                        if (sla2.Source.EndToken.IsValue(n, null))
                        {
                            ok = true;
                            break;
                        }
                    }
                }
            }
            if (gr != null && gr.Model.Pacients.Count > 0)
            {
                foreach (string n in gr.Model.Pacients)
                {
                    if (sla2.Source != null)
                    {
                        if (sla2.Source.EndToken.IsValue(n, null))
                        {
                            ok = true;
                            break;
                        }
                    }
                }
            }
            if (ok)
            {
                if (res.Count == 0)
                {
                    res.Add(new SemanticLink()
                    {
                        Question = Pullenti.Semantic.Utils.ControlModelQuestion.BaseGenetive, Role = SemanticRole.Pacient, Idiom = true
                    });
                }
                foreach (SemanticLink r in res)
                {
                    r.Rank += 4;
                    if (r.Role == SemanticRole.Common)
                    {
                        r.Role = SemanticRole.Strong;
                    }
                    if (npt1.EndToken.Next == sla2.BeginToken)
                    {
                        r.Rank += 2;
                    }
                    r.Idiom = true;
                }
            }
            return(res);
        }
Ejemplo n.º 9
0
        static List <SemanticLink> _tryCreateInf(Pullenti.Ner.MetaToken master, Pullenti.Ner.Core.VerbPhraseToken vpt2, Pullenti.Semantic.Utils.DerivateGroup gr)
        {
            Pullenti.Semantic.Utils.ControlModelItem cit = FindControlItem(master, gr);
            List <SemanticLink> res = new List <SemanticLink>();
            SemanticRole?       rol = null;

            if (cit != null && cit.Links.ContainsKey(Pullenti.Semantic.Utils.ControlModelQuestion.ToDo))
            {
                rol = cit.Links[Pullenti.Semantic.Utils.ControlModelQuestion.ToDo];
            }
            if (rol != null)
            {
                res.Add(new SemanticLink()
                {
                    Rank = (rol.Value != SemanticRole.Common ? 2 : 1), Question = Pullenti.Semantic.Utils.ControlModelQuestion.ToDo
                });
            }
            return(res);
        }
Ejemplo n.º 10
0
 static Pullenti.Semantic.Utils.ControlModelItem FindControlItem(Pullenti.Ner.MetaToken mt, Pullenti.Semantic.Utils.DerivateGroup gr)
 {
     if (gr == null)
     {
         return(null);
     }
     if (mt is Pullenti.Ner.Core.NounPhraseToken)
     {
         Pullenti.Ner.Token t = (mt as Pullenti.Ner.Core.NounPhraseToken).Noun.EndToken;
         foreach (Pullenti.Semantic.Utils.ControlModelItem m in gr.Model.Items)
         {
             if (m.Word != null)
             {
                 if (t.IsValue(m.Word, null))
                 {
                     return(m);
                 }
             }
         }
         foreach (Pullenti.Semantic.Utils.DerivateWord w in gr.Words)
         {
             if (w.Attrs.IsVerbNoun)
             {
                 if (t.IsValue(w.Spelling, null))
                 {
                     return(gr.Model.FindItemByTyp(Pullenti.Semantic.Utils.ControlModelItemType.Noun));
                 }
             }
         }
         return(null);
     }
     if (mt is Pullenti.Ner.Core.VerbPhraseItemToken)
     {
         Pullenti.Ner.Core.VerbPhraseItemToken ti = mt as Pullenti.Ner.Core.VerbPhraseItemToken;
         bool rev = ti.IsVerbReversive || ti.IsVerbPassive;
         foreach (Pullenti.Semantic.Utils.ControlModelItem it in gr.Model.Items)
         {
             if (rev && it.Typ == Pullenti.Semantic.Utils.ControlModelItemType.Reflexive)
             {
                 return(it);
             }
             else if (!rev && it.Typ == Pullenti.Semantic.Utils.ControlModelItemType.Verb)
             {
                 return(it);
             }
         }
     }
     return(null);
 }