语料中筛选出英文单词并统计词频,正则切割匹配
来源:互联网 发布:java错误无法加载主类 编辑:程序博客网 时间:2024/06/05 14:18
1.正则的使用匹配
2.dic.setdefault()的使用
3、内建函数enumerate(sequence,start=0)的使用
4、内建函数sorted(),key,reversed参数设置
5、str.lower()string大小写转换
#coding:utf-8import reimport osimport timeimport codecsPATH = 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 condef 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
0 0
- 语料中筛选出英文单词并统计词频,正则切割匹配
- 英文单词词频统计代码
- 统计英文单词词频
- 英文单词词频统计
- 统计英文单词词频(c版)
- 使用Python+NLTK实现英文单词词频统计
- 使用Python+NLTK实现英文单词词频统计
- JAVA中正则表达式匹配,替换,查找,切割的方法
- IKanalyzer分词实例并统计词频
- Python分词并进行词频统计
- Spark创建WordCount并统计词频
- 正则匹配出数据
- 统计文本中出现最高的词频数并存储为excel
- 正则匹配大小写字母、汉字、特殊字符,并统计次数
- 词频统计
- 词频统计
- 词频统计
- 词频统计
- java mina FundTradeProtocolDecoder
- javascript中日期Date类的setMonth问题 多一个月
- PHP调用APNS服务发送提醒样例
- java mina FundTradeProtocolEncoder
- Zend/EventManager(Part2)
- 语料中筛选出英文单词并统计词频,正则切割匹配
- nyoj14
- Be confident
- HTML5学习参考资料整理
- Git使用教程(以gitcafe为例)
- Android启动模式:singleTask的深究--其真正含义的解读之app间试验研究2
- windows下编译object-c
- IOS 远程推送 学习笔记 更新于2016年9月9日
- Connection