Exemplo n.º 1
0
        public SToken Add(string token)
        {
            if (string.IsNullOrWhiteSpace(token))
            {
                return(null);
            }

            return(Context(db =>
            {
                SToken sToken = new SToken
                {
                    Systime = DateTime.Now,
                    Createtime = DateTime.Now,
                    Invalid = false,
                    Operator = "hjh",
                    Token = token,
                    Tid = TRandom.Instance.GetRandomString(20)
                };

                db.STokens.InsertOnSubmit(sToken);

                db.SubmitChanges();

                return sToken;
            }));
        }
 private Task SendValidatingEmailAsync(SToken token)
 {
     return(Task.Run(() =>
     {
         token.ValidationCode = CodeGenerator.GetCode(8);
         logger.LogInformation("Validation Code for User(id: " + token.UserID + ") is: " + token.ValidationCode);
         var mailMsg = new MailMessage();
         mailMsg.From = new MailAddress("*****@*****.**", "Do-Not-Reply");
         mailMsg.To.Add(new MailAddress(token.Email));
         mailMsg.Subject = "线上教学系统:密码找回验证码";
         mailMsg.Body = "请勿将验证码泄露给他人!\n验证码:" + token.ValidationCode;
         var client = new SmtpClient();
         client.Host = "smtp.qq.com";
         client.Port = 587;
         client.EnableSsl = true;
         client.Credentials = new NetworkCredential("*****@*****.**", "dlshuyxgvyodbjbc");
         try
         {
             client.SendAsync(mailMsg, null);
         }
         catch (Exception e)
         {
             logger.LogError(e.Message);
             throw new Exception("邮件发送失败");
         }
     }));
 }
        public async Task <string> AddSTokenAsync(User userInfo, string email)
        {
            if (userInfo == null)
            {
                throw new Exception("用户不存在");
            }
            if (userInfo.Email != email)
            {
                throw new Exception("邮箱错误");
            }
            var t   = new SToken(userInfo.Id, email);
            var tID = CodeGenerator.GetCode(20);

            t.ID = tID;
            await SendValidatingEmailAsync(t);

            tokenMap.Add(tID, t);
            return(tID);
        }
Exemplo n.º 4
0
        public EDocument(string documentText)
        {
            DocumentText = documentText;
            RootTocken = new SToken();
            SToken newTocken = new SToken();

            RootTocken.Configure(0, string.Empty + "Program", SElementType.Undefined, string.Empty + " * ");
            newTocken.Configure(0, string.Empty + "..", SElementType.Undefined, string.Empty);
            RootTocken.TokenNodeType = SElementType.Undefined;
            newTocken.TokenNodeType = SElementType.Undefined;

            RootTocken.Link(SToken.NULL, SToken.NULL, null, null);
            newTocken.Link(rootTocken, SToken.NULL, null, null);

            RootTocken.TokenChilds.Add(newTocken);
            parentToken = RootTocken;
            currentToken = RootTocken.TokenChilds[0];

            InitializePersons();            
        }
Exemplo n.º 5
0
        /*
        private int p;
        private string p_2;
        private SElementType sElementType;
        private string p_3;
        */
        #endregion NULL

        #region DEFINE

        internal void Link(SToken tokenParent, SToken tokenBefore, SToken tokenAfter, List<SToken> tokenChilds)
        {
            TokenParent = tokenParent;
            TokenBefore = tokenBefore;
            if (tokenAfter != null)
            {
                if (TokenAfter == null)
                {
                    TokenAfter = new List<SToken>();
                    TokenAfter.Add(tokenAfter);
                    if (tokenAfter.TokenChilds == null)
                    {
                        tokenAfter.TokenChilds = new List<SToken>();
                    }
                }
            }
            if (tokenChilds == null)
            {
                tokenChilds = new List<SToken>();
            }
            TokenChilds = tokenChilds;
        }
Exemplo n.º 6
0
        private void Wrap(SToken token)
        {

            while (token != null)
            {
                //if (token.TokenType == SElementType.Symbol)
                {

                    CodeFile newCodeFile = new CodeFile();
                    ElementNamespace namespaceItem = new ElementNamespace();
                    ElementClass classItem = new ElementClass();

                    bool isNeededCodeFileCreation = true;
                    foreach (CodeFile existentCodeFile in program.CodeFiles)
                    {
                        if (existentCodeFile.FileName == ToUp(token.TokenString))
                        {
                            isNeededCodeFileCreation = false;
                            newCodeFile = existentCodeFile;
                        }
                    }
                    if (isNeededCodeFileCreation)
                    {
                        newCodeFile.FileName = ToUp(token.TokenString);
                        program.CodeFiles.Add(newCodeFile);
                        namespaceItem.Name = "GeneratedApplication";
                        newCodeFile.Namespaces.Add(namespaceItem);
                        classItem.Name = ToUp(token.TokenString);
                        namespaceItem.ElementClasses.Add(classItem);
                    }


                    if (token.TokenProperties != null)
                    {
                        SToken tokenToRead = token;
                         if (token.TokenProperties[0].TokenNodeType == SElementType.FoldOpen)
                          {
                             foreach (SToken localTokenF in token.TokenProperties[0].TokenChilds)
                              {
                                 SToken localToken = localTokenF;
                                 while (localToken != null)
                                 {
                                     Wrap(localToken);
                                     ElementProperty newProperty = new ElementProperty();
                                     switch (localToken.TokenMultiplicity)
                                     {
                                         case SElementMultiplicity.Some:
                                             newProperty.Name = ToUp(localToken.TokenString) + "s";
                                             newProperty.PropertyType = "List<" + ToUp(localToken.TokenString) + ">";
                                             break;
                                         case SElementMultiplicity.Explicit:
                                             newProperty.Name = ToUp(localToken.TokenString) + "s";
                                             newProperty.PropertyType = "" + ToUp(localToken.TokenString) + " " + token.TokenMultiplicityLevel + "";
                                             break;
                                         default:
                                             newProperty.Name = ToUp(localToken.TokenString) + "P";
                                             newProperty.PropertyType = ToUp(localToken.TokenString);
                                             break;
                                     }
                                     //newProperty.PropertyType = localToken.TokenString;
                                     newProperty.ElementClassName = ToUp(localToken.TokenString);
                                     classItem.ElementProperties.Add(newProperty);
                                     if (localToken.TokenAfter != null)
                                     {
                                         localToken = localToken.TokenAfter[0];
                                     }
                                     else
                                     {
                                         localToken = null;
                                     }
                                 }
                             }
                         }
                         else
                         foreach (SToken localToken in token.TokenProperties)
                        {

                            Wrap(localToken);
                            ElementProperty newProperty = new ElementProperty();
                            switch (localToken.TokenMultiplicity)
                            {
                                case SElementMultiplicity.Some:
                                    newProperty.Name = ToUp(localToken.TokenString) + "s";
                                    newProperty.PropertyType = "List<" + ToUp(localToken.TokenString) + ">";
                                    break;
                                case SElementMultiplicity.Explicit:
                                    newProperty.Name = ToUp(localToken.TokenString) + "s";
                                    newProperty.PropertyType = "" + ToUp(localToken.TokenString) + " " + token.TokenMultiplicityLevel + "";
                                    break;
                                default:
                                    newProperty.Name = ToUp(localToken.TokenString) + "P";
                                    newProperty.PropertyType = ToUp(localToken.TokenString);
                                    break;
                            }
                            //newProperty.PropertyType = localToken.TokenString;
                            newProperty.ElementClassName = ToUp(localToken.TokenString);
                            classItem.ElementProperties.Add(newProperty);
                        }
                    }
                    if (token.TokenActivities != null)
                     {
                        foreach (SToken localToken in token.TokenActivities)
                        {
                            Wrap(localToken);
                            ElementMethod newMethod = new ElementMethod();

                            newMethod.Name = ToUp(localToken.TokenString);
                            newMethod.ElementAccessType = ElementMethod.DataAccessType.isPublic;
                            
                            //newProperty.PropertyType = localToken.TokenString;
                            newMethod.ElementClassName = ToUp(localToken.TokenString);
                            classItem.ElementMethods.Add(newMethod);
                        }
                    }


                    List<string> influencesA = new List<string>();
                    List<string> influencesB = new List<string>();
                    List<string> influencesC = new List<string>();
                    List<string> influencesD = new List<string>();
                        
                    if (token.TokenInfluenceA != null)
                    {

                        foreach (SToken localToken in token.TokenInfluenceA)
                        {
                            /*{
                                foreach (SToken tokenA in token.TokenInfluenceA)
                                {
                                    if (token.TokenString.Equals(tokenA))
                                    {
                                        System.Windows.Forms.MessageBox.Show("No need to repeat yourself");
                                        break;
                                    }
                                }
                            }*/
                            if (!influencesA.Contains(localToken.TokenString))
                            {
                                influencesA.Add(localToken.TokenString);
                            }
                            else
                            {
                                System.Windows.Forms.MessageBox.Show("No need to repeat yourself!");
                                break;
                            }
                            if (influencesB.Contains(localToken.TokenString))
                            {
                                 System.Windows.Forms.MessageBox.Show("Your statement is inconsistent!");
                                break;
                            }
                            if (influencesD.Contains(localToken.TokenString))
                            {
                                System.Windows.Forms.MessageBox.Show("You defined a rebound effect!");
                                break;
                            }
                            
                            //Wrap(localToken);
                            ElementMethod newMethod = new ElementMethod();

                            newMethod.Name = ToUp(localToken.TokenString);
                            newMethod.ElementAccessType = ElementMethod.DataAccessType.isPublic;
                            newMethod.IsStatic = true;
                            //newProperty.PropertyType = localToken.TokenString;
                            newMethod.Name = "Increase" + ToUp(localToken.TokenString);
                            classItem.ElementMethods.Add(newMethod);
                        }
                    }

                    if (token.TokenInfluenceB != null)
                    {
                        foreach (SToken localToken in token.TokenInfluenceB)
                        {
                            if (!influencesB.Contains(localToken.TokenString))
                            {
                                influencesB.Add(localToken.TokenString);
                            }
                            else
                            {
                                System.Windows.Forms.MessageBox.Show("No need to repeat yourself!");
                                break;
                            }
                            if (influencesA.Contains(localToken.TokenString))
                            {
                                System.Windows.Forms.MessageBox.Show("Your statement is inconsistent!");
                                break;
                            }
                            if (influencesC.Contains(localToken.TokenString))
                            {
                                System.Windows.Forms.MessageBox.Show("You defined a rebound effect!");
                                break;
                            }
                            //Wrap(localToken);
                            ElementMethod newMethod = new ElementMethod();

                            newMethod.Name = ToUp(localToken.TokenString);
                            newMethod.ElementAccessType = ElementMethod.DataAccessType.isPublic;
                            newMethod.IsStatic = true;
                            
                            //newProperty.PropertyType = localToken.TokenString;
                            newMethod.Name = "Decrease" + ToUp(localToken.TokenString);
                            classItem.ElementMethods.Add(newMethod);
                        }
                    }

                    if (token.TokenInfluenceC != null)
                    {
                        foreach (SToken localToken in token.TokenInfluenceC)
                        {
                            if (!influencesC.Contains(localToken.TokenString))
                            {
                                influencesC.Add(localToken.TokenString);
                            }
                            else
                            {
                                System.Windows.Forms.MessageBox.Show("No need to repeat yourself!");
                                break;
                            }
                            if (influencesD.Contains(localToken.TokenString))
                            {
                                System.Windows.Forms.MessageBox.Show("Your statement is inconsistent!");
                                break;
                            }
                            if (influencesB.Contains(localToken.TokenString))
                            {
                                System.Windows.Forms.MessageBox.Show("You defined a rebound effect!");
                                break;
                            }
                            Wrap(localToken);
                            ElementMethod newMethod = new ElementMethod();

                            newMethod.Name = ToUp(localToken.TokenString);
                            newMethod.ElementAccessType = ElementMethod.DataAccessType.isPublic;
                            newMethod.IsStatic = true;
                            
                            //newProperty.PropertyType = localToken.TokenString;
                            newMethod.Name = "IncreasedBy" + ToUp(localToken.TokenString);
                            classItem.ElementMethods.Add(newMethod);
                        }
                    }

                    if (token.TokenInfluenceD != null)
                    {
                        foreach (SToken localToken in token.TokenInfluenceD)
                        {
                            if (!influencesD.Contains(localToken.TokenString))
                            {
                                influencesD.Add(localToken.TokenString);
                            }
                            else
                            {
                                System.Windows.Forms.MessageBox.Show("No need to repeat yourself!");
                                break;
                            }
                            if (influencesC.Contains(localToken.TokenString))
                            {
                                System.Windows.Forms.MessageBox.Show("Your statement is inconsistent!");
                                break;
                            }
                            if (influencesA.Contains(localToken.TokenString))
                            {
                                System.Windows.Forms.MessageBox.Show("You defined a rebound effect!");
                                break;
                            }
                            Wrap(localToken);
                            ElementMethod newMethod = new ElementMethod();

                            newMethod.Name = ToUp(localToken.TokenString);
                            newMethod.ElementAccessType = ElementMethod.DataAccessType.isPublic;
                            newMethod.IsStatic = true;
                            
                            //newProperty.PropertyType = localToken.TokenString;
                            newMethod.Name = "DecreasedBy" + ToUp(localToken.TokenString);
                            classItem.ElementMethods.Add(newMethod);
                        }
                    }

                }
                foreach (SToken tokenlist in token.TokenChilds)
                {
                    Wrap(tokenlist);
                }
                if (token.TokenAfter != null)
                {
                    token = token.TokenAfter[0];
                }
                else
                {
                    token = null;
                }
            }
        }
Exemplo n.º 7
0
        public string ToString(SToken token)
        {
            int acoladeBracketBlock = 0;
            int angleBracketBlock = 0;
            int rightBracketBlock = 0;
            int roundBracketBlock = 0;

            StringBuilder tokenCollectionAsString = new StringBuilder();

            while (token != null)
            {
                string color;
                switch (token.TokenType)
                {
                    case SElementType.Asignement:
                        color = "red";
                        break;
                    case SElementType.Comparator:
                        color = "orange";
                        break;
                    case SElementType.Incertitude:
                        color = "magenta";
                        break;
                    case SElementType.Iterator:
                        color = "orange";
                        break;
                    case SElementType.Logic:
                        color = "red";
                        break;
                    case SElementType.Multiplicity:
                        color = "green";
                        break;
                    case SElementType.Operator:
                        color = "blue";
                        break;
                    case SElementType.Selector:
                        color = "green";
                        break;
                    case SElementType.Symbol:
                        color = "purple";
                        break;
                    default:
                        color = "black";
                        break;
                }
                //*** if (((!(token.TokenAfter == null) && !(token.TokenAfter.TokenAfter == null) && (!(token.TokenAfter.TokenAfter.TokenAfter == null && !string.IsNullOrEmpty(token.TokenAfter.TokenString) && ")]>}".Contains(token.TokenAfter.TokenString[0]))))) || token.TokenType == SElementType.Undefined)
                if (token.TokenAfter != null)
                {
                    tokenCollectionAsString.Append("<span style='color:" + color + "'>" + System.Security.SecurityElement.Escape(pluralize(token)) + "</span> ");
                    //tokenCollectionAsString.Append("<span style='color:" + color + "'>" + System.Security.SecurityElement.Escape(token.TokenString) + "</span> ");
                }
                else
                {
                    tokenCollectionAsString.Append("<span style='color:" + color + "'>" + System.Security.SecurityElement.Escape(pluralize(token)) + "</span> ");
                    //tokenCollectionAsString.Append("<span style='color:" + color + "'>" + System.Security.SecurityElement.Escape(token.TokenString) + "</span> ");
                }
                if (token.TokenChilds != null)
                    foreach (SToken codeTokenlist in token.TokenChilds)
                    {
                        tokenCollectionAsString.Append(ToString(codeTokenlist));
                        /*if (token.TokenType == SElementType.AcoladeBracketBlock && tokenCollectionAsString.ToString().TrimEnd().EndsWith(";"))
                        {
                            tokenCollectionAsString.Append("</br>");
                        }*/
                    }
                if (token.TokenProperties != null)
                    foreach (SToken codeTokenlist in token.TokenProperties)
                    {
                        tokenCollectionAsString.Append(ToString(codeTokenlist));
                        /*if (token.TokenType == SElementType.AcoladeBracketBlock && tokenCollectionAsString.ToString().TrimEnd().EndsWith(";"))
                        {
                            tokenCollectionAsString.Append("</br>");
                        }*/
                    } 
                if (token.TokenActivities != null)
                    foreach (SToken codeTokenlist in token.TokenActivities)
                    {
                        tokenCollectionAsString.Append(ToString(codeTokenlist));
                        /*if (token.TokenType == SElementType.AcoladeBracketBlock && tokenCollectionAsString.ToString().TrimEnd().EndsWith(";"))
                        {
                            tokenCollectionAsString.Append("</br>");
                        }*/
                    }
                token = GetNext(token);

           }

            return tokenCollectionAsString.ToString();
        }
Exemplo n.º 8
0
        private List<SToken> ExplodeStoken(SToken sToken, SToken token, string form)
        {
            List<SToken> tokens = new List<SToken>();
            //foreach (string form in forms)
            {
                string text = sToken.TokenString;
                string textBefore = string.Empty;
                string textAfter = string.Empty;

                if (text.Contains(form))
                {
                    textBefore = " "+text.Substring(0, text.IndexOf(form)).Trim()+" ";
                    textAfter = " "+text.Substring(text.IndexOf(form) + form.Length).Trim()+" ";
                }

               // while (text.Contains(form))
                {
                    SToken tokenInside = new SToken();
                    SToken tokenBefore = new SToken();
                    SToken tokenAfter = new SToken();
                    tokenInside.Configure(token.TokenId, token.TokenString, token.TokenType, token.TokenCode);
                    tokenInside.Link(sToken.TokenParent, tokenBefore, tokenAfter, sToken.TokenChilds);
                    tokenInside.TokenMultiplicity = sToken.TokenMultiplicity;
                    tokenInside.TokenMultiplicityLevel = sToken.TokenMultiplicityLevel;
                    tokenBefore.Configure(sToken.TokenId, textBefore, sToken.TokenType, sToken.TokenCode);
                    tokenBefore.Link(sToken.TokenParent, sToken.TokenBefore, tokenInside, token.TokenChilds);
                    tokenAfter.Configure(sToken.TokenId, textAfter, sToken.TokenType, sToken.TokenCode);
                    //*** tokenAfter.Link(sToken.TokenParent, tokenInside, sToken.TokenAfter, token.TokenChilds);

                   //***  tokenInside.TokenAfter = tokenAfter;

                    if (string.IsNullOrEmpty(tokenBefore.TokenString.Trim()))
                    {
                        tokenInside.TokenBefore = tokenBefore.TokenBefore;
                    }
                    if (string.IsNullOrEmpty(tokenAfter.TokenString.Trim()))
                    {
                        //*** tokenInside.TokenAfter = tokenAfter.TokenAfter;
                    }

                    if (sToken.TokenMultiplicity != SElementMultiplicity.Undefined)
                    {
                        tokenInside.TokenMultiplicity = sToken.TokenMultiplicity;
                        tokenBefore.TokenMultiplicity = sToken.TokenMultiplicity;
                        tokenInside.TokenMultiplicity = sToken.TokenMultiplicity;
                    }
                    else
                    {
                        tokenInside.TokenMultiplicity = token.TokenMultiplicity;
                        tokenBefore.TokenMultiplicity = token.TokenMultiplicity;
                        tokenInside.TokenMultiplicity = token.TokenMultiplicity;
                    }
                    tokenAfter.TokenBefore = tokenBefore;
                    if (sToken.TokenBefore != null)
                    {
                        //*** sToken.TokenBefore.TokenAfter = tokenBefore;
                    }
                    //*** if (sToken.TokenAfter != null)
                    {
                        //*** sToken.TokenAfter.TokenBefore = tokenAfter;
                    }
                    
                    if (!string.IsNullOrEmpty(tokenBefore.TokenString.Trim()))
                    {
                        tokens.Add(tokenBefore);
                    } 
                    tokens.Add(tokenInside);
                    if (!string.IsNullOrEmpty(tokenAfter.TokenString.Trim()))
                    {
                        tokens.Add(tokenAfter);
                    }
                }
            }
            return tokens;
        }
Exemplo n.º 9
0
 public void AdoptMultiplicity(int multiplicityId, string multiplicatorText, List<string> forms, string code)
 {
     SToken token = new SToken();
     //token.Configure();
     //token.Link();
     //multiplicityId, SElementType.Multiplicity, multiplicatorText, SToken.NULL, SToken.NULL, code);
     StringBuilder tokenCollectionAsString = new StringBuilder();
     SToken localCurrentToken = currentToken;
     while (localCurrentToken != null)
     {
         for (int i = 0; i < forms.Count; i++)
         {
             if (localCurrentToken.TokenType == SElementType.Undefined)
             {
                 if (localCurrentToken.TokenString.Contains(forms[i]))
                 {
                     List<SToken> explodedToken = ExplodeStoken(localCurrentToken, token, forms[i]);
                     tokenCollection.InsertRange(tokenCollection.IndexOf(localCurrentToken), explodedToken);
                     tokenCollection.Remove(localCurrentToken);
                     i = 0;
                     localCurrentToken = explodedToken[explodedToken.Count - 1];
                 }
             }
         }
         //*** localCurrentToken = localCurrentToken.TokenAfter;
     }
 }
Exemplo n.º 10
0
        void AppendToken(string c)
        {
            SElementType[] conditionType = new SElementType[] { SElementType.CollectionAnd, SElementType.CollectionOr };
            SToken newToken = SetNext(currentToken);
            newToken.Configure(0, "..", SElementType.Symbol, "");
            newToken.TokenNodeType = SElementType.Undefined;
            if (!sticked && nextIsAPropertyFoTheCurrent)
            {
                if (currentToken.TokenProperties == null)
                {
                    currentToken.TokenProperties = new List<SToken>();
                }
                newToken.Link(currentToken.TokenParent, null, null, null);
                currentToken.TokenProperties.Add(newToken);
                newToken.TokenDescribed = currentToken;
                currentToken = newToken;

                nextIsAPropertyFoTheCurrent = false;
                return;
            }

           
            if (!sticked && conditionType.Contains(currentToken.TokenParent.TokenType))
            {
                currentToken = currentToken.TokenParent.TokenAfter[currentToken.TokenParent.TokenAfter.Count-1];
            }
            newToken.Link(currentToken.TokenParent, currentToken, null, null);
            //currentToken = newToken;
            AdoptAsCurrent(newToken);

        }
Exemplo n.º 11
0
        private SToken AppendToken(string tokenText, string c)
        {
            //ParseDocument();
            if (!sticked)
            {
                if (currentToken.TokenParent.TokenNodeType == SElementType.FoldOpen || currentToken.TokenParent.TokenType == SElementType.CollectionAnd || currentToken.TokenParent.TokenType == SElementType.CollectionOr)
                {
                    //*** currentToken.TokenBefore.TokenAfter = currentToken.TokenAfter; // link
                    //*** currentToken.TokenBefore = currentToken.TokenParent.TokenAfter;
                    //*** currentToken.TokenParent.TokenAfter.TokenAfter = currentToken;
                    currentToken.TokenParent = currentToken.TokenParent.TokenParent;
                }
                else
                {
                    SToken newToken = new SToken();
                    newToken.Configure(0, "", SElementType.Undefined, "");
                    newToken.Link(currentToken.TokenParent, currentToken, null, null);
                    // if the multilplicity is not defined get the colection multiplicity
                    if (currentToken.TokenMultiplicity == SElementMultiplicity.Undefined)
                    {
                        newToken.TokenMultiplicity = currentToken.TokenParent.TokenMultiplicity;
                    }
                    //currentToken = newToken;
                    currentToken.TokenString = c;
                    //*** currentToken.TokenAfter = newToken;
                    //*** currentToken = currentToken.TokenAfter;
                }
            }
            else
            {
                currentToken.TokenString = c;

                //currentToken = newToken;
            }
            sticked = false;
            return currentToken;
        }
Exemplo n.º 12
0
 private void AddBlock(string tokenText, string c)
 {
     currentToken.TokenString = tokenText + c;
     SToken aToken = new SToken();
     aToken.Configure(0, string.Empty, SElementType.Undefined, "");
     aToken.Link(SToken.NULL, SToken.NULL, parentToken, new List<SToken>());
     parentToken.TokenChilds.Add(aToken);
     currentToken = aToken;
 }
    static void Main(string[] args)
    {
        string parsed_sentences = @"F:\Corpus\parsing\ru\SENT4.parsing.txt";
        string result_path      = @"f:\tmp\negative3.dat";
        string dict_path        = @"e:\MVoice\lem\bin-windows64\dictionary.xml";

        // Загрузим морфологический словарь, он нам понадобится для смены падежей слов.
        SolarixGrammarEngineNET.GrammarEngine2 gren = new SolarixGrammarEngineNET.GrammarEngine2();
        gren.Load(dict_path, true);


        string[] files = null;
        if (System.IO.Directory.Exists(parsed_sentences))
        {
            files = System.IO.Directory.GetFiles(parsed_sentences, "*.parsing.txt");
        }
        else
        {
            files = new string[1] {
                parsed_sentences
            }
        };

        List <TreeTemplate> templates = new List <TreeTemplate>();

        templates.Add(new TreeTemplate("[class:ГЛАГОЛ]( <PREPOS_ADJUNCT>[class:ПРЕДЛОГ].<OBJECT>[class:МЕСТОИМЕНИЕ,СУЩЕСТВИТЕЛЬНОЕ,ПРИЛАГАТЕЛЬНОЕ] )"));

        sample_count = 0;

        using (wrt = new System.IO.StreamWriter(result_path))
        {
            foreach (string file in files)
            {
                Console.WriteLine("Processing {0}...", file);

                using (Sentences src = new Sentences(file))
                {
                    while (src.Next())
                    {
                        Sentence sent = src.GetFetched();
                        if (sent.root != null)
                        {
                            #region AdjNounAdjNoun

                            for (int i1 = 0; i1 < sent.Tokens.Count; ++i1)
                            {
                                if (sent.Tokens[i1].part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                {
                                    SNode node1 = sent.Nodes.Where(z => z.index == i1).First();
                                    if (node1.edge_types.Count > 0)
                                    {
                                        int edge1 = -1;

                                        for (int j1 = 0; j1 < node1.edge_types.Count; ++j1)
                                        {
                                            if (node1.edge_types[j1] == "ATTRIBUTE")
                                            {
                                                if (sent.Tokens[node1.children[j1].index].part_of_speech == "ПРИЛАГАТЕЛЬНОЕ")
                                                {
                                                    edge1 = j1;
                                                    break;
                                                }
                                            }
                                        }

                                        if (edge1 != -1)
                                        {
                                            // Нашли первое существительное с атрибутирующим прилагательным.
                                            int noun_ie1 = gren.FindEntry(sent.Tokens[i1].word, gren.FindPartOfSpeech(sent.Tokens[i1].part_of_speech));

                                            SToken adj1 = sent.Tokens[node1.children[edge1].index];
                                            adj1.word = adj1.word.ToLower();


                                            for (int i2 = i1 + 2; i2 < sent.Tokens.Count; ++i2)
                                            {
                                                if (sent.Tokens[i2].part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                                {
                                                    int noun_ie2 = gren.FindEntry(sent.Tokens[i2].word, gren.FindPartOfSpeech(sent.Tokens[i2].part_of_speech));

                                                    if (noun_ie1 != noun_ie2)
                                                    {
                                                        int gender1 = gren.GetEntryAttrState(noun_ie1, SolarixGrammarEngineNET.GrammarEngineAPI.GENDER_ru);
                                                        int gender2 = gren.GetEntryAttrState(noun_ie2, SolarixGrammarEngineNET.GrammarEngineAPI.GENDER_ru);
                                                        if (gender1 == gender2)
                                                        {
                                                            string number1 = sent.Tokens[i1].tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];
                                                            string number2 = sent.Tokens[i2].tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];

                                                            if (number1 == number2)
                                                            {
                                                                SNode node2 = sent.Nodes.Where(z => z.index == i2).First();
                                                                if (node2.edge_types.Count > 0)
                                                                {
                                                                    int edge2 = -1;

                                                                    for (int j2 = 0; j2 < node2.edge_types.Count; ++j2)
                                                                    {
                                                                        if (node2.edge_types[j2] == "ATTRIBUTE")
                                                                        {
                                                                            if (sent.Tokens[node2.children[j2].index].part_of_speech == "ПРИЛАГАТЕЛЬНОЕ")
                                                                            {
                                                                                edge2 = j2;
                                                                                break;
                                                                            }
                                                                        }
                                                                    }

                                                                    if (edge2 != -1)
                                                                    {
                                                                        // Нашли второе существительное с атрибутирующим прилагательным.
                                                                        SToken adj2 = sent.Tokens[node2.children[edge2].index];
                                                                        adj2.word = adj2.word.ToLower();


                                                                        // Сгенерируем предложение, в котором эти прилагательные поменяны местами.
                                                                        List <SToken> tokens2 = new List <SToken>();
                                                                        foreach (SToken t in sent.Tokens)
                                                                        {
                                                                            if (t.index == adj1.index)
                                                                            {
                                                                                tokens2.Add(adj2);
                                                                            }
                                                                            else if (t.index == adj2.index)
                                                                            {
                                                                                tokens2.Add(adj1);
                                                                            }
                                                                            else
                                                                            {
                                                                                tokens2.Add(t);
                                                                            }
                                                                        }

                                                                        StoreSample(sent, tokens2);
                                                                    }
                                                                    else
                                                                    {
                                                                        // у второго существительного нет атрибутирующего прилагательного.
                                                                        // перенесем прилагательное от первого ко второму существительному.

                                                                        List <SToken> tokens2 = new List <SToken>();
                                                                        foreach (SToken t in sent.Tokens)
                                                                        {
                                                                            if (t.index == adj1.index)
                                                                            {
                                                                                continue;
                                                                            }
                                                                            else if (t.index == i2)
                                                                            {
                                                                                tokens2.Add(adj1);
                                                                                tokens2.Add(sent.Tokens[i2]);
                                                                            }
                                                                            else
                                                                            {
                                                                                tokens2.Add(t);
                                                                            }
                                                                        }

                                                                        StoreSample(sent, tokens2);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            #endregion AdjNounAdjNoun


                            #region NounNoun
                            // Ищем два существительных, связанных в конструкцию родительного дополнения:
                            // "восход солнца"
                            // Генерируем предложение с перестановкой слов и сменой падежей:
                            // "солнце восхода"
                            foreach (SNode n1 in sent.Nodes)
                            {
                                SToken t1 = sent.Tokens[n1.index]; // восход
                                if (t1.part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                {
                                    if (n1.children.Count > 0)
                                    {
                                        int gen_edge_index = n1.edge_types.IndexOf("RIGHT_GENITIVE_OBJECT");
                                        if (gen_edge_index != -1)
                                        {
                                            SToken t2 = sent.Tokens[n1.children[gen_edge_index].index]; // солнца

                                            if (gen_edge_index != -1 && t2.part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                            {
                                                List <SToken> tokens2 = new List <SToken>();
                                                bool          t12_ok  = true;
                                                foreach (SToken t in sent.Tokens)
                                                {
                                                    if (t.index == t1.index)
                                                    {
                                                        // сюда вставляем слово "солнца" и меняем его падеж на падеж слова t
                                                        string t_case   = t.tags.Where(z => z.StartsWith("ПАДЕЖ:")).First().Split(':')[1];
                                                        string t_number = t2.tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];
                                                        int    ie_t2    = gren.FindEntry(t2.lemma, gren.FindPartOfSpeech(t2.part_of_speech));
                                                        if (ie_t2 != -1)
                                                        {
                                                            List <int> coords = new List <int>();
                                                            List <int> states = new List <int>();
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.CASE_ru);
                                                            states.Add(gren.FindState(SolarixGrammarEngineNET.GrammarEngineAPI.CASE_ru, t_case));
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru);
                                                            states.Add(gren.FindState(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru, t_number));
                                                            List <string> forms = gren.GenerateWordforms(ie_t2, coords, states);
                                                            if (forms.Count > 0)
                                                            {
                                                                string new_word = gren.RestoreCasing(ie_t2, forms[0]);
                                                                SToken new_t    = new SToken();
                                                                new_t.index          = t.index;
                                                                new_t.word           = new_word;
                                                                new_t.lemma          = t.lemma;
                                                                new_t.part_of_speech = t1.part_of_speech;
                                                                tokens2.Add(new_t);
                                                            }
                                                            else
                                                            {
                                                                t12_ok = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            t12_ok = false;
                                                        }
                                                    }
                                                    else if (t.index == t2.index)
                                                    {
                                                        // сюда вставляем слово "восход" и меняем его падеж на родительный
                                                        string t_number = t1.tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];
                                                        int    ie_t1    = gren.FindEntry(t1.lemma, gren.FindPartOfSpeech(t1.part_of_speech));
                                                        if (ie_t1 != -1)
                                                        {
                                                            List <int> coords = new List <int>();
                                                            List <int> states = new List <int>();
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.CASE_ru);
                                                            states.Add(SolarixGrammarEngineNET.GrammarEngineAPI.GENITIVE_CASE_ru);
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru);
                                                            states.Add(gren.FindState(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru, t_number));
                                                            List <string> forms = gren.GenerateWordforms(ie_t1, coords, states);
                                                            if (forms.Count > 0)
                                                            {
                                                                string new_word = gren.RestoreCasing(ie_t1, forms[0]);
                                                                SToken new_t    = new SToken();
                                                                new_t.index          = t.index;
                                                                new_t.word           = new_word;
                                                                new_t.lemma          = t.lemma;
                                                                new_t.part_of_speech = t.part_of_speech;
                                                                tokens2.Add(new_t);
                                                            }
                                                            else
                                                            {
                                                                t12_ok = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            t12_ok = false;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        tokens2.Add(t);
                                                    }
                                                }

                                                if (t12_ok)
                                                {
                                                    StoreSample(sent, tokens2);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion NounNoun


                            #region PrepObject
                            // Ищем предлог.
                            foreach (SToken token_prepos in sent.Tokens)
                            {
                                if (token_prepos.part_of_speech == "ПРЕДЛОГ")
                                {
                                    // Нашли предлог. Теперь перенесем его в другую позицию.
                                    List <Tuple <SToken, float> > tokens2 = sent.Tokens.Where(z => z.index != token_prepos.index).Select(z => new Tuple <SToken, float>(z, z.index)).ToList();

                                    foreach (var t2 in tokens2.Where(z => z.Item1.index != token_prepos.index + 1))
                                    {
                                        // Ставим предлог в позицию ПЕРЕД токеном t2 и генерируем предложение.
                                        List <Tuple <SToken, float> > tokens3 = new List <Tuple <SToken, float> >();
                                        tokens3.AddRange(tokens2);
                                        tokens3.Add(new Tuple <SToken, float>(token_prepos, t2.Item2 - 0.5f));

                                        StoreSample(sent, tokens3.OrderBy(z => z.Item2).Select(z => z.Item1).ToList());
                                    }
                                }
                            }
                            #endregion PrepObject


                            /*
                             *                          foreach (TreeTemplate t in templates)
                             *                          {
                             *                              if (t.Match(sent))
                             *                              {
                             *                              }
                             *                          }
                             */
                        }
                    }
                }
            }
        }

        Console.WriteLine("\nsample_count={0}", sample_count);

        return;
    }
}
    public Sentence(XmlNode n_sent)
    {
        text = n_sent.SelectSingleNode("text").InnerText;

        // токены
        tokens = new List <SToken>();
        int token_index = 0;

        foreach (XmlNode n_token in n_sent.SelectNodes("tokens/token"))
        {
            SToken t = new SToken(token_index, n_token);
            tokens.Add(t);
            token_index++;
        }

        // дерево зависимостей
        List <int>            root_index   = new List <int>();
        Dictionary <int, int> child2parent = new Dictionary <int, int>();
        Dictionary <KeyValuePair <int, int>, string> edge_type = new Dictionary <KeyValuePair <int, int>, string>();
        Dictionary <int, List <int> > parent2child             = new Dictionary <int, List <int> >();

        foreach (XmlNode n_token in n_sent.SelectNodes("syntax_tree/node"))
        {
            int child_index = int.Parse(n_token["token"].InnerText);

            if (n_token.Attributes["is_root"] != null && n_token.Attributes["is_root"].Value == "true")
            {
                root_index.Add(child_index);
            }
            else
            {
                int parent_index = int.Parse(n_token["parent"].InnerText);
                child2parent.Add(child_index, parent_index);

                edge_type.Add(new KeyValuePair <int, int>(child_index, parent_index), n_token["link_type"].InnerText);

                List <int> child_idx;
                if (!parent2child.TryGetValue(parent_index, out child_idx))
                {
                    child_idx = new List <int>();
                    parent2child.Add(parent_index, child_idx);
                }

                child_idx.Add(child_index);
            }
        }

        nodes = new List <SNode>();
        for (int inode = 0; inode < tokens.Count; ++inode)
        {
            SNode n = new SNode();
            n.index = inode;
            n.word  = tokens[inode];
            nodes.Add(n);
        }

        // проставим родителей и детей в каждом узле
        for (int inode = 0; inode < nodes.Count; ++inode)
        {
            SNode node = nodes[inode];

            if (!root_index.Contains(node.index))
            {
                SNode parent_node = nodes[child2parent[node.index]];
                node.parent = parent_node;

                parent_node.children.Add(node);
                parent_node.edge_types.Add(edge_type[new KeyValuePair <int, int>(node.index, parent_node.index)]);
            }
            else
            {
                root = node;
            }
        }
    }
Exemplo n.º 15
0
 private void RegisterPerson()
 {
     switch (currentToken.TokenMultiplicity)
     {
         case SElementMultiplicity.One:
             tokenHe = currentToken;
             break;
         case SElementMultiplicity.Explicit:
             tokenHe = currentToken;
             break;
     }
 }
Exemplo n.º 16
0
    public Sentence(XmlNode n_sent)
    {
        text = n_sent.SelectSingleNode("text").InnerText;

        // токены
        tokens = new List<SToken>();
        int token_index = 0;
        foreach (XmlNode n_token in n_sent.SelectNodes("tokens/token"))
        {
            SToken t = new SToken(token_index, n_token);
            tokens.Add(t);
            token_index++;
        }

        // дерево зависимостей
        List<int> root_index = new List<int>();
        Dictionary<int, int> child2parent = new Dictionary<int, int>();
        Dictionary<KeyValuePair<int, int>, string> edge_type = new Dictionary<KeyValuePair<int, int>, string>();
        Dictionary<int, List<int>> parent2child = new Dictionary<int, List<int>>();

        foreach (XmlNode n_token in n_sent.SelectNodes("syntax_tree/node"))
        {
            int child_index = int.Parse(n_token["token"].InnerText);

            if (n_token.Attributes["is_root"] != null && n_token.Attributes["is_root"].Value == "true")
                root_index.Add(child_index);
            else
            {
                int parent_index = int.Parse(n_token["parent"].InnerText);
                child2parent.Add(child_index, parent_index);

                edge_type.Add(new KeyValuePair<int, int>(child_index, parent_index), n_token["link_type"].InnerText);

                List<int> child_idx;
                if (!parent2child.TryGetValue(parent_index, out child_idx))
                {
                    child_idx = new List<int>();
                    parent2child.Add(parent_index, child_idx);
                }

                child_idx.Add(child_index);
            }
        }

        nodes = new List<SNode>();
        for (int inode = 0; inode < tokens.Count; ++inode)
        {
            SNode n = new SNode();
            n.index = inode;
            n.word = tokens[inode];
            nodes.Add(n);
        }

        // проставим родителей и детей в каждом узле
        for (int inode = 0; inode < nodes.Count; ++inode)
        {
            SNode node = nodes[inode];

            if (!root_index.Contains(node.index))
            {
                SNode parent_node = nodes[child2parent[node.index]];
                node.parent = parent_node;

                parent_node.children.Add(node);
                parent_node.edge_types.Add(edge_type[new KeyValuePair<int, int>(node.index, parent_node.index)]);
            }
            else
            {
                root = node;
            }
        }
    }
Exemplo n.º 17
0
 void AppendTokenDown()
 {
     SToken newToken = new SToken();
     newToken.Configure(0, "append", SElementType.Undefined, "");
     newToken.Link(currentToken.TokenParent, currentToken, null, null);
     //*** currentToken.TokenAfter = newToken;
     //*** currentToken = currentToken.TokenAfter;
 }
Exemplo n.º 18
0
 public bool AdoptSymbol(int symbolId, string symbolText, List<string> forms, string code, SElementMultiplicity multiplicity)
 {
     bool simbolFound = false;
     SToken token = new SToken();
     if (currentToken.TokenBefore != null)
     {
         token = currentToken;
     }
     token.Configure(symbolId, symbolText, SElementType.Symbol, "[" + symbolId + ":0]");
     token.Link(currentToken.TokenParent, currentToken, null, null);
     if (currentToken.TokenBefore != null)
     {
         token.TokenBefore = currentToken.TokenBefore;
     }
     if (token.TokenMultiplicity == SElementMultiplicity.Undefined)
     token.TokenMultiplicity = multiplicity;
     StringBuilder tokenCollectionAsString = new StringBuilder();
     SToken localCurrentToken = currentToken;
     while (localCurrentToken != null)
     {
         for (int i = 0; i < forms.Count; i++)
         {
             if (localCurrentToken.TokenType == SElementType.Undefined)
             {
                 if (localCurrentToken.TokenString.Contains(forms[i]))
                 {
                     List<SToken> explodedToken = ExplodeStoken(localCurrentToken, token, forms[i]);
                     // Try child
                     int ii = localCurrentToken.TokenParent.TokenChilds.IndexOf(localCurrentToken);
                     if (ii > -1)
                     {
                         localCurrentToken.TokenParent.TokenChilds.Insert(localCurrentToken.TokenParent.TokenChilds.IndexOf(localCurrentToken), explodedToken[0]);
                         localCurrentToken.TokenParent.TokenChilds.Remove(localCurrentToken);
                         i = 0;
                         //if (explodedToken[explodedToken.Count - 1]!=null)
                         localCurrentToken = explodedToken[explodedToken.Count - 1];
                         simbolFound = true;
                     }
                     // Try properties
                     if (currentToken.TokenDescribed != null)
                     {
                         ii = localCurrentToken.TokenDescribed.TokenProperties.IndexOf(localCurrentToken);
                         if (ii > -1)
                         {
                             localCurrentToken.TokenDescribed.TokenProperties.Insert(localCurrentToken.TokenDescribed.TokenProperties.IndexOf(localCurrentToken), explodedToken[0]);
                             localCurrentToken.TokenDescribed.TokenProperties.Remove(localCurrentToken);
                             i = 0;
                             //if (explodedToken[explodedToken.Count - 1]!=null)
                             localCurrentToken = explodedToken[explodedToken.Count - 1];
                             simbolFound = true;
                         }
                     }
                 }
             }
         }
         localCurrentToken = GetNext(localCurrentToken);
     }
     return simbolFound;
 }
Exemplo n.º 19
0
        void FoldToken(string c)
        {
            bool foldNeeded = true;
            SElementType[] conditionType = new SElementType[] { SElementType.CollectionAnd, SElementType.CollectionOr };
            if (currentToken.TokenBefore != null) // if there is nothing before fold
            {
                if (conditionType.Contains(currentToken.TokenBefore.TokenType)) // if there is another terminated fold before then fold
                {
                    //foldNeeded = false;
                }
                if (currentToken.TokenBefore.TokenType == SElementType.Collection && currentToken.TokenString == "and") // Fold needed ?
                {
                    foldNeeded = false;
                }
                if (currentToken.TokenParent.TokenNodeType == SElementType.FoldOpen)
                {
                    foldNeeded = false;
                }
            }
            if (foldNeeded)
            {
                SToken firstToFold = currentToken;
                if (currentToken.TokenBefore != null)
                {
                    if (currentToken.TokenBefore.TokenBefore != null && currentToken.TokenBefore.TokenNodeType == SElementType.FoldClose && currentToken.TokenBefore.TokenBefore.TokenNodeType != SElementType.CollectionAnd)
                    {
                        while (firstToFold.TokenBefore != null && firstToFold.TokenNodeType != SElementType.FoldOpen)
                        {
                            firstToFold = firstToFold.TokenBefore;
                        }
                    }
                    else
                    {
                        firstToFold = currentToken.TokenBefore;
                    }
                }
                // Insert Folding Markers
                SToken bracketOpenToken = new SToken();
                SToken bracketCloseToken = new SToken();

                bracketOpenToken.Configure(0, "(", SElementType.Collection, "");
                bracketCloseToken.Configure(0, ")", SElementType.Collection, "");

                bracketOpenToken.TokenNodeType = SElementType.FoldOpen;
                bracketCloseToken.TokenNodeType = SElementType.FoldClose;

                if (currentToken.TokenBefore != null)
                {
                    bracketOpenToken.TokenMultiplicity = currentToken.TokenBefore.TokenMultiplicity;
                }


                bracketOpenToken.Link(firstToFold.TokenParent, firstToFold.TokenBefore, bracketCloseToken, new List<SToken>() { firstToFold });
                bracketCloseToken.Link(firstToFold.TokenParent, bracketOpenToken, null, null);
                firstToFold.Link(bracketOpenToken, null, null, firstToFold.TokenChilds);

                if (bracketOpenToken.TokenBefore != null)
                {
                   bracketOpenToken.TokenBefore.TokenAfter[0] = bracketOpenToken;
                }
                SToken localToken = firstToFold;
                while (localToken != null && localToken.TokenAfter != null)
                {
                    localToken.TokenParent = bracketOpenToken;
                    localToken = localToken.TokenAfter[0];
                }
                // If bracketOpenToken is the first in clilds replace the lider
                if (bracketOpenToken.TokenBefore == null)
                {
                    bracketOpenToken.TokenParent.TokenChilds.Remove(firstToFold);
                    bracketOpenToken.TokenParent.TokenChilds.Add(bracketOpenToken);
                }
            //currentToken = bracketCloseToken;
            }

            switch (c)
            {
                case "and":
                    currentToken.TokenParent.TokenType = SElementType.CollectionAnd;
                    currentToken.TokenParent.TokenString = "( && ";
                    break;
                case "or":
                    currentToken.TokenParent.TokenType = SElementType.CollectionOr;
                    currentToken.TokenParent.TokenString = "( || ";
                    break;
            }
            
            if (currentToken.TokenParent.TokenNodeType != SElementType.FoldOpen)
            {
                //currentToken = currentToken.TokenParent;
            }
            else
            {
                //AppendToken();
            }
            
            
        }
Exemplo n.º 20
0
 public void AdoptLogic(int logicId, string logicText, List<string> forms, string code)
 {
     SToken token = new SToken();
     token.Configure(logicId, code, SElementType.Logic, logicText);
     token.Link(SToken.NULL, SToken.NULL, null, null);
     StringBuilder tokenCollectionAsString = new StringBuilder();
     SToken localCurrentToken = currentToken;
     while (localCurrentToken != null)
     {
         for (int i = 0; i < forms.Count; i++)
         {
             if (localCurrentToken.TokenType == SElementType.Undefined)
             {
                 if (localCurrentToken.TokenString.Contains(forms[i]))
                 {
                     List<SToken> explodedToken = ExplodeStoken(localCurrentToken, token, forms[i]);
                     tokenCollection.AddRange(explodedToken);
                     tokenCollection.Remove(localCurrentToken);
                     i = 0;
                     localCurrentToken = explodedToken[explodedToken.Count - 1];
                 }
             }
         }
         //*** localCurrentToken = localCurrentToken.TokenAfter;
     }
 }
Exemplo n.º 21
0
 private SToken GetNext(SToken token)
 {
     if (token.TokenAfter == null)
     {
         return null;
     }
     else
     {
         return token.TokenAfter[0];
     }
 }
Exemplo n.º 22
0
 /// <summary>
 /// Create a collection of tokens from the input string
 /// </summary>
 /// <returns></returns>
 public List<SToken> TokenizeInput(string tokenizableString)
 {
     List<SToken> tokenizedInput = new List<SToken>();
     SToken newToken = new SToken();
     newToken.Configure(0, string.Empty, SElementType.Undefined, tokenizableString);
     newToken.Link(SToken.NULL, SToken.NULL, null, null);
     tokenizedInput.Add(newToken);
     return tokenizedInput;
 }
Exemplo n.º 23
0
 private SToken SetNext(SToken token)
 {
     SToken newToken = new SToken();
     if (token.TokenAfter == null)
     {
         token.TokenAfter = new List<SToken>();
         token.TokenAfter.Add(newToken);
     }
     else
     {
         if (newEvaluation)
         {
             token.TokenAfter.Add(newToken);
         }
     }
     newEvaluation = false;
     return newToken;
 }
Exemplo n.º 24
0
 string pluralize(SToken token)
 {
     switch (token.TokenMultiplicity)
     {
         case SElementMultiplicity.All:
             return "*" + pluralize(token.TokenString);
             break;
         case SElementMultiplicity.One:
             return "1" + token.TokenString;
             break;
         case SElementMultiplicity.Explicit:
             switch (token.TokenMultiplicityLevel)
             {
                 case 0:
                     return "No " + token.TokenString + "s";
                 case 1:
                     return "One " + token.TokenString + "s";
                 default:
                     return token.TokenMultiplicityLevel+"X " + token.TokenString + "s";
                     break;
             }
             break;
         case SElementMultiplicity.Some:
             return "" + token.TokenString + "s[]";
             break;
     }
     return token.TokenString;
 }
Exemplo n.º 25
0
        private bool SubstituteSymbols(SToken localCurrentToken)
        {
            try
            {
                bool simbolFound = false;
                //SToken localCurrentToken = currentToken;
                //while (localCurrentToken.TokenBefore != null && localCurrentToken.TokenBefore.TokenType == SElementType.Undefined)
                {
                }
                while (localCurrentToken != null)
                {
                    int offset = 0;
                    if (localCurrentToken.TokenType == SElementType.Undefined)
                        while (!NothingMoreToreplace(localCurrentToken, offset))
                        {
                            string candidate = GetFirstCandidateToReplace(localCurrentToken, offset);
                            candidate = candidate.Trim();
                            offset += candidate.Length;
                            while (!string.IsNullOrEmpty(candidate))
                            {
                                bool found = false;
                                string partialCandidate = candidate;
                                while (!found)
                                {
                                    SObjectCollection objectCollection = new SObjectCollection();
                                    objectCollection.AddByName(singularize(partialCandidate), 0); // logics.getSymbsBySubstring(partialCandidate, 0, 100);
                                    if (objectCollection.SObject.Count < 1)
                                    {
                                        if (partialCandidate.LastIndexOf(' ') > -1)
                                        {
                                            partialCandidate = partialCandidate.Substring(0, partialCandidate.LastIndexOf(' '));
                                        }
                                        else
                                        {
                                            candidate = candidate.Substring(partialCandidate.Length).Trim();//
                                            /*for (int i = 0; i < candidate.Length; i++)
                                            {
                                                if (char.IsWhiteSpace(candidate[i]))
                                                {
                                                    ReplaceAt++;
                                                }
                                                else
                                                {
                                                    candidate = candidate.Substring(i);
                                                    return;
                                                }
                                            }*/
                                            //string substitute = "[0:0]";
                                            /***>> if (Symbol.CreateUnknown && (char.IsWhiteSpace(SubsritutedQuery[SubsritutedQuery.Length - 1]) || (SubsritutedQuery[SubsritutedQuery.Length - 1] == '!')))
                                            {
                                                Symbol symbol = new Symbol(partialCandidate, true);
                                                substitute = "[" + symbol.SymbolID[0] + ":" + symbol.SymbolID[1] + "]";
                                            } <<***/

                                            //SubsritutedQuery = SubsritutedQuery.Substring(0, ReplaceAt) + substitute + SubsritutedQuery.Substring(ReplaceAt + partialCandidate.Length);
                                            //ReplaceAt = ReplaceAt + substitute.Length + 1; // because i have a spave after substitude
                                            found = true;
                                        }
                                    }
                                    else
                                    {
                                        bool needReplaceAtPluse = false;
                                        string substitute = "";
                                        foreach (SymbolID symbol in objectCollection.SObject)
                                        {
                                            substitute = symbol.ToString();
                                            simbolFound = AdoptSymbol(symbol.Location.A, symbol.Names[0], new List<string>() { partialCandidate }, "[" + symbol.Location.A + ":0]", pluralize(symbol.Names[0]).Equals(partialCandidate) ? SElementMultiplicity.All : SElementMultiplicity.One);

                                            currentToken = currentToken.TokenParent.TokenChilds[0];
                                            //*** while (currentToken.TokenAfter != null)
                                            {
                                                //*** currentToken = currentToken.TokenAfter;
                                            }
                                            //currentToken.TokenParent.TokenChilds
                                            //currentToken.TokenType = localCurrentToken.TokenType;
                                            //currentToken.TokenNodeType = localCurrentToken.TokenNodeType;
                                            //currentToken.TokenMultiplicity = localCurrentToken.TokenMultiplicity;
                                            //currentToken = currentToken.TokenParent.TokenChilds[0];
                                            //*** if (currentToken.TokenAfter != null)
                                            {
                                                //*** currentToken.TokenAfter.TokenBefore = currentToken;
                                            }
                                            //offset = 0;
                                        }
                                        if (partialCandidate.Length < candidate.Length)
                                        {
                                            candidate = candidate.Substring(partialCandidate.Length + 1); // this is not possible if we are at the end of candidate
                                            needReplaceAtPluse = true;
                                        }
                                        else
                                        {
                                            candidate = string.Empty;
                                        }
                                        if (needReplaceAtPluse)
                                        {
                                            //ReplaceAt++;
                                        }
                                        found = true;
                                    }
                                }
                            }
                        }
                    localCurrentToken = GetNext(localCurrentToken);
                }
                if (simbolFound && false)
                {
                    SToken newToken = new SToken();
                    newToken.Configure(0, "", SElementType.Undefined, "");
                    newToken.Link(currentToken.TokenParent, currentToken, null, null);
                    //*** currentToken.TokenAfter = newToken;
                    currentToken = newToken;
                }
                return simbolFound;
            }
            catch (Exception ss)
            {
            }
            return false;
        }
Exemplo n.º 26
0
        private List<SToken> ExplodeStoken(SToken sToken, SToken token, string form)
        {
            List<SToken> tokens = new List<SToken>();
            //foreach (string form in forms)
            {
                string text = sToken.TokenString;
                string textBefore = string.Empty;
                string textAfter = string.Empty;

                if (text.Contains(form))
                {
                    textBefore = " "+text.Substring(0, text.IndexOf(form)).Trim()+" ";
                    textAfter = " "+text.Substring(text.IndexOf(form) + form.Length).Trim()+" ";
                }

               // while (text.Contains(form))
                {
                    SToken tokenInside = new SToken();
                    tokenInside.Configure(token.TokenId, token.TokenString, token.TokenType, token.TokenCode);
                    tokenInside.Link(SToken.NULL, SToken.NULL, null, null);
                    SToken tokenBefore = new SToken();
                    tokenBefore.Configure(sToken.TokenId, textBefore, sToken.TokenType,  sToken.TokenCode);
                    tokenBefore.Link(sToken.TokenBefore, tokenInside, null, null);
                    SToken tokenAfter = new SToken();
                    tokenAfter.Configure(sToken.TokenId, textAfter, sToken.TokenType, sToken.TokenCode);
                    //*** tokenAfter.Link(tokenInside, sToken.TokenAfter, null, null);
                    tokenInside.TokenBefore = tokenBefore;
                    if (sToken.TokenBefore != null)
                    {
                       //***  sToken.TokenBefore.TokenAfter = tokenBefore;
                    }
                    //*** if (sToken.TokenAfter != null)
                    {
                        //*** sToken.TokenAfter.TokenBefore = tokenAfter;
                    }
                    //*** tokenInside.TokenAfter = tokenAfter;
                    
                    tokens.Add(tokenBefore);
                    tokens.Add(tokenInside);
                    tokens.Add(tokenAfter);
                }
            }
            return tokens;
        }
Exemplo n.º 27
0
 private SToken AdoptAsCurrent(SToken token)
 {
     if (lastToken != currentToken && lastTokenCollection != currentToken && isPreoperand)
     {
         if (currentToken.TokenMultiplicity == SElementMultiplicity.One)
         {
             lastToken = currentToken;
         }
         else
         {
             lastTokenCollection = currentToken;
         }
         currentToken = token;
     }
     else
     {
         currentToken = token;
     }
     return currentToken;
 }
Exemplo n.º 28
0
 private void AddPropertyToClass(SToken token, SToken localToken, SElementMultiplicity sElementMultiplicity)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 29
0
        private string GetFirstCandidateToReplace(SToken currentToken, int offset)
        {
            bool found = false;
            int start = -1, end = -1;
            for (int i = offset; i < currentToken.TokenString.Length; i++)
            {
                if (char.IsLetter(currentToken.TokenString[i]))
                {
                    if (start < 0)
                   {
                        start = i;
                        end = i;
                        found = true;
                    }
                    else
                    {
                        end = i;
                    }
                }
                else
                {
                    if (found)
                    {
                        //                        if (!char.IsWhiteSpace(SubsritutedQuery[i]))
                        {
                            ReplaceAt = start;
                            return currentToken.TokenString.Substring(start, end + 1 - start);
                        }
                    }
                }

            }
            if (found)
            {
                ReplaceAt = start;
                return currentToken.TokenString.Substring(start, end + 1 - start);
            }
            else
                return string.Empty;
        }
Exemplo n.º 30
0
        private void InitializePersons()
        {
            tokenI = new SToken();
            tokenYou = new SToken();
            tokenShe = new SToken();
            tokenHe = new SToken();
            tokenWe = new List<SToken>();
            tokenYous = new List<SToken>();
            tokenThey = new List<SToken>();

            tokenI.TokenString = "Florin";
            tokenYou.TokenString = "Irina";
            tokenShe.TokenString = "Irina";


            tokenI.TokenMultiplicity = SElementMultiplicity.One;
            tokenYou.TokenMultiplicity = SElementMultiplicity.One;
            tokenShe.TokenMultiplicity = SElementMultiplicity.One;
            tokenHe.TokenMultiplicity = SElementMultiplicity.One;


            tokenI.TokenType = SElementType.Symbol;
            tokenYou.TokenType = SElementType.Symbol;
            tokenShe.TokenType = SElementType.Symbol;
            tokenHe.TokenType = SElementType.Symbol;

            tokenWe.Add(tokenI);
            tokenWe.Add(tokenYou);
        }
Exemplo n.º 31
0
 private bool NothingMoreToreplace(SToken currentToken, int offset)
 {
     for (int i = offset; i < currentToken.TokenString.Length; i++)
     {
         if (char.IsLetter(currentToken.TokenString[i]))
         { return false; }
     }
     return true;
 }
Exemplo n.º 32
0
        public void ParseAsNatural()
        {
            StringBuilder currentPhrase = new StringBuilder();
            bool handled = false;
            foreach (string c in documentText.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
            {
                switch (c)
                {
                    case ".":
                        //currentToken.TokenString = currentPhrase.ToString();
                        handled = true;
                        AddBlock(currentPhrase.ToString(), c);
                        currentPhrase = new StringBuilder();
                        break;
                    case "he":
                    case "she":
                    case "it":
                        handled = true;
                        currentToken = LastToken;
                        break;
                    case "increase": // Set influenc
                    case "decrease":
                    case "increasedBy": // Set influence
                    case "decreasedBy":
                        handled = true;
                        SetInfluence(currentToken, c);
                        currentPhrase = new StringBuilder();
                        break;
                    case "they":
                        handled = true;
                        newEvaluation = true;
                        CurrentToken = lastTokenCollection;
                        AppendToken(c);
                        //SetNext();
                        break;
                    case ",":
                        if (FoldNeeded)
                        {
                            CreateNewCollection();
                        }
                        else 
                        {
                            AppendToCollection();
                        }
                        break;
                    case "and":
                    case "or":
                         EscapeFromCollection(c);
                        //currentToken.TokenString = currentPhrase.ToString();
                        handled = true;
                        //Append();
                        //AddToCollection(currentPhrase.ToString(), c);
                        currentPhrase = new StringBuilder();
                        //AppendT(c);
                        break;
                    case "can": // Set a function
                         handled = true;
                         if (currentToken.TokenDescribed == null)
                        currentToken.TokenDescribed = currentToken.TokenBefore;
                        if (currentToken.TokenDescribed.TokenActivities == null)
                        {
                            currentToken.TokenDescribed.TokenActivities = new List<SToken>();
                        }
                        currentToken.TokenDescribed.TokenActivities.Add(currentToken);
                        currentToken.Link(currentToken.TokenParent, null, null, null);
                        currentPhrase = new StringBuilder();
                        isPreoperand = false;
                        break;
                    case "have": // Set a property
                        handled = true;
                        currentToken.TokenDescribed = currentToken.TokenBefore;
                        if (currentToken.TokenDescribed.TokenProperties == null)
                        {
                            currentToken.TokenDescribed.TokenProperties = new List<SToken>();
                        }
                        currentToken.TokenDescribed.TokenProperties.Add(currentToken);
                        currentToken.TokenDescribed.TokenAfter = null;
                        currentToken.Link(currentToken.TokenParent, null, null, null);
                        currentPhrase = new StringBuilder();
                        nextIsAPropertyFoTheCurrent = true;
                        isPreoperand = false;
                        break;
                    case "a":
                    case "an":
                    case "some":
                    case "many":
                    case "all":
                    case "one":
                    case "no":
                    case "1":
                    case "0":
                        handled = true;
                        SetMultiplicityFlag(c);
                        currentPhrase = new StringBuilder();
                        break;
                    default:
                        // is it a multiplication factor?
                        int multiplicityLevel = 0;
                        int.TryParse(c, out multiplicityLevel);
                        if (multiplicityLevel != 0)
                        {
                            handled = true;
                            SetMultiplicityLevel(multiplicityLevel);
                            currentPhrase = new StringBuilder();
                            break;
                        }
                        handled = true;
                        string actualWord = c;
                        if (c.EndsWith("`s"))
                        {
                            nextIsAPropertyFoTheCurrent = true;
                            actualWord = c.Substring(0, c.Length - 2);
                        }
                        CurrentToken.TokenString = actualWord;

                        SToken existentToken = KnownTockens.FirstOrDefault(p => p.TokenString == actualWord);
                        if (existentToken != null)
                        {
                            CurrentToken = existentToken;
                        }
                        else
                        {
                            KnownTockens.Add(CurrentToken);
                        }
                        
                        if (!SubstituteSymbols(currentToken))
                        {
                            if (CurrentToken.TokenMultiplicity == SElementMultiplicity.Undefined)
                                CurrentToken.TokenMultiplicity = currentToken.TokenParent.TokenMultiplicity;
                        }
                        //AppendToken(c);
                        if (CurrentToken.TokenDescribed != null)
                        {
                            if (CurrentToken.TokenDescribed.TokenInfluenceC != null)
                            {
                                foreach (SToken token in CurrentToken.TokenDescribed.TokenInfluenceC)
                                {
                                    if (CurrentToken.TokenDescribed.TokenInfluenceC.Count > 0)
                                    {
                                        if (token.TokenInfluenceA == null)
                                        {
                                            token.TokenInfluenceA = new List<SToken>();
                                        }
                                    }
                                    token.TokenInfluenceA.Add(CurrentToken.TokenDescribed);
                                }
                                CurrentToken.TokenDescribed.TokenInfluenceC = null;
                            }
                            if (CurrentToken.TokenDescribed.TokenInfluenceD != null)
                            {
                            }
                        }
                        AppendT(c);
                        
                        break;
                }
                if (!handled)
                {
                    currentPhrase.Append(" " + c);
                }
                else
                {
                    handled = false;
                }
                //RefreshPersons();
            }
        }
Exemplo n.º 33
0
        private SToken FoldBelowWithPrewious(string c, SElementType[] conditionType)
        {
            // Fold Needed ?
            bool foldNeeded = true;
            if (currentToken.TokenBefore != null) // if there is nothing before fold
            {
                if (conditionType.Contains(currentToken.TokenBefore.TokenType)) // if there is another terminated fold before then fold
                {
                    //foldNeeded = false;
                }
                if (currentToken.TokenBefore.TokenType == SElementType.Collection && c == "and") // Fold needed ?
                {
                    foldNeeded = false;
                }
                if (true)
                {

                }
            }

            // Fold Location ?
            SToken firstToFold = currentToken;
            if (foldNeeded)
            {
                if (currentToken.TokenNodeType == SElementType.FoldClose && currentToken.TokenBefore.TokenNodeType != SElementType.CollectionAnd)
                {
                    while (firstToFold.TokenBefore != null && firstToFold.TokenNodeType != SElementType.FoldOpen)
                    {
                        firstToFold = firstToFold.TokenBefore;
                    }
                }


                // Insert Folding Markers
                SToken bracketOpenToken = new SToken();
                bracketOpenToken.Configure(0, "(", SElementType.Collection, "");
                bracketOpenToken.TokenNodeType = SElementType.FoldOpen; 
                bracketOpenToken.TokenMultiplicity = currentToken.TokenMultiplicity;
                SToken bracketCloseToken = new SToken();
                bracketCloseToken.Configure(0, ")", SElementType.Collection, "");
                bracketCloseToken.TokenNodeType = SElementType.FoldClose;
                bracketOpenToken.Link(firstToFold.TokenParent, firstToFold.TokenBefore, bracketCloseToken, new List<SToken>() { firstToFold });
                if (bracketOpenToken.TokenBefore != null)
                {
                    //*** bracketOpenToken.TokenBefore.TokenAfter = bracketOpenToken;
                }
                SToken localToken = firstToFold;
                while (localToken != null)
                {
                    localToken.TokenParent = bracketOpenToken;
                    //*** localToken = localToken.TokenAfter;
                }
                bracketCloseToken.Link(firstToFold.TokenParent, bracketOpenToken, null, null);
                // If bracketOpenToken is the first in clilds replace the lider
                if (bracketOpenToken.TokenBefore == null)
                {
                    bracketOpenToken.TokenParent.TokenChilds.Remove(firstToFold);
                    bracketOpenToken.TokenParent.TokenChilds.Add(bracketOpenToken);
                }
                // Fold element
                //*** firstToFold.Link(bracketOpenToken, null, firstToFold.TokenAfter, firstToFold.TokenChilds);

            }
            else
            {
                SToken localToken = null;
                if (currentToken.TokenParent.TokenChilds.Count > 0)
                {
                    localToken = currentToken.TokenParent.TokenChilds[0];
                    //*** while (localToken.TokenAfter != null)
                    {
                        //*** localToken = localToken.TokenAfter;
                    }
                }
                currentToken = localToken;
               // localToken.TokenAfter = new SToken(0, "", SElementType.Undefined, "");
               // currentToken.TokenBefore.TokenAfter = currentToken.TokenAfter;
               // currentToken.TokenBefore = localToken;
               // currentToken.TokenParent = localToken.TokenParent;
               // currentToken.TokenBefore.TokenAfter = currentToken;
            }
            // Right
            //sticked = true; // keep next token into array 
            return currentToken;
        }
Exemplo n.º 34
0
    static void Main(string[] args)
    {
        string parsed_sentences = @"F:\Corpus\parsing\ru\SENT4.parsing.txt";
        string result_path = @"f:\tmp\negative3.dat";
        string dict_path = @"e:\MVoice\lem\bin-windows64\dictionary.xml";

        // Загрузим морфологический словарь, он нам понадобится для смены падежей слов.
        SolarixGrammarEngineNET.GrammarEngine2 gren = new SolarixGrammarEngineNET.GrammarEngine2();
        gren.Load(dict_path, true);


        string[] files = null;
        if (System.IO.Directory.Exists(parsed_sentences))
            files = System.IO.Directory.GetFiles(parsed_sentences, "*.parsing.txt");
        else
            files = new string[1] { parsed_sentences };

        List<TreeTemplate> templates = new List<TreeTemplate>();
        templates.Add(new TreeTemplate("[class:ГЛАГОЛ]( <PREPOS_ADJUNCT>[class:ПРЕДЛОГ].<OBJECT>[class:МЕСТОИМЕНИЕ,СУЩЕСТВИТЕЛЬНОЕ,ПРИЛАГАТЕЛЬНОЕ] )"));

        sample_count = 0;

        using (wrt = new System.IO.StreamWriter(result_path))
        {
            foreach (string file in files)
            {
                Console.WriteLine("Processing {0}...", file);

                using (Sentences src = new Sentences(file))
                {
                    while (src.Next())
                    {
                        Sentence sent = src.GetFetched();
                        if (sent.root != null)
                        {
                            #region AdjNounAdjNoun

                            for (int i1 = 0; i1 < sent.Tokens.Count; ++i1)
                            {
                                if (sent.Tokens[i1].part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                {
                                    SNode node1 = sent.Nodes.Where(z => z.index == i1).First();
                                    if (node1.edge_types.Count > 0)
                                    {
                                        int edge1 = -1;

                                        for (int j1 = 0; j1 < node1.edge_types.Count; ++j1)
                                        {
                                            if (node1.edge_types[j1] == "ATTRIBUTE")
                                            {
                                                if (sent.Tokens[node1.children[j1].index].part_of_speech == "ПРИЛАГАТЕЛЬНОЕ")
                                                {
                                                    edge1 = j1;
                                                    break;
                                                }
                                            }
                                        }

                                        if (edge1 != -1)
                                        {
                                            // Нашли первое существительное с атрибутирующим прилагательным.
                                            int noun_ie1 = gren.FindEntry(sent.Tokens[i1].word, gren.FindPartOfSpeech(sent.Tokens[i1].part_of_speech));

                                            SToken adj1 = sent.Tokens[node1.children[edge1].index];
                                            adj1.word = adj1.word.ToLower();


                                            for (int i2 = i1 + 2; i2 < sent.Tokens.Count; ++i2)
                                            {
                                                if (sent.Tokens[i2].part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                                {
                                                    int noun_ie2 = gren.FindEntry(sent.Tokens[i2].word, gren.FindPartOfSpeech(sent.Tokens[i2].part_of_speech));

                                                    if (noun_ie1 != noun_ie2)
                                                    {
                                                        int gender1 = gren.GetEntryAttrState(noun_ie1, SolarixGrammarEngineNET.GrammarEngineAPI.GENDER_ru);
                                                        int gender2 = gren.GetEntryAttrState(noun_ie2, SolarixGrammarEngineNET.GrammarEngineAPI.GENDER_ru);
                                                        if (gender1 == gender2)
                                                        {
                                                            string number1 = sent.Tokens[i1].tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];
                                                            string number2 = sent.Tokens[i2].tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];

                                                            if (number1 == number2)
                                                            {
                                                                SNode node2 = sent.Nodes.Where(z => z.index == i2).First();
                                                                if (node2.edge_types.Count > 0)
                                                                {
                                                                    int edge2 = -1;

                                                                    for (int j2 = 0; j2 < node2.edge_types.Count; ++j2)
                                                                    {
                                                                        if (node2.edge_types[j2] == "ATTRIBUTE")
                                                                        {
                                                                            if (sent.Tokens[node2.children[j2].index].part_of_speech == "ПРИЛАГАТЕЛЬНОЕ")
                                                                            {
                                                                                edge2 = j2;
                                                                                break;
                                                                            }
                                                                        }
                                                                    }

                                                                    if (edge2 != -1)
                                                                    {
                                                                        // Нашли второе существительное с атрибутирующим прилагательным.
                                                                        SToken adj2 = sent.Tokens[node2.children[edge2].index];
                                                                        adj2.word = adj2.word.ToLower();


                                                                        // Сгенерируем предложение, в котором эти прилагательные поменяны местами.
                                                                        List<SToken> tokens2 = new List<SToken>();
                                                                        foreach (SToken t in sent.Tokens)
                                                                        {
                                                                            if (t.index == adj1.index)
                                                                            {
                                                                                tokens2.Add(adj2);
                                                                            }
                                                                            else if (t.index == adj2.index)
                                                                            {
                                                                                tokens2.Add(adj1);
                                                                            }
                                                                            else
                                                                            {
                                                                                tokens2.Add(t);
                                                                            }
                                                                        }

                                                                        StoreSample(sent, tokens2);
                                                                    }
                                                                    else
                                                                    {
                                                                        // у второго существительного нет атрибутирующего прилагательного.
                                                                        // перенесем прилагательное от первого ко второму существительному.

                                                                        List<SToken> tokens2 = new List<SToken>();
                                                                        foreach (SToken t in sent.Tokens)
                                                                        {
                                                                            if (t.index == adj1.index)
                                                                            {
                                                                                continue;
                                                                            }
                                                                            else if (t.index == i2)
                                                                            {
                                                                                tokens2.Add(adj1);
                                                                                tokens2.Add(sent.Tokens[i2]);
                                                                            }
                                                                            else
                                                                            {
                                                                                tokens2.Add(t);
                                                                            }
                                                                        }

                                                                        StoreSample(sent, tokens2);

                                                                    }

                                                                }

                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            #endregion AdjNounAdjNoun


                            #region NounNoun
                            // Ищем два существительных, связанных в конструкцию родительного дополнения:
                            // "восход солнца"
                            // Генерируем предложение с перестановкой слов и сменой падежей:
                            // "солнце восхода"
                            foreach (SNode n1 in sent.Nodes)
                            {
                                SToken t1 = sent.Tokens[n1.index]; // восход
                                if (t1.part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                {
                                    if (n1.children.Count > 0)
                                    {
                                        int gen_edge_index = n1.edge_types.IndexOf("RIGHT_GENITIVE_OBJECT");
                                        if (gen_edge_index != -1)
                                        {
                                            SToken t2 = sent.Tokens[n1.children[gen_edge_index].index]; // солнца

                                            if (gen_edge_index != -1 && t2.part_of_speech == "СУЩЕСТВИТЕЛЬНОЕ")
                                            {
                                                List<SToken> tokens2 = new List<SToken>();
                                                bool t12_ok = true;
                                                foreach (SToken t in sent.Tokens)
                                                {
                                                    if (t.index == t1.index)
                                                    {
                                                        // сюда вставляем слово "солнца" и меняем его падеж на падеж слова t
                                                        string t_case = t.tags.Where(z => z.StartsWith("ПАДЕЖ:")).First().Split(':')[1];
                                                        string t_number = t2.tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];
                                                        int ie_t2 = gren.FindEntry(t2.lemma, gren.FindPartOfSpeech(t2.part_of_speech));
                                                        if (ie_t2 != -1)
                                                        {
                                                            List<int> coords = new List<int>();
                                                            List<int> states = new List<int>();
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.CASE_ru);
                                                            states.Add(gren.FindState(SolarixGrammarEngineNET.GrammarEngineAPI.CASE_ru, t_case));
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru);
                                                            states.Add(gren.FindState(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru, t_number));
                                                            List<string> forms = gren.GenerateWordforms(ie_t2, coords, states);
                                                            if (forms.Count > 0)
                                                            {
                                                                string new_word = gren.RestoreCasing(ie_t2, forms[0]);
                                                                SToken new_t = new SToken();
                                                                new_t.index = t.index;
                                                                new_t.word = new_word;
                                                                new_t.lemma = t.lemma;
                                                                new_t.part_of_speech = t1.part_of_speech;
                                                                tokens2.Add(new_t);
                                                            }
                                                            else
                                                            {
                                                                t12_ok = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            t12_ok = false;
                                                        }
                                                    }
                                                    else if (t.index == t2.index)
                                                    {
                                                        // сюда вставляем слово "восход" и меняем его падеж на родительный
                                                        string t_number = t1.tags.Where(z => z.StartsWith("ЧИСЛО:")).First().Split(':')[1];
                                                        int ie_t1 = gren.FindEntry(t1.lemma, gren.FindPartOfSpeech(t1.part_of_speech));
                                                        if (ie_t1 != -1)
                                                        {
                                                            List<int> coords = new List<int>();
                                                            List<int> states = new List<int>();
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.CASE_ru);
                                                            states.Add(SolarixGrammarEngineNET.GrammarEngineAPI.GENITIVE_CASE_ru);
                                                            coords.Add(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru);
                                                            states.Add(gren.FindState(SolarixGrammarEngineNET.GrammarEngineAPI.NUMBER_ru, t_number));
                                                            List<string> forms = gren.GenerateWordforms(ie_t1, coords, states);
                                                            if (forms.Count > 0)
                                                            {
                                                                string new_word = gren.RestoreCasing(ie_t1, forms[0]);
                                                                SToken new_t = new SToken();
                                                                new_t.index = t.index;
                                                                new_t.word = new_word;
                                                                new_t.lemma = t.lemma;
                                                                new_t.part_of_speech = t.part_of_speech;
                                                                tokens2.Add(new_t);
                                                            }
                                                            else
                                                            {
                                                                t12_ok = false;
                                                            }
                                                        }
                                                        else
                                                        {
                                                            t12_ok = false;
                                                        }

                                                    }
                                                    else
                                                    {
                                                        tokens2.Add(t);
                                                    }
                                                }

                                                if (t12_ok)
                                                {
                                                    StoreSample(sent, tokens2);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion NounNoun


                            #region PrepObject
                            // Ищем предлог.
                            foreach (SToken token_prepos in sent.Tokens)
                            {
                                if (token_prepos.part_of_speech == "ПРЕДЛОГ")
                                {
                                    // Нашли предлог. Теперь перенесем его в другую позицию.
                                    List<Tuple<SToken, float>> tokens2 = sent.Tokens.Where(z => z.index != token_prepos.index).Select(z => new Tuple<SToken, float>(z, z.index)).ToList();

                                    foreach (var t2 in tokens2.Where(z => z.Item1.index != token_prepos.index + 1))
                                    {
                                        // Ставим предлог в позицию ПЕРЕД токеном t2 и генерируем предложение.
                                        List<Tuple<SToken, float>> tokens3 = new List<Tuple<SToken, float>>();
                                        tokens3.AddRange(tokens2);
                                        tokens3.Add(new Tuple<SToken, float>(token_prepos, t2.Item2 - 0.5f));

                                        StoreSample(sent, tokens3.OrderBy(z => z.Item2).Select(z => z.Item1).ToList());
                                    }
                                }
                            }
                            #endregion PrepObject


                            /*
                                                        foreach (TreeTemplate t in templates)
                                                        {
                                                            if (t.Match(sent))
                                                            {
                                                            }
                                                        }
                            */
                        }
                    }
                }
            }
        }

        Console.WriteLine("\nsample_count={0}", sample_count);

        return;
    }
Exemplo n.º 35
0
 public void AdoptOperator(int operatorId, string operatorText, List<string> forms, string code)
 {
     SToken token = new SToken();
     token.Configure(operatorId, string.Empty, SElementType.Operator, operatorText);
     token.Link(SToken.NULL, SToken.NULL, null, null);
     StringBuilder tokenCollectionAsString = new StringBuilder();
     SToken localCurrentToken = currentToken;
     while (localCurrentToken != null)
     {
         for (int i = 0; i < forms.Count; i++)
         {
             if (localCurrentToken.TokenType == SElementType.Undefined)
             {
                 if (localCurrentToken.TokenString.Contains(forms[i]))
                 {
                     List<SToken> explodedToken = ExplodeStoken(localCurrentToken, token, forms[i]);
                     currentToken.TokenParent.TokenChilds.InsertRange(currentToken.TokenParent.TokenChilds.IndexOf(localCurrentToken), explodedToken);
                     currentToken.TokenParent.TokenChilds.Remove(localCurrentToken);
                     i = 0;
                     localCurrentToken = explodedToken[explodedToken.Count - 1];
                 }
             }
         }
         //*** localCurrentToken = localCurrentToken.TokenAfter;
     }
 }