run_evaluate.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from argparse import ArgumentParser
  2. from fun_text_processing.inverse_text_normalization.inverse_normalize import InverseNormalizer
  3. from fun_text_processing.text_normalization.data_loader_utils import (
  4. evaluate,
  5. known_types,
  6. load_files,
  7. training_data_to_sentences,
  8. training_data_to_tokens,
  9. )
  10. '''
  11. Runs Evaluation on data in the format of : <semiotic class>\t<unnormalized text>\t<`self` if trivial class or normalized text>
  12. like the Google text normalization data https://www.kaggle.com/richardwilliamsproat/text-normalization-for-english-russian-and-polish
  13. '''
  14. def parse_args():
  15. parser = ArgumentParser()
  16. parser.add_argument("--input", help="input file path", type=str)
  17. parser.add_argument(
  18. "--lang", help="language", choices=['en', 'id', 'ja', 'de', 'es', 'pt', 'ru', 'fr', 'vi', 'ko', 'zh', 'fil'], default="en", type=str
  19. )
  20. parser.add_argument(
  21. "--cat",
  22. dest="category",
  23. help="focus on class only (" + ", ".join(known_types) + ")",
  24. type=str,
  25. default=None,
  26. choices=known_types,
  27. )
  28. parser.add_argument("--filter", action='store_true', help="clean data for inverse normalization purposes")
  29. return parser.parse_args()
  30. if __name__ == "__main__":
  31. # Example usage:
  32. # python run_evaluate.py --input=<INPUT> --cat=<CATEGORY> --filter
  33. args = parse_args()
  34. if args.lang == 'en':
  35. from fun_text_processing.inverse_text_normalization.en.clean_eval_data import filter_loaded_data
  36. file_path = args.input
  37. inverse_normalizer = InverseNormalizer()
  38. print("Loading training data: " + file_path)
  39. training_data = load_files([file_path])
  40. if args.filter:
  41. training_data = filter_loaded_data(training_data)
  42. if args.category is None:
  43. print("Sentence level evaluation...")
  44. sentences_un_normalized, sentences_normalized, _ = training_data_to_sentences(training_data)
  45. print("- Data: " + str(len(sentences_normalized)) + " sentences")
  46. sentences_prediction = inverse_normalizer.inverse_normalize_list(sentences_normalized)
  47. print("- Denormalized. Evaluating...")
  48. sentences_accuracy = evaluate(
  49. preds=sentences_prediction, labels=sentences_un_normalized, input=sentences_normalized
  50. )
  51. print("- Accuracy: " + str(sentences_accuracy))
  52. print("Token level evaluation...")
  53. tokens_per_type = training_data_to_tokens(training_data, category=args.category)
  54. token_accuracy = {}
  55. for token_type in tokens_per_type:
  56. print("- Token type: " + token_type)
  57. tokens_un_normalized, tokens_normalized = tokens_per_type[token_type]
  58. print(" - Data: " + str(len(tokens_normalized)) + " tokens")
  59. tokens_prediction = inverse_normalizer.inverse_normalize_list(tokens_normalized)
  60. print(" - Denormalized. Evaluating...")
  61. token_accuracy[token_type] = evaluate(tokens_prediction, tokens_un_normalized, input=tokens_normalized)
  62. print(" - Accuracy: " + str(token_accuracy[token_type]))
  63. token_count_per_type = {token_type: len(tokens_per_type[token_type][0]) for token_type in tokens_per_type}
  64. token_weighted_accuracy = [
  65. token_count_per_type[token_type] * accuracy for token_type, accuracy in token_accuracy.items()
  66. ]
  67. print("- Accuracy: " + str(sum(token_weighted_accuracy) / sum(token_count_per_type.values())))
  68. print(" - Total: " + str(sum(token_count_per_type.values())), '\n')
  69. for token_type in token_accuracy:
  70. if token_type not in known_types:
  71. raise ValueError("Unexpected token type: " + token_type)
  72. if args.category is None:
  73. c1 = ['Class', 'sent level'] + known_types
  74. c2 = ['Num Tokens', len(sentences_normalized)] + [
  75. token_count_per_type[known_type] if known_type in tokens_per_type else '0' for known_type in known_types
  76. ]
  77. c3 = ["Denormalization", sentences_accuracy] + [
  78. token_accuracy[known_type] if known_type in token_accuracy else '0' for known_type in known_types
  79. ]
  80. for i in range(len(c1)):
  81. print(f'{str(c1[i]):10s} | {str(c2[i]):10s} | {str(c3[i]):5s}')
  82. else:
  83. print(f'numbers\t{token_count_per_type[args.category]}')
  84. print(f'Denormalization\t{token_accuracy[args.category]}')