Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将许多正则表达式操作组合在一起_Python_Regex_Nlp_Text Processing - Fatal编程技术网

Python 将许多正则表达式操作组合在一起

Python 将许多正则表达式操作组合在一起,python,regex,nlp,text-processing,Python,Regex,Nlp,Text Processing,我正在使用python进行一个文本处理的NLP项目,在这个项目中,我需要在提取特征之前进行数据清理。 我正在使用正则表达式操作清理字符的特殊字符和数字分隔符,但我在许多操作中单独执行所有这些操作,这会使速度变慢。我想以尽可能少的操作或更快的方式实现 我的代码如下 def remove_special_char(x): if type(x) is str: x = x.replace('-', ' ').replace('(', ',').replace(')', ',')

我正在使用python进行一个文本处理的NLP项目,在这个项目中,我需要在提取特征之前进行数据清理。 我正在使用正则表达式操作清理字符的特殊字符和数字分隔符,但我在许多操作中单独执行所有这些操作,这会使速度变慢。我想以尽可能少的操作或更快的方式实现

我的代码如下

def remove_special_char(x):
    if type(x) is str:
        x = x.replace('-', ' ').replace('(', ',').replace(')', ',')
        x = re.compile(r"\s+").sub(" ", x).strip()
        x = re.sub(r'[^A-Z a-z 0-9-,.x]+', '', x).lower()
        x = re.sub(r"([0-9]+(\.[0-9]+)?)",r" \1 ", x).strip()
        x = x.replace(",,",",")
        return x
    else:
        return x 

有人能帮我吗?

对于各种操作,您有不同的替换字符串,因此无法真正合并它们

不过,您可以预先编译所有regexp,但我怀疑这不会有多大区别:

paren_re=re.compile(r“[()]”)
空格\u re=re.compile(r“\s+”)
标识=重新编译(r“[^A-Za-z0-9-,.x]+”)
数字\u re=re.compile(r“([0-9]+(\.[0-9]+)?)”)
def删除特殊字符(x):
如果存在(x,str):
x=x。替换(“-”,“”)
x=对价子(“,”x)
x=空白字符(“,x)
x=识别子(“,x).lower()
x=编号(r“\1”,x).strip()
x=x。替换(“,”,“,”)
返回x

您是否分析了您的程序以确定这是瓶颈?

对于不同的操作,您有不同的替换字符串,因此无法真正合并它们

不过,您可以预先编译所有regexp,但我怀疑这不会有多大区别:

paren_re=re.compile(r“[()]”)
空格\u re=re.compile(r“\s+”)
标识=重新编译(r“[^A-Za-z0-9-,.x]+”)
数字\u re=re.compile(r“([0-9]+(\.[0-9]+)?)”)
def删除特殊字符(x):
如果存在(x,str):
x=x。替换(“-”,“”)
x=对价子(“,”x)
x=空白字符(“,x)
x=识别子(“,x).lower()
x=编号(r“\1”,x).strip()
x=x。替换(“,”,“,”)
返回x

您是否分析了您的程序以确定这是瓶颈?

除了在函数外部准备编译模式外,您还可以通过对所有一对一或一对无转换使用translate来获得一些性能:

import string
mappings     = {'-':' ', '(':',', ')':','}            # add more mappings as needed
mappings.update({ c:' ' for c in string.whitespace }) # white spaces become spaces
mappings.update({c:c.lower() for c in string.ascii_uppercase}) # set to lowercase
specialChars = str.maketrans(mappings)

def remove_special_char(x):
    x = x.translate(specialChars)
    ...
    return x

除了在函数外部准备编译的模式外,还可以通过对所有一对一或一对无转换使用translate来获得一些性能:

import string
mappings     = {'-':' ', '(':',', ')':','}            # add more mappings as needed
mappings.update({ c:' ' for c in string.whitespace }) # white spaces become spaces
mappings.update({c:c.lower() for c in string.ascii_uppercase}) # set to lowercase
specialChars = str.maketrans(mappings)

def remove_special_char(x):
    x = x.translate(specialChars)
    ...
    return x