I've the following text:
text_main = "The following leagues were identified: sport: basketball league: N.B.A. style: c.123>d sport: soccer league: E.P.L. sport: football league: N.F.L. style: c.124>d. The other leagues aren't that important."
I need to extract all the sport names (which are coming after sport:
) and style (which are coming after style:
) and create new columns as sports
and style
. I'm trying the following code to extract the main sentence (sometimes text are huge):
m = re.split(r'(?<=\.)\s+(?=[A-Z]\w+)', text_main)
text = list(filter(lambda x: re.search(r'leagues were identified', x, flags=re.IGNORECASE), m))[0]
print(text)
The following leagues were identified: sport: basketball league: N.B.A. style: c.123>d sport: soccer league: E.P.L. sport: football league: N.F.L. style: c.124>d.
Then I'm extracting the sport and style names and putting them into a dataframe:
if 'sport:' in text:
sport_list = re.findall(r'sport:\W*(\w+)', text)
df = pd.DataFrame({'sports': sport_list})
print(df)
sports
0 basketball
1 soccer
2 football
However, I'm having troubles to extract the styles, as all the styles have period .
after the 1st letter (c
) and few has sign >
. Also, not all the sports have style info.
Desired output:
sports style
0 basketball c.123>d
1 soccer NA
2 football c.124>d
What would be the smartest way of doing it? Any suggestions would be appreciated. Thanks!