|
|
|
|
|
""" |
|
|
Process word list from tab-separated input file with strict character filtering. |
|
|
Only allows: a-z, A-Z, German umlauts (öäüÖÄÜß), and hyphenation characters. |
|
|
""" |
|
|
|
|
|
import sys |
|
|
import unicodedata |
|
|
import re |
|
|
|
|
|
def is_quote_character(char): |
|
|
"""Check if a character is a quote using Unicode categories.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
category = unicodedata.category(char) |
|
|
return category in ('Pi', 'Pf', 'Ps', 'Pe') or char in ('"', "'", '`', '´', ''', ''', '"', '"', '‚', '„', '‹', '›') |
|
|
|
|
|
def has_broken_encoding(word): |
|
|
""" |
|
|
Check if word contains broken Unicode characters. |
|
|
|
|
|
Returns True if the word contains: |
|
|
- U+FFFD (�) - Unicode replacement character |
|
|
- Other common signs of encoding issues |
|
|
""" |
|
|
if not word: |
|
|
return False |
|
|
|
|
|
|
|
|
if '\ufffd' in word: |
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
suspicious_patterns = [ |
|
|
'\ufffd', |
|
|
'\x00', |
|
|
] |
|
|
|
|
|
return any(pattern in word for pattern in suspicious_patterns) |
|
|
|
|
|
def clean_word(word): |
|
|
""" |
|
|
Clean word by: |
|
|
1. Removing quotes (using Unicode quote categories) from beginning and end |
|
|
2. Removing leading hyphens (but not internal ones) |
|
|
""" |
|
|
if not word: |
|
|
return word |
|
|
|
|
|
|
|
|
while word and is_quote_character(word[0]): |
|
|
word = word[1:] |
|
|
|
|
|
|
|
|
while word and is_quote_character(word[-1]): |
|
|
word = word[:-1] |
|
|
|
|
|
|
|
|
word = word.lstrip('-') |
|
|
|
|
|
return word |
|
|
|
|
|
def is_valid_word(word): |
|
|
""" |
|
|
Check if word contains only allowed characters: |
|
|
- a-z, A-Z |
|
|
- German umlauts: öäüÖÄÜß |
|
|
- Hyphenation characters: - (hyphen-minus), ‐ (hyphen), – (en dash), — (em dash), (soft hyphen) |
|
|
""" |
|
|
if not word: |
|
|
return False |
|
|
|
|
|
|
|
|
allowed_pattern = re.compile(r'^[a-zA-ZöäüÖÄÜß\-‐–—]+$') |
|
|
|
|
|
return bool(allowed_pattern.match(word)) |
|
|
|
|
|
def process_wordlist(input_file, output_file=None): |
|
|
""" |
|
|
Process the word list file with strict character filtering. |
|
|
|
|
|
Args: |
|
|
input_file: Path to input file or file object |
|
|
output_file: Path to output file (optional, defaults to stdout) |
|
|
""" |
|
|
|
|
|
if isinstance(input_file, str): |
|
|
with open(input_file, 'r', encoding='utf-8') as f: |
|
|
lines = f.readlines() |
|
|
else: |
|
|
lines = input_file.readlines() |
|
|
|
|
|
|
|
|
seen_words = set() |
|
|
results = [] |
|
|
|
|
|
for line in lines: |
|
|
line = line.strip() |
|
|
if not line: |
|
|
continue |
|
|
|
|
|
parts = line.split('\t') |
|
|
if len(parts) < 2: |
|
|
continue |
|
|
|
|
|
try: |
|
|
index = int(parts[0]) |
|
|
word = parts[1] |
|
|
|
|
|
|
|
|
if index < 100: |
|
|
continue |
|
|
|
|
|
|
|
|
if has_broken_encoding(word): |
|
|
continue |
|
|
|
|
|
|
|
|
cleaned_word = clean_word(word) |
|
|
|
|
|
|
|
|
if has_broken_encoding(cleaned_word): |
|
|
continue |
|
|
|
|
|
|
|
|
if ' ' in cleaned_word: |
|
|
continue |
|
|
|
|
|
|
|
|
if not is_valid_word(cleaned_word): |
|
|
continue |
|
|
|
|
|
|
|
|
if cleaned_word and cleaned_word not in seen_words: |
|
|
seen_words.add(cleaned_word) |
|
|
results.append(cleaned_word) |
|
|
|
|
|
except (ValueError, IndexError): |
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
if output_file: |
|
|
with open(output_file, 'w', encoding='utf-8') as f: |
|
|
for word in results: |
|
|
f.write(word + '\n') |
|
|
else: |
|
|
for word in results: |
|
|
print(word) |
|
|
|
|
|
return results |
|
|
|
|
|
if __name__ == '__main__': |
|
|
if len(sys.argv) < 2: |
|
|
print("Usage: python process_wordlist_narrow.py <input_file> [output_file]") |
|
|
print(" If output_file is not specified, results are printed to stdout") |
|
|
print("\nThis script only allows words containing:") |
|
|
print(" - Letters: a-z, A-Z") |
|
|
print(" - German umlauts: öäüÖÄÜß") |
|
|
print(" - Hyphenation characters: - ‐ – — ") |
|
|
sys.exit(1) |
|
|
|
|
|
input_file = sys.argv[1] |
|
|
output_file = sys.argv[2] if len(sys.argv) > 2 else None |
|
|
|
|
|
process_wordlist(input_file, output_file) |