Spaces:
Running
Running
File size: 989 Bytes
edbe5dd 7e0f94e edbe5dd ae80c9a edbe5dd ae80c9a ed0a74e ae80c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import nltk
nltk.download("punkt")
nltk.download("stopwords")
import re
from nltk.corpus import stopwords
from pandas import DataFrame
stop_words = stopwords.words("english")
def process(df: DataFrame):
"""Text2KG post-processing."""
drop_list = []
for i, row in df.iterrows():
# remove stopwords (pronouns)
if (row.subject in stop_words) or (row.object in stop_words):
drop_list.append(i)
# remove broken triplets
elif row.hasnans:
drop_list.append(i)
# lowercase nodes/edges, remove articles
else:
article_pattern = r'^(the|a|an) (.+)'
be_pattern = r'^(are|is) (a )?(.+)'
df.at[i, "subject"] = re.sub(article_pattern, r'\2', row.subject.lower())
df.at[i, "relation"] = re.sub(be_pattern, r'\3', row.relation.lower())
df.at[i, "object"] = re.sub(article_pattern, r'\2', row.object.lower())
return df.drop(drop_list) |