spacy-pattern-builder icon indicating copy to clipboard operation
spacy-pattern-builder copied to clipboard

The example code given on the github default page does not work

Open Code4SAFrankie opened this issue 6 years ago • 5 comments

The example code given on the github default page does not work:

The line:

matcher = DependencyTreeMatcher(doc.vocab)

Gives the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'DependencyTreeMatcher' is not defined

If I replace this with:

matcher = DependencyMatcher(doc.vocab)

It then errors futher down on:

matcher.add('pattern', None, pattern)
matches = matcher(doc)
for match_id, token_idxs in matches:
     tokens = [doc[i] for i in token_idxs]
     tokens = sorted(tokens, key=lambda w: w.i)  # Make sure tokens are in their original order
     print(tokens)  # [We, introduce, methods]

with the error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 2, in <listcomp>
  File "doc.pyx", line 296, in spacy.tokens.doc.Doc.__getitem__
TypeError: '<' not supported between instances of 'list' and 'int'

I'm on Windows 10 with WinPython (Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32). Thanks otherwise, I have been looking for pattern matching for ages without luck!

Code4SAFrankie avatar Jan 28 '20 09:01 Code4SAFrankie

@Code4SAFrankie Did you manage to solve this? I am looking into using this for pattern matching and I also still get the same error

laurencgreen avatar Jul 30 '20 11:07 laurencgreen

@laurencgreen @Code4SAFrankie I solved it, check my branch in the pull request. https://github.com/cyclecycle/spacy-pattern-builder/pull/2

sai-prasanna avatar Jul 30 '20 12:07 sai-prasanna

Greetings, I still get the same error, I've already check pull request yet I did not really get how to solve the problem. Can you please guide me?

Thanks.

fatihbozdag avatar Sep 13 '20 21:09 fatihbozdag

Hi all, I have the same error. After checking what is inside matches (it's really a list of list of int, i.e. [[1, 0, 3]], but not a list of int), so iterating over token_idxs[0] instead of token_idxs solves the problem.

iamkissg avatar Sep 17 '20 00:09 iamkissg

Hi all, I have the same error. After checking what is inside matches (it's really a list of list of int, i.e. [[1, 0, 3]], but not a list of int), so iterating over token_idxs[0] instead of token_idxs solves the problem.

Thanks for the reply, Yet now I got (name 'token_idxs' is not defined) error.

fatihbozdag avatar Sep 19 '20 21:09 fatihbozdag