2011-09-25 10 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'

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