标签:afterwords Python text 练习 stop words Spark counts
一、安装Spark
验证截图:
(Spark运行成功)
(基本操作以及简单运算)
二、Python编程练习:英文文本的词频统计
源码:
path='/home/hadoop/wc/f1.txt' with open(path) as f: #text read and down text=f.read() text = text.lower() #replace marks for ch in '!"@#$%^&*()+,-./:;<=>?@[\\]_`~{|}': text=text.replace(ch," ") words = text.split() #set non-count words stop_words = ['so','out','all','for','of','to','on','in','if','by','under','it','at','into','with','about','i','am','are','is','a','the','and','that','before','her','she','my','be','an','from','would','me','got'] lenwords=len(words) afterwords=[] #count words for i in range(lenwords): z=1 for j in range(len(stop_words)): #avoid non-count words if words[i]==stop_words[j]: continue else: if z==len(stop_words): afterwords.append(words[i]) break z=z+1 continue #get freq counts = {} for word in afterwords: counts[word] = counts.get(word,0) + 1 items = list(counts.items()) items.sort(key=lambda x:x[1],reverse=True) i=1 while i<=len(items): word,count = items[i-1] print("{0:<20}{1}".format(word,count)) i=i+1
运行结果:
标签:afterwords,Python,text,练习,stop,words,Spark,counts 来源: https://www.cnblogs.com/animabread/p/15954074.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。