溫馨提示×

Linux wordcount如何進行多語言統(tǒng)計

小樊
81
2024-09-21 05:21:42
欄目: 智能運維

在Linux中,可以使用word-count工具進行多語言統(tǒng)計

  1. 首先確保已經(jīng)安裝了python-docx庫。如果沒有安裝,可以使用以下命令安裝:
pip install python-docx
  1. 創(chuàng)建一個名為multi_language_wordcount.py的Python腳本,并添加以下內(nèi)容:
import sys
import docx
from collections import defaultdict

def count_words(file_path):
    word_count = defaultdict(int)
    doc = docx.Document(file_path)

    for paragraph in doc.paragraphs:
        for run in paragraph.runs:
            if run.text:
                words = run.text.split()
                for word in words:
                    word_count[word.lower()] += 1

    return word_count

def main():
    if len(sys.argv) != 2:
        print("Usage: python multi_language_wordcount.py <file_path>")
        sys.exit(1)

    file_path = sys.argv[1]
    if not file_path.endswith('.docx'):
        print("Error: File must be a .docx file")
        sys.exit(1)

    word_count = count_words(file_path)
    for word, count in word_count.items():
        print(f"{word}: {count}")

if __name__ == "__main__":
    main()

這個腳本可以處理.docx格式的多語言文檔。你可以根據(jù)需要修改它以處理其他文件格式。

  1. 保存腳本并在終端中運行:
python multi_language_wordcount.py <file_path>

<file_path>替換為你要分析的.docx文件的路徑。腳本將輸出每個單詞及其出現(xiàn)次數(shù)。

0