#python #pandas
#python #панды
Вопрос:
У меня есть столбец отзывов, и я хочу разбить каждый отзыв на предложения. Я также хочу, чтобы предложения представляли собой список в одной строке. Прямо сейчас, с кодом, который я использую, я должен указать строку, однако я хотел бы, чтобы код мог выполнять итерации по каждой строке, содержащей обзор. Это большой набор данных (примерно 75000 строк, и каждый обзор содержит около 4-10 предложений).
Я попытался добавить «для строки в df.iterrows ():» выше «для текста в столбце», однако это не работает.
Я также включил пример обзоров, которые я использую: example_reviews
import re
alphabets= "([A-Za-z])"
prefixes = "(Mr|St|Mrs|Ms|Dr|Prof|Capt|Cpt|Lt|Mt)[.]"
suffixes = "(Inc|Ltd|Jr|Sr|Co)"
starters = "(Mr|Mrs|Ms|Dr|Hes|Shes|Its|Theys|Theirs|Ours|Wes|Buts|Howevers|Thats|Thiss|Wherever)"
acronyms = "([A-Z][.][A-Z][.](?:[A-Z][.])?)"
websites = "[.](com|net|org|io|gov|me|edu)"
digits = "([0-9])"
def split_into_sentences1(column):
for text in column:
text = " " text " "
text = text.replace("n"," ")
text = re.sub(prefixes,"\1<prd>",text)
text = re.sub(websites,"<prd>\1",text)
text = re.sub(digits "[.]" digits,"\1<prd>\2",text)
if "Ph.D" in text: text = text.replace("Ph.D.","Ph<prd>D<prd>")
text = re.sub("s" alphabets "[.] "," \1<prd> ",text)
text = re.sub(acronyms " " starters,"\1<stop> \2",text)
text = re.sub(alphabets "[.]" alphabets "[.]" alphabets "[.]","\1<prd>\2<prd>\3<prd>",text)
text = re.sub(alphabets "[.]" alphabets "[.]","\1<prd>\2<prd>",text)
text = re.sub(" " suffixes "[.] " starters," \1<stop> \2",text)
text = re.sub(" " suffixes "[.]"," \1<prd>",text)
text = re.sub(" " alphabets "[.]"," \1<prd>",text)
if "e.g." in text: text = text.replace("e.g.","e<prd>g<prd>")
if "i.e." in text: text = text.replace("i.e.","i<prd>e<prd>")
if "..." in text: text = text.replace("...","<prd><prd><prd>")
if "”" in text: text = text.replace(".”","”.")
if """ in text: text = text.replace("."","".")
if "!" in text: text = text.replace("!"",""!")
if "?" in text: text = text.replace("?"",""?")
text = text.replace(".",".<stop>")
text = text.replace("?","?<stop>")
text = text.replace("!","!<stop>")
text = text.replace("<prd>",".")
sentences = text.split("<stop>")
sentences = sentences[:-1]
sentences = [s.strip() for s in sentences]
return sentences
Ответ №1:
Попробуйте применить
df[[review]].apply(split_into_sentences1, axis=1)