PhraseMatcher
PhraseMatcher 可让您高效匹配大型术语列表。虽然 Matcher 允许您基于词符描述列表进行序列匹配,但 PhraseMatcher 接受 Doc 对象形式的匹配模式。具体示例请参阅 使用指南。
PhraseMatcher.__init__ 方法
创建基于规则的PhraseMatcher。设置不同的attr匹配属性将改变用于确定匹配的token属性。默认情况下,系统会检查传入的Doc中是否存在具有相同ORTH值(即原始token文本)的token序列。若使用LOWER属性进行匹配,则实现不区分大小写的匹配,因为此时仅比较小写形式的token文本。理论上,也可以匹配具有相同词性标注或依存标签的序列。
如果设置了validate=True,在添加模式时会执行额外的验证。目前,它会检查Doc是否分配了对于产生匹配结果不必要的属性(例如,当PhraseMatcher基于词符文本匹配时却分配了词性标注)。由于这通常会导致创建模式时的性能显著下降,系统会显示UserWarning警告。
| 名称 | 描述 |
|---|---|
vocab | The vocabulary object, which must be shared with the documents the matcher will operate on. Vocab |
attr | The token attribute to match on. Defaults to ORTH, i.e. the verbatim token text. Union[int, str] |
validate | Validate patterns added to the matcher. bool |
PhraseMatcher.__call__ 方法
在Doc或Span上查找所有与提供模式匹配的标记序列。
| 名称 | 描述 |
|---|---|
doclike | The Doc or Span to match over. Union[Doc,Span] |
| 仅关键字 | |
as_spans v3.0 | Instead of tuples, return a list of Span objects of the matches, with the match_id assigned as the span label. Defaults to False. bool |
| RETURNS | A list of (match_id, start, end) tuples, describing the matches. A match tuple describes a span doc[start:end]. The match_id is the ID of the added match pattern. If as_spans is set to True, a list of Span objects is returned instead. Union[List[Tuple[int, int, int]], List[Span]] |
PhraseMatcher.__len__ 方法
获取添加到匹配器中的规则数量。请注意,这里仅返回规则的数量(与ID数量相同),而不是单个模式的数量。
| 名称 | 描述 |
|---|---|
| 返回值 | 规则的数量。int |
PhraseMatcher.__contains__ 方法
检查匹配器是否包含针对某个匹配ID的规则。
| 名称 | 描述 |
|---|---|
key | The match ID. str |
| 返回值 | 匹配器是否包含该匹配ID的规则。bool |
PhraseMatcher.add 方法
向匹配器添加规则,包含一个ID键、一个或多个模式,以及一个用于处理匹配项的回调函数。回调函数将接收参数matcher、doc、i和matches。如果给定ID的模式已存在,则会扩展这些模式。on_match回调将被覆盖。
| 名称 | 描述 |
|---|---|
key | An ID for the thing you’re matching. str |
docs | Doc objects of the phrases to match. List[Doc] |
| 仅关键字 | |
on_match | Callback function to act on matches. Takes the arguments matcher, doc, i and matches. Optional[Callable[[Matcher,Doc, int, List[tuple], Any]] |
PhraseMatcher.remove 方法
通过匹配ID从匹配器中移除规则。如果键不存在,则会引发KeyError错误。
| 名称 | 描述 |
|---|---|
key | The ID of the match rule. str |