08

# coding=gb2312
import sys
import string

if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}:
    print("usage: uniqueword filename_1 filename_2 ... filename_n")
    sys.exit()
 
else:
    words = {}
    strip = string.whitespace + string.punctuation + string.digits + "\"'"
 
    for filename in sys.argv[1:]:
        for line in open(filename):
            for word in line.split():
                word = word.strip(strip)
                if len(word) >= 1:
                    words[word] = words.get(word, 0) + 1





在Ubuntu中实现运行。

  • 准备txt文件
  • 编写py文件
  • python3运行py文件分析txt文件。

 

2.用MapReduce实现词频统计

2.1编写Map函数

  • 编写mapper.py
  • 授予可运行权限
  • 本地测试mapper.py

 

2.2编写Reduce函数

  • 编写reducer.py
  • 授予可运行权限
  • 本地测试reducer.py

 

posted @ 2021-11-30 09:27  蛋炒饭香  阅读(10)  评论(0编辑  收藏  举报