首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python极性regex -删除非英语,保留数字标点符号和表情符号

Python极性regex -删除非英语,保留数字标点符号和表情符号
EN

Stack Overflow用户
提问于 2022-03-14 13:16:38
回答 1查看 367关注 0票数 0

我有用于这项任务的python代码。

代码语言:javascript
复制
import re
import string

emoji_pat = '[\U0001F300-\U0001F64F\U0001F680-\U0001F6FF\u2600-\u26FF\u2700-\u27BF]'
shrink_whitespace_reg = re.compile(r'\s{2,}')

def clean_text(raw_text):
    reg = re.compile(r'({})|[^a-zA-Z0-9 -{}]'.format(emoji_pat,r"\\".join(list(string.punctuation)))) # line a
    result = reg.sub(lambda x: ' {} '.format(x.group(1)) if x.group(1) else ' ', raw_text)
    return shrink_whitespace_reg.sub(' ', result).lower()

我试着用极地polars.internals.series.StringNameSpace.contains

代码语言:javascript
复制
But I got an exceptions 
ComputeError: regex error: Syntax(

regex parse error:
    ([--☀-⛿✀-➿])|[^a-zA-Z0-9 -!\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\]\\^\\_\\`\\{\\}\\~]
                     ^^
error: unclosed character class

汉语、英语和未知的例子

代码语言:javascript
复制
texts = ['水虫対策にはコレが一番ですね','','I love polars!-ã„ã¤ã‚‚ã•らã•ら.','So good .']
df = pd.DataFrame({'text':texts})

d = df.text.apply(clean_text)

预期:

代码语言:javascript
复制
0                    
1                  
2    i love polars! .
3         so good  .
Name: text, dtype: object

另一个问题:

它比使用re快吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-21 16:02:18

代码语言:javascript
复制
import polars as pl

emoji_pat = "[\U0001F300-\U0001F64F\U0001F680-\U0001F6FF\u2600-\u26FF\u2700-\u27BF]"

texts = ['水虫対策にはコレが一番ですね','','I |love|  polars!-ã„ã¤ã‚‚ã•らã•ら.','So good       .']

df = pl.DataFrame(pl.Series("text", texts))

In [78]: df
Out[78]:
shape: (4, 1)
┌─────────────────────────────────────┐
│ text                                │
│ ---                                 │
│ str                                 │
╞═════════════════════════════════════╡
│ 水虫対策にはコレが一番ですね        │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│                                 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ I |love|  polars!-ã„ã¤ã‚‚ã•らã•... │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ So good       .                   │
└─────────────────────────────────────┘

# Add cleaned column (rust regex requires "[" inside [] to be escaped).
df_cleaned = df.with_column(
    pl.col("text").str.replace_all(
        "[^a-zA-Z0-9 " + string.punctuation.replace("[", "\[") + emoji_pat + "]+",
        ""
    ).str.replace_all(
        "\s{2,}", " "
    ).str.to_lowercase().alias("text_cleaned")
)

In[79]: df_cleaned
Out[79]:
shape: (4, 2)
┌─────────────────────────────────────┬────────────────────┐
│ text                                ┆ text_cleaned       │
│ ---                                 ┆ ---                │
│ str                                 ┆ str                │
╞═════════════════════════════════════╪════════════════════╡
│ 水虫対策にはコレが一番ですね        ┆                    │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│                                 ┆                │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ I |love|  polars!-ã„ã¤ã‚‚ã•らã•... ┆ i |love| polars!-. │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ So [good]       .                 ┆ so [good]  .     │
└─────────────────────────────────────┴────────────────────┘
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71468399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档