因为训练word2vec模型,需要处理数据集发现其中有很多特殊符号,所以使用 python3 p y t h o n 3 进行了数据预处理,去除了预料中的特殊符号,这里使用unicode加正则表示式来进行相关的匹配。
import re
string = "12345464我不是药神123456abcdefgABCVDFF?/ ,。,.:;:''';'''[]{}()()《》"
print(string)
12345464我不是药神123456abcdefgABCVDFF?/ ,。,.:;:''';'''[]{}()()《》
sub_str = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])","",string)
print(sub_str)
12345464我不是药神123456abcdefgABCVDFF
正则表达式的使用
函数 | 说明 |
---|---|
sub(pattern,repl,string) | 把字符串中的所有匹配表达式pattern中的地方替换成repl |
[^**] | 表示不匹配此字符集中的任何一个字符 |
\u4e00-\u9fa5 | 汉字的unicode范围 |
\u0030-\u0039 | 数字的unicode范围 |
\u0041-\u005a | 大写字母unicode范围 |
\u0061-\u007a | 小写字母unicode范围 |