如何清除包含“句点”的缩写(例如“。”,“ st。”,“ rd。”),但保留“。”在句子结尾吗?

问题描述 投票:1回答:1

我正在R中处理句子级LDA,目前正在尝试使用sent_detect()包中的openNLP函数将我的文本数据拆分为单个句子。

但是,我的文本数据包含很多缩写,带有“句号”,但是not标记句子的结尾。以下是一些示例:“ st。帕特里克节”,“牛津st。”,“蓝色rd。”,“ 例如

是否存在创建gsub()函数以解决此类2个字符的缩写并删除其“。”符号的方式,以便sent_detect()函数不会错误地检测到该符号?不幸的是,这些缩写并不总是在两个词之间,但有时它们确实也可以标记一个句子的结尾:

示例:

“我真的很喜欢牛津街。”-“街区”。标记句子的结尾和“。”应该保留。

vs

“牛津街很忙。”-“街区”。不位于句子末尾,因此应替换“。”符号。

我不确定是否有解决方案,但是也许其他更熟悉句子级分析的人知道如何处理此类问题的方法。谢谢!

r regex text-mining topic-modeling
1个回答
0
投票

考虑到您之前提出的问题,建议您调查一下textclean软件包。该软件包中包含很多您想要的内容。任何丢失的功能都可以使用,重用或扩展。

只需替换“ st。”可能会导致出现问题,因为这可能意味着出现街道或圣人,但“圣帕特里克节”很容易找到。您将要面临的问题是列出可能发生的情况,并找到可能的替代方法。最容易使用的是转换表。在下面,我为一些缩写及其预期的长名称创建一个表格。现在,由您(或您的客户)决定最终结果所需的内容。最好的方法是在excel或数据库中创建一个表并将其加载到data.frame中(并存储在易于访问的位置)。根据您的文字,这可能需要很多工作,但可以提高结果的质量。

示例:

library(textclean)

text <- c("I really liked Oxford st.", "Oxford st. was very busy.",
          "e.g. st. Patricks day was on oxford st. and blue rd.")


# Create abbreviations table, making sure that we are looking for rd. and not just rd. Also should it be road or could it mean something else?

abbreviations <- data.frame(abbreviation = c("st. patricks day", "oxford st.", "rd\\.", "e.g."),
                            replacement = c("saint patricks day","oxford street","road", "eg"))


# I use the replace_contraction function since you can replace the default contraction table with your own table.

text <- replace_contraction(text, abbreviations)

text
[1] "I really liked oxford street"                             "oxford street was very busy."                            
[3] "eg saint patricks day was on oxford street and blue road"

# as the result from above show missing end marks we use the following function to add them again.

text <- add_missing_endmark(text, ".")

text
[1] "I really liked oxford street."                             "oxford street was very busy."                             
[3] "eg saint patricks day was on oxford street and blue road."

textclean具有一系列的replace_zzz函数,大多数基于软件包中的mgsub函数。查看所有功能的文档,以了解它们的功能。

© www.soinside.com 2019 - 2024. All rights reserved.