语料中筛选出英文单词并统计词频,正则切割匹配
2014-10-31 18:13
676 查看
1.正则的使用匹配
2.dic.setdefault()的使用
3、内建函数enumerate(sequence,start=0)的使用
4、内建函数sorted(),key,reversed参数设置
5、str.lower()string大小写转换
#coding:utf-8 import re import os import time import codecs PATH = os.path.dirname(__file__) s = u'what a Beautiful woRld'.lower() pattern = re.compile(u'[^a-z]+', re.U)#在非英文出进行切割 for con in pattern.split(s.lower()):#将所有英文转化为小写 if len(con) <= 1: continue else: print con def get_english_words(): '''过滤出语料中夹杂在汉语中的英文单词,并统计出现的词频''' eng_freq_dic = {} pattern = re.compile(u'[^a-z]+', re.U) cut_filename = r'E:\SVN\linguistic_model\data\combine_msg_comment.txt' with codecs.open(cut_filename, encoding='utf-8') as f: for line in f.readlines(): for con in pattern.split(line.lower()): if len(con) <= 1:#过滤掉单字母 continue else: count = eng_freq_dic.setdefault(con, 0) + 1 #若没有该key,则保存该key且设value其为0。若有则value加1 eng_freq_dic[con] = count#整个英文单词及其出现的频度 eng_filename = os.path.join(PATH, 'english_words_original.txt') eng_to_write_list = sorted([(k,v) for (k, v) in eng_freq_dic.items()], key=lambda x:x[1], reverse=True)#按照词频的高低进行倒序排列 codecs.open(eng_filename, mode='wb', encoding='utf-8').writelines([item[0]+'\t'+str(item[1])+'\n' for item in eng_to_write_list])#词频为int类型,转化为str类型以后写入到本地文件中 def chose_top_n(): '''筛选出top2000,并写入到文件''' line_list = [] filename = os.path.join(PATH, 'english_words_original.txt') with codecs.open(filename, encoding='utf-8') as f: for index,line in enumerate(f.readlines(), start=1):#enumerate(sequence, start=0)用法,显示可迭代序列中元素及其位置,start参数可以确定起始下标,默认情况下为0 print index, line.strip() time.sleep(1) line_list.append(line) if index == 2000: top_filename = os.path.join(PATH, 'top_2000_english_words.txt') codecs.open(top_filename, mode='wb', encoding='utf-8').writelines(line_list) break
相关文章推荐
- python实现统计汉字/英文单词数的正则表达式
- 编程统计一个英文文本文件中单词词频
- 统计英文单词词频
- python实现统计汉字/英文单词数的正则表达式
- c++ 统计英文文本中每个单词的词频并且按照词频对每行排序
- 英文单词词频统计
- 使用Python+NLTK实现英文单词词频统计
- 统计英文单词词频(c版)
- 英文单词词频统计代码
- python实现统计汉字/英文单词数的正则表达式
- 软件体系结构课程设计:词频统计程序(包含英文单词和数字double,int)
- c++ 统计英文文本中每个单词的词频并且按照词频对每行排序
- 统计一个英文文本的单词词频
- 使用Python+NLTK实现英文单词词频统计
- C++语言,统计一篇英文文章中的单词数(用正则表达式实现)
- 统计英文文本中的词频
- 小玩具——单词词频统计
- 正则表达式的四大操作(匹配,切割,替换,获取)
- 黑马程序员---正则表达式笔记(匹配,切割,替换,获取)
- 使用Lucene词频统计与d3.cloud展示的中文英文词云系统