mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-26 18:06:29 +03:00
554df9ef20
* Rename all MDX file to `.mdx`
* Lock current node version (#11885)
* Apply Prettier (#11996)
* Minor website fixes (#11974) [ci skip]
* fix table
* Migrate to Next WEB-17 (#12005)
* Initial commit
* Run `npx create-next-app@13 next-blog`
* Install MDX packages
Following: 77b5f79a4d/packages/next-mdx/readme.md
* Add MDX to Next
* Allow Next to handle `.md` and `.mdx` files.
* Add VSCode extension recommendation
* Disabled TypeScript strict mode for now
* Add prettier
* Apply Prettier to all files
* Make sure to use correct Node version
* Add basic implementation for `MDXRemote`
* Add experimental Rust MDX parser
* Add `/public`
* Add SASS support
* Remove default pages and styling
* Convert to module
This allows to use `import/export` syntax
* Add import for custom components
* Add ability to load plugins
* Extract function
This will make the next commit easier to read
* Allow to handle directories for page creation
* Refactoring
* Allow to parse subfolders for pages
* Extract logic
* Redirect `index.mdx` to parent directory
* Disabled ESLint during builds
* Disabled typescript during build
* Remove Gatsby from `README.md`
* Rephrase Docker part of `README.md`
* Update project structure in `README.md`
* Move and rename plugins
* Update plugin for wrapping sections
* Add dependencies for plugin
* Use plugin
* Rename wrapper type
* Simplify unnessary adding of id to sections
The slugified section ids are useless, because they can not be referenced anywhere anyway. The navigation only works if the section has the same id as the heading.
* Add plugin for custom attributes on Markdown elements
* Add plugin to readd support for tables
* Add plugin to fix problem with wrapped images
For more details see this issue: https://github.com/mdx-js/mdx/issues/1798
* Add necessary meta data to pages
* Install necessary dependencies
* Remove outdated MDX handling
* Remove reliance on `InlineList`
* Use existing Remark components
* Remove unallowed heading
Before `h1` components where not overwritten and would never have worked and they aren't used anywhere either.
* Add missing components to MDX
* Add correct styling
* Fix broken list
* Fix broken CSS classes
* Implement layout
* Fix links
* Fix broken images
* Fix pattern image
* Fix heading attributes
* Rename heading attribute
`new` was causing some weird issue, so renaming it to `version`
* Update comment syntax in MDX
* Merge imports
* Fix markdown rendering inside components
* Add model pages
* Simplify anchors
* Fix default value for theme
* Add Universe index page
* Add Universe categories
* Add Universe projects
* Fix Next problem with copy
Next complains when the server renders something different then the client, therfor we move the differing logic to `useEffect`
* Fix improper component nesting
Next doesn't allow block elements inside a `<p>`
* Replace landing page MDX with page component
* Remove inlined iframe content
* Remove ability to inline HTML content in iFrames
* Remove MDX imports
* Fix problem with image inside link in MDX
* Escape character for MDX
* Fix unescaped characters in MDX
* Fix headings with logo
* Allow to export static HTML pages
* Add prebuild script
This command is automatically run by Next
* Replace `svg-loader` with `react-inlinesvg`
`svg-loader` is no longer maintained
* Fix ESLint `react-hooks/exhaustive-deps`
* Fix dropdowns
* Change code language from `cli` to `bash`
* Remove unnessary language `none`
* Fix invalid code language
`markdown_` with an underscore was used to basically turn of syntax highlighting, but using unknown languages know throws an error.
* Enable code blocks plugin
* Readd `InlineCode` component
MDX2 removed the `inlineCode` component
> The special component name `inlineCode` was removed, we recommend to use `pre` for the block version of code, and code for both the block and inline versions
Source: https://mdxjs.com/migrating/v2/#update-mdx-content
* Remove unused code
* Extract function to own file
* Fix code syntax highlighting
* Update syntax for code block meta data
* Remove unused prop
* Fix internal link recognition
There is a problem with regex between Node and browser, and since Next runs the component on both, this create an error.
`Prop `rel` did not match. Server: "null" Client: "noopener nofollow noreferrer"`
This simplifies the implementation and fixes the above error.
* Replace `react-helmet` with `next/head`
* Fix `className` problem for JSX component
* Fix broken bold markdown
* Convert file to `.mjs` to be used by Node process
* Add plugin to replace strings
* Fix custom table row styling
* Fix problem with `span` inside inline `code`
React doesn't allow a `span` inside an inline `code` element and throws an error in dev mode.
* Add `_document` to be able to customize `<html>` and `<body>`
* Add `lang="en"`
* Store Netlify settings in file
This way we don't need to update via Netlify UI, which can be tricky if changing build settings.
* Add sitemap
* Add Smartypants
* Add PWA support
* Add `manifest.webmanifest`
* Fix bug with anchor links after reloading
There was no need for the previous implementation, since the browser handles this nativly. Additional the manual scrolling into view was actually broken, because the heading would disappear behind the menu bar.
* Rename custom event
I was googeling for ages to find out what kind of event `inview` is, only to figure out it was a custom event with a name that sounds pretty much like a native one. 🫠
* Fix missing comment syntax highlighting
* Refactor Quickstart component
The previous implementation was hidding the irrelevant lines via data-props and dynamically generated CSS. This created problems with Next and was also hard to follow. CSS was used to do what React is supposed to handle.
The new implementation simplfy filters the list of children (React elements) via their props.
* Fix syntax highlighting for Training Quickstart
* Unify code rendering
* Improve error logging in Juniper
* Fix Juniper component
* Automatically generate "Read Next" link
* Add Plausible
* Use recent DocSearch component and adjust styling
* Fix images
* Turn of image optimization
> Image Optimization using Next.js' default loader is not compatible with `next export`.
We currently deploy to Netlify via `next export`
* Dont build pages starting with `_`
* Remove unused files
* Add Next plugin to Netlify
* Fix button layout
MDX automatically adds `p` tags around text on a new line and Prettier wants to put the text on a new line. Hacking with JSX string.
* Add 404 page
* Apply Prettier
* Update Prettier for `package.json`
Next sometimes wants to patch `package-lock.json`. The old Prettier setting indended with 4 spaces, but Next always indends with 2 spaces. Since `npm install` automatically uses the indendation from `package.json` for `package-lock.json` and to avoid the format switching back and forth, both files are now set to 2 spaces.
* Apply Next patch to `package-lock.json`
When starting the dev server Next would warn `warn - Found lockfile missing swc dependencies, patching...` and update the `package-lock.json`. These are the patched changes.
* fix link
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
* small backslash fixes
* adjust to new style
Co-authored-by: Marcus Blättermann <marcus@essenmitsosse.de>
270 lines
18 KiB
Plaintext
270 lines
18 KiB
Plaintext
---
|
|
title: Matcher
|
|
teaser: Match sequences of tokens, based on pattern rules
|
|
tag: class
|
|
source: spacy/matcher/matcher.pyx
|
|
---
|
|
|
|
The `Matcher` lets you find words and phrases using rules describing their token
|
|
attributes. Rules can refer to token annotations (like the text or
|
|
part-of-speech tags), as well as lexical attributes like `Token.is_punct`.
|
|
Applying the matcher to a [`Doc`](/api/doc) gives you access to the matched
|
|
tokens in context. For in-depth examples and workflows for combining rules and
|
|
statistical models, see the [usage guide](/usage/rule-based-matching) on
|
|
rule-based matching.
|
|
|
|
## Pattern format {id="patterns"}
|
|
|
|
> ```json
|
|
> ### Example
|
|
> [
|
|
> {"LOWER": "i"},
|
|
> {"LEMMA": {"IN": ["like", "love"]}},
|
|
> {"POS": "NOUN", "OP": "+"}
|
|
> ]
|
|
> ```
|
|
|
|
A pattern added to the `Matcher` consists of a list of dictionaries. Each
|
|
dictionary describes **one token** and its attributes. The available token
|
|
pattern keys correspond to a number of
|
|
[`Token` attributes](/api/token#attributes). The supported attributes for
|
|
rule-based matching are:
|
|
|
|
| Attribute | Description |
|
|
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
|
| `ORTH` | The exact verbatim text of a token. ~~str~~ |
|
|
| `TEXT` | The exact verbatim text of a token. ~~str~~ |
|
|
| `NORM` | The normalized form of the token text. ~~str~~ |
|
|
| `LOWER` | The lowercase form of the token text. ~~str~~ |
|
|
| `LENGTH` | The length of the token text. ~~int~~ |
|
|
| `IS_ALPHA`, `IS_ASCII`, `IS_DIGIT` | Token text consists of alphabetic characters, ASCII characters, digits. ~~bool~~ |
|
|
| `IS_LOWER`, `IS_UPPER`, `IS_TITLE` | Token text is in lowercase, uppercase, titlecase. ~~bool~~ |
|
|
| `IS_PUNCT`, `IS_SPACE`, `IS_STOP` | Token is punctuation, whitespace, stop word. ~~bool~~ |
|
|
| `IS_SENT_START` | Token is start of sentence. ~~bool~~ |
|
|
| `LIKE_NUM`, `LIKE_URL`, `LIKE_EMAIL` | Token text resembles a number, URL, email. ~~bool~~ |
|
|
| `SPACY` | Token has a trailing space. ~~bool~~ |
|
|
| `POS`, `TAG`, `MORPH`, `DEP`, `LEMMA`, `SHAPE` | The token's simple and extended part-of-speech tag, morphological analysis, dependency label, lemma, shape. ~~str~~ |
|
|
| `ENT_TYPE` | The token's entity label. ~~str~~ |
|
|
| `ENT_IOB` | The IOB part of the token's entity tag. ~~str~~ |
|
|
| `ENT_ID` | The token's entity ID (`ent_id`). ~~str~~ |
|
|
| `ENT_KB_ID` | The token's entity knowledge base ID (`ent_kb_id`). ~~str~~ |
|
|
| `_` | Properties in [custom extension attributes](/usage/processing-pipelines#custom-components-attributes). ~~Dict[str, Any]~~ |
|
|
| `OP` | Operator or quantifier to determine how often to match a token pattern. ~~str~~ |
|
|
|
|
Operators and quantifiers define **how often** a token pattern should be
|
|
matched:
|
|
|
|
> ```json
|
|
> ### Example
|
|
> [
|
|
> {"POS": "ADJ", "OP": "*"},
|
|
> {"POS": "NOUN", "OP": "+"}
|
|
> {"POS": "PROPN", "OP": "{2}"}
|
|
> ]
|
|
> ```
|
|
|
|
| OP | Description |
|
|
| ------- | ---------------------------------------------------------------------- |
|
|
| `!` | Negate the pattern, by requiring it to match exactly 0 times. |
|
|
| `?` | Make the pattern optional, by allowing it to match 0 or 1 times. |
|
|
| `+` | Require the pattern to match 1 or more times. |
|
|
| `*` | Allow the pattern to match 0 or more times. |
|
|
| `{n}` | Require the pattern to match exactly _n_ times. |
|
|
| `{n,m}` | Require the pattern to match at least _n_ but not more than _m_ times. |
|
|
| `{n,}` | Require the pattern to match at least _n_ times. |
|
|
| `{,m}` | Require the pattern to match at most _m_ times. |
|
|
|
|
Token patterns can also map to a **dictionary of properties** instead of a
|
|
single value to indicate whether the expected value is a member of a list or how
|
|
it compares to another value.
|
|
|
|
> ```json
|
|
> ### Example
|
|
> [
|
|
> {"LEMMA": {"IN": ["like", "love", "enjoy"]}},
|
|
> {"POS": "PROPN", "LENGTH": {">=": 10}},
|
|
> ]
|
|
> ```
|
|
|
|
| Attribute | Description |
|
|
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| `REGEX` | Attribute value matches the regular expression at any position in the string. ~~Any~~ |
|
|
| `FUZZY` | Attribute value matches if the `fuzzy_compare` method matches for `(value, pattern, -1)`. The default method allows a Levenshtein edit distance of at least 2 and up to 30% of the pattern string length. ~~Any~~ |
|
|
| `FUZZY1`, `FUZZY2`, ... `FUZZY9` | Attribute value matches if the `fuzzy_compare` method matches for `(value, pattern, N)`. The default method allows a Levenshtein edit distance of at most N (1-9). ~~Any~~ |
|
|
| `IN` | Attribute value is member of a list. ~~Any~~ |
|
|
| `NOT_IN` | Attribute value is _not_ member of a list. ~~Any~~ |
|
|
| `IS_SUBSET` | Attribute value (for `MORPH` or custom list attributes) is a subset of a list. ~~Any~~ |
|
|
| `IS_SUPERSET` | Attribute value (for `MORPH` or custom list attributes) is a superset of a list. ~~Any~~ |
|
|
| `INTERSECTS` | Attribute value (for `MORPH` or custom list attribute) has a non-empty intersection with a list. ~~Any~~ |
|
|
| `==`, `>=`, `<=`, `>`, `<` | Attribute value is equal, greater or equal, smaller or equal, greater or smaller. ~~Union[int, float]~~ |
|
|
|
|
As of spaCy v3.5, `REGEX` and `FUZZY` can be used in combination with `IN` and
|
|
`NOT_IN`.
|
|
|
|
## Matcher.\_\_init\_\_ {id="init",tag="method"}
|
|
|
|
Create the rule-based `Matcher`. If `validate=True` is set, all patterns added
|
|
to the matcher will be validated against a JSON schema and a `MatchPatternError`
|
|
is raised if problems are found. Those can include incorrect types (e.g. a
|
|
string where an integer is expected) or unexpected property names.
|
|
|
|
> #### Example
|
|
>
|
|
> ```python
|
|
> from spacy.matcher import Matcher
|
|
> matcher = Matcher(nlp.vocab)
|
|
> ```
|
|
|
|
| Name | Description |
|
|
| --------------- | ----------------------------------------------------------------------------------------------------- |
|
|
| `vocab` | The vocabulary object, which must be shared with the documents the matcher will operate on. ~~Vocab~~ |
|
|
| `validate` | Validate all patterns added to this matcher. ~~bool~~ |
|
|
| `fuzzy_compare` | The comparison method used for the `FUZZY` operators. ~~Callable[[str, str, int], bool]~~ |
|
|
|
|
## Matcher.\_\_call\_\_ {id="call",tag="method"}
|
|
|
|
Find all token sequences matching the supplied patterns on the `Doc` or `Span`.
|
|
|
|
Note that if a single label has multiple patterns associated with it, the
|
|
returned matches don't provide a way to tell which pattern was responsible for
|
|
the match.
|
|
|
|
> #### Example
|
|
>
|
|
> ```python
|
|
> from spacy.matcher import Matcher
|
|
>
|
|
> matcher = Matcher(nlp.vocab)
|
|
> pattern = [{"LOWER": "hello"}, {"LOWER": "world"}]
|
|
> matcher.add("HelloWorld", [pattern])
|
|
> doc = nlp("hello world!")
|
|
> matches = matcher(doc)
|
|
> ```
|
|
|
|
| Name | Description |
|
|
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| `doclike` | The `Doc` or `Span` to match over. ~~Union[Doc, Span]~~ |
|
|
| _keyword-only_ | |
|
|
| `as_spans` <Tag variant="new">3</Tag> | Instead of tuples, return a list of [`Span`](/api/span) objects of the matches, with the `match_id` assigned as the span label. Defaults to `False`. ~~bool~~ |
|
|
| `allow_missing` <Tag variant="new">3</Tag> | Whether to skip checks for missing annotation for attributes included in patterns. Defaults to `False`. ~~bool~~ |
|
|
| `with_alignments` <Tag variant="new">3.0.6</Tag> | Return match alignment information as part of the match tuple as `List[int]` with the same length as the matched span. Each entry denotes the corresponding index of the token in the pattern. If `as_spans` is set to `True`, this setting is ignored. 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]]~~ |
|
|
|
|
## Matcher.\_\_len\_\_ {id="len",tag="method",version="2"}
|
|
|
|
Get the number of rules added to the matcher. Note that this only returns the
|
|
number of rules (identical with the number of IDs), not the number of individual
|
|
patterns.
|
|
|
|
> #### Example
|
|
>
|
|
> ```python
|
|
> matcher = Matcher(nlp.vocab)
|
|
> assert len(matcher) == 0
|
|
> matcher.add("Rule", [[{"ORTH": "test"}]])
|
|
> assert len(matcher) == 1
|
|
> ```
|
|
|
|
| Name | Description |
|
|
| ----------- | ---------------------------- |
|
|
| **RETURNS** | The number of rules. ~~int~~ |
|
|
|
|
## Matcher.\_\_contains\_\_ {id="contains",tag="method",version="2"}
|
|
|
|
Check whether the matcher contains rules for a match ID.
|
|
|
|
> #### Example
|
|
>
|
|
> ```python
|
|
> matcher = Matcher(nlp.vocab)
|
|
> assert "Rule" not in matcher
|
|
> matcher.add("Rule", [[{'ORTH': 'test'}]])
|
|
> assert "Rule" in matcher
|
|
> ```
|
|
|
|
| Name | Description |
|
|
| ----------- | -------------------------------------------------------------- |
|
|
| `key` | The match ID. ~~str~~ |
|
|
| **RETURNS** | Whether the matcher contains rules for this match ID. ~~bool~~ |
|
|
|
|
## Matcher.add {id="add",tag="method",version="2"}
|
|
|
|
Add a rule to the matcher, consisting of an ID key, one or more patterns, and an
|
|
optional callback function to act on the matches. The callback function will
|
|
receive the arguments `matcher`, `doc`, `i` and `matches`. If a pattern already
|
|
exists for the given ID, the patterns will be extended. An `on_match` callback
|
|
will be overwritten.
|
|
|
|
> #### Example
|
|
>
|
|
> ```python
|
|
> def on_match(matcher, doc, id, matches):
|
|
> print('Matched!', matches)
|
|
>
|
|
> matcher = Matcher(nlp.vocab)
|
|
> patterns = [
|
|
> [{"LOWER": "hello"}, {"LOWER": "world"}],
|
|
> [{"ORTH": "Google"}, {"ORTH": "Maps"}]
|
|
> ]
|
|
> matcher.add("TEST_PATTERNS", patterns, on_match=on_match)
|
|
> doc = nlp("HELLO WORLD on Google Maps.")
|
|
> matches = matcher(doc)
|
|
> ```
|
|
|
|
<Infobox title="Changed in v3.0" variant="warning">
|
|
|
|
As of spaCy v3.0, `Matcher.add` takes a list of patterns as the second argument
|
|
(instead of a variable number of arguments). The `on_match` callback becomes an
|
|
optional keyword argument.
|
|
|
|
```diff
|
|
patterns = [[{"TEXT": "Google"}, {"TEXT": "Now"}], [{"TEXT": "GoogleNow"}]]
|
|
- matcher.add("GoogleNow", on_match, *patterns)
|
|
+ matcher.add("GoogleNow", patterns, on_match=on_match)
|
|
```
|
|
|
|
</Infobox>
|
|
|
|
| Name | Description |
|
|
| ----------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| `match_id` | An ID for the thing you're matching. ~~str~~ |
|
|
| `patterns` | Match pattern. A pattern consists of a list of dicts, where each dict describes a token. ~~List[List[Dict[str, Any]]]~~ |
|
|
| _keyword-only_ | |
|
|
| `on_match` | Callback function to act on matches. Takes the arguments `matcher`, `doc`, `i` and `matches`. ~~Optional[Callable[[Matcher, Doc, int, List[tuple], Any]]~~ |
|
|
| `greedy` <Tag variant="new">3</Tag> | Optional filter for greedy matches. Can either be `"FIRST"` or `"LONGEST"`. ~~Optional[str]~~ |
|
|
|
|
## Matcher.remove {id="remove",tag="method",version="2"}
|
|
|
|
Remove a rule from the matcher. A `KeyError` is raised if the match ID does not
|
|
exist.
|
|
|
|
> #### Example
|
|
>
|
|
> ```python
|
|
> matcher.add("Rule", [[{"ORTH": "test"}]])
|
|
> assert "Rule" in matcher
|
|
> matcher.remove("Rule")
|
|
> assert "Rule" not in matcher
|
|
> ```
|
|
|
|
| Name | Description |
|
|
| ----- | --------------------------------- |
|
|
| `key` | The ID of the match rule. ~~str~~ |
|
|
|
|
## Matcher.get {id="get",tag="method",version="2"}
|
|
|
|
Retrieve the pattern stored for a key. Returns the rule as an
|
|
`(on_match, patterns)` tuple containing the callback and available patterns.
|
|
|
|
> #### Example
|
|
>
|
|
> ```python
|
|
> matcher.add("Rule", [[{"ORTH": "test"}]])
|
|
> on_match, patterns = matcher.get("Rule")
|
|
> ```
|
|
|
|
| Name | Description |
|
|
| ----------- | --------------------------------------------------------------------------------------------- |
|
|
| `key` | The ID of the match rule. ~~str~~ |
|
|
| **RETURNS** | The rule, as an `(on_match, patterns)` tuple. ~~Tuple[Optional[Callable], List[List[dict]]]~~ |
|