Skip to main content

Build and match patterns for semantic role labelling

Project description

d# Role Pattern

Build and match linguistic patterns for role labelling. Provides an example-driven approach to generate and refine patterns.

Uses graph-based pattern matching, built on SpaCy.


With pip:

pip install role-pattern-nlp

Example usage

# First, parse a string to create a SpaCy Doc object
import en_core_web_sm
text = "Forging involves the shaping of metal using localized compressive forces."
nlp = en_core_web_sm.load()
doc = nlp(text)

from role_pattern_nlp import RolePatternBuilder

# Provide an example by mapping role labels to tokens
match_example = {
    'arg1': [doc[0]],  # [Forging]
    'pred': [doc[1]],  # [involves]
    'arg2': [doc[3]],  # [shaping]

''' Create a dictionary of all the features we want the RolePatternBuilder to have access to
when building and refining patterns '''
feature_dict = {'DEP': 'dep_', 'TAG': 'tag_'}

# Instantiate the pattern builder
role_pattern_builder = RolePatternBuilder(feature_dict)

#  Build a pattern. It will use all the features in the feature_dict by default
role_pattern =  

# Match against any doc with the role_pattern
matches = role_pattern.match(doc)
[{'arg1': [Forging], 'arg2': [shaping], 'pred': [involves]}]

See examples/ for demonstration as to how to refine a pattern using negative examples.




The dependency pattern in the form used to create the SpaCy DependencyMatcher object.


The list of labels that corresponds to the tokens matched by the pattern.

Built with

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

role-pattern-nlp-0.2.0.tar.gz (12.1 kB view hashes)

Uploaded Source

Built Distribution

role_pattern_nlp-0.2.0-py3-none-any.whl (16.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page