-1

I am trying to figure out how to split a sentence in python based on the first word of the sentence. So like if the word is 'Find' it should be stored in a different variable than if the starting word is 'Consider'. I tried using a loop and checking the first but can't figure out a way to extract the first word. I am new to python so any help would be appreciated! Thanks in advance

Example text:

Consider a thin‐walled, metallic tube of length L = 1 m and inside diameter Di = 3 mm. Water enters the tube at ˙ m = 0.015 kg / s m˙=0.015 kg/s and Tm,i = 97°C. a. What is the outlet temperature of the water if the tube surface temperature is maintained at 27°C? ( b. If a 0.5‐mm‐thick layer of insulation of k = 0.05 W/m-K is applied to the tube and its outer surface is maintained at 27°C, what is the outlet temperature of the water? c. If the outer surface of the insulation is no longer maintained at 27°C but is allowed to exchange heat by free convection with ambient air at 27°C, what is the outlet temperature of the water? The free convection heat transfer coefficient is 5 W/m2 -K.

Will Da Silva
  • 6,386
  • 2
  • 27
  • 52
Asit Singh
  • 17
  • 2
  • 3
    It's not really clear what you hope to have at the end of this. Do you want a dictionary of first words referencing sentences or something else? – Mark Jun 13 '21 at 20:29

1 Answers1

2

We will first split the text into sentences using the code from this question: Splitting and saving parts of a input text

Next we create a defaultdict of lists. We can then index into it with the first word from each sentence, and append that sentence. This lets us collect sentences by their first word. Some normalization (such as lowercasing the first word when indexing the defaultdict) might be advisable.

from collections import defaultdict
import re

s = 'Consider a thin‐walled, metallic tube of length L = 1 m and inside diameter Di = 3 mm. Water enters the tube at ˙ m = 0.015 kg / s m˙=0.015 kg/s and Tm,i = 97°C. a. What is the outlet temperature of the water if the tube surface temperature is maintained at 27°C? ( b. If a 0.5‐mm‐thick layer of insulation of k = 0.05 W/m-K is applied to the tube and its outer surface is maintained at 27°C, what is the outlet temperature of the water? c. If the outer surface of the insulation is no longer maintained at 27°C but is allowed to exchange heat by free convection with ambient air at 27°C, what is the outlet temperature of the water? The free convection heat transfer coefficient is 5 W/m2 -K.'

d = defaultdict(list)
for x in re.split(r'[\.!?]\s', s):
    d[x.split(' ')[0]].append(x)

We can then fetch all of the sentences beginning with "Consider" or "Find" like so:

>>> d['Consider']
['Consider a thin‐walled, metallic tube of length L = 1 m and inside diameter Di = 3 mm']
>>> d['Find']
[]
Will Da Silva
  • 6,386
  • 2
  • 27
  • 52