How to compose each word in the dataframe into a sentence, and generate the next sentence after the period or question mark?

Viewed 39

How to compose each word in the dataframe into a sentence, and generate the next sentence after the period or a question mark?

the original dataframe looks like this:

start_time  end_time words

0.1           0.2     I

0.3           0.4     AM

0.5           0.6     GOOD.

0.7           0.8     HOW

0.9           1.0     ABOUT

1.1           1.2     YOU?

1.3           1.4      OK!

the result I want to get looks like this:

start_time  end_time   words

0.1          0.6     I AM GOOD.

0.7          1.2     HOW ABOUT YOU?

1.3          1.4     OK!

This is my dataframe:

    data = {'start_time': [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3],
       'end_time': [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4],
       'word':['I','AM','OK.','HOW', 'ABOUT', 'YOU?','OK!']}
df = pd.DataFrame(data, columns = ['start_time', 'end_time','word'])

Is there any suggested algorithm to help this problem, thank you very much!

1 Answers

Try:

import re

pattern = re.compile(r"\.|\!|\?$")

df_out = df.groupby(
    df.word.apply(lambda x: bool(pattern.search(x))).shift().fillna(0).cumsum()
).agg({"start_time": "first", "end_time": "last", "word": " ".join})
print(df_out)

Prints:

      start_time  end_time            word
word                                      
0            0.1       0.6        I AM OK.
1            0.7       1.2  HOW ABOUT YOU?
2            1.3       1.4             OK!
Related