2013-02-17 43 views
11

मैं शब्द आवृत्तियों को गिनने के लिए अपनी परियोजना को तेज़ करने की कोशिश कर रहा हूं। मेरे पास 360+ टेक्स्ट फाइलें हैं, और मुझे शब्दों की कुल संख्या और शब्दों की दूसरी सूची से प्रत्येक शब्द की संख्या प्राप्त करने की आवश्यकता है। मुझे पता है कि यह एक पाठ फ़ाइल के साथ कैसे करें।पायथन - टेक्स्ट फ़ाइल में शब्दों की सूची की शब्द आवृत्तियों को ढूंढना

>>> import nltk 
>>> import os 
>>> os.chdir("C:\Users\Cameron\Desktop\PDF-to-txt") 
>>> filename="1976.03.txt" 
>>> textfile=open(filename,"r") 
>>> inputString=textfile.read() 
>>> word_list=re.split('\s+',file(filename).read().lower()) 
>>> print 'Words in text:', len(word_list) 
#spits out number of words in the textfile 
>>> word_list.count('inflation') 
#spits out number of times 'inflation' occurs in the textfile 
>>>word_list.count('jobs') 
>>>word_list.count('output') 

'मुद्रास्फीति', 'नौकरियां', 'आउटपुट' व्यक्ति की आवृत्तियों को प्राप्त करने के लिए यह बहुत कठिन है। क्या मैं इन शब्दों को एक सूची में डाल सकता हूं और एक ही समय में सूची में सभी शब्दों की आवृत्ति पा सकता हूं? पाइथन के साथ मूल रूप से this

उदाहरण: इस के बजाय:

>>> word_list.count('inflation') 
3 
>>> word_list.count('jobs') 
5 
>>> word_list.count('output') 
1 

मैं ऐसा करना चाहते हैं (मैं जानता हूँ कि यह असली कोड नहीं है, यह है कि मैं क्या पर मदद के लिए पूछ रहा हूँ है):

>>> list1='inflation', 'jobs', 'output' 
>>>word_list.count(list1) 
'inflation', 'jobs', 'output' 
3, 5, 1 

शब्दों की मेरी सूची में 10-20 शब्द होने जा रहे हैं, इसलिए मुझे केवल पायथन को शब्दों की सूची की ओर ध्यान देने में सक्षम होना चाहिए। यह भी अच्छा होगा अगर उत्पादन स्तंभों के रूप में शब्दों और पंक्तियों

के रूप में आवृत्तियों के साथ एक एक्सेल स्प्रेडशीट में नकल हो + पेस्ट करने में सक्षम था

उदाहरण:

inflation, jobs, output 
3, 5, 1 

और अंत में, किसी को भी मदद कर सकते हैं के लिए इस को स्वचालित सभी टेक्स्टफाइल? मुझे लगता है कि मैं सिर्फ पायथन को फ़ोल्डर की ओर इंगित करता हूं और यह 360+ टेक्स्ट फ़ाइलों में से प्रत्येक के लिए नई सूची से उपर्युक्त शब्द को गिन सकता है। काफी आसान लगता है, लेकिन मैं थोड़ा फंस गया हूँ। कोई मदद?

इस प्रकार की उत्पादन शानदार होगा: Filename1 मुद्रास्फीति, रोजगार, उत्पादन 3, 5, 1

Filename2 
inflation, jobs, output 
7, 2, 4 

Filename3 
inflation, jobs, output 
9, 3, 5 

धन्यवाद!

उत्तर

14

collections.Counter() यदि यह आपकी समस्या को समझता है तो यह कवर किया गया है।

दस्तावेज़ों का उदाहरण आपकी समस्या से मेल खाता प्रतीत होता है।

# Tally occurrences of words in a list 
cnt = Counter() 
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: 
    cnt[word] += 1 
print cnt 


# Find the ten most common words in Hamlet 
import re 
words = re.findall('\w+', open('hamlet.txt').read().lower()) 
Counter(words).most_common(10) 

आप ऊपर के उदाहरण से ऐसा करने में सक्षम होना चाहिए:

import re 
import collections 
words = re.findall('\w+', open('1976.03.txt').read().lower()) 
print collections.Counter(words) 

संपादित अनुभवहीन दृष्टिकोण एक ही रास्ता दिखाने के लिए।

wanted = "fish chips steak" 
cnt = Counter() 
words = re.findall('\w+', open('1976.03.txt').read().lower()) 
for word in words: 
    if word in wanted: 
     cnt[word] += 1 
print cnt 
+0

मैं अब कई घंटों तक काउंटर के साथ बेवकूफ बनाना किया गया है, और अभी भी यह नहीं मिल सकता है। – CoS

+0

उपरोक्त उदाहरण मुझे मेरे टेक्स्टफाइल (मेरे मामले में 3000 से अधिक अद्वितीय शब्द) के सभी अद्वितीय शब्दों का मिलान करने जा रहा है। मुझे केवल टेक्स्टफाइल में 10-20 विशिष्ट शब्दों के लिए टैली की आवश्यकता है। – CoS

+0

मुझे लगता है कि सूची के लिए काम करेगा, बहुत धन्यवाद! मैंने काउंटर पेज पर घंटों के लिए घंटे – CoS

4

एक संभावित क्रियान्वयन (काउंटर का प्रयोग करके) ...

इसके बजाय उत्पादन मुद्रण की, मुझे लगता है कि यह आसान हो एक csv फ़ाइल के लिए लिखने और आयात कि एक्सेल में करने के लिए होगा। http://docs.python.org/2/library/csv.html पर देखें और print_summary को प्रतिस्थापित करें।

import os 
from collections import Counter 
import glob 

def word_frequency(fileobj, words): 
    """Build a Counter of specified words in fileobj""" 
    # initialise the counter to 0 for each word 
    ct = Counter(dict((w, 0) for w in words)) 
    file_words = (word for line in fileobj for word in line.split()) 
    filtered_words = (word for word in file_words if word in words) 
    return Counter(filtered_words) 


def count_words_in_dir(dirpath, words, action=None): 
    """For each .txt file in a dir, count the specified words""" 
    for filepath in glob.iglob(os.path.join(dirpath, '*.txt')): 
     with open(filepath) as f: 
      ct = word_frequency(f, words) 
      if action: 
       action(filepath, ct) 


def print_summary(filepath, ct): 
    words = sorted(ct.keys()) 
    counts = [str(ct[k]) for k in words] 
    print('{0}\n{1}\n{2}\n\n'.format(
     filepath, 
     ', '.join(words), 
     ', '.join(counts))) 


words = set(['inflation', 'jobs', 'output']) 
count_words_in_dir('./', words, action=print_summary) 
+0

उपरोक्त कौन से चर को बदलने की आवश्यकता है? मुझे अपनी विशिष्ट निर्देशिका में कहां रखना है? – CoS

+0

रॉब, क्या आप कृपया मुझे बता सकते हैं कि उपर्युक्त कोड में मुझे निर्देशिका फ़ोल्डर को रखना चाहिए जिसमें मैं काम कर रहा हूं और उन शब्दों की सूची जिनमें मुझे रूचि है? मुझे यकीन नहीं है कि मुझे आपके द्वारा परिभाषित 3 कार्यों में क्या करना है। – CoS

+1

निर्देशिका की पथ जिसे आप संसाधित करना चाहते हैं वह फ़ंक्शन 'count_words_in_dir()' का पहला तर्क है। कोड की अंतिम पंक्ति देखें। लक्ष्य शब्दों का आपका सेट एक ही फ़ंक्शन के लिए दूसरा तर्क है। अंतिम रेखा देखें। –

0

एक साधारण कार्यात्मक कोड एक पाठ फ़ाइल में शब्द आवृत्तियों गिनती करने के लिए:

{ 
import string 

def process_file(filename): 
hist = dict() 
f = open(filename,'rb') 
for line in f: 
    process_line(line,hist) 
return hist 

def process_line(line,hist): 

line = line.replace('-','.') 

for word in line.split(): 
    word = word.strip(string.punctuation + string.whitespace) 
    word.lower() 

    hist[word] = hist.get(word,0)+1 

hist = process_file(filename) 
print hist 
}