本文整理汇总了Python中win32com.client.Dispatch.Format方法的典型用法代码示例。如果您正苦于以下问题:Python Dispatch.Format方法的具体用法?Python Dispatch.Format怎么用?Python Dispatch.Format使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类win32com.client.Dispatch
的用法示例。
在下文中一共展示了Dispatch.Format方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: recognition_callback
# 需要导入模块: from win32com.client import Dispatch [as 别名]
# 或者: from win32com.client.Dispatch import Format [as 别名]
def recognition_callback(self, StreamNumber, StreamPosition, RecognitionType, Result):
try:
newResult = Dispatch(Result)
phrase_info = newResult.PhraseInfo
rule_name = phrase_info.Rule.Name
#---------------------------------------------------------------
# Build a list of rule names for each element.
# First populate it with the top level rule name.
element = phrase_info.Rule
name = element.Name
start = element.FirstElement
count = element.NumberOfElements
rule_names = [name] * count
# Walk the tree of child rules and put their names in the list.
stack = [collection_iter(phrase_info.Rule.Children)]
while stack:
try: element = next(stack[-1])
except StopIteration: stack.pop(); continue
name = element.Name
start = element.FirstElement
count = element.NumberOfElements
rule_names[start:start + count] = [name] * count
if element.Children:
stack.append(collection_iter(element.Children))
#---------------------------------------------------------------
# Prepare the words and rule names for the element parsers.
replacements = [False] * len(rule_names)
if phrase_info.Replacements:
for replacement in collection_iter(phrase_info.Replacements):
begin = replacement.FirstElement
end = begin + replacement.NumberOfElements
replacements[begin] = replacement.Text
for index in range(begin + 1, end):
replacements[index] = True
results = []
rule_set = list(set(rule_names))
elements = phrase_info.Elements
for index in range(len(rule_names)):
element = elements.Item(index)
rule_id = rule_set.index(rule_names[index])
# Map dictation rule IDs to 1M so that dragonfly recognizes
# the words as dictation.
if rule_names[index] == "dgndictation":
rule_id = 1000000
replacement = replacements[index]
info = [element.LexicalForm, rule_id,
element.DisplayText, element.DisplayAttributes,
replacement]
results.append(info)
#---------------------------------------------------------------
# Retain audio
# Only write audio data and metadata if the directory exists.
retain_dir = self.engine._retain_dir
if retain_dir and not os.path.isdir(retain_dir):
self.engine._log.warning(
"Audio was not retained because '%s' was not a "
"directory" % retain_dir
)
elif retain_dir:
try:
file_stream = Dispatch("SAPI.SpFileStream")
# Note: application can also retrieve smaller portions
# of the audio stream by specifying a starting phrase
# element and phrase element length.
audio_stream = newResult.Audio()
# Make sure we have audio data, which we wouldn't from a
# mimic or if the retain flag wasn't set above.
if audio_stream:
# Write audio data.
file_stream.Format = audio_stream.Format
now = datetime.now()
filename = ("retain_%s.wav"
% now.strftime("%Y-%m-%d_%H-%M-%S_%f"))
wav_path = os.path.join(retain_dir, filename)
flags = constants.SSFMCreateForWrite
file_stream.Open(wav_path, flags)
try:
file_stream.Write(audio_stream.GetData())
finally:
file_stream.Close()
# Write metadata
words = ' '.join([r[2] for r in results])
audio_length = int(newResult.Times.Length) / 1e7
tsv_path = os.path.join(retain_dir, "retain.tsv")
with open(tsv_path, "a") as tsv_file:
tsv_file.write('\t'.join([
filename, str(audio_length),
#.........这里部分代码省略.........