public void TwoWordProperModifierContainsBoth() { HeadedPhrase hp = new HeadedPhrase(Words.jan, new WordSet(new[] { "Mato" })); Assert.IsTrue(hp.Contains(new Word("jan"))); Assert.IsTrue(hp.Contains(new Word("Mato"))); }
public void TwoWordProperModifierContainsThre() { HeadedPhrase hp = new HeadedPhrase(Words.jan, new WordSet(new[] { "lili", "suli" })); Assert.IsTrue(hp.Contains(new Word("jan"))); Assert.IsTrue(hp.Contains(new Word("lili"))); Assert.IsTrue(hp.Contains(new Word("suli"))); }
public void ParseAndToString() { HeadedPhrase hp = HeadedPhrase.Parse("jan Mato tomo suli"); Assert.IsTrue(hp.Contains(new Word("jan"))); Assert.IsTrue(hp.Contains(new Word("Mato"))); Assert.IsTrue(hp.Contains(new Word("tomo"))); Assert.IsTrue(hp.Contains(new Word("suli"))); Assert.AreEqual("jan Mato tomo suli", hp.ToString()); }
public static bool TryParse(string value, out HeadedPhrase result) { try { result = HeadedPhraseConverter.Parse(value); return(true); } catch (Exception) { result = null; return(false); } }
public void ParticleHeadWordWhut() { try { //li, pi, la, e, are illegal modifiers. HeadedPhrase hp = new HeadedPhrase(new Word("pi"), new WordSet(new[] { "lili", "suli", "li", "pi" })); Assert.AreEqual("jan lili suli li pi", hp.ToString()); } catch (InvalidOperationException) { Assert.Pass(); return; } catch (TpSyntaxException) { Assert.Pass(); return; } Assert.Fail("Expected some sort of exception."); }
public void ParticleModifiersWhut() { try { //li, pi, la, e, are illegal modifiers. HeadedPhrase hp = new HeadedPhrase(Words.jan, new WordSet(new[] { "lili", "suli", "li", "pi" })); Assert.AreEqual("jan lili suli li pi", hp.ToString()); } catch (InvalidOperationException) { //Assert.Pass(); Throws SuccessException-- huh? return; } catch (TpSyntaxException) { //Assert.Pass(); Throws SuccessException-- huh? return; } Assert.Fail(); }
public Chain ProcessPiChain(string value) { if (String.IsNullOrEmpty(value)) { throw new ArgumentException("Cannot parse null/empty subjects"); } if (value.ContainsCheck(" la ") || value.EndCheck(" la")) { throw new ArgumentException("Contains la. This is not possible in a pi chain.: " + value); } if (memoize) { if (piChainMemo.ContainsKey(value)) { return(piChainMemo[value]); } } string piChains = value; string[] piLessTokens = Splitters.SplitOnPi(piChains); List <HeadedPhrase> piCollection = new List <HeadedPhrase>(); foreach (string piLessToken in piLessTokens) { HeadedPhrase piPhrase = HeadedPhraseParser(piLessToken); piCollection.Add(piPhrase); } Chain piChainFinished = new Chain(Particles.pi, piCollection.ToArray()); if (memoize) { piChainMemo.Add(value, piChainFinished); } return(piChainFinished); }
/// <summary> /// Parses simple headed phrases fine. Parses some headed phrases with PP modifiers, but /// not if the PP is in maximal form. /// </summary> /// <param name="value"></param> /// <returns></returns> public HeadedPhrase HeadedPhraseParser(string value) { if (String.IsNullOrEmpty(value)) { throw new ArgumentException("Impossible to parse a null or zero length string."); } //#if DEBUG // string copyValue = String.Copy(value); //#endif if (memoize) { if (headedPhraseParserMemo.ContainsKey(value)) { return(headedPhraseParserMemo[value]); } } foreach (string particle in new[] { "pi", "la", "e", "li" }) { if (value.StartsOrContainsOrEnds(particle)) { throw new TpSyntaxException("Headed phrases have no particles. This one has " + particle + " ref: " + value + " (Did we forget the li between the subject and verb?)"); } } PrepositionalPhrase[] pp = null; if (value.ContainsCheck("~")) { string[] headAndPreps = Splitters.SplitOnPrepositions(value); value = headAndPreps[0]; pp = ProcessPrepositionalPhrases(ArrayExtensions.Tail(headAndPreps)).ToArray(); } //No Pi! TokenParserUtils pu = new TokenParserUtils(); Word[] words = pu.ValidWords(value); if (words.Length == 0) { throw new TpParseException("Failed to parse: " + value); } //Word head = words[0]; Word[] tail = words;//ArrayExtensions.Tail(words); //EXTRACT MORPHOLOGICAL STRUCTURE OF MODIFIERS HERE. var mergedTail = TurnThisWordsIntoWordsWithTaggedWords(tail); HeadedPhrase phrase = new HeadedPhrase(mergedTail[0], new WordSet(ArrayExtensions.Tail(mergedTail.ToArray())), pp); //#if DEBUG // if (copyValue != value) // { // throw new TpParseException("Invariant violation: " + copyValue +" --- "+ value); // } //#endif // if (memoize) // { // headedPhraseParserMemo[value] = phrase; // } return(phrase); }
public Sentence ProcessSimpleSentence(string sentence, Punctuation punctuation, string original) { //Think this is causing a bug. ////HACK: Still need a better way to deal with quotes. //if (sentence.EndCheck("»") || sentence.EndCheck("«")) //{ // sentence = sentence.Substring(0, sentence.Length - 1); //} //Comment? Get out of here! if (sentence.StartCheck("///")) { Comment c = new Comment(sentence); return(new Sentence(c, diagnostics)); } //Simple exclamation! Get out of here! if (Exclamation.IsExclamation(sentence)) { return(new Sentence(new Exclamation(new HeadedPhrase(new Word(sentence))), punctuation, new SentenceDiagnostics(original, sentence))); } List <Vocative> headVocatives = null; //jan Mato o, ale li pona. Head vocative! //kin la o moku. //not a vocative (hopefully dealt with elsewhere) //jan Mato o moku! //Head vocative, & imperative, with 2nd o discarded //jan Mato o o moku! //Head vocative, & imperative, with 2nd o discarded if (sentence.ContainsCheck(" o o "))//Explicit vocative & imperative { //Okay, we know exactly when the head vocatives end. headVocatives = new List <Vocative>(); string justHeadVocatives = sentence.Substring(0, sentence.IndexOf(" o o ", StringComparison.Ordinal)); //Process head vocatives. ProcessHeadVocatives(Splitters.SplitOnO(justHeadVocatives), headVocatives, allAreVocatives: true); //BUG: Add the dummy! (And it still doesn't work!) sentence = "jan Sanwan o " + sentence.Substring(sentence.IndexOf(" o o ", StringComparison.Ordinal) + 5); } //Starts with o, then we have imperative & no head vocatives. bool endsOrStartsWithO = sentence.StartCheck("o ") && sentence.EndCheck(" o"); if (!endsOrStartsWithO) { //jan So o! (We already deal with degenerate vocative sentences elsewhere) //jan So o sina li nasa. //jan So o nasa! //jan So o mi mute o nasa. <-- This is the problem. //These could be vocatives or imperatives. if (sentence.ContainsCheck(" o ", " o,", ",o ") && sentence.ContainsCheck(" li ")) { headVocatives = new List <Vocative>(); ProcessHeadVocatives(Splitters.SplitOnO(sentence), headVocatives, allAreVocatives: false); //int firstLi = sentence.IndexOf(" li "); int lastO = sentence.LastIndexOf(" o ", StringComparison.Ordinal); if (lastO < 0) { lastO = sentence.LastIndexOf(" o,", StringComparison.Ordinal); } sentence = sentence.Substring(lastO + 2); } } //Process tag conjunctions and tag questions Particle conjunction = null; TagQuestion tagQuestion = null; if (sentence.StartCheck("taso ")) { conjunction = Particles.taso; sentence = sentence.Substring(5); } else if (sentence.StartCheck("anu ")) { conjunction = Particles.anu; sentence = sentence.Substring(4); } else if (sentence.StartCheck("en ")) { //Well, either parse it or throw. Otherwise, this gets skipped. //is this legal? conjunction = Particles.en; sentence = sentence.Substring(3); } else if (sentence.StartCheck("ante ")) //never seen it. { conjunction = Particles.ante; sentence = sentence.Substring(5); } //Should already have ? stripped off if (sentence.EndsWith(" anu seme")) { tagQuestion = new TagQuestion(); sentence = sentence.Substring(0, sentence.LastIndexOf(" anu seme", StringComparison.Ordinal)); } if (sentence.EndCheck(" li")) { throw new TpParseException("Something went wrong-- sentenc ends with li. " + sentence); } if (sentence.StartsOrContainsOrEnds("la")) { throw new TpParseException("If it contains a la, anywhere, it isn't a simple sentence. " + sentence); } bool isHortative = false; bool isImperative = false; if (sentence.StartCheck("o ") && sentence.ContainsCheck(" li ")) { //o mi mute li moku isHortative = true; sentence = sentence.RemoveLeadingWholeWord("o"); } if (sentence.StartCheck("o ") && !sentence.ContainsCheck(" li ")) { //o pana e pan isImperative = true; //sentence = sentence.RemoveLeadingWholeWord("o"); } // someting o ==> vocative string[] liParts = Splitters.SplitOnLiOrO(sentence); if (liParts.Length == 1 && Exclamation.IsExclamation(liParts[0])) { //HACK: Duplicate code. & it only deals with a single final puncution mark. string possiblePunctuation = sentence[sentence.Length - 1].ToString(); if (Punctuation.TryParse(possiblePunctuation, out punctuation)) { sentence = sentence.Substring(0, sentence.Length - 1); } //The whole thing is o! (or pakala! or the like) //pona a! a a a! ike a! TokenParserUtils tpu = new TokenParserUtils(); Word[] tokes = tpu.ValidWords(sentence); HeadedPhrase parts = new HeadedPhrase(tokes[0], new WordSet(ArrayExtensions.Tail(tokes))); bool modifiersAreA = true; foreach (Word w in parts.Modifiers) { if (w == "a") { continue; //peculiar to exclamations & repeats. } if (w == "kin") { continue; //modifies just about anything } modifiersAreA = false; } if (modifiersAreA) { Exclamation exclamation = new Exclamation(parts); Sentence s = new Sentence(exclamation, punctuation, diagnostics); return(s); } } //Degenerate sentences. if (liParts[liParts.Length - 1].Trim(new char[] { ',', '«', '»', '!', ' ' }) == "o") { //We have a vocative sentence... Vocative vocative = new Vocative(ProcessEnPiChain(liParts[0])); Sentence s = new Sentence(vocative, punctuation, diagnostics); return(s); } string subjects = liParts[0].Trim(); ComplexChain subjectChain = null; int startAt = 1; //slot 0 is normally a subject if (subjects.Contains("«")) { int foo = 3; } if (subjects.StartCheck("o ") || subjects.StartCheck("«o ")) { //This is a verb phrase with implicit subjects! startAt = 0; } else { subjectChain = ProcessEnPiChain(subjects); } PredicateList verbPhrases = new PredicateList(); for (int i = startAt; i < liParts.Length; i++) { string predicate = liParts[i].Trim(); verbPhrases.Add(ProcessPredicates(predicate)); } //Head or complete sentence. Sentence parsedSentence = new Sentence(subjectChain, verbPhrases, diagnostics, new SentenceOptionalParts { Conjunction = conjunction, //Etc Punctuation = punctuation, IsHortative = isHortative, TagQuestion = tagQuestion, HeadVocatives = headVocatives != null ? headVocatives.ToArray() : null }); return(parsedSentence); }
public void ThreeWordDoubleProperModifier() { HeadedPhrase hp = new HeadedPhrase(Words.jan, new WordSet(new[] { "Mato", "Maton" })); Assert.AreEqual("jan Mato Maton", hp.ToString()); //ti is illegal }
public void TwoWordProperModifier() { HeadedPhrase hp = new HeadedPhrase(Words.jan, new WordSet(new[] { "Mato" })); Assert.AreEqual("jan Mato", hp.ToString()); }
public void TestToStringThreeWords() { HeadedPhrase hp = new HeadedPhrase(Words.jan, new WordSet(new [] { "lili", "suli" })); Assert.AreEqual("jan lili suli", hp.ToString()); }
public void CanSerialize() { HeadedPhrase hp = HeadedPhrase.Parse("jan Mato tomo suli"); Assert.NotNull(hp.ToJsonDcJs()); }
public static Chain PiChainFactory(HeadedPhrase phrase) { return(new Chain(Particles.pi, new[] { phrase })); }