知识卡片
正则分词要按实际字符范围解释
示例用单词字符、可选直撇号内部片段以及单独非空白标点作匹配。非捕获组(?:…)不是向前查找,直撇号规则也不自动接受弯撇号、双引号或逗号错拼;可以支持的收缩形式需用实际正反样例核验。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
- · 思维的词元
原书出处 · 1 处
- · 查看原书依据
>>> pattern = r'\w+(?:\'\w+)?|[^\w\s]' #1 >>> texts = [text] >>> texts.append("There's no such thing as survival of the fittest. " ... "Survival of the most adequate, maybe.") >>> tokens = list(re.findall(pattern, texts[-1])) >>> tokens[:8] ["There's", 'no', 'such', 'thing', 'as', 'survival', 'of', 'the'] >>> tokens[8:16] ['fittest', '.', 'Survival', 'of', 'the', 'most', 'adequate', ','] >>> tokens[16:] ['maybe', '.'] #1 The lookahead pattern (?:\'\w+)? detects whether the word
参考来源
- 位置:《Natural Language Processing in Action, Second Edition》第02章《思维的词元》(来源项目:a19c7933-4eda-4176-aebd-30fe7e1ad292;源文件:extracted/Natural Language Processing in Action, Second Edition/_epub-src/OEBPS/Text/chapter-2.html)
- 结论依据:摘录中关于正则分词要按实际字符范围解释的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。
- 原始内容:>>> pattern = r'\w+(?:\'\w+)?|[^\w\s]' #1 >>> texts = [text] >>> texts.append("There's no such thing as survival of the fittest. " ... "Survival of the most adequate, maybe.") >>> tokens = list(re.findall(pattern, texts[-1])) >>> tokens[:8] ["There's", 'no', 'such', 'thing', 'as', 'survival', 'of', 'the'] >>> tokens[8:16] ['fittest', '.', 'Survival', 'of', 'the', 'most', 'adequate', ','] >>> tokens[16:] ['maybe', '.'] #1 The lookahead pattern (?:\'\w+)? detects whether the word