python - regexp_tokenize 和阿拉伯文本

标签 python regex nltk

我正在使用 regexp_tokenize()从没有任何标点符号的阿拉伯语文本中返回标记:

import re,string,sys
from nltk.tokenize import  regexp_tokenize

def PreProcess_text(Input):
  tokens=regexp_tokenize(Input, r'[،؟!.؛]\s*', gaps=True)
  return tokens

H = raw_input('H:')
Cleand= PreProcess_text(H)
print  '\n'.join(Cleand) 

它工作正常,但问题是当我尝试打印文本时。

文本 ايمان،سعد 的输出:

    ?يم
    ?ن
    ?
    ?
    ? 

但如果文本是英文的,即使带有阿拉伯标点符号,它也会打印出正确的结果。

文本 hiÌeman 的输出:

     hi
     eman

最佳答案

当您使用 raw_input 时,符号被编码为字节。

你需要将它转换成Unicode字符串

H.decode('utf8')

你可以保留你的正则表达式:

tokens=regexp_tokenize(Input, r'[،؟!.؛]\s*', gaps=True)

关于python - regexp_tokenize 和阿拉伯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39170944/

相关文章:

python - 安装包之间的pycurl版本冲突

python - 无法在均衡群体财富的程序中找到错误 (UVA 10137, "The Trip")

python - 安全地覆盖 RAM 中的 Python 变量?

regex - Vi 编辑器 : Replacing in all lines except the ones that begin with #

regex - 使用 unix linux 实用程序处理段落中的文本模式

python - 使用 python 在循环内正则表达式搜索非常慢

java - 匹配也包含括号的字符串的正则表达式

python - 在 NLTK 中实现词袋朴素贝叶斯分类器

python-2.7 - 使用 Pandas 和 NLTK 在 Python 2.7 中编码/解码数据

python - 使用 TF-IDF 方案从给定句子中提取关键短语的任何有效解决方法?