2.安装Spark与Python练习

一、安装Spark

1、检查基础环境hadoop,jdk

 

3、相关文件配置

4、环境配置

5、运行python代码

二、Python编程练习:英文文本的词频统计

1.准备文本文件

path='/home/hadoop/lsh.txt'
with open(path) as f:
  text=f.read()

 

3.预处理:大小写,标点符号,停用词,分词

words = text.lower().split()
for c in '!"#$%^&*()_+-=@[]{}|\?/<>,.:;~·`、“”‘’':
      text = text.replace(c, " ")

4.统计每个单词出现的次数,词频大小排序

for word in words:
    t[word]=t.get(word,0)+1
tlist=list(t.items())
tlist.sort(key=lambda x:x[1],reverse=True)

5.结果写文件

text = open("lsh.txt", "w", encoding='UTF-8')
text.write(str(tlist))

 6.运行结果

 

posted @ 2022-03-07 11:03  an23  阅读(43)  评论(0)    收藏  举报