Guest User

Untitled

a guest
Jan 17th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. Example caption:
  2. JOHN SMITH AKA JOHN R SMITH FKA JOHNNY R SMITH
  3.  
  4. Desired output:
  5. Alias Type Found: AKA
  6. Alias Caption Found: JOHN R SMITH
  7. Alias Type Found: FKA
  8. Alias Caption Found: JOHNNY R SMITH
  9.  
  10. void Main()
  11. {
  12. var caption = "JOHN SMITH AKA JOHN R SMITH FKA JOHNNY R SMITH";
  13. caption.Split().ParseAliases( (t,c)=>{
  14. Console.WriteLine ("Alias Type Found: {0}",t);
  15. Console.WriteLine ("Alias Caption Found: {0}",c);
  16. });
  17. }
  18.  
  19. public delegate void AliasRetrievedDelegate(string aliasType, string aliasCaption);
  20.  
  21. public static class ParserExtensions{
  22. private static IEnumerable<string> aliasTypes = new[]{"AKA","FKA"};
  23.  
  24. public static void ParseAliases(this IEnumerable<string> tokens,
  25. aliasRetrievedDelegate d,
  26. int startIdx = 0){
  27. // TODO
  28.  
  29. }
  30. }
  31.  
  32. public static class ParserExtensions
  33. {
  34. private static IEnumerable<string> aliasTypes = new[]{"AKA","FKA"};
  35.  
  36. public static void ParseAliases(this IEnumerable<string> tokens,
  37. Action<string, string> d,
  38. int startIdx = 0)
  39. {
  40. var aliases = tokens.Skip(startIdx)
  41. .GroupMatchesWithTrailing(x => aliasTypes.Contains(x));
  42. foreach(var alias in aliases)
  43. {
  44. string aliasType = alias.Item1;
  45. string aliasName = string.Join(" ", alias.Item2.ToArray());
  46. d(alias.Type, alias.Name);
  47. }
  48. }
  49.  
  50. private static IEnumerable<Tuple<T, List<T>>> GroupMatchesWithTrailing<T>(
  51. this IEnumerable<T> source,
  52. Func<T, bool> predicate)
  53. {
  54. var items = source.SkipWhile(x => predicate(x) == false);
  55. using (IEnumerator<T> iterator = items.GetEnumerator())
  56. {
  57. bool hasItems = iterator.MoveNext();
  58. while(hasItems)
  59. {
  60. T match = iterator.Current;
  61. List<T> trailing = new List<T>();
  62. hasItems = iterator.MoveNext();
  63. while(hasItems && predicate(iterator.Current) == false)
  64. {
  65. trailing.Add(iterator.Current);
  66. hasItems = iterator.MoveNext();
  67. }
  68. yield return Tuple.Create(match, trailing);
  69. }
  70. }
  71. }
  72. }
Add Comment
Please, Sign In to add comment