Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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/8/python-3.x/15.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
Regex Python正则表达式-解析字符串并用逗号和;美元_Regex_Python 3.x_Spacy - Fatal编程技术网

Regex Python正则表达式-解析字符串并用逗号和;美元

Regex Python正则表达式-解析字符串并用逗号和;美元,regex,python-3.x,spacy,Regex,Python 3.x,Spacy,我有一个这样的句子- stmt ="Is abc service accessible to sd, rc & odd in XYZ?" 我在字符串中有这样的实体- str_entities= "abc service$Service,sd,rc & odd$Processes, XYZ$Name" 我想将这些实体转换为spacy json格式,例如,其中包含每个实体的单词索引和该实体的标签 (stmt, {"entities": [(3,14, 'Service'),(30

我有一个这样的句子-

stmt ="Is abc service accessible to sd, rc & odd in XYZ?"
我在字符串中有这样的实体-

str_entities= "abc service$Service,sd,rc & odd$Processes, XYZ$Name"
我想将这些实体转换为spacy json格式,例如,其中包含每个实体的单词索引和该实体的标签

(stmt, {"entities": [(3,14, 'Service'),(30, 41, 'Processes'), (46, 48, 'Name')]})
我尝试了下面的正则表达式,但它只适用于一种情况,我想要一个通用的,可以提取任何数量的实体

re.findall("(.*.?)\$(\S+),(.*.?)\$(\S+)", str_entities)

我在许多文件中都有100000行这样的行,希望生成这种格式,以便可以训练自定义空间模型。我无法生成实体,因为逗号正在多次拆分实体,我正在丢失单词以从stmt中查找索引。

我不确定我是否正确理解您的意思,但我会使用

[^ ^,]+[^\$]*\$[^\$^,]+
因此,您可以同时获得字符串和实体,它们仍然与$符号组合在一起。之后,您可以使用for语句遍历元素并获得索引值

enentity_list=[]
for e in re.findall("[^ ^,]+[^\$]*\$[^\$^,]+", str_entities):
    entity_list.append((stmt.index(e.split('$')[0]),stmt.index(e.split('$')[0])+len(e.split('$')[0]),e.split('$')[1]))
json=(stmt,{"entities":entity_list})
你可以试穿一下