Dear Sir, Thanx for your prompt reply, I would be trying to work on your suggestion and get back to you as soon as possible. Best Regards, Subhabrata.
On Sun, May 3, 2009 at 10:47 PM, Chris Rebert <c...@rebertia.com> wrote: > On Sun, May 3, 2009 at 9:51 AM, SUBHABRATA BANERJEE > <subhakolkata1...@gmail.com> wrote: > > Dear Group, > > > > > > > > I am working on a code like the following: > > > > > > > > from decimal import* > > > > #SAMPLE TEST PROGRAM FOR FILE > > > > def sample_file_test(n): > > > > #FILE FOR STORING PROBABILITY VALUES > > > > open_file=open("/python26/Newfile1.txt","r+") > > Is there a reason you must output the results to the same file the > input came from? It's possible this is part of your problems. > > > > > #OPENING OF ENGLISH CORPUS > > > > open_corp_eng=open("/python26/TOTALENGLISHCORPUS1.txt","r") > > > > #READING THE ENGLISH CORPUS > > > > corp_read=open_corp_eng.read() > > > > #CONVERTING THE CORPUS FILE IN WORDS > > > > corp_word=corp_read.split() > > > > #EXTRACTING WORDS FROM CORPUS FILE OF WORDS > > > > for word in corp_word: > > > > #COUNTING THE WORD > > > > count1=corp_word.count(word) > > Note: Your program is currently O(N^2) rather than O(N) because you > re-count the number of occurrences of each word /on every occurrence > of the word/. > > > #COUNTING TOTAL NUMBER OF WORDS > > > > count2=len(corp_word) > > > > #COUNTING PROBABILITY OF WORD > > > > count1_dec=Decimal(count1) > > > > count2_dec=Decimal(count2) > > > > getcontext().prec = 6 > > > > prob_count=count1_dec/count2_dec > > > > print prob_count > > > > string_of_prob_count=str(prob_count) > > > > file_input_val=open_file.write(string_of_prob_count) > > > > open_file.close() > > You shouldn't be closing the file until the /entire loop/ has finished > writing to the file. So the previous line should be dedented. > > > > > > > > > The problems I am getting: > > > > (i) The probability values are not being stored properly > in > > file. > > Also, you're currently not putting any separator between consecutive > entires, so it's all going to run together as one long line. > Have you considered using one of the std lib modules to output the > file in a well-defined human-readable format such as JSON or CSV? > > > (ii) “Newfile1.txt” is storing not the series of values > but > > an arbitrary value from series 0.00000143096 > > > > (iii) As I was testing it again it gave me another error > > > > Traceback (most recent call last): > > > > File "<pyshell#2>", line 1, in <module> > > > > sample_file_test(1) > > > > File "C:\Python26\testprogramforfiles1.py", line 25, in > sample_file_test > > > > file_input_val=open_file.write(string_of_prob_count) > > > > ValueError: I/O operation on closed file > > > Cheers, > Chris > -- > http://blog.rebertia.com >
-- http://mail.python.org/mailman/listinfo/python-list