my_recognizer.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import warnings
  2. from asl_data import SinglesData
  3. def recognize(models: dict, test_set: SinglesData):
  4. """Recognize test word sequences from word models set
  5. :param models: dict of trained models
  6. {'SOMEWORD': GaussianHMM model object,
  7. 'SOMEOTHERWORD': GaussianHMM model object, ...}
  8. :param test_set: SinglesData object
  9. :return: (list, list) as probabilities, guesses
  10. both lists are ordered by the test set word_id
  11. probabilities is a list of dictionaries where each key a word and
  12. value is Log Liklihood.
  13. [{SOMEWORD': LogLvalue, 'SOMEOTHERWORD' LogLvalue, ... },
  14. {SOMEWORD': LogLvalue, 'SOMEOTHERWORD' LogLvalue, ... },
  15. ]
  16. guesses is a list of the best guess words ordered by the test
  17. set word_id.
  18. ['WORDGUESS0', 'WORDGUESS1', 'WORDGUESS2',...]
  19. """
  20. warnings.filterwarnings("ignore", category=DeprecationWarning)
  21. probabilities = []
  22. guesses = []
  23. #warnings.filterwarnings("ignore", category=DeprecationWarning)
  24. probabilities = []
  25. guesses = []
  26. X_lengths = test_set.get_all_Xlengths()
  27. for X, lengths in X_lengths.values():
  28. log_l = {} # Save the likelihood of a word
  29. max_score = float("-inf") # Save max score as recognizer iterates
  30. best_guess = None # Save best guess as recognizer iterates
  31. for word, model in models.items():
  32. try:
  33. # Score word using model
  34. word_score = model.score(X, lengths)
  35. log_l[word] = word_score
  36. if word_score > max_score:
  37. max_score = word_score
  38. best_guess = word
  39. except:
  40. # Unable to process word
  41. log_l[word] = float("-inf")
  42. guesses.append(best_guess)
  43. probabilities.append(log_l)
  44. return probabilities, guesses