New We are sorting MAX channels into categories and territories. Read the news

Пример использования цепей Маркова.



Для генерации текста.

import os
import pandas as pd
import markovify
from tqdm import tqdm

# Папка с текстами
folder_path = "data"

# Чтение текстовых файлов в один DataFrame
files = [f for f in os.listdir(folder_path) if f.endswith(".txt")]

# Создаем список для хранения данных
data = []

# Чтение каждого файла и добавление в DataFrame
for file in tqdm(files, desc="Чтение файлов"):
with open(os.path.join(folder_path, file), 'r', encoding='utf-8') as f:
text = f.read().lower() # Приводим текст к нижнему регистру
data.append({'id': os.path.splitext(file)[0], 'text': text})

# Создаем DataFrame
df_texts = pd.DataFrame(data)

# Объединяем все тексты в один
all_text = " ".join(df_texts['text'])

# Строим марковскую модель
# Используем прогресс-бар для анализа текста
print("Создание марковской цепи...")
text_model = markovify.Text(all_text, state_size=2)

# Функция для генерации фразы
def generate_phrase(model, length=10):
phrase = []
current_sentence = model.make_sentence()
if not current_sentence:
return ""

phrase.append(current_sentence)

# Генерация дополнительных фраз
for _ in range(length - 1):
current_sentence = model.make_sentence()
if not current_sentence:
break
phrase.append(current_sentence)

return " ".join(phrase)

# Пример генерации фразы
generated_phrase = generate_phrase(text_model, length=10)
print("Generated phrase:", generated_phrase)
Views: 5
Open in MAX Report an inaccuracy

Report an inaccuracy

Describe what needs fixing. If you have a source, add a link.

You are leaving datool.ru

The link leads to an external website. datool.ru is not responsible for its content — check the address before entering passwords or payment details.

Redirecting in 10 s

Go