Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Example caption:
- JOHN SMITH AKA JOHN R SMITH FKA JOHNNY R SMITH
- Desired output:
- Alias Type Found: AKA
- Alias Caption Found: JOHN R SMITH
- Alias Type Found: FKA
- Alias Caption Found: JOHNNY R SMITH
- void Main()
- {
- var caption = "JOHN SMITH AKA JOHN R SMITH FKA JOHNNY R SMITH";
- caption.Split().ParseAliases( (t,c)=>{
- Console.WriteLine ("Alias Type Found: {0}",t);
- Console.WriteLine ("Alias Caption Found: {0}",c);
- });
- }
- public delegate void AliasRetrievedDelegate(string aliasType, string aliasCaption);
- public static class ParserExtensions{
- private static IEnumerable<string> aliasTypes = new[]{"AKA","FKA"};
- public static void ParseAliases(this IEnumerable<string> tokens,
- aliasRetrievedDelegate d,
- int startIdx = 0){
- // TODO
- }
- }
- public static class ParserExtensions
- {
- private static IEnumerable<string> aliasTypes = new[]{"AKA","FKA"};
- public static void ParseAliases(this IEnumerable<string> tokens,
- Action<string, string> d,
- int startIdx = 0)
- {
- var aliases = tokens.Skip(startIdx)
- .GroupMatchesWithTrailing(x => aliasTypes.Contains(x));
- foreach(var alias in aliases)
- {
- string aliasType = alias.Item1;
- string aliasName = string.Join(" ", alias.Item2.ToArray());
- d(alias.Type, alias.Name);
- }
- }
- private static IEnumerable<Tuple<T, List<T>>> GroupMatchesWithTrailing<T>(
- this IEnumerable<T> source,
- Func<T, bool> predicate)
- {
- var items = source.SkipWhile(x => predicate(x) == false);
- using (IEnumerator<T> iterator = items.GetEnumerator())
- {
- bool hasItems = iterator.MoveNext();
- while(hasItems)
- {
- T match = iterator.Current;
- List<T> trailing = new List<T>();
- hasItems = iterator.MoveNext();
- while(hasItems && predicate(iterator.Current) == false)
- {
- trailing.Add(iterator.Current);
- hasItems = iterator.MoveNext();
- }
- yield return Tuple.Create(match, trailing);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment