python - 如何优雅地从字符串中删除长度为 n 的椭圆(NLP with spacy)?
问题描述
我目前正在对这个垃圾短信数据集进行数据清理。这些短信中有很多省略号,例如:
mystr = 'Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...'
如您所见,有 2 个句点 ( ..
) 或 3 个句点 ( ...
)的椭圆
我最初的解决方案是编写一个函数spacy_tokenizer
来标记我的字符串,删除停用词和标点符号:
import spacy
nlp = spacy.load('en_core_web_sm')
from nltk.corpus import stopwords
stopWords = set(stopwords.words('english'))
print(stopWords)
import string
punctuations = string.punctuation
def spacy_tokenizer(sentence):
# Create token object
mytokens = nlp(sentence)
# Case normalization and Lemmatization
mytokens = [ word.lemma_.lower() if word.lemma_ != "-PRON-" else word.lower_ for word in mytokens ]
# Remove stop words and punctuations
mytokens = [ word.strip(".") for word in mytokens if word not in stopWords and word not in punctuations ]
# return preprocessed list of tokens
return mytokens
然而,这个函数并没有去掉省略号
IN: print(spacy_tokenizer(mystr))
OUT: ['go', 'jurong', 'point', 'crazy', '', 'available', 'bugis', 'n', 'great', 'world', 'la', 'e', 'buffet', '', 'cine', 'get', 'amore', 'wat', '']
如您所见,有些标记len(token) = 0
显示为''
我的解决方法是添加另一个列表理解spacy_tokenizer
,看起来像这样:[ word for word in mytokens if len(word) > 0]
def spacy_tokenizer(sentence):
# Create token object
mytokens = nlp(sentence)
# Case normalization and Lemmatization
mytokens = [ word.lemma_.lower() if word.lemma_ != "-PRON-" else word.lower_ for word in mytokens ]
# Remove stop words and punctuations
mytokens = [ word.strip(".") for word in mytokens if word not in stopWords and word not in punctuations ]
# remove empty strings
mytokens = [ word for word in mytokens if len(word) > 0]
return mytokens
IN: print(spacy_tokenizer(mystr))
OUT: ['go', 'jurong', 'point', 'crazy', 'available', 'bugis', 'n', 'great', 'world', 'la', 'e', 'buffet', 'cine', 'get', 'amore', 'wat']
所以新函数给出了预期的结果,但这不是我认为的最优雅的解决方案。有没有人有任何替代的想法?
解决方案
这将删除 2 或 3 个周期的椭圆:
import re
regex = r"[.]{2,3}"
test_str = "Go until jurong point, crazy.. Available only. in bugis n great world la e buffet... Cine there got amore wat..."
subst = ""
result = re.sub(regex, subst, test_str)
if result:
print (result)
如果你愿意,你也可以在这里玩。
推荐阅读
- ios - 错误:react-native-permissions: NativeModule.RNPermissions 为空。尝试使用 react-native-qrcode-scanner
- docker - 从 Anaconda3 Docker 容器运行 Spyder
- c++ - 如何适当地用 cmake 解决编译情况?
- windows - 使用 miniconda3 (Anaconda Prompt) 在 Windows 中安装时如何在后台运行 apache-superset
- ios - 'AppDelegate' 没有可见的@interface 声明
- java - 将 Spring Boot 从 1.5.3 升级到 2.3.4 时出现 `mongoMappingContext` 错误
- github - 在完成合并之前,我可以要求检查“签字”复选框吗?
- flutter - 如何在注册表单中添加图像
- javascript - Kendo JS:在网格上调用 setOptions 而不会触发数据的重新加载
- java - 无法关闭 Java 确认子窗口 (Vaadin 8)