简体   繁体   中英

Spacy matcher - how to find tokens with matching custom attribute

I have a custom token attribute ._.group_id , which is some int value. I want to create a pattern which looks for tokens that have the same value for group_id , without explicitly referencing what the value is. So for example, something like this:

pattern = [{'LOWER': 'hello', '_': {'group_id': 4}}, {'LOWER': 'world', '_': {'group_id': 4}}]

But instead of specifying 'group_id': 4 , all I care about is that the tokens in the pattern have the same value for group_id (whatever that may be). So rather than just matching all instances of 'hello world', there is an additional constraint that all the tokens must have the same group_id . Is this sort of functionality possible in spacy? Thanks!

You can't do this directly in the matcher.

What you can do is pass the results of a matcher to the function, and with that you can check for any conditions you can describe in code, including things like whether tokens have the same custom attribute. Take a look at the docs on on_match rules .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM