2013-01-21 30 views
0

我有一個CSV文件看起來像:(約10,000行)詞彙大小的文件

Lorem ipsum dolor sit amet , 12:01 
consectetuer adipiscing elit, sed , 12:02 

等等

這是一個相當大的文件 我想獲得所有文本行的總詞彙量大小一起。也就是說,忽略第二列(時間),降低一切,然後計算不同單詞的數量。

發佈: 1)如何分隔每行內的每個單詞 2)如何將所有內容小寫並刪除非字母字符。

到目前爲止,我有以下代碼:

import csv 
with open('/Users/file.csv', 'rb') as file: 
    vocabulary = [] 
    i = 0 
    reader = csv.reader(file, delimiter=',') 
    for row in reader: 
     for word in row: 
      if row in vocabulary: 
       break 
      else: 
       vocabulary.append(word) 
       i = i +1 
print i 

謝謝您的幫助!

回答

1

你有幾乎所需的東西。一個缺點是小寫轉換,可以簡單地用word.lower()完成。

你缺少的另一件事是分裂成詞。對於此任務,您應該使用.split(),默認情況下,它將按每個空白字符分隔,即空格,製表符等。

您將遇到的一個問題是區分文本中的逗號和列分隔逗號。也許不要使用csv-reader,而是簡單地閱讀每一行並刪除時間,然後將其分成單詞。

import re 

with open('/Users/file.csv', 'rb') as file: 
    for line in file: 
     line = re.sub(" , [0-2][0-9]:[0-5][0-9]", "", line) 
     line = re.sub("[,|!|.|?|\"]", "", line) 
     words = [w.lower() for w in line.split()] 
     for word in words: 
      ... 

如果要刪除其他字符,請將其包含在第二個正則表達式中。如果性能對您很重要,您應該在for循環之前編譯一次兩個正則表達式。

+0

謝謝你跑,你能確切地說明我可以使用.split()嗎?不知道如何。 – Julia

+0

擴大了我的範例,希望能解決大部分問題。 –

+0

謝謝!有沒有辦法擺脫字符「沒有假表達?」 – Julia

3

Python csv模塊是一個很棒的庫,但經常用它來簡化任務可能是一個矯枉過正的問題。 這種特殊情況下,對我來說,是一個典型的例子,在使用CSV模塊可事情

要我過去複雜,

  • 只是通過文件迭代,
  • 拆分上逗號每一行,並提取所述第一分割
  • 然後在白色空間分割剩餘部分
  • 轉換每個字爲小寫
  • 地帶出所有的標點符號和挖其
  • 和理解的結果爲一組

是線性直接的方法

一個例子具有以下文件內容

Lorem Ipsum is simply dummy "text" of the ,0 
printing and typesetting; industry. Lorem,1 
Ipsum has been the industry's standard ,2 
dummy text ever since the 1500s, when an,3 
unknown printer took a galley of type and,4 
scrambled it to make a type specimen ,5 
book. It has survived not only five ,6 
centuries, but also the leap into electronic,7 
typesetting, remaining essentially unch,8 
anged. It was popularised in the 1960s with ,9 
the release of Letraset sheets conta,10 
ining Lorem Ipsum passages, and more rec,11 
ently with desktop publishing software like,12 
!!Aldus PageMaker!! including versions of,13 
Lorem Ipsum.,14 

>>> from string import digits, punctuation 
>>> remove_set = digits + punctuation 
>>> with open("test.csv") as fin: 
    words = {word.lower().strip(remove_set) for line in fin 
     for word in line.rsplit(",",1)[0].split()} 


>>> words 
set(['and', 'pagemaker', 'passages', 'sheets', 'galley', 'text', 'is', 'in', 'it', 'anged', 'an', 'simply', 'type', 'electronic', 'was', 'publishing', 'also', 'unknown', 'make', 'since', 'when', 'scrambled', 'been', 'desktop', 'to', 'only', 'book', 'typesetting', 'rec', "industry's", 'has', 'ever', 'into', 'more', 'printer', 'centuries', 'dummy', 'with', 'specimen', 'took', 'but', 'standard', 'five', 'survived', 'leap', 'not', 'lorem', 'a', 'ipsum', 'essentially', 'unch', 'conta', 'like', 'ining', 'versions', 'of', 'industry', 'ently', 'remaining', 's', 'printing', 'letraset', 'popularised', 'release', 'including', 'the', 'aldus', 'software']) 
+0

謝謝你的幫助,但是有時在我感興趣的文本行中有逗號... – Julia

+0

正如Abhijit提到的,你使用set鍵入以處理您的重複數據刪除。你使用的數組方法很慢,我相信n^2。至少你可以將字作爲關鍵字添加到字典中,由於散列,這會更快。集合是要走的路,因爲他們做同樣的事情,是爲了這個。 –

+0

我想你忘記了OP所要的「刪除非字母字符」部分,儘管這也可以不用're'來完成。 – martineau