the objective is to extract from the text the sentences before the word Definition or indications
def extract(doc):
if( len(doc) != 0 ):
if ('Definition' in doc):
sentence = doc.split('Definition')[0]
elif ('Definition' not in doc and 'indications' in doc):
sentence = doc.split('indications')[0]
return sentence
else :
return doc
It return error : UnboundLocalError: local variable 'sentence' referenced before assignment
CodePudding user response:
You can try the below code.This is a simple method.we can just print the output just after the if and and elif statement.And we can also add an else statement to print if there is no word "Dosage" and "Contradictions" in the sentence.
def just_indication(doc):
if( len(doc) != 0 ):
if ('Dosage' in doc):
sentence = doc.split('Dosage')[0]
print(sentence)
elif ('Dosage' not in doc and 'Contraindications' in doc):
sentence = doc.split('Contraindications')[0]
print(sentence)
else:
print("Error! word Dosage and Contraindications not present in doc")
else:
print("doc empty")
just_indication("this is the sentence which has the word Contraindications in it")
CodePudding user response:
I think it's because you declare "sentence" in an if statment, try declar it right after your def, like this :
def just_indication(doc):
sentence = ""
if( len(doc) != 0 ):
if ('Dosage' in doc):
sentence = doc.split('Dosage')[0]
elif ('Dosage' not in doc and 'Contraindications' in doc):
sentence = doc.split('Contraindications')[0]
return sentence
else :
return doc