Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ConsoleLib.Console;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Text;
- using System;
- using XRL.World;
- using HistoryKit;
- namespace XRL.Language
- {
- public static class Grammar
- {
- public static string Pluralize(string word)
- {
- if (string.IsNullOrEmpty(word)) return word;
- if (word[0] == '=') return "=pluralize=" + word;
- string plural = "";
- if (singularToPlural.TryGetValue(word, out plural)) return plural;
- if (irregularPluralization.TryGetValue(word, out plural)) return FoundPlural(word, plural);
- Match match;
- match = Regex.Match(word, @"(.*?)(&.(?:\^.)?)$");
- if (match.Success) return FoundPlural(word, Pluralize(match.Groups[1].Value) + match.Groups[2].Value);
- match = Regex.Match(word, @"^(&.(?:\^.)?)(.*?)$");
- if (match.Success) return FoundPlural(word, match.Groups[1].Value + Pluralize(match.Groups[2].Value));
- match = Regex.Match(word, "^([*\\-_~'\"/])(.*)(\\1)$");
- if (match.Success) return FoundPlural(word, match.Groups[1].Value + Pluralize(match.Groups[2].Value) + match.Groups[3].Value);
- match = Regex.Match(word, @"(.*?)( +)$");
- if (match.Success) return FoundPlural(word, Pluralize(match.Groups[1].Value) + match.Groups[2].Value);
- match = Regex.Match(word, @"^( +)(.*?)$");
- if (match.Success) return FoundPlural(word, match.Groups[1].Value + Pluralize(match.Groups[2].Value));
- match = Regex.Match(word, @"^(.*)( \(.*\))$");
- if (match.Success) return FoundPlural(word, Pluralize(match.Groups[1].Value) + match.Groups[2].Value);
- match = Regex.Match(word, @"^(.*)( \[.*\])$");
- if (match.Success) return FoundPlural(word, Pluralize(match.Groups[1].Value) + match.Groups[2].Value);
- match = Regex.Match(word, @"^(.*)( (?:of|in a|in an|in the|into|for|from|o'|to|with) .*)$", RegexOptions.IgnoreCase);
- if (match.Success) return FoundPlural(word, Pluralize(match.Groups[1].Value) + match.Groups[2].Value);
- match = Regex.Match(word, @"^(.*)( (?:mk\.?|mark) *(?:[ivx]+))$", RegexOptions.IgnoreCase);
- if (match.Success) return FoundPlural(word, Pluralize(match.Groups[1].Value) + match.Groups[2].Value);
- if (!word.Contains(" "))
- {
- match = Regex.Match(word, @"^(.*)-(.*)$");
- if (match.Success) return FoundPlural(word, match.Groups[1].Value + "-" + Pluralize(match.Groups[2].Value));
- }
- if (identicalPluralization.Contains(word)) return FoundPlural(word, word);
- if (latinPluralization.Contains(word))
- {
- if (word.EndsWith("us")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "i");
- if (word.EndsWith("a")) return FoundPlural(word, word + "e");
- if (word.EndsWith("num")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "i");
- if (word.EndsWith("um") || word.EndsWith("on")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "a");
- if (word.EndsWith("en")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "ina");
- if (word.EndsWith("is")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "es");
- if (word.EndsWith("es")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "ites");
- if (word.EndsWith("ex") || word.EndsWith("ix")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "ices");
- return FoundPlural(word, word);
- }
- if (greekPluralization1.Contains(word))
- {
- if (word.EndsWith("os") || word.EndsWith("on")) return FoundPlural(word, word.Substring(0, word.Length - 1) + "i");
- if (word.EndsWith("is") || word.EndsWith("ix") || word.EndsWith("as")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "des");
- if (word.EndsWith("ys")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "daes");
- if (word.EndsWith("ma")) return FoundPlural(word, word + "ta");
- if (word.EndsWith("a")) return FoundPlural(word, word + "e");
- if (word.EndsWith("x")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "ges");
- if (word.EndsWith("or")) return FoundPlural(word, word + "es");
- if (word.EndsWith("r")) return FoundPlural(word, word + "oi");
- return FoundPlural(word, word + "a");
- }
- if (greekPluralization2.Contains(word))
- {
- if (word.EndsWith("on")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "a");
- if (word.EndsWith("is")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "es");
- return FoundPlural(word, word);
- }
- if (hebrewPluralization.Contains(word))
- {
- if (word.EndsWith("ah")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "ot");
- if (word.EndsWith("da")) return FoundPlural(word, word.Substring(0, word.Length - 1) + "ot");
- if (word.EndsWith("esh")) return FoundPlural(word, word.Substring(0, word.Length - 3) + "ashot");
- if (word.EndsWith("ch") || word.EndsWith("kh")) return FoundPlural(word, word + "ot");
- if (word.EndsWith("a")) return FoundPlural(word, word.Substring(0, word.Length - 1) + "im");
- return FoundPlural(word, word + "im");
- }
- if (word.Contains(" "))
- {
- string[] words = word.Split(' ');
- StringBuilder build = Event.NewStringBuilder();
- if (dualPluralization.Contains(words[words.Length - 1]))
- {
- for (int i = 0; i < words.Length - 2; i++)
- {
- build.Append(words[i]);
- build.Append(" ");
- }
- build.Append(Pluralize(words[words.Length - 2]));
- build.Append(" ");
- build.Append(Pluralize(words[words.Length - 1]));
- }
- else
- {
- for (int i = 0; i < words.Length - 1; i++)
- {
- build.Append(words[i]);
- build.Append(" ");
- }
- build.Append(Pluralize(words[words.Length - 1]));
- }
- return FoundPlural(word, build.ToString().Trim());
- }
- if (ColorUtility.HasUpperExceptFormatting(word))
- {
- if (ColorUtility.IsAllUpperExceptFormatting(word)) return FoundPlural(word, ColorUtility.ToUpperExceptFormatting(Pluralize(ColorUtility.ToLowerExceptFormatting(word))));
- if (ColorUtility.IsFirstUpperExceptFormatting(word)) return FoundPlural(word, ColorUtility.CapitalizeExceptFormatting(Pluralize(ColorUtility.ToLowerExceptFormatting(word))));
- return FoundPlural(word, Pluralize(ColorUtility.ToLowerExceptFormatting(word)));
- }
- if (word.EndsWith("elf") || word.EndsWith("olf") || word.EndsWith("arf") || word.EndsWith("alf")) return FoundPlural(word, word.Substring(0, word.Length - 1) + "ves");
- if (word.EndsWith("man")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "en");
- if (word.EndsWith("ife")) return FoundPlural(word, word.Substring(0, word.Length - 2) + "ves");
- if (word.EndsWith("mensch")) return FoundPlural(word, word + "en");
- if (word.Length == 1) return FoundPlural(word, word + "s");
- char last = word[word.Length - 1];
- char secLast = word[word.Length - 2];
- string lastTwo = word.Substring(word.Length - 2, 2);
- plural = word;
- if (last == 'z' && (secLast == 'a' || secLast == 'e' || secLast == 'i' || secLast == 'o' || secLast == 'u'))
- {
- plural += "z";
- }
- if (last == 's' || last == 'x' || last == 'z' || lastTwo == "sh" || lastTwo == "ss" || lastTwo == "ch" || (last == 'o' && secLast != 'o' && secLast != 'b'))
- {
- plural += "es";
- }
- else if (last == 'y' && secLast != 'a' && secLast != 'e' && secLast != 'i' && secLast != 'o' && secLast != 'u')
- {
- plural = plural.Substring(0, plural.Length - 1) + "ies";
- }
- else
- {
- plural += "s";
- }
- return FoundPlural(word, plural);
- }
- /**
- * support method for Pluralize()
- */
- private static string FoundPlural(string word, string plural)
- {
- if (!singularToPlural.ContainsKey(word)) singularToPlural.Add(word, plural);
- if (!pluralToSingular.ContainsKey(plural)) pluralToSingular.Add(plural, word);
- return plural;
- }
- public static string ThirdPerson(string word, bool PrependSpace = false)
- {
- // not prepending a space even if PrependSpace is true in this case is intentional
- if (string.IsNullOrEmpty(word)) return word;
- string thirdPerson = "";
- if (PrependSpace)
- {
- if (firstPersonToThirdPersonWithSpace.TryGetValue(word, out thirdPerson)) return thirdPerson;
- }
- else
- {
- if (firstPersonToThirdPerson.TryGetValue(word, out thirdPerson)) return thirdPerson;
- }
- if (irregularThirdPerson.TryGetValue(word, out thirdPerson)) return FoundThirdPerson(word, thirdPerson, PrependSpace);
- Match match;
- match = Regex.Match(word, @"(.*?)(&.(?:\^.)?)$");
- if (match.Success) return FoundThirdPerson(word, ThirdPerson(match.Groups[1].Value) + match.Groups[2].Value, PrependSpace);
- match = Regex.Match(word, @"^(&.(?:\^.)?)(.*?)$");
- if (match.Success) return FoundThirdPerson(word, match.Groups[1].Value + ThirdPerson(match.Groups[2].Value), PrependSpace);
- match = Regex.Match(word, "^([*\\-_~'\"/])(.*)(\\1)$");
- if (match.Success) return FoundThirdPerson(word, match.Groups[1].Value + ThirdPerson(match.Groups[2].Value) + match.Groups[3].Value, PrependSpace);
- match = Regex.Match(word, @"(.*?)( +)$");
- if (match.Success) return FoundThirdPerson(word, ThirdPerson(match.Groups[1].Value) + match.Groups[2].Value, PrependSpace);
- match = Regex.Match(word, @"^( +)(.*?)$");
- if (match.Success) return FoundThirdPerson(word, match.Groups[1].Value + ThirdPerson(match.Groups[2].Value), PrependSpace);
- match = Regex.Match(word, @"^(.+)( (?:and|or) )(.+)$", RegexOptions.IgnoreCase);
- if (match.Success) return FoundThirdPerson(word, ThirdPerson(match.Groups[1].Value) + match.Groups[2].Value + ThirdPerson(match.Groups[3].Value), PrependSpace);
- if (word.Contains(" "))
- {
- string[] words = word.Split(' ');
- StringBuilder build = Event.NewStringBuilder();
- for (int i = 0; i < words.Length - 1; i++)
- {
- build.Append(words[i]);
- build.Append(" ");
- }
- build.Append(ThirdPerson(words[words.Length - 1]));
- return FoundThirdPerson(word, build.ToString(), PrependSpace);
- }
- else
- {
- match = Regex.Match(word, @"^(.*)-(.*)$");
- if (match.Success) return FoundThirdPerson(word, match.Groups[1].Value + "-" + ThirdPerson(match.Groups[2].Value), PrependSpace);
- }
- if (ColorUtility.HasUpperExceptFormatting(word))
- {
- if (ColorUtility.IsAllUpperExceptFormatting(word)) return FoundThirdPerson(word, ColorUtility.ToUpperExceptFormatting(ThirdPerson(ColorUtility.ToLowerExceptFormatting(word))), PrependSpace);
- if (ColorUtility.IsFirstUpperExceptFormatting(word)) return FoundThirdPerson(word, ColorUtility.CapitalizeExceptFormatting(ThirdPerson(ColorUtility.ToLowerExceptFormatting(word))), PrependSpace);
- return FoundThirdPerson(word, ThirdPerson(ColorUtility.ToLowerExceptFormatting(word)), PrependSpace);
- }
- if (word.Length == 1) return FoundThirdPerson(word, word + "s", PrependSpace);
- char last = word[word.Length - 1];
- char secLast = word[word.Length - 2];
- string lastTwo = word.Substring(word.Length - 2, 2);
- thirdPerson = word;
- if (last == 'z' && (secLast == 'a' || secLast == 'e' || secLast == 'i' || secLast == 'o' || secLast == 'u'))
- {
- thirdPerson += "z";
- }
- if (last == 's' || last == 'x' || last == 'z' || lastTwo == "sh" || lastTwo == "ss" || lastTwo == "ch" || (last == 'o' && secLast != 'o' && secLast != 'b'))
- {
- thirdPerson += "es";
- }
- else if (last == 'y' && secLast != 'a' && secLast != 'e' && secLast != 'i' && secLast != 'o' && secLast != 'u')
- {
- thirdPerson = thirdPerson.Substring(0, thirdPerson.Length - 1) + "ies";
- }
- else
- {
- thirdPerson += "s";
- }
- return FoundThirdPerson(word, thirdPerson, PrependSpace);
- }
- /**
- * support method for ThirdPerson()
- */
- private static string FoundThirdPerson(string firstPerson, string thirdPerson, bool PrependSpace)
- {
- if (!firstPersonToThirdPerson.ContainsKey(firstPerson)) firstPersonToThirdPerson.Add(firstPerson, thirdPerson);
- if (!thirdPersonToFirstPerson.ContainsKey(thirdPerson)) thirdPersonToFirstPerson.Add(thirdPerson, firstPerson);
- if (PrependSpace)
- {
- thirdPerson = " " + thirdPerson;
- if (!firstPersonToThirdPersonWithSpace.ContainsKey(firstPerson)) firstPersonToThirdPersonWithSpace.Add(firstPerson, thirdPerson);
- }
- return thirdPerson;
- }
- public static string PastTenseOf( string verb )
- {
- if (verb == "sleep") return "slept";
- if (verb == "sit") return "sat";
- if (verb == "drink") return "drank";
- if (verb == "put") return "put";
- if (verb == "are") return "was";
- if (verb == "have") return "had";
- if (verb == "eat") return "ate";
- if ( verb.EndsWith("e")) return verb + "d";
- return verb+"ed";
- }
- public static string CardinalNo( int num )
- {
- return (num == 0 ? "no" : Cardinal(num));
- }
- public static string Cardinal(int num)
- {
- if (num == 0) return "zero";
- StringBuilder result = Event.NewStringBuilder();
- if (num < 0)
- {
- result.Append("negative");
- num = -num;
- }
- int magnitude = (int) Math.Floor(Math.Log10(num));
- ProcessMagnitudes(ref num, ref magnitude, result);
- if (num >= 20)
- {
- int ones = num % 10;
- int tens = (num - ones) / 10;
- num = ones;
- switch (tens)
- {
- case 2: result.Append("twent"); break;
- case 3: result.Append("thirt"); break;
- case 4: result.Append("fort"); break;
- case 5: result.Append("fift"); break;
- case 6: result.Append("sixt"); break;
- case 7: result.Append("sevent"); break;
- case 8: result.Append("eight"); break;
- case 9: result.Append("ninet"); break;
- }
- if (num == 0)
- {
- result.Append("y");
- return result.ToString();
- }
- result.Append("y-");
- }
- switch (num)
- {
- case 1: result.Append("one"); break;
- case 2: result.Append("two"); break;
- case 3: result.Append("three"); break;
- case 4: result.Append("four"); break;
- case 5: result.Append("five"); break;
- case 6: result.Append("six"); break;
- case 7: result.Append("seven"); break;
- case 8: result.Append("eight"); break;
- case 9: result.Append("nine"); break;
- case 10: result.Append("ten"); break;
- case 11: result.Append("eleven"); break;
- case 12: result.Append("twelve"); break;
- case 13: result.Append("thirteen"); break;
- case 14: result.Append("fourteen"); break;
- case 15: result.Append("fifteen"); break;
- case 16: result.Append("sixteen"); break;
- case 17: result.Append("seventeen"); break;
- case 18: result.Append("eighteen"); break;
- case 19: result.Append("nineteen"); break;
- }
- return result.ToString();
- }
- public static string Ordinal(int num)
- {
- if (num == 0) return "zeroth";
- sharedStringBuilder.Length = 0;
- if (num < 0)
- {
- sharedStringBuilder.Append("negative");
- num = -num;
- }
- int magnitude = (int) Math.Floor(Math.Log10(num));
- ProcessMagnitudes(ref num, ref magnitude, sharedStringBuilder, "th");
- if (num >= 20)
- {
- int ones = num % 10;
- int tens = (num - ones) / 10;
- num = ones;
- switch (tens)
- {
- case 2: sharedStringBuilder.Append("twent"); break;
- case 3: sharedStringBuilder.Append("thirt"); break;
- case 4: sharedStringBuilder.Append("fort"); break;
- case 5: sharedStringBuilder.Append("fift"); break;
- case 6: sharedStringBuilder.Append("sixt"); break;
- case 7: sharedStringBuilder.Append("sevent"); break;
- case 8: sharedStringBuilder.Append("eight"); break;
- case 9: sharedStringBuilder.Append("ninet"); break;
- }
- if (num == 0)
- {
- sharedStringBuilder.Append("ieth");
- return sharedStringBuilder.ToString();
- }
- sharedStringBuilder.Append("y-");
- }
- switch (num)
- {
- case 1: sharedStringBuilder.Append("first"); break;
- case 2: sharedStringBuilder.Append("second"); break;
- case 3: sharedStringBuilder.Append("third"); break;
- case 4: sharedStringBuilder.Append("fourth"); break;
- case 5: sharedStringBuilder.Append("fifth"); break;
- case 6: sharedStringBuilder.Append("sixth"); break;
- case 7: sharedStringBuilder.Append("seventh"); break;
- case 8: sharedStringBuilder.Append("eighth"); break;
- case 9: sharedStringBuilder.Append("ninth"); break;
- case 10: sharedStringBuilder.Append("tenth"); break;
- case 11: sharedStringBuilder.Append("eleventh"); break;
- case 12: sharedStringBuilder.Append("twelfth"); break;
- case 13: sharedStringBuilder.Append("thirteenth"); break;
- case 14: sharedStringBuilder.Append("fourteenth"); break;
- case 15: sharedStringBuilder.Append("fifteenth"); break;
- case 16: sharedStringBuilder.Append("sixteenth"); break;
- case 17: sharedStringBuilder.Append("seventeenth"); break;
- case 18: sharedStringBuilder.Append("eighteenth"); break;
- case 19: sharedStringBuilder.Append("nineteenth"); break;
- }
- return sharedStringBuilder.ToString();
- }
- /**
- * support method for Cardinal() and Ordinal()
- */
- private static void ProcessMagnitude(ref int num, ref int magnitude, StringBuilder result, string place)
- {
- if (magnitude > 3) magnitude -= magnitude % 3;
- int offset = (int) Math.Floor(Math.Exp(magnitude * Math.Log(10)));
- int remainder = num % offset;
- int val = num - remainder;
- int count = val / offset;
- if (count > 0)
- {
- result.Append(Cardinal(count));
- result.Append(" ");
- result.Append(place);
- num = remainder;
- if (num > 0) result.Append(" ");
- }
- magnitude--;
- }
- /**
- * support method for Cardinal() and Ordinal()
- */
- private static bool ProcessMagnitudes(ref int num, ref int magnitude, StringBuilder result, string suffix = null)
- {
- switch (magnitude)
- {
- case 20:
- case 19:
- case 18:
- ProcessMagnitude(ref num, ref magnitude, result, "quintillion");
- if (num == 0)
- {
- if (suffix != null) result.Append(suffix);
- return true;
- }
- goto case 15;
- case 17:
- case 16:
- case 15:
- ProcessMagnitude(ref num, ref magnitude, result, "quadrillion");
- if (num == 0)
- {
- if (suffix != null) result.Append(suffix);
- return true;
- }
- goto case 12;
- case 14:
- case 13:
- case 12:
- ProcessMagnitude(ref num, ref magnitude, result, "trillion");
- if (num == 0)
- {
- if (suffix != null) result.Append(suffix);
- return true;
- }
- goto case 9;
- case 11:
- case 10:
- case 9:
- ProcessMagnitude(ref num, ref magnitude, result, "billion");
- if (num == 0)
- {
- if (suffix != null) result.Append(suffix);
- return true;
- }
- goto case 6;
- case 8:
- case 7:
- case 6:
- ProcessMagnitude(ref num, ref magnitude, result, "million");
- if (num == 0)
- {
- if (suffix != null) result.Append(suffix);
- return true;
- }
- goto case 3;
- case 5:
- case 4:
- case 3:
- ProcessMagnitude(ref num, ref magnitude, result, "thousand");
- if (num == 0)
- {
- if (suffix != null) result.Append(suffix);
- return true;
- }
- goto case 2;
- case 2:
- if (magnitude > 1)
- {
- ProcessMagnitude(ref num, ref magnitude, result, "hundred");
- if (num == 0)
- {
- if (suffix != null) result.Append(suffix);
- return true;
- }
- }
- break;
- }
- return false;
- }
- public static string MakeOrList(string[] words)
- {
- if (words.Length == 1) return words[0];
- if (words.Length == 2) return words[0] + " or " + words[1];
- sharedStringBuilder.Length = 0;
- for (int i = 0; i < words.Length - 2; i++)
- {
- sharedStringBuilder.Append(words[i]);
- sharedStringBuilder.Append(",");
- }
- sharedStringBuilder.Append(words[words.Length - 1]);
- return sharedStringBuilder.ToString();
- }
- public static string MakeOrList( List<string> words )
- {
- if( words.Count == 1 ) return words[0];
- if( words.Count == 2 ) return words[0] + " or " + words[1];
- sharedStringBuilder.Length = 0;
- for ( int i = 0; i < words.Count-1; i++ )
- {
- sharedStringBuilder.Append(words[i]);
- sharedStringBuilder.Append(", ");
- }
- sharedStringBuilder.Append("or ");
- sharedStringBuilder.Append(words[words.Count - 1]);
- return sharedStringBuilder.ToString();
- }
- public static string MakeAndList(List<string> words)
- {
- if (words.Count == 1) return words[0];
- if (words.Count == 2) return words[0] + " and " + words[1];
- sharedStringBuilder.Length = 0;
- for (int i = 0; i < words.Count - 1; i++)
- {
- sharedStringBuilder.Append(words[i]);
- sharedStringBuilder.Append(", ");
- }
- sharedStringBuilder.Append("and ");
- sharedStringBuilder.Append(words[words.Count - 1]);
- return sharedStringBuilder.ToString();
- }
- public static string MakePossessive(string word)
- {
- if (word.EndsWith("s")) return word + "'";
- else return word + "'s";
- }
- public static string MakeTitleCase(string Phrase)
- {
- string[] words = Phrase.Split(' ');
- bool isFirstWord = true;
- sharedStringBuilder.Length = 0;
- foreach (string word in words)
- {
- if (isFirstWord)
- {
- sharedStringBuilder.Append(InitialCap(word));
- isFirstWord = false;
- }
- else
- if (IsLowerCapWord(word)) sharedStringBuilder.Append(word);
- else sharedStringBuilder.Append(InitialCap(word));
- sharedStringBuilder.Append(" ");
- }
- return sharedStringBuilder.ToString().TrimEnd(' ');
- }
- public static string InitialCap(string Word)
- {
- if (String.IsNullOrEmpty(Word)) return Word;
- if (Word.Contains("-"))
- {
- string[] pieces = Word.Split('-');
- for (int i = 0; i < pieces.Length; i++)
- {
- if (pieces[i].Length > 1) pieces[i] = char.ToUpper(pieces[i][0]).ToString() + pieces[i].Substring(1);
- }
- return String.Join("-", pieces);
- }
- return char.ToUpper(Word[0]).ToString() + Word.Substring(1);
- }
- public static bool IsSupportingWord(string Word)
- {
- string LowerWord = Word.Any(char.IsUpper) ? Word.ToLower() : Word;
- return
- prepositions.Contains(LowerWord)
- || articles.Contains(LowerWord)
- || conjunctions.Contains(LowerWord)
- || badEndingWords.Contains(LowerWord)
- || badStartingWords.Contains(LowerWord)
- ;
- }
- public static bool IsLowerCapWord(string Word)
- {
- string LowerWord = Word.Any(char.IsUpper) ? Word.ToLower() : Word;
- return
- shortPrepositions.Contains(LowerWord)
- || articles.Contains(LowerWord)
- || conjunctions.Contains(LowerWord)
- ;
- }
- public static bool IsBadTitleStartingWord(string Word)
- {
- string LowerWord = Word.Any(char.IsUpper) ? Word.ToLower() : Word;
- return badStartingWords.Contains(LowerWord);
- }
- private static bool IsArticleException(string Word)
- {
- string LowerWord = Word.Any(char.IsUpper) ? Word.ToLower() : Word;
- return articleExceptions.Contains(LowerWord);
- }
- public static string RemoveBadTitleEndingWords(string Phrase)
- {
- string[] words = Phrase.Split(' ');
- if (IsSupportingWord(words[words.Length - 1]))
- {
- words[words.Length - 1] = "";
- return RemoveBadTitleEndingWords(String.Join(" ", words).TrimEnd(' '));
- }
- else return Phrase;
- }
- public static string RemoveBadTitleStartingWords(string Phrase)
- {
- string[] words = Phrase.Split(' ');
- if (IsBadTitleStartingWord(words[0]))
- {
- words[0] = "";
- return RemoveBadTitleStartingWords(String.Join(" ", words).TrimStart(' '));
- }
- else return Phrase;
- }
- public static string RandomShePronoun()
- {
- if (Rules.Stat.Random(0, 1) == 1) return "he";
- else return "she";
- }
- public static string ObjectPronoun(string subjectPronoun)
- {
- if (subjectPronoun == "he") return "him";
- else return "her";
- }
- public static string PossessivePronoun(string subjectPronoun)
- {
- if (subjectPronoun == "he") return "his";
- else return "her";
- }
- public static string InitCap(string word)
- {
- if( string.IsNullOrEmpty(word)) return "";
- if (word.Length == 1 ) return Char.ToUpper( word[0] ).ToString();
- char[] a = word.ToCharArray();
- a[0] = char.ToUpper( a[0] );
- return new string( a );
- }
- public static string InitCapWithFormatting(string word)
- {
- if( string.IsNullOrEmpty(word)) return "";
- if (word.Length == 1 ) return Char.ToUpper( word[0] ).ToString();
- char[] a = word.ToCharArray();
- for( int x=0;x<word.Length;x++ )
- {
- if( word[x] == '&' || word[x] == '^')
- {
- x++;
- }
- else
- {
- a[x] = char.ToUpper( a[x] );
- break;
- }
- }
- return new string( a );
- }
- public static string CapAfterNewlines(string text)
- {
- if (string.IsNullOrEmpty(text)) return "";
- sharedStringBuilder.Length = 0;
- string[] sentences = text.Split('\n');
- for (int i = 0, j = sentences.Length; i < j; i++)
- {
- sharedStringBuilder.Append(InitCap(sentences[i]));
- if (i < j - 1) sharedStringBuilder.Append('\n');
- }
- return sharedStringBuilder.ToString();
- }
- public static string InitLower(string word)
- {
- if( string.IsNullOrEmpty( word ) ) return "";
- if( word.Length == 1 ) return Char.IsUpper(word[0]) ? Char.ToLower( word[0] ).ToString() : word;
- if( word[0] == '&' && word.Length >= 3) return word.Substring(0,2) + Char.ToLower(word[2]).ToString() + word.Substring(3);
- else return Char.ToLower(word[0]).ToString() + word.Substring(1);
- }
- public static string InitLowerIfArticle(string word)
- {
- if (word.Length < 7) return word;
- if ( word.Substring(0, 5).Contains("A ") || word.Substring(0, 6).Contains("The ") || word.Substring(0, 5).Contains("An ") || word.Substring(0, 7).Contains("Some "))
- {
- if (word[0] == '&') return word.Substring(0, 2) + Char.ToLower(word[2]).ToString() + word.Substring(3);
- else return Char.ToLower(word[0]).ToString() + word.Substring(1);
- }
- else return word;
- }
- public static string LowerArticles(string phrase)
- {
- return phrase.Replace("A ", "a ").Replace("The ", "the ").Replace("Some ", "some ");
- }
- public static string MakeTitleCaseWithArticle(string phrase)
- {
- if (phrase.StartsWith("the ") || phrase.StartsWith("The ") || phrase.StartsWith("some ") || phrase.StartsWith("Some "))
- {
- string capPhrase = MakeTitleCase(phrase);
- return Char.ToLower(capPhrase[0]).ToString() + capPhrase.Substring(1);
- }
- else return MakeTitleCase(phrase);
- }
- public static bool IndefiniteArticleShouldBeAn(string Word)
- {
- if (String.IsNullOrEmpty(Word))
- {
- return false;
- }
- string LowerWord = Word.Any(char.IsUpper) ? Word.ToLower() : Word;
- char first = LowerWord[0];
- return ((first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u') ^ IsArticleException(LowerWord)) && !LowerWord.StartsWith("one-");
- }
- public static string A(string word, bool capitalize = false)
- {
- if (word[0] == '=') return "=article=" + word;
- return (IndefiniteArticleShouldBeAn(word) ? (capitalize ? "An " : "an ") : (capitalize ? "A " : "a ")) + word;
- }
- public static void A(string word, StringBuilder result, bool capitalize = false)
- {
- if (word[0] == '=')
- {
- result.Append("=article=").Append(word);
- }
- else
- {
- result.Append(IndefiniteArticleShouldBeAn(word) ? (capitalize ? "An " : "an ") : (capitalize ? "A " : "a "));
- result.Append(word);
- }
- }
- public static string ConvertAtoAn(string sentence)
- {
- string[] words = sentence.Split(' ');
- sharedStringBuilder.Length = 0;
- for (int i = 0; i < words.Length; i++)
- {
- sharedStringBuilder.Append(words[i]);
- if (i < words.Length - 1)
- {
- if ((words[i].Equals("a") || words[i].Equals("A")) && !String.IsNullOrEmpty(words[i + 1]) && IndefiniteArticleShouldBeAn(words[i + 1]))
- {
- sharedStringBuilder.Append("n");
- }
- sharedStringBuilder.Append(" ");
- }
- }
- return sharedStringBuilder.ToString();
- }
- public static string GetWordRoot(string word)
- {
- string objWord = GetRandomMeaningfulWord(word);
- string result = "";
- String[] substrings = Regex.Split(objWord, @"(?=[aeiouy])");
- for (int i = 0; i <= substrings.Length - 1; i++)
- {
- if (i == substrings.Length - 1)
- {
- result += substrings[i].TrimEnd(new char[] { 'a', 'e', 'i', 'o', 'u', 'y' });
- }
- else result += substrings[i];
- }
- return result;
- }
- public static string Adjectify(string word)
- {
- string suffix = new string[] { "ian", "ic", "-like", "ary", "ique" }.GetRandomElement();
- word = TrimTrailingS(word);
- if (suffix[0] != '-') word = GetWordRoot(word);
- return word + suffix;
- }
- public static string TrimLeadingThe( string phrase )
- {
- if (phrase.StartsWith("the ") && phrase.Length >= 5) return phrase.Substring(4);
- else return phrase;
- }
- public static string TrimTrailingS( string word )
- {
- if (word[word.Length - 1] == 's') return word.Substring(0, word.Length-1);
- return word;
- }
- public static string ShortenName(string name)
- {
- return name.Split(',')[0];
- }
- public static string TrimTrailingPunctuation( string phrase )
- {
- if (phrase.Length == 0) return phrase;
- if (punctuation.Contains(phrase[phrase.Length - 1])) return TrimTrailingPunctuation(phrase.Substring(0, phrase.Length - 1));
- else return phrase;
- }
- public static string GetRandomMeaningfulWord(string phrase)
- {
- String[] splitWords = phrase.Split(' ');
- string word = splitWords[Rules.Stat.Random(0, splitWords.Length - 1)];
- while (prepositions.Contains(word) || articles.Contains(word) || conjunctions.Contains(word) || ordinalsRoman.Contains(word) || demonstrativePronouns.Contains(word))
- {
- word = splitWords[Rules.Stat.Random(0, splitWords.Length - 1)];
- }
- return word;
- }
- public static string Stutterize( string sentence, string word )
- {
- String[] splitSentence = sentence.Split(' ');
- if (splitSentence.Length > 3)
- {
- int index = Rules.Stat.Random(0, splitSentence.Length - 3);
- int stuttersLeft = 0;
- for (int i = 0; i < splitSentence.Length - 1; i++)
- {
- if (i == index) stuttersLeft = 4;
- if (stuttersLeft == 1)
- {
- splitSentence[i] = splitSentence[i] + "... " + word;
- stuttersLeft--;
- }
- else
- if (stuttersLeft > 0)
- {
- splitSentence[i] = splitSentence[i] + "...";
- stuttersLeft--;
- }
- }
- return String.Join(" ", splitSentence);
- }
- return sentence;
- }
- public static string GetRomanNumeral( int n )
- {
- if (n == 0) return "";
- if (n == 1) return "I";
- if (n == 2) return "II";
- if (n == 3) return "III";
- if (n == 4) return "IV";
- if (n == 5) return "V";
- if (n == 6) return "VI";
- if (n == 7) return "VII";
- if (n == 8) return "VIII";
- if (n == 9) return "IX";
- if (n == 10) return "X";
- else return "";
- }
- public static string GetOrdinalNumber( int n )
- {
- if (n == 0) return "";
- if (n == 1) return "first";
- if (n == 2) return "second";
- if (n == 3) return "third";
- if (n == 4) return "fourth";
- if (n == 5) return "fifth";
- if (n == 6) return "sixth";
- if (n == 7) return "seventh";
- if (n == 8) return "eigth";
- if (n == 9) return "ninth";
- if (n == 10) return "tenth";
- else return "";
- }
- public static string Weirdify( string word, int Chance = 100 )
- {
- char a = weirdLowerAs.GetRandomElement();
- char A = weirdUpperAs.GetRandomElement();
- char e = weirdLowerEs.GetRandomElement();
- char E = weirdLowerEs.GetRandomElement();
- char i = weirdLowerIs.GetRandomElement();
- char I = weirdUpperIs.GetRandomElement();
- char o = weirdLowerOs.GetRandomElement();
- char O = weirdUpperOs.GetRandomElement();
- char u = weirdLowerUs.GetRandomElement();
- char U = weirdUpperUs.GetRandomElement();
- char c = weirdLowerCs.GetRandomElement();
- char f = weirdLowerFs.GetRandomElement();
- char n = weirdLowerNs.GetRandomElement();
- char t = weirdLowerTs.GetRandomElement();
- char y = weirdLowerYs.GetRandomElement();
- char B = weirdUpperBs.GetRandomElement();
- char C = weirdUpperCs.GetRandomElement();
- char Y = weirdUpperYs.GetRandomElement();
- char L = weirdUpperLs.GetRandomElement();
- char R = weirdUpperRs.GetRandomElement();
- char N = weirdUpperNs.GetRandomElement();
- if (If.Chance(Chance)) word = word.Replace('a', a);
- if (If.Chance(Chance)) word = word.Replace('A', A);
- if (If.Chance(Chance)) word = word.Replace('e', e);
- if (If.Chance(Chance)) word = word.Replace('E', E);
- if (If.Chance(Chance)) word = word.Replace('i', i);
- if (If.Chance(Chance)) word = word.Replace('I', I);
- if (If.Chance(Chance)) word = word.Replace('o', o);
- if (If.Chance(Chance)) word = word.Replace('O', O);
- if (If.Chance(Chance)) word = word.Replace('u', u);
- if (If.Chance(Chance)) word = word.Replace('U', U);
- if (If.Chance(Chance)) word = word.Replace('c', c);
- if (If.Chance(Chance)) word = word.Replace('f', f);
- if (If.Chance(Chance)) word = word.Replace('n', n);
- if (If.Chance(Chance)) word = word.Replace('t', t);
- if (If.Chance(Chance)) word = word.Replace('y', y);
- if (If.Chance(Chance)) word = word.Replace('B', B);
- if (If.Chance(Chance)) word = word.Replace('C', C);
- if (If.Chance(Chance)) word = word.Replace('Y', Y);
- if (If.Chance(Chance)) word = word.Replace('L', L);
- if (If.Chance(Chance)) word = word.Replace('R', R);
- if (If.Chance(Chance)) word = word.Replace('N', N);
- return word;
- }
- public static bool ContainsBadWords( string phrase )
- {
- phrase = phrase.ToLower();
- foreach ( string s in badWords ) if (phrase.Contains(s)) return true;
- return false;
- }
- public static char[] weirdLowerAs = { (char)131, (char)132, (char)133, (char)134, (char)160, (char)166, (char)224, (char)97 };
- public static char[] weirdUpperAs = { (char)65, (char)142, (char)143, (char)146 };
- public static char[] weirdLowerEs = { (char)101, (char)130, (char)136, (char)137, (char)138, (char)238 };
- public static char[] weirdUpperEs = { (char)69, (char)144, (char)288 };
- public static char[] weirdLowerIs = { (char)105, (char)139, (char)140, (char)141, (char)161, (char)173 };
- public static char[] weirdUpperIs = { (char)73, (char)173, (char)179 };
- public static char[] weirdLowerOs = { (char)111, (char)147, (char)148, (char)149, (char)162, (char)229, (char)235, (char)248 };
- public static char[] weirdUpperOs = { (char)79, (char)153, (char)232, (char)233, (char)237 };
- public static char[] weirdLowerUs = { (char)117, (char)150, (char)151, (char)163, (char)230 };
- public static char[] weirdUpperUs = { (char)85, (char)154, (char)230 };
- public static char[] weirdLowerCs = { (char)99, (char)135, (char)155 };
- public static char[] weirdLowerFs = { (char)102, (char)159 };
- public static char[] weirdLowerNs = { (char)110, (char)164, (char)227, (char)239 };
- public static char[] weirdLowerTs = { (char)116, (char)231 };
- public static char[] weirdLowerYs = { (char)121, (char)152 };
- public static char[] weirdUpperBs = { (char)66, (char)225 };
- public static char[] weirdUpperCs = { (char)67, (char)128 };
- public static char[] weirdUpperYs = { (char)89, (char)157 };
- public static char[] weirdUpperLs = { (char)76, (char)156 };
- public static char[] weirdUpperRs = { (char)82, (char)158 };
- public static char[] weirdUpperNs = { (char)78, (char)165, (char)238 };
- static string[] shortPrepositions = { "at", "by", "in", "of", "on", "to", "up", "from", "with", "into", "over" };
- static string[] prepositions = { "at", "by", "in", "of", "on", "to", "up", "from", "with", "into" };
- static string[] articles = { "an", "a", "the" };
- static string[] demonstrativePronouns = { "this", "that", "those", "these", "such", "none", "neither", "who", "whom", "whose" };
- static string[] conjunctions = { "and", "but", "or", "nor", "for", "as" };
- static string[] ordinalsRoman = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" };
- static string[] badEndingWords = { "this", "were", "every", "are", "which", "their", "has", "your", "that", "who", "our", "additional", "its", "he", "her", "during", "no", "she's", "he's", "than", "they" };
- static string[] badStartingWords = { "were", "though", "them", "him,", "her", "is" };
- static string[] articleExceptions = { "herb", "herbal", "hour", "one", "ubiquitous", "ubiquitously", "ubiquity", "unary", "unicorn", "unicorns", "unicycle", "unidirectional", "unidirectionality", "unidirectionally", "unification", "unifications", "unified", "unifier", "unifies", "uniform", "uniformed", "uniformity", "uniformly", "uniforms", "unify", "unifying", "union", "unionization", "unionize", "unionized", "unionizer", "unionizers", "unionizes", "unionizing", "unions", "uniprocessor", "unique", "uniquely", "uniqueness", "unison", "unit", "unitary", "unities", "uniting", "unity", "univalve", "univalved", "univalves", "universal", "universal", "universality", "universally", "universe", "universes", "universities", "university", "usable", "use", "useless" };
- static string[] badWords = { "nigg", "fag" };
- private static char[] punctuation = { '.', '!', ',', ':', ';' };
- private static Dictionary<string, string> singularToPlural = new Dictionary<string, string>();
- private static Dictionary<string, string> pluralToSingular = new Dictionary<string, string>();
- private static Dictionary<string, string> irregularPluralization = new Dictionary<string, string>{
- {"atterkop", "atterkoppen"},
- {"attorney general", "attorneys general"},
- {"bergrisi", "bergrisar"},
- {"child", "children"},
- {"childe", "childer"},
- {"commando", "commandos"},
- {"court martial", "courts martial"},
- {"die", "dice"},
- {"djinn", "djinni"},
- {"dunadan", "dunadain"},
- {"eldjotun", "eldjotnar"},
- {"eldthurs", "eldthursar"},
- {"felljotun", "felljotnar"},
- {"fife", "fifes"},
- {"fomor", "fomori"},
- {"foot", "feet"},
- {"forefoot", "forefeet"},
- {"genus", "genera"},
- {"goose", "geese"},
- {"hindfoot", "hindfeet"},
- {"hrimthurs", "hrimthursar"},
- {"ifrit", "ifriti"},
- {"jabberwock", "jabberwocky"},
- {"jerky", "jerkys"},
- {"jotun", "jotnar"},
- {"knight templar", "knights templar"},
- {"leaf", "leaves"},
- {"loaf", "loaves"},
- {"longstaff", "longstaves"},
- {"mosquito", "mosquitos"},
- {"mouse", "mice"},
- {"notary public", "notaries public"},
- {"octopus", "octopodes"},
- {"opus", "opera"},
- {"ordo", "ordines"},
- {"ox", "oxen"},
- {"pancreas", "pancreata"},
- {"person", "people"},
- {"platypus", "platypoda"},
- {"plus", "plusses"},
- {"quarterstaff", "quarterstaves"},
- {"rhinox", "rhinoxen"},
- {"risi", "risar"},
- {"secretary general", "secretaries general"},
- {"shaman", "shamans"},
- {"staff", "staves"},
- {"sturmjotun", "sturmjotnar"},
- {"surgeon general", "surgeons general"},
- {"talisman", "talismans"},
- {"thief", "thieves"},
- {"tooth", "teeth"},
- {"topaz", "topazes"},
- {"townsperson", "townspeople"},
- };
- private static string[] identicalPluralization = { "barracks", "bison", "buffalo", "cannon", "caribou", "chosen", "corps", "deer", "einheriar", "fish", "fruit", "geisha", "gi", "hellspawn", "katana", "kraken", "lamia", "kris", "means", "moose", "naga", "ninja", "nunchaku", "oni", "remains", "sai", "scissors", "series", "sheep", "shrimp", "shuriken", "spawn", "species", "sputum", "waterworks", "wakizashi", "yeti", "yoroi", "young" };
- private static string[] latinPluralization = { "abacus", "adytum", "alkalus", "alumnus", "alumno", "alumna", "anima", "animo", "animus", "antenna", "appendix", "arboretum", "astrum", "automaton", "axis", "bacterium", "ballista", "cacosteum", "cactus", "cestus", "cinctus", "cognomen", "corpus", "datum", "desideratum", "dictum", "dominatrix", "drosophilium", "ellipsis", "emerita", "emerito", "emeritus", "epona", "eques", "equus", "erratum", "esophagus", "exoculus", "exodus", "fascia", "focus", "forum", "fungus", "haruspex", "hippocampus", "hippopotamus", "hypha", "iambus", "illuminata", "illuminato", "illuminatus", "imperator", "imperatrix", "incarnus", "larva", "locus", "lorica", "maga", "mago", "magus", "manica", "matrix", "medium", "melia", "momentum", "neurosis", "nexus", "nomen", "nucleus", "patagium", "pegasus", "penis", "persona", "phenomenon", "phoenix", "pilum", "plexus", "praenomen", "psychosis", "quantum", "radius", "rectum", "sanctum", "scintilla", "scriptorium", "scrotum", "scutum", "septum", "simulacrum", "stratum", "substratum", "testis", "tympani", "ultimatum", "uterus", "vagina", "vertex", "vomitorium", "vortex", "vulva" };
- private static string[] greekPluralization1 = { "archon", "aristos", "astron", "bebelos", "charisma", "chimera", "daimon", "domos", "echthros", "eidolon", "ephemeris", "epopis", "hegemon", "horos", "hystrix", "kentaur", "kharisma", "kudos", "laryngis", "larynx", "lemma", "logos", "mestor", "minotaur", "mnemon", "mythos", "omphalos", "ouros", "patris", "pharynx", "pragma", "rhetor", "rhinoceros", "schema", "stigma", "telos", "topos" };
- private static string[] greekPluralization2 = { "diokesis", "ganglion", "noumenon", "numenon", "praxis", "therion" };
- private static string[] hebrewPluralization = { "aswad", "chaya", "cherub", "golem", "kabbalah", "keruv", "khaya", "nefesh", "nephil", "neshamah", "qabalah", "ruach", "ruakh", "sephirah", "seraph", "yechida", "yekhida" };
- private static string[] dualPluralization = { "emerita", "emeritus" };
- private static Dictionary<string, string> firstPersonToThirdPerson = new Dictionary<string, string>();
- private static Dictionary<string, string> firstPersonToThirdPersonWithSpace = new Dictionary<string, string>();
- private static Dictionary<string, string> thirdPersonToFirstPerson = new Dictionary<string, string>();
- private static Dictionary<string, string> irregularThirdPerson = new Dictionary<string, string>{
- {"'re", "'s"},
- {"'ve", "'s"},
- {"are", "is"},
- {"aren't", "isn't"},
- {"cannot", "cannot"},
- {"can't", "can't"},
- {"could", "could"},
- {"couldn't", "couldn't"},
- {"don't", "doesn't"},
- {"grew", "grew"},
- {"had", "had"},
- {"have", "has"},
- {"may", "may"},
- {"might", "might"},
- {"must", "must"},
- {"shall", "shall"},
- {"shouldn't", "shouldn't"},
- {"should", "should"},
- {"sought", "sought"},
- {"were", "was"},
- {"will", "will"},
- {"won't", "won't"},
- {"wouldn't", "wouldn't"},
- {"would", "would"},
- };
- private static StringBuilder sharedStringBuilder = new StringBuilder(128);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement