KeyboardInterrupt close failed in file object destructor:sys.excepthook is missing lost sys.stderr

J

Jai

Code
#################################

#!/usr/bin/env python


import sys, re




def find_position(line):
pun = ""
if re.search(r"[.?!]+", line):
pun = re.search(r"[.?!]+", line).group()
pos = line.find(pun)
pos = pos+len(pun)-1
return pos




def sentence_splitter(filename):

f = open(filename, "r")

for line in f:
line = line.strip()
print line + "\n"
while line:
pos = find_position(line)
line2 = line[ : pos+1].split(" ")
length = len(line2)
last_word = line2[length -1]

try:
if re.search(r"[A-Z]+.*", last_word) or line[pos+1] != " " or line[pos+2].islower() :
print line[:pos+1],
line = line[pos+1:]

else:
print line[ : pos+1]
line = line[pos+1 :]


except :
print " error here!!"

f.close()
return " bye bye"





if __name__=="__main__":
print sentence_splitter(sys.argv[1])


##########################3

exicution

python sentence_splitter6.py README | more


###############

README

Mr. Smith bought example.cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it. Did he mind? Adam Jones Jr. thinks he didn't. In any case, this isn't true... Well, with a probability of .9 it isn't. The result should be:
~
############################


output

Mr. Smith bought example.cheapsite.com for 1.5 million dollars, i.e. he paid a lot for it. Did he mind? Adam Jones Jr. thinks he didn't.
In any case, this isn't true... Well, with a probability of .9 it isn't. The result should be:

Mr. Smith bought example. cheapsite. com for 1. 5 million dollars, i. e. he paid a lot for it.
Did he mind?
Adam Jones Jr. thinks he didn't.
In any case, this isn't true...
Well, with a probability of . 9 it isn't.

##########################################################3

error

KeyboardInterrupt
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

##########################################


please help what is this i have try lot but unable to remove it
 
N

Neil Cerutti

MOn Tue, Nov 19, 2013 at 10:35 AM, Jai
please help what is this i have try lot but unable to remove it

Your code is getting into an infinite loop.

One problem is, I suspect:
def find_position(line):
pun = ""
if re.search(r"[.?!]+", line):
pun = re.search(r"[.?!]+", line).group()
pos = line.find(pun)
pos = pos+len(pun)-1
return pos

When your search fails, this function will return 0. Fix that.

In general your problem isn't well defined enough for me to make
sense of your algorithm. Can you show sample input and output?
Can you describe the algorithm in plain English?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top