写一段python代码n-gram
时间: 2024-02-18 21:57:51 浏览: 69
n-gram-correct.zip
以下是一个简单的示例,演示了如何使用n-gram模型来生成文本:
```python
import random
# 构建n-gram模型
def build_ngram_model(corpus, n):
ngram_model = {}
for sentence in corpus:
words = sentence.split()
for i in range(len(words)-n+1):
ngram = ' '.join(words[i:i+n-1])
next_word = words[i+n-1]
if ngram in ngram_model:
ngram_model[ngram].append(next_word)
else:
ngram_model[ngram] = [next_word]
return ngram_model
# 根据n-gram模型生成文本
def generate_text(ngram_model, n, length):
current_ngram = random.choice(list(ngram_model.keys()))
generated_text = current_ngram
for _ in range(length-n+1):
if current_ngram in ngram_model:
next_word = random.choice(ngram_model[current_ngram])
generated_text += ' ' + next_word
current_ngram = ' '.join(generated_text.split()[-n+1:])
else:
break
return generated_text
# 示例用法
corpus = [
"I am happy",
"You are sad",
"He is angry",
"She is tired"
]
ngram_model = build_ngram_model(corpus, 2)
generated_text = generate_text(ngram_model, 2, 10)
print(generated_text)
```
这段代码首先定义了一个`build_ngram_model`函数,用于构建n-gram模型。然后定义了一个`generate_text`函数,用于根据n-gram模型生成文本。最后,给出了一个示例用法,使用一个简单的语料库构建了一个2-gram模型,并生成了一段长度为10的文本。
阅读全文