Given an array:
arr = ['Get your face right in there.', 'You’re like, wait, though, isn’t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that’s exactly why we like to hang out together and get wild about all the foods. That’s this lasagna florentine.']
I want to get a string from the array with elements with a total of 15 words or more, in it. So, since the first element does no have 15 words, the output will go over to the next element, and will output the whole array (in this case). ex. If the first element has 12 words and second has 20, the output string will have 12+20 = 32 words.
Here's my code:
def CleanText(text):
text = str(text)
forbidden = [r'
', r'', r'.', r'?', r'!', r'(', r')', r'/', r'<', r'>', '\', '\r', '</div>', '</a>']
for i in forbidden:
text.replace(i, '')
return text
def ExtractText(arr):
for i in arr:
#return i
if len(i) >= 15:
return CleanText(i)
elif len(arr[i]) < 15:
attachedString = CleanText(arr[i+1])
return CleanText(arr[i]) + attachedString
arr = ['Get your face right in there.', 'You’re like, wait, though, isn’t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that’s exactly why we like to hang out together and get wild about all the foods. That’s this lasagna florentine.']
print(ExtractFirstPara(arr))
Output: Get your face right in there.
However, it comes alright when I don't generalize it. That is, when I explicitly say this:
def ExtractText(arr):
for i in arr[0]:
if len(i) >= 15:
return CleanText(arr[0])
else:
attachedString = CleanText(arr[1])
return CleanText(arr[0]) + attachedString
Output:
Get your face right in there.You’re like, wait, though, isn’t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that’s exactly why we like to hang out together and get wild about all the foods. That’s this lasagna florentine.
However, in certain cases, where a string's length with the first two elements does not add up to 15, this explicitness (if that's even a word) is not possible.
For this purpose, I need to generalize.