- 5.6 存储标注器
5.6 存储标注器
在大语料库上训练一个标注器可能需要大量的时间。没有必要在每次我们需要的时候训练一个标注器,很容易将一个训练好的标注器保存到一个文件以后重复使用。让我们保存我们的标注器t2到文件t2.pkl。
>>> from pickle import dump>>> output = open('t2.pkl', 'wb')>>> dump(t2, output, -1)>>> output.close()
现在,我们可以在一个单独的 Python 进程中,我们可以载入保存的标注器。
>>> from pickle import load>>> input = open('t2.pkl', 'rb')>>> tagger = load(input)>>> input.close()
现在让我们检查它是否可以用来标注。
>>> text = """The board's action shows what free enterprise... is up against in our complex maze of regulatory laws .""">>> tokens = text.split()>>> tagger.tag(tokens)[('The', 'AT'), ("board's", 'NN$'), ('action', 'NN'), ('shows', 'NNS'),('what', 'WDT'), ('free', 'JJ'), ('enterprise', 'NN'), ('is', 'BEZ'),('up', 'RP'), ('against', 'IN'), ('in', 'IN'), ('our', 'PP$'), ('complex', 'JJ'),('maze', 'NN'), ('of', 'IN'), ('regulatory', 'NN'), ('laws', 'NNS'), ('.', '.')]
