<jupyter_start><jupyter_text>![ngram_formula1](./ngram_formula1.jpg)![ngram_formula2](./ngram_formula2.jpg)<jupyter_code>def get_sentence_probability(sentence:str,one_gram_dict:dict,two_gram_dict:dict):
    new_sentence=''.join(re.findall(r'\w+',sentence))
    words=list(jieba.cut(new_sentence))
    final_prob=1.0
    for index,word in enumerate(words[1:]):
        final_prob*=two_gram_dict.get(words[index-1]+words[index],1.0)*1.0/one_gram_dict.get(words[index-1],len(one_gram_dict))
    final_prob*=one_gram_dict.get(words[0],1.0)/sum(one_gram_dict.values())
    return final_prob    
sentence1='今天不好天气，我打算不出去'
print(get_sentence_probability(sentence1,one_gram_dict,two_gram_dict))
sentence2='今天天气不好，我不打算出去'
print(get_sentence_probability(sentence2,one_gram_dict,two_gram_dict))
sentence3='今天是个好天气'
print(get_sentence_probability(sentence3,one_gram_dict,two_gram_dict))<jupyter_output>1.4531058008952092e-20
1.898952810001505e-16
5.951564476572636e-11
{'fraction_non_alphanumeric': 0.14225941422594143, 'fraction_numerical': 0.08054393305439331, 'mean_word_length': 10.258823529411766, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '205047', 'license': 'MIT', 'n_tokens_mistral': 456, 'n_tokens_neox': 408, 'n_words': 21, 'path': 'rule_and_pattern_based_lanuage_generation/source_code/n-gram_language_model.ipynb', 'repo_name': 'huakeda1/Basic-algorithm-and-framework-study-for-AI', 'size': '956'}