本文整理汇总了Python中Main.calculate_probabilities方法的典型用法代码示例。如果您正苦于以下问题:Python Main.calculate_probabilities方法的具体用法?Python Main.calculate_probabilities怎么用?Python Main.calculate_probabilities使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Main
的用法示例。
在下文中一共展示了Main.calculate_probabilities方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import Main [as 别名]
# 或者: from Main import calculate_probabilities [as 别名]
def main(args):
"""Then entry point to this program.
Arguments:
args -- a list of specifications to how the program should be run. The format should be:
- output_file -- The file to write the result to
- markov_order -- The number of words each generated word uses (must be integer greater than 0)
- training_file -- This is a varargs argument. Each training_file will be used for data to generate words.
"""
if len(args) < 2:
sys.stderr.write("Usage: <markov_order> <training_file> ...")
return
try:
chunk_size = int(args[0]) # the size of each the word chunks that will make up the output
except ValueError:
sys.stderr.write("Numerical arguments must be non-negative integers")
return
files = args[1:] # the training files
# Error Handling
if chunk_size < 1:
sys.stderr.write("Markov order must exceed 0")
return
"""maps tuples of words maps of the words that are preceded by the tuples in the training docs.
Each map matches the following-word with the number of times it occurs as a following word"""
bigram_counts = {}
try:
for file_name in files: # for each file passed in, store data from that file in global maps
process_file(file_name, chunk_size, bigram_counts)
except IOError:
print "Invalid file path"
return
print "Training data recorded"
probabilities = {}
Main.calculate_probabilities(
bigram_counts, probabilities
) # determine probabilities of generating a word given a previous word
print "Markov probabilities calculated"
result = produce_text(probabilities, chunk_size)
print "RESULT: " + result