import json

def parse_phrases_from_file(file_path):
    """
    Парсит файл в формате JSON Lines и извлекает все значения поля 'phrase'
    
    Args:
        file_path (str): путь к файлу
        
    Returns:
        list: список всех фраз
    """
    phrases = []
    
    with open(file_path, 'r', encoding='utf-8') as file:
        for line_num, line in enumerate(file, 1):
            line = line.strip()
            if not line:  # пропускаем пустые строки
                continue
            
            try:
                data = json.loads(line)
                if 'phrase' in data and data['phrase']:
                    phrases.append(data['phrase'])
            except json.JSONDecodeError as e:
                print(f"Ошибка в строке {line_num}: {e}")
    
    return phrases


def save_phrases_to_file(phrases, output_path):
    """
    Сохраняет список фраз в текстовый файл (каждая фраза на новой строке)
    """
    with open(output_path, 'w', encoding='utf-8') as f:
        for phrase in phrases:
            f.write(phrase + '\n')
    print(f"Сохранено {len(phrases)} фраз в {output_path}")


# Пример использования
if __name__ == "__main__":
    input_file = "app.log"      # путь к твоему файлу с данными
    output_file = "phrases.txt"  # куда сохранить только фразы
    
    # Парсим фразы
    all_phrases = parse_phrases_from_file(input_file)
    
    print(f"Найдено фраз: {len(all_phrases)}")
    
    # Выводим первые 5 для примера
    for i, phrase in enumerate(all_phrases[:5], 1):
        print(f"{i}. {phrase}")
    
    # Сохраняем в файл
    save_phrases_to_file(all_phrases, output_file)