calc_read_score.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import azure.cognitiveservices.speech as speechsdk
  2. import re
  3. import argparse
  4. import time
  5. import cn2an
  6. pattern = '53948358944853994358398458549394835354988459349358'
  7. standard = [(25,44),(20,33),(17,27),(15,24),(14,22),(12,20)]
  8. unit = 10000000
  9. speech_config = speechsdk.SpeechConfig(subscription="934d6f1c2e8c4817b01f98cb93451fba", region='eastasia')
  10. speech_config.speech_recognition_language = "zh-CN"
  11. speech_config.set_property(speechsdk.PropertyId.SpeechServiceConnection_InitialSilenceTimeoutMs, "50000")
  12. speech_config.request_word_level_timestamps()
  13. def find_last_digit_position(input_string):
  14. input_string = re.sub(r"[,,。:?]","", input_string)
  15. pattern = r'\d'
  16. matches = list(re.finditer(pattern, input_string))
  17. if matches:
  18. last_match = matches[-1]
  19. return last_match.start()
  20. else:
  21. return -1
  22. def find_first_digit_position(input_string):
  23. input_string = re.sub(r"[,,。:?]","", input_string)
  24. pattern = r'\d'
  25. result = re.search(pattern,input_string)
  26. if result:
  27. return result.start()
  28. else:
  29. return -1
  30. def rec(speech_recognizer):
  31. done = False
  32. def stop_cb(evt):
  33. # print('CLOSING on {}'.format(evt))
  34. speech_recognizer.stop_continuous_recognition()
  35. nonlocal done
  36. done = True
  37. recognized_text = []
  38. json_result = []
  39. def handle_recognized(evt):
  40. # print('RECOGNIZED: {}'.format(evt))
  41. recognized_text.append(evt.result.text)
  42. json_result.append(evt.result.json)
  43. # speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
  44. speech_recognizer.recognized.connect(handle_recognized)
  45. # speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
  46. # speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED {}'.format(evt)))
  47. # speech_recognizer.canceled.connect(lambda evt: print('CANCELED {}'.format(evt)))
  48. speech_recognizer.session_stopped.connect(stop_cb)
  49. speech_recognizer.canceled.connect(stop_cb)
  50. speech_recognizer.start_continuous_recognition()
  51. while not done:
  52. time.sleep(.5)
  53. return recognized_text, json_result
  54. def read_time(text,json):
  55. start = 9999
  56. end = 0
  57. for i,j in zip(text,json):
  58. if len(i)>0:
  59. s = find_first_digit_position(i)
  60. e = find_last_digit_position(i)
  61. j_result = eval(j)
  62. tmp_start = j_result['NBest'][0]['Words'][s]['Offset']/unit
  63. tmp_end = j_result['NBest'][0]['Words'][e]['Offset']/unit + j_result['NBest'][0]['Words'][e]['Duration']/unit
  64. if tmp_start< start:
  65. start = tmp_start
  66. if tmp_end > end:
  67. end = tmp_end
  68. return start, end
  69. def check_digit(text):
  70. if len(text) != len(pattern):
  71. return -1
  72. else:
  73. count = 0
  74. for i in range(len(text)):
  75. if text[i]==pattern[i]:
  76. count += 1
  77. if count<47:
  78. return -1
  79. else:
  80. return 1
  81. def calc_read_time(filename, grade):
  82. audio_config = speechsdk.audio.AudioConfig(filename=filename)
  83. speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
  84. text, json = rec(speech_recognizer)
  85. start, end = read_time(text,json)
  86. duration = end - start
  87. score = round(duration, 3)
  88. digit_text = ''.join(text)
  89. digit_text = cn2an.transform(digit_text)
  90. digit_text = re.sub("\D","",digit_text)
  91. if check_digit(digit_text)==1:
  92. if score<=standard[grade-1][0]:
  93. c = 'a'
  94. elif score>standard[grade-1][1]:
  95. c = 'c'
  96. else:
  97. c = 'b'
  98. print(f'{score}-{c}')
  99. return score, c, digit_text
  100. else:
  101. return 0
  102. if __name__ == '__main__':
  103. parse = argparse.ArgumentParser()
  104. parse.add_argument('filename', type=str)
  105. parse.add_argument('grade',type=int)
  106. args = parse.parse_args()
  107. calc_read_time(args.filename, args.grade)