萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> 程式語言綜合

Python 統計單詞出現頻率:命令列工具、Counter 與大檔案流式處理

需求分析

給定一或多個文字檔案,統計每個單詞出現次數,按頻率排序輸出。核心挑戰:大檔案記憶體、標點符號處理、Unicode 正規化、效能。

方案演進

版本 1:基礎版 (原文改良)


#!/usr/bin/env python3
import sys
import string
from collections import Counter

def main():
    if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'):
        print(f"用法: {sys.argv[0]} file1.txt [file2.txt ...]")
        sys.exit(1)
    
    # 要移除的字元:空白、標點、數字、引號
    strip_chars = string.whitespace + string.punctuation + string.digits + "\"'"
    counter = Counter()
    
    for filename in sys.argv[1:]:
        try:
            with open(filename, 'r', encoding='utf-8', errors='ignore') as f:
                for line in f:
                    for word in line.split():
                        clean = word.strip(strip_chars).lower()
                        if len(clean) >= 2:  # 忽略單字元
                            counter[clean] += 1
        except OSError as e:
            print(f"讀取 {filename} 失敗: {e}", file=sys.stderr)
    
    # 輸出:按頻率降序,同頻率按字母序
    for word, count in counter.most_common():
        print(f"'{word}' occurs {count} times")

if __name__ == '__main__':
    main()

版本 2:正則精確匹配單詞邊界


import re
import sys
from collections import Counter

# Unicode 感知單詞模式:字母、數字、底線、連字號、撇號
# 使用 regex 模組 (pip install regex) 可支援 \p{L} Unicode 字母
WORD_RE = re.compile(r"\b[\w'-]+\b", re.UNICODE)

def count_words_regex(filenames):
    counter = Counter()
    for fname in filenames:
        try:
            with open(fname, 'r', encoding='utf-8', errors='ignore') as f:
                for line in f:
                    words = WORD_RE.findall(line.lower())
                    counter.update(words)
        except OSError as e:
            print(f"錯誤: {e}", file=sys.stderr)
    return counter

版本 3:生產級 (大檔案、多執行緒、進度顯示)


#!/usr/bin/env python3
import sys
import re
import argparse
import mmap
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path

WORD_RE = re.compile(r"[a-zA-Z']+")  # 簡單 ASCII 詞,效能最佳

def count_file(path: Path) -> Counter:
    """單檔案計數 (記憶體映射加速)"""
    local_counter = Counter()
    try:
        with open(path, 'rb') as f:
            # 記憶體映射:OS 負責分頁,大檔案極快
            with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
                # 解碼為字串 (假設 UTF-8)
                text = mm.read().decode('utf-8', errors='ignore').lower()
                words = WORD_RE.findall(text)
                local_counter.update(words)
    except OSError as e:
        print(f"讀取失敗 {path}: {e}", file=sys.stderr)
    return local_counter

def merge_counters(counters):
    """合併多個 Counter"""
    total = Counter()
    for c in counters:
        total.update(c)
    return total

def main():
    parser = argparse.ArgumentParser(description='統計文字檔單詞頻率')
    parser.add_argument('files', nargs='+', type=Path, help='輸入檔案或目錄')
    parser.add_argument('-r', '--recursive', action='store_true', help='遞迴掃描目錄')
    parser.add_argument('-t', '--top', type=int, default=0, help='只顯示前 N 高頻詞 (0=全部)')
    parser.add_argument('-j', '--jobs', type=int, default=4, help='並行執行緒數')
    parser.add_argument('--min-len', type=int, default=2, help='最小單詞長度')
    parser.add_argument('-o', '--output', type=Path, help='輸出 CSV 檔案')
    args = parser.parse_args()
    
    # 收集所有檔案
    files = []
    for path in args.files:
        if path.is_dir():
            if args.recursive:
                files.extend(path.rglob('*.txt'))
            else:
                files.extend(path.glob('*.txt'))
        else:
            files.append(path)
    
    if not files:
        print("未找到任何 .txt 檔案", file=sys.stderr)
        sys.exit(1)
    
    print(f"掃描 {len(files)} 個檔案,使用 {args.jobs} 執行緒...")
    
    # 並行處理
    with ThreadPoolExecutor(max_workers=args.jobs) as executor:
        futures = {executor.submit(count_file, f): f for f in files}
        all_counters = []
        for fut in as_completed(futures):
            all_counters.append(fut.result())
    
    # 合併
    total = merge_counters(all_counters)
    
    # 過濾最小長度
    if args.min_len > 1:
        total = Counter({w: c for w, c in total.items() if len(w) >= args.min_len})
    
    # 輸出
    items = total.most_common(args.top) if args.top > 0 else total.most_common()
    
    if args.output:
        import csv
        with open(args.output, 'w', newline='', encoding='utf-8') as f:
            writer = csv.writer(f)
            writer.writerow(['word', 'count'])
            writer.writerows(items)
        print(f"結果已寫入 {args.output}")
    else:
        for word, count in items:
            print(f"{word:<20} {count:>10}")

if __name__ == '__main__':
    main()

核心模組深度解析

collections.Counter

  • 繼承自 dict,專為計數優化
  • counter[key] 回傳 0 而非 KeyError
  • counter.update(iterable) 批次累加
  • counter.most_common(n) 取前 N 高頻 (堆排序 O(k log n))
  • 支援數學運算:c1 + c2、c1 - c2、c1 & c2 (交集最小值)、c1 | c2 (聯集最大值)

正則表達式優化

模式說明效能
\w+字母數字底線快
[a-z']+ + IGNORECASE純字母+撇號最快
\b\w+\b單詞邊界較慢 (邊界檢查)
\p{L}+ (regex 模組)所有 Unicode 字母中等

效能基準 (參考)


# 100MB 文字檔,單核心
# 方案                    耗時    記憶體
# read() + findall()      2.1s    ~200MB (全文載入)
# mmap + findall()        1.3s    ~50MB  (OS 頁面快取)
# 逐行 readline + findall 1.8s    ~10MB  (流式)
# 多執行緒 (4核心)        0.5s    依檔案數

擴充功能建議

  • 停用詞過濾:載入 NLTK/spaCy 停用詞表 if word not in STOP_WORDS
  • 詞幹提取/詞形還原:nltk.PorterStemmer、spacy.lemmatizer
  • N-gram 統計:zip(words, words[1:]) 統計雙詞組合
  • 分散式處理:MapReduce (Hadoop/Spark)、Dask、Ray
  • 視覺化:WordCloud 詞雲、Matplotlib 長尾分佈圖

總結

Python 統計單詞頻率,核心三件套:re 精確切詞、Counter 高效計數、mmap/ThreadPool 處理大檔案。根據資料規模選擇適當架構:小檔案單執行緒足夠,大檔案用記憶體映射+多執行緒,海量資料上分散式計算框架。

copyright © 萬盛學電腦網 all rights reserved