提高清洁功能性能
我对 python 相当陌生,我试图了解什么是紧固清洁功能的最佳方法。 (我可以使用多处理吗?异步函数?将此函数拆分为较小的函数并使用多线程?)
def clean(s):
"""
:param s: string to be processed
:return: processed string: see comments in the source code for more info
"""
s = re.sub(r'([a-z])([A-Z])', r'\1\. \2', s) # before lower case
s = s.lower()
s = re.sub(r'etc\.?','',s)
s = re.sub(r'\(.*?\)', ' ', s)
s = re.sub(r'(on?\s?\d+\s?\.?\:?\d*\s?am)|(on?\s?\d+\s?\.?\:?\d*\s?pm)','',s)
s = re.sub(r'(at?\s?\d+\s?\.?\:?\d*\s?am)|(at?\s?\d+\s?\.?\:?\d*\s?pm)','',s)
s = re.sub(r'[^\D+\s?]', ' ',s)
s = re.sub(r'^\-\s?', '',s)
s = re.sub(r'[$€£]+', '',s)
s = re.sub(r'(\s+\-\s+)', ' ',s)
s = re.sub(r'(\,+\s?\,?)', ',',s)
s = re.sub(r'\#+', ' ',s)
s = re.sub(r'[.!]+', '.',s)
s = re.sub(r'\s?\&+\s?', ' and ', s)
s = re.sub(r'([a-z])\1{2,}', r'\1', s)
s = re.sub(r'([\W+])\1{1,}', r'\1', s)
s = re.sub(r'\*|\W\*|\*\W', '', s)
s = re.sub(r'\W+?\.', '.', s)
s = re.sub(r'(\.|\?|!)(\w)', r'\1 \2', s)
s = re.sub(r'(.{2,}?)\1{1,}', r'\1', s)
s = re.sub(r'\s{2,}', ' ', s)
s = re.sub(r'\([^)]*\)','',s)
return s.strip()
df['Sentences'].apply(clean)
感谢您的帮助
I'm fairly new to python and I'm trying to understand what is the best approach to fasten a cleaning function. (can I use multiprocessing? asynchronous functions? split this function in smaller functions and use multithreading?)
def clean(s):
"""
:param s: string to be processed
:return: processed string: see comments in the source code for more info
"""
s = re.sub(r'([a-z])([A-Z])', r'\1\. \2', s) # before lower case
s = s.lower()
s = re.sub(r'etc\.?','',s)
s = re.sub(r'\(.*?\)', ' ', s)
s = re.sub(r'(on?\s?\d+\s?\.?\:?\d*\s?am)|(on?\s?\d+\s?\.?\:?\d*\s?pm)','',s)
s = re.sub(r'(at?\s?\d+\s?\.?\:?\d*\s?am)|(at?\s?\d+\s?\.?\:?\d*\s?pm)','',s)
s = re.sub(r'[^\D+\s?]', ' ',s)
s = re.sub(r'^\-\s?', '',s)
s = re.sub(r'[$€£]+', '',s)
s = re.sub(r'(\s+\-\s+)', ' ',s)
s = re.sub(r'(\,+\s?\,?)', ',',s)
s = re.sub(r'\#+', ' ',s)
s = re.sub(r'[.!]+', '.',s)
s = re.sub(r'\s?\&+\s?', ' and ', s)
s = re.sub(r'([a-z])\1{2,}', r'\1', s)
s = re.sub(r'([\W+])\1{1,}', r'\1', s)
s = re.sub(r'\*|\W\*|\*\W', '', s)
s = re.sub(r'\W+?\.', '.', s)
s = re.sub(r'(\.|\?|!)(\w)', r'\1 \2', s)
s = re.sub(r'(.{2,}?)\1{1,}', r'\1', s)
s = re.sub(r'\s{2,}', ' ', s)
s = re.sub(r'\([^)]*\)','',s)
return s.strip()
df['Sentences'].apply(clean)
Thanks for your Help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论