-
-
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) |
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
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.
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.
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 errorIndexError: list index out of range
inspan = doc[matches[k][1]:matches[k][2]]
.Grateful for any suggestion/solution, Thanks.
Any Solutions for this?
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 thecandidate_sentences["sentence"]
which it stores the file of wiki_sentence, and I get errorIndexError: list index out of range
inspan = 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
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 thecandidate_sentences["sentence"]
which it stores the file of wiki_sentence, and I get errorIndexError: list index out of range
inspan = 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
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 thecandidate_sentences["sentence"]
which it stores the file of wiki_sentence, and I get errorIndexError: list index out of range
inspan = 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
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