1 #加法平滑技术(加1) 2 def additive_smoothing(data): 3 global sentence, d, probability_key_value 4 probability = 1 # 存储句子概率的中间变量。 5 output = data.split() 6 output.insert(0, "BOS") 7 output.append("EOS") 8 # 上面三行代码,对输入的句子进行处理。 例如:句子[ i am a student ]处理结果为['BOS','i','am','a','student','EOS'] 9 for i in range(len(output) - 1): 10 sentence[output[i]] = output[i + 1] # 处理后格式:sentence{ BOS:i , i:am, am:a, a:student, student:EOS} 11 for j in range(len(output) - 1): 12 value =( 1+find(output[j], output[j + 1])) / (V+ d[output[j]]) 13 probability_key_value[str(output[j]) + '-' + str(output[j + 1])] = value 14 print(probability_key_value) 15 probability *= value 16 sentence={} 17 probability_key_value={} 18 return probability