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