提高清洁功能性能

发布于 2025-01-10 06:21:28 字数 1188 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文