我试图通过使用正则表达式删除标点符号(除了 - &。)。标点符号删除的输入是预处理标记。但是,我的代码出现语法错误(SyntaxError:无效语法)。我在哪里需要修改以修复语法错误?
我试图通过更改正则表达式来修复它。我是正则表达式的新手。一些建议可能会帮助我修复错误或识别我的错误。
我的代码如下:
regex = re.compile('[%s]' % re.escape(string.punctuation))
token_without_punctuation = []
for x in tokenized_doc1:
y = []
for token in x:
tokens = regex.sub(ur"\p{P}(?<![\-.])", "", token)
y.append(tokens)
token_without_punctuation.append(y)
print(token_without_punctuation)
我的代码输入如下:
[['The', 'intelligent', 'directory', 'enquiry', 'assistant', '(', 'YPA', ')', 'project', 'is', 'an', 'example', '(', 'going', 'back', 'quite', 'a', 'few', 'years', 'now', '...', ')', 'where', 'the', 'extraction', 'of', 'information', 'from', 'partially', 'structured', 'data', 'together', 'with', 'engineering', 'issues', 'played', 'major', 'roles', 'in', 'making', 'the', 'YPA', 'a', 'usable', 'online', 'system', '.'], ['I', 'am', 'developing', 'techniques', 'that', 'allow', 'the', 'extraction', 'of', 'conceptual', 'information', 'from', 'document', 'collections', 'and', 'the', 'utilization', 'of', 'such', 'knowledge', 'in', 'retrieval', 'tasks', '.'], ['The', 'type', 'of', 'documents', 'can', 'range', 'from', 'Web', 'pages', 'to', 'newspaper', 'articles', 'or', 'other', 'forms', 'of', 'vaguely/partially', 'structured', 'data', '.']]
错误如下
File "<ipython-input-108-0c96ff0d8e79>", line 10
tokens = regex.sub(ur"\p{P}(?<![\-.])", "", token)
^
SyntaxError: invalid syntax
如果有人帮助识别我的错误,那将是非常好的。
附加信息:
我也试过下面的代码。
tokens = regex.sub(u'', token)
if not token == u'':
上面提到的行修复了错误,但它删除了所有标点符号。我也尝试过tokens = regex.sub(u'\p{P}(?<![\-.])', token)
。在这种情况下,我收到另一个错误,如下所示,
TypeError: 'str' object cannot be interpreted as an integer
如何在不出错的情况下删除标点符号(。和 - 除外)?
代码中的regex
变量是一个已编译的re
对象。您稍后将其用作PyPi regex
模块参考,您甚至使用只有PyPi regex
模块才能“理解”的正则表达式。这根本不正确。
请注意,要消除re
和regex
之间的所有歧义,我建议将regex
变量重命名为punct_rx
。
现在,您使用'[%s]' % re.escape(string.punctuation)
代码形成标点正则表达式。你得到[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^_\`\{\|\}\~]
(demo)。也许,在构建正则表达式时删除-
和.
是有意义的。将该代码更改为re.compile('[%s]' % re.escape(string.punctuation.replace(".", "").replace("-", "")))
,您将获得[\!\"\#\$\%\&\'\(\)\*\+\,\/\:\;\<\=\>\?\@\[\\\]\^_\`\{\|\}\~]
模式(demo)。
然后,剩下的就是将替换代码修复为tokens = punct_rx.sub("", token)
。
查看完整修复:
import re, string
tokenized_doc1 = [['The', 'intelligent', 'directory', 'enquiry', 'assistant', '(', 'YPA', ')', 'project', 'is', 'an', 'example', '(', 'going', 'back', 'quite', 'a', 'few', 'years', 'now', '...', ')', 'where', 'the', 'extraction', 'of', 'information', 'from', 'partially', 'structured', 'data', 'together', 'with', 'engineering', 'issues', 'played', 'major', 'roles', 'in', 'making', 'the', 'YPA', 'a', 'usable', 'online', 'system', '.'], ['I', 'am', 'developing', 'techniques', 'that', 'allow', 'the', 'extraction', 'of', 'conceptual', 'information', 'from', 'document', 'collections', 'and', 'the', 'utilization', 'of', 'such', 'knowledge', 'in', 'retrieval', 'tasks', '.'], ['The', 'type', 'of', 'documents', 'can', 'range', 'from', 'Web', 'pages', 'to', 'newspaper', 'articles', 'or', 'other', 'forms', 'of', 'vaguely/partially', 'structured', 'data', '.']]
punct_rx = re.compile('[%s]' % re.escape(string.punctuation.replace(".", "").replace("-", "")))
token_without_punctuation = []
for x in tokenized_doc1:
y = []
for token in x:
tokens = punct_rx.sub("", token)
y.append(tokens)
token_without_punctuation.append(y)
print(token_without_punctuation)