From 430affc347423c8312130e5963da93fd471ff3dc Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Wed, 26 Aug 2015 19:17:02 +0200 Subject: [PATCH] * Fix missing n_patterns property in Matcher class. Fix from_dir method --- spacy/matcher.pyx | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/spacy/matcher.pyx b/spacy/matcher.pyx index 9d1220648..2cc91a368 100644 --- a/spacy/matcher.pyx +++ b/spacy/matcher.pyx @@ -99,7 +99,7 @@ def map_attr_name(attr): cdef class Matcher: cdef Pool mem cdef vector[Pattern*] patterns - cdef readonly int n_patterns + cdef readonly Vocab vocab def __init__(self, vocab, patterns): self.mem = Pool() @@ -107,6 +107,19 @@ cdef class Matcher: for entity_key, (etype, attrs, specs) in sorted(patterns.items()): self.add(entity_key, etype, attrs, specs) + @classmethod + def from_dir(cls, data_dir, Vocab vocab): + patterns_loc = path.join(data_dir, 'vocab', 'gazetteer.json') + if path.exists(patterns_loc): + patterns_data = open(patterns_loc).read() + patterns = json.loads(patterns_data) + return cls(vocab, patterns) + else: + return cls(vocab, {}) + + property n_patterns: + def __get__(self): return self.patterns.size() + def add(self, entity_key, etype, attrs, specs): if isinstance(entity_key, basestring): entity_key = self.vocab.strings[entity_key] @@ -120,16 +133,6 @@ cdef class Matcher: spec = _convert_strings(spec, self.vocab.strings) self.patterns.push_back(init_pattern(self.mem, spec, etype)) - @classmethod - def from_dir(cls, vocab, data_dir): - patterns_loc = path.join(data_dir, 'vocab', 'gazetteer.json') - if path.exists(patterns_loc): - patterns_data = open(patterns_loc).read() - patterns = json.loads(patterns_data) - return cls(vocab, patterns) - else: - return cls(vocab, {}) - def __call__(self, Doc doc): cdef vector[Pattern*] partials cdef int n_partials = 0