public override string Transliterate(string text, bool dashes = false) { SourceTextWithIterator source = new SourceTextWithIterator(text); string destination = string.Empty; while (source.LeftCharacters > 0) { string add; Rule rule = Rules.FindBestRule(source); if (rule.RawSource == string.Empty) { add = source.CurrentCharacter.ToString(); source.IncrementPosition(); } else { add = rule.Apply(source); } if (dashes && destination.Length > 0 && char.IsLetter(destination.Last()) && add.Length > 0 && char.IsLetter(add.First())) { destination += '-'; } destination += add; } return(destination); }
public bool CanApply(SourceTextWithIterator text) { if (text.LeftCharacters < this.Source.Length) { return(false); } if (WordBegins) { if (text.InWord || !char.IsLetter(text.CurrentCharacter)) { return(false); } } for (int i = 0, texti = text.Position; i < this.Source.Length; i++, texti++) { if (jokersLookUp[i] == -1) { if (this.Source[i] != char.ToLower(text[texti])) { return(false); } } else // So it is a joker { if (jokers[Source[i]].IndexOf(char.ToLower(text[texti])) == -1) { return(false); } } } if (WordEnds && Length != text.LeftCharacters) { if (IsLetter(text[text.Position + Length])) { return(false); } } if (this.counterExamples != null) { if (this.counterExamples.Contains(text.CurrentWord)) { return(false); } } return(true); }
/// <summary> /// Finds the best applicable rule for the given <c>SourceTextWithIterator</c> in the collection. /// </summary> /// <param name="source"></param> /// <returns>The best applicable rule if any, otherwise an empty rule (i.e. source == empty string)</returns> public Rule FindBestRule(SourceTextWithIterator source) { Rule best = new Rule("", "", null, null); foreach (Rule rule in this.Items) { if (rule.CanApply(source) && (rule > best)) { best = rule; } } return(best); }
public string Apply(SourceTextWithIterator text) { StringBuilder ret = new StringBuilder(this.Destination); for (int i = 0, texti = text.Position; i < this.Source.Length; i++, texti++) { if (jokersLookUp[i] != -1) { string C = text[texti].ToString(); string c = C.ToLower(); if (ruleCollection.Contains(c)) { ret[jokersLookUp[i]] = ruleCollection[c].Destination[0]; } else { ret[jokersLookUp[i]] = c[0]; } if (c[0] != C[0]) { ret[jokersLookUp[i]] = char.ToUpper(ret[jokersLookUp[i]]); } } } switch (text.IncrementPosition(this.Source.Length)) { case WordCapitals.UpperCase: return(ret.ToString().ToUpper()); case WordCapitals.CapitalLetter: { string rets = ret.ToString(); if (rets != string.Empty) { rets = char.ToUpper(rets[0]) + rets.Substring(1); } return(rets); } } return(ret.ToString()); }
public string Transliterate2(string text) { SourceTextWithIterator source = new SourceTextWithIterator(text); string destination = string.Empty; while (source.LeftCharacters > 0) { string add; Rule rule = null; // = ruleCollection.FindBestRule(source); if (rule.RawSource == string.Empty) { add = source.CurrentCharacter.ToString(); source.IncrementPosition(); } else { rule.Apply(source); } } return(destination); }
public void CanApplyTest() { var target = new Oggy.Transliterator.Transliterator(MockRepository.Create(), "EN", "SR"); SourceTextWithIterator text = new SourceTextWithIterator("still"); Rule rule; // Can Apply rule = new Rule("|s", "", new Dictionary <char, string>(), null); Assert.IsTrue(rule.CanApply(text)); rule = new Rule("s", "", new Dictionary <char, string>(), null); Assert.IsTrue(rule.CanApply(text)); rule = new Rule("st", "", new Dictionary <char, string>(), null); Assert.IsTrue(rule.CanApply(text)); // Can not Apply rule = new Rule("s|", "", new Dictionary <char, string>(), null); Assert.IsFalse(rule.CanApply(text)); rule = new Rule("s@", "", new Dictionary <char, string>(), null); Assert.IsFalse(rule.CanApply(text)); rule = new Rule("stil|", "", new Dictionary <char, string>(), null); Assert.IsFalse(rule.CanApply(text)); }