Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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_Nlp - Fatal编程技术网

如何在python中分离混合词(波斯语和英语)

如何在python中分离混合词(波斯语和英语),python,nlp,Python,Nlp,您好,我有一个字符串数据集,有些字符串有混合词,如下所示: سلام12World دوربینdigital سال2012good 。。。 我想要的结果是: 12 سلام world دوربین digital 2012 سال good 这是我的密码: def spliteKeyWord(str): regex = r"[\u200b-\u200c]|[0-9]+|[a-zA-Z]+\'*[a-z]*" matches

您好,我有一个字符串数据集,有些字符串有混合词,如下所示:

    سلام12World
    دوربینdigital
    سال2012good
。。。 我想要的结果是:

   12 سلام world
   دوربین digital
   2012 سال good
这是我的密码:

 def spliteKeyWord(str):
     regex = r"[\u200b-\u200c]|[0-9]+|[a-zA-Z]+\'*[a-z]*"
     matches = re.findall(regex, str, re.UNICODE)
     return matches

但是这段代码没有显示我想要的输出。有可能获得类似的输出吗?

您可以使用带有交替模式的
re.findall

def spliteKeyWord(s):
    return re.findall(r'[\dA-Za-z]+|[^\dA-Za-z\W]+', s, re.UNICODE)
引用时,可以使用此正则表达式解析非ascii字符:

words = ['12سلامWorld','دوربینdigital','2012سالgood']

for w in words:
    re.split(r'([^\x00-\x7F]+)', w)


# ['12', 'سلام', 'World']
# ['', 'دوربین', 'digital']
# ['2012', 'سال', 'good']

这将在非ascii字之间分割所有内容。

谢谢@blhsing,此函数对数据帧的文本列有效吗?我相信
pandas
中的
str.methods
支持regex,我测试了这个函数是正确的,但是当有一个单词“iPhone6”这个函数把这个单词转换成“iPhone”、“6'@ GETDATA”时,我更新了我的答案,以便数字和英文字母组合在一起。亲爱的BLHEY,你的代码不考虑标点符号。