Download trace_handler.py from vppmp/vptruyen: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/spaces/vppmp/vptruyen/resolve/main/trace_handler.py
- Command line
-
hf download hf://spaces/vppmp/vptruyen/trace_handler.py
-
curl -L -o trace_handler.py https://huggingface.co/spaces/vppmp/vptruyen/resolve/main/trace_handler.py
2.5 kB
| # trace_handler.py | |
| import re | |
| def process_content_for_trace(content: str) -> list[dict]: | |
| """ | |
| Tìm các dòng có chứa từ bị ẩn (*), hoặc các chuỗi văn bản không mong muốn. | |
| Args: | |
| content: Nội dung của chương truyện cần tìm kiếm. | |
| Returns: | |
| Một danh sách các dictionary, mỗi dictionary chứa dòng đầy đủ và | |
| các từ khóa/chuỗi văn bản bị cấm được tìm thấy. | |
| """ | |
| results = [] | |
| # --- PHẦN NÂNG CẤP --- | |
| # 1. Định nghĩa tất cả các mẫu cần tìm kiếm | |
| patterns_to_find = [ | |
| r'\b\w*\*+\w*\b', # Mẫu 1: Tìm các từ chứa ký tự '*' (ví dụ: b**n) | |
| r'text được lấy tại', # Mẫu 2: Tìm chuỗi "text được lấy tại" | |
| r'truyenfull' # Mẫu 3: Tìm chữ "truyenfull" | |
| ] | |
| # 2. Gộp các mẫu thành một biểu thức chính quy duy nhất bằng toán tử OR `|` | |
| # - `re.IGNORECASE`: để tìm kiếm không phân biệt chữ hoa/thường (ví dụ: truyenfull, TruyenFull, TRUYENFULL đều sẽ được tìm thấy) | |
| combined_pattern = re.compile("|".join(patterns_to_find), re.IGNORECASE) | |
| # 3. Quét theo từng dòng (line) thay vì từng câu để bắt được các dòng ghi chú nguồn tin tốt hơn | |
| lines = content.splitlines() | |
| for line in lines: | |
| # Bỏ qua các dòng trống | |
| if not line.strip(): | |
| continue | |
| # Tìm tất cả các từ/chuỗi khớp với mẫu trong dòng hiện tại | |
| found_keywords = combined_pattern.findall(line) | |
| # Nếu tìm thấy bất kỳ kết quả nào | |
| if found_keywords: | |
| # Dùng set để loại bỏ các kết quả trùng lặp trên cùng một dòng, sau đó nối lại | |
| # Ví dụ: "Nguồn text được lấy tại TruyenFull, đọc truyện tại truyenfull" -> "text được lấy tại, truyenfull" | |
| unique_keywords_str = ", ".join(sorted(list(set(keyword.lower() for keyword in found_keywords)))) | |
| # Thêm kết quả vào danh sách. | |
| # Giữ nguyên key là 'masked_words' để tương thích với hàm ghi Excel đã có. | |
| results.append({ | |
| "sentence": line.strip(), | |
| "masked_words": unique_keywords_str | |
| }) | |
| return results |