Skip to content

Instantly share code, notes, and snippets.

@prateekjoshi565
Created October 6, 2019 13:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prateekjoshi565/8751194336f170234a32ca852d729ae2 to your computer and use it in GitHub Desktop.
Save prateekjoshi565/8751194336f170234a32ca852d729ae2 to your computer and use it in GitHub Desktop.
def get_relation(sent):
doc = nlp(sent)
# Matcher class object
matcher = Matcher(nlp.vocab)
#define the pattern
pattern = [{'DEP':'ROOT'},
{'DEP':'prep','OP':"?"},
{'DEP':'agent','OP':"?"},
{'POS':'ADJ','OP':"?"}]
matcher.add("matching_1", None, pattern)
matches = matcher(doc)
k = len(matches) - 1
span = doc[matches[k][1]:matches[k][2]]
return(span.text)
@michaelpetryk
Copy link

Hello! Any advice would be helpful. If I issue:

get_relation("John completed the task”)

leads to the error:


TypeError Traceback (most recent call last)
in
----> 1 get_relation("John completed the task")

in get_relation(sent)
12 {'POS':'ADJ','OP':"?"}]
13
---> 14 matcher.add("matching_1", None, pattern)
15
16 matches = matcher(doc)

~/opt/anaconda3/lib/python3.7/site-packages/spacy/matcher/matcher.pyx in spacy.matcher.matcher.Matcher.add()

TypeError: add() takes exactly 2 positional arguments (3 given)

Thanks in advance!

Michael

@michaelpetryk
Copy link

I have solved the above issue. See https://spacy.io/api/matcher for details.

Old version of the code that no longer works:

matcher.add("matching_1", None, pattern)

The new syntax that works is:

matcher.add("matching_1", [pattern], on_match=None)

Cheers,

Michael

@prateekjoshi565
Copy link
Author

prateekjoshi565 commented Feb 22, 2021 via email

@eecmyang
Copy link

eecmyang commented Nov 25, 2021

Hello, @prateekjoshi565

I've been learning from your article about knowlege graph.

In this example code snippet, whenever I try to execute this function to the candidate_sentences["sentence"] which it stores the file of wiki_sentence, and I get error IndexError: list index out of range in span = doc[matches[k][1]:matches[k][2]].

Grateful for any suggestion/solution,
Thanks.

@Bekgrec
Copy link

Bekgrec commented Mar 8, 2022

I have solved the above issue. See https://spacy.io/api/matcher for details.

Old version of the code that no longer works:

matcher.add("matching_1", None, pattern)

The new syntax that works is:

matcher.add("matching_1", [pattern], on_match=None)

Cheers,

Michael

That helps a lot. Thx.

@Harshm27
Copy link

Hello, @prateekjoshi565

I've been learning from your article about knowlege graph.

In this example code snippet, whenever I try to execute this function to the candidate_sentences["sentence"] which it stores the file of wiki_sentence, and I get error IndexError: list index out of range in span = doc[matches[k][1]:matches[k][2]].

Grateful for any suggestion/solution, Thanks.

Any Solutions for this?

@PoorDoomer
Copy link

Hello, @prateekjoshi565
I've been learning from your article about knowlege graph.
In this example code snippet, whenever I try to execute this function to the candidate_sentences["sentence"] which it stores the file of wiki_sentence, and I get error IndexError: list index out of range in span = doc[matches[k][1]:matches[k][2]].
Grateful for any suggestion/solution, Thanks.

Any Solutions for this?

If anyone have found the solution if will be great

@Ldabai
Copy link

Ldabai commented May 20, 2022

Hello, @prateekjoshi565
I've been learning from your article about knowlege graph.
In this example code snippet, whenever I try to execute this function to the candidate_sentences["sentence"] which it stores the file of wiki_sentence, and I get error IndexError: list index out of range in span = doc[matches[k][1]:matches[k][2]].
Grateful for any suggestion/solution, Thanks.

Any Solutions for this?

def get_relation(sent):
    doc = nlp(sent)
    # Matcher class object
    matcher = Matcher(nlp.vocab)
    relation=[]
    # define the pattern
    pattern = [{'DEP': 'ROOT'},
               {'DEP': 'prep', 'OP': "?"},
               {'DEP': 'agent', 'OP': "?"},
               {'POS': 'ADJ', 'OP': "?"}]

    matcher.add("matching_1", [pattern], on_match=None)

    matches = matcher(doc)
  
    for mathc_id, start, end in matches:
        matched_span = doc[start: end]
        # print(matched_span.text)
        relation.append(matched_span.text)
    return relation

You can try this. Maybe the span is limited to 256, so there will be some errors when we run the old code.
But when I run the new codes, the result is different from the author's result.
My result is like this:
[is] 365
[was] 299
[released, released on] 88
[are] 77
[include] 72
[were] 68
[released] 41
[composed, composed by] 33
['s] 32

@Ldabai
Copy link

Ldabai commented May 20, 2022

Hello, @prateekjoshi565
I've been learning from your article about knowlege graph.
In this example code snippet, whenever I try to execute this function to the candidate_sentences["sentence"] which it stores the file of wiki_sentence, and I get error IndexError: list index out of range in span = doc[matches[k][1]:matches[k][2]].
Grateful for any suggestion/solution, Thanks.

Any Solutions for this?

If anyone have found the solution if will be great

u can try my code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment