user8218582
user8218582

Reputation:

Attribute Error : Issue while training POS Tags in SpaCy NLP

from __future__ import unicode_literals
from __future__ import print_function

import plac
from pathlib import Path

from spacy.vocab import Vocab
from spacy.tagger import Tagger
from spacy.tokens import Doc
from spacy.gold import GoldParse

import random


TAG_MAP = {
    'N': {"pos": "NOUN"},
    'V': {"pos": "VERB"},
    'J': {"pos": "ADJ"}
}

# Usually you'll read this in, of course. Data formats vary.
# Ensure your strings are unicode.
DATA = [
    (
        ["ids", "id's", "invoiceid", "invoice_id", "Ray"],
        ["N", "N", "N", "N", "N"]
    )
]


def ensure_dir(path):
    if not path.exists():
        path.mkdir()


def main(output_dir=None):
    output_dir = '/home/Ray/Tagger'
    if output_dir is not None:
        output_dir = Path(output_dir)
        ensure_dir(output_dir)
        ensure_dir(output_dir / "pos")
        ensure_dir(output_dir / "vocab")

    vocab = Vocab(tag_map=TAG_MAP)

    tagger = Tagger(vocab)
    for i in range(25):
        for words, tags in DATA:
            doc = Doc(vocab, words=words)
            gold = GoldParse(doc, tags=tags)
            tagger.update(doc, gold)
        random.shuffle(DATA)
    tagger.model.end_training()
    doc = Doc(vocab, orths_and_spaces=zip(
        ["ID", "Id", "iD", "IDs", "id", "ids", "id's", "ID's", "invoice id", "inv id", "ray"], [True] * 11))
    tagger(doc)
    for word in doc:
        print(word.text, word.tag_, word.pos_)
    if output_dir is not None:
        tagger.model.dump(str(output_dir / 'pos' / 'model'))
        with (output_dir / 'vocab' / 'strings.json').open('w') as file_:
            tagger.vocab.strings.dump(file_)


if __name__ == '__main__':
    plac.call(main)

The output returns the following with error:-

ID N NOUN
Id N NOUN
iD N NOUN
IDs N NOUN
id N NOUN
ids N NOUN
id's N NOUN
ID's N NOUN
invoice id N NOUN
inv id N NOUN
ray N NOUN
Traceback (most recent call last):
  File "/home/Ray/spaCy-2.0.0-alpha/examples/training/train_tagger.py", line 75, in <module>
    plac.call(main)
  File "/usr/local/lib/python2.7/dist-packages/plac_core.py", line 328, in call
    cmd, result = parser.consume(arglist)
  File "/usr/local/lib/python2.7/dist-packages/plac_core.py", line 207, in consume
    return cmd, self.func(*(args + varargs + extraopts), **kwargs)
  File "/home/Ray/spaCy-2.0.0-alpha/examples/training/train_tagger.py", line 71, in main
    tagger.vocab.strings.dump(file_)
AttributeError: 'spacy.strings.StringStore' object has no attribute 'dump'

Process finished with exit code 1

When I don't give the path (i.e: Output_dir = '/path'), then it works fine. If I won't give any path, how will I find where the 'pos' and 'vocab' folders are generated. Please help

Upvotes: 1

Views: 263

Answers (2)

Amar Powar
Amar Powar

Reputation: 87

In spacy v2, all serialization methods have been updated to use a consistent API

So StringStore.dump has been replaced with StringStore.to_disk and StringStore.to_bytes.

I think this should work.

Upvotes: 0

user4341848
user4341848

Reputation:

Replace StringStore.dump with StringStore.to_disk and StringStore.to_bytes. This has been updated in the new documentation.

Upvotes: 1

Related Questions