不负相思意
使用 spacy 匹配与使用正则表达式匹配字符串的问题在于,使用 spacy 你[几乎]永远不会提前知道分词器会对你的字符串做什么:有空间:doc = nlp("This is my telephone number (0) 20 111 2222")[tok.text for tok in doc]['This', 'is', 'my', 'telephone', 'number', '(', '0', ')', '20', '111', '2222']没有空格:doc = nlp("This is my telephone number (0)20 111 2222")[tok.text for tok in doc]['This', 'is', 'my', 'telephone', 'number', '(', '0)20', '111', '2222']考虑到这一点,您可以编写 2 个模式来获取两种格式:doc = nlp("My telephone number is either (0)20 111 2222 or (0) 20 111 2222")matcher = Matcher(nlp.vocab, validate=True)pattern1 = [ {'ORTH': '('}, {'SHAPE': 'd'}, {'ORTH': ')'}, {'SHAPE': 'dd'}, {'ORTH': '-', 'OP': '?'}, {'SHAPE': 'ddd'}, {'ORTH': '-', 'OP': '?'}, {'SHAPE': 'dddd'}]pattern2 = [ {'ORTH': '('}, {'TEXT':{'REGEX':'[\d]\)[\d]*'}}, {'ORTH': '-', 'OP': '?'}, {'SHAPE': 'ddd'}, {'ORTH': '-', 'OP': '?'}, {'SHAPE': 'dddd'}]matcher.add('PHONE_NUMBER_E', None, pattern1, pattern2)matches = matcher(doc)for match_id, start, end in matches: string_id = nlp.vocab.strings[match_id] span = doc[start:end] print(span)(0)20 111 2222(0) 20 111 2222