ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

2.安装Spark与Python练习

2022-03-06 22:31:58  阅读:173  来源: 互联网

标签:Python items 练习 stop ljjstory words Spark txt counts


一、安装Spark

1.检查基础环境hadoop,jdk

 

 

2.配置文件

vim /usr/local/spark/conf/spark-env.sh

 

 

3.环境配置

gedit ~/.bashrc

 

4.试运行Python代码

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

1.准备文本文件

2.读文件

f=open("mystory.txt","r")
ljjstory=f.read()

 

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

#所有字母变为小写
ljjstory.lower()
#将各种特殊字符和标点符号替换为空格
for ch in string.punctuation:
    ljjstory=ljjstory.replace(ch," ")
#将停用词去除
stop_words = ['an','a','as','so','out','all','for','of','to','on','in','if','by','under','it','at','into','with','about']
lenwords=len(words)
newstory=[]
for i in range(lenwords):
    a=1
    for j in range(len(stop_words)):
        if words[i]==stop_words[j]:
            continue
        else:
            if a==len(stop_words):
                newstory.append(words[i])
                break
            a=a+1
            continue

 

4.分词

words=ljjstory.split()

5.统计每个单词出现的次数

counts = {}
#创建空词典存放统计结果
for word in newstory:
    counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)

 

6.按词频大小排序

while i<=len(items):     word,count = items[i-1]     print("{0:<20}{1}".format(word,count))     i=i+1

7.结果写文件

txt= open("newstory.txt", "w",encoding='UTF-8')
txt.write(str(items))
print("文件写入成功")
pass
print(items)

 运行截图:

 

 

 

标签:Python,items,练习,stop,ljjstory,words,Spark,txt,counts
来源: https://www.cnblogs.com/jarenblogs/p/15948661.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有