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|He\s|She\s|It\s|They\s|Their\s|Our\s|We\s|But\s|However\s|That\s|This\s|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
解决方案
尝试申请
df[[review]].apply(split_into_sentences1, axis=1)
推荐阅读
- ruby-on-rails - Rails 教程 SampleApp 在本地运行良好,但在 heroku 上启动时崩溃
- typescript - Typescript 避免使用泛型导出类型
- c++ - 第一个使用 cmake 和 GoogleTest (gtest) 的项目
- snowflake-cloud-data-platform - 通过雪花外部表加载多个文件
- blueprintjs - BlueprintJS 覆盖/门户未正确关闭
- c - C整数隐式转换
- http - 通过网站启动 Google Cloud VM 实例
- c - 程序不读取/不让在 fgets 语句中输入任何内容
- node.js - 如何将对象从此处导出到另一个文件?
- android - Nativewscript-vue graphql:TypeError:无法读取未定义的属性“地图”