protected SentenceNode addNewWord(string newWord, int counterAdd) { // Dodawanie nowego słowa do listy, // i aktualizacja prawdopodobieństw. // Jan Grzywacz. total += counterAdd; SentenceNode child = null; // Sprawdzamy, czy mamy już takie słowo. int index = findChildIndex(newWord); if (index != -1) { // Jeżeli tak, aktualizujemy jego licznik. child = children[index]; counters[index] += counterAdd; } else { // Jeśli nie, dodajemy nowe. child = new SentenceNode(newWord, sylabizator); children.Add(child); counters.Add(counterAdd); marked.Add(false); } return(child); }
static void Main(string[] args) { Sylabizator.Sylabizator sylabizator = new Sylabizator.Sylabizator(); //findAllEnglishSentences(100); //findAllPolishSentences(12); //cutStrangeWords(); Nodev2 root = new Nodev2(' '); root.createGraph(); root.teach(); string[] results = new string[100]; for (int i = 0; i < 100; i++) { results[i] = root.generateNewWord(); Console.WriteLine("{0}", results[i]); } Console.Write("\n"); SentenceNode sentenceRoot = new SentenceNode("NULL", sylabizator); //sylabizator.Model.updateLanguage(Sylabizator.SylabizatorLanguage.Polish); sentenceRoot.teach("english_sentences.txt"); for (int i = 0; i < 100; i++) { Console.WriteLine("{0}", sentenceRoot.generateNewSentence()); } Console.Write("\n"); //sentenceRoot.teachRandomWords("strange_words.txt", 50); //List<string> poem = sentenceRoot.generatePoem(4, 15, 4); List <string> poem = sentenceRoot.generatePoem(8, 7, 2); //List<string> poem = sentenceRoot.generatePoem(9, 4, 3); //List<string> poem = sentenceRoot.generatePoem(24, 3, 2); Console.Write("\n\n"); for (int i = 0; i < poem.Count; i++) { Console.WriteLine(poem[i]); } Console.WriteLine("\nFinished."); Console.ReadKey(); return; }
protected SentenceNode randomChildWithSyllables(int syllablesLeft, int lTotal, int maxL, Stack <string> rhymes) { // Znajdywanie losowego dziecka, z dwoma ograniczeniami. // Nie może być w nim więcej sylab niż podana liczba, // i końcową sylabą musi być podana (chyba, że damy null). // Jan Grzywacz. List <SentenceNode> tmpChildren = children; List <int> tmpCounters = counters; int tmpTotal = total; List <SentenceNode> newChildren = new List <SentenceNode>(); List <int> newCounters = new List <int>(); int newTotal = 0; // Wybieramy możliwych kandydatów do nowej listy. for (int i = 1; i < children.Count; i++) { if ((validateWord(getWord(i), syllablesLeft, lTotal, maxL, rhymes)) && (!marked[i])) { newChildren.Add(children[i]); newCounters.Add(counters[i]); newTotal += counters[i]; //Console.WriteLine(children[i].word); } } // Jeżeli nie ma już nic, zwracamy null. if (newChildren.Count == 0) { mini_repeat_counter = 0; return(null); } // Zamiana naszych list... children = newChildren; counters = newCounters; total = newTotal; // Aby można było skorzystać ze starej funkcji. SentenceNode newChild = randomChild(); // Przywracanie normalnych list. children = tmpChildren; counters = tmpCounters; total = tmpTotal; return(newChild); }
protected void teachNewSingleWord(string word) { // Wrzucamy do gotowej sieci słowo w losowe miejsce. // Jan Grzywacz. SentenceNode victim = totallyRandomChild(); if (victim.word == "NULL") { victim = this; } SentenceNode newChild = addNewWord(word, 0); victim.appendChild(newChild); SentenceNode anotherVictim = totallyRandomChild(); newChild.appendChild(anotherVictim); }
protected void appendChild(SentenceNode child) { // Dodawanie istniejącego już słowa do listy dzieci. // Jan Grzywacz. total++; // Sprawdzamy, czy mamy już takie słowo. int index = findChildIndex(child.word); if (index != -1) { // Jeżeli tak, aktualizujemy jego licznik. counters[index]++; } else { // Jeśli nie, dodajemy je. children.Add(child); counters.Add(1); marked.Add(false); } }
protected void teachNewSentence(string sentence) { // Analogiczna metoda do tej z NodeV2, // różni się wstępną obróbką zdań. // Jan Grzywacz. // Usuwanie znaków interpunkcyjnych i dużych liter. sentence = sentence.ToLower(); sentence = sentence.RemoveAll( new string[] { "\"", ".", ",", "!", "?", "\\", ":", "-", "_" }); // Dzielenie zdania na słowa. string[] words = sentence.Split(new char[] { ' ' }); SentenceNode lastChild = null; SentenceNode newChild = null; for (int i = 0; i < words.Length; i++) { if (lastChild == null) { // Słowo rozpoczynające. newChild = addNewWord(words[i], 1); } else { // Normalne słowo - dodajemy je do listy dziecka. newChild = addNewWord(words[i], 0); lastChild.appendChild(newChild); } lastChild = newChild; } // Ostatnie słowo - dodajemy zakończenie. lastChild.counters[0]++; lastChild.total++; }
protected SentenceNode randomChild() { // Znajdywanie losowego dziecka na podstawie // utworzonego rozkładu prawdopodobieństwa. // Jan Grzywacz. SentenceNode newChild = children[0]; double myRand = rand.NextDouble() * total; double dist = 0; for (int i = 0; i < children.Count; i++) { // Tworzymy pseudo-dystrybuantę. dist += counters[i]; if (myRand < dist) { newChild = children[i]; break; } } //Console.WriteLine(myRand + "/" + total); return(newChild); }