2011-09-25 56 views
0

我想指望从包含数据如下文本文件的话:字数问题

ROK : 
    ROK/(NN) 
New : 
    New/(SV) 
releases, : 
    releases/(NN) + ,/(SY) 
week : 
    week/(EP) 
last : 
    last/(JO) 
compared : 
    compare/(VV) + -ed/(EM) 
year : 
    year/(DT) 
releases : 
    releases/(NN) 

像/(NN)/(SV),和/(EP)被认为是类的表达。 我想在每个类别之前提取单词并计算整个文本中有多少单词。

我想在这样一个新的文本文件写一个结果:

(NN) 
releases 2 
ROK 1 

(SY) 
New 1 
, 1 

(EP) 
week 1 

(JO) 
last 1 

...... 

请帮我!

这里是我的车库代码; _;它不起作用。

import os, sys 
import re 

wordset = {} 
for line in open('E:\\mach.txt', 'r'): 
    if '/(' in line: 
     word = re.findall(r'(\w)/\(', line) 
     print word 
     if word not in wordset: wordset[word]=1 
     else: wordset[word]+=1 

f = open('result.txt', 'w') 
for word in wordset: 
    print>> f, word, wordset[word] 
f.close() 

回答

1
from __future__ import print_function                                                         
import re                                                                


REGEXP = re.compile(r'(\w+)/(\(.*?\))')                                                         


def main():                                                                
    words = {}                                                               

    with open('E:\\mach.txt', 'r') as fp: 
     for line in fp:                                                              
      for item, category in REGEXP.findall(line):                                                      
       words.setdefault(category, {}).setdefault(item, 0)                                                   
       words[category][item] += 1                                                         

    with open('result.txt', 'w') as fp:                                                          
     for category, words in sorted(words.items()):                                                      
      print(category, file=fp)                                                          
      for word, count in words.items():                                                        
       print(word, count, sep=' ', file=fp)                                                      
      print(file=fp)                                                             
    return 0                                                               

if __name__ == '__main__':                                                            
    raise SystemExit(main()) 

不客气(= 如果你希望,也算那怪异的 “-ed” 或 “” 调正则表达式匹配除空白任何字符:

REGEXP = re.compile(r'([^\s]+)/(\(.*?\))') 
+0

此代码显示出色的性能!但实际上我想解析韩国的话。在这种情况下,此代码无法正常工作。你有什么主意吗? – ooozooo

+0

哦..刚刚找到你的评论。你还需要帮助吗? –

0

你正在尝试使用列表(是的话是一个列表)作为指标下面是你应该做的:

import re 

wordset = {} 
for line in open('testdata.txt', 'r'): 
    if '/(' in line: 
     words = re.findall(r'(\w)/\(', line) 
     print words 
     for word in words: 
      if word not in wordset: 
      wordset[word]=1 
      else: 
      wordset[word]+=1 

f = open('result.txt', 'w') 
for word in wordset: 
    print>> f, word, wordset[word] 
f.close() 

你很幸运,我想LEA蟒蛇,否则我不会尝试你的代码。下次发布你遇到的错误!我敢打赌这是

TypeError: unhashable type: 'list'

如果你想得到好的答案,帮助我们帮助你是很重要的!