Created
February 15, 2023 09:44
-
-
Save minhqnd/d1ccf7d6b45d604f34c39cf2fc7702be to your computer and use it in GitHub Desktop.
lọc từ điển https://raw.githubusercontent.com/undertheseanlp/dictionary/master/dictionary/words.txt ra các từ có 2 chữ để chơi nối từ, tạo bởi ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
# Đường dẫn đến file txt ban đầu | |
input_file_path = "words.txt" | |
# Đường dẫn đến file txt mới sẽ ghi ra | |
output_file_path = "tudien.txt" | |
# Đọc nội dung file txt ban đầu | |
with open(input_file_path, "r") as input_file: | |
content = input_file.read() | |
# Tìm tất cả các chuỗi "text" trong nội dung file | |
texts = re.findall(r'"text": "([\w\s-]+)",', content) | |
# Tạo danh sách mới để lưu các chuỗi có đúng 2 từ | |
filtered_texts = [] | |
for text in texts: | |
# Tách các từ bằng khoảng trắng | |
words = text.split() | |
# Kiểm tra xem chuỗi có đúng 2 từ không | |
if len(words) == 2: | |
filtered_texts.append(text) | |
# Ghi danh sách các chuỗi có đúng 2 từ ra file mới | |
with open(output_file_path, "w") as output_file: | |
for text in filtered_texts: | |
output_file.write(f"{text}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment