Add framework for inter-version compatibility
This commit is contained in:
parent
91e35a0d61
commit
a5d7143a13
|
@ -2,3 +2,4 @@
|
||||||
|
|
||||||
from gptc.compiler import compile
|
from gptc.compiler import compile
|
||||||
from gptc.classifier import Classifier
|
from gptc.classifier import Classifier
|
||||||
|
from gptc.exceptions import *
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import gptc.tokenizer
|
import gptc.tokenizer, gptc.compiler, gptc.exceptions
|
||||||
|
import warnings
|
||||||
|
|
||||||
class Classifier:
|
class Classifier:
|
||||||
"""A text classifier.
|
"""A text classifier.
|
||||||
|
@ -16,7 +17,22 @@ class Classifier:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, model):
|
def __init__(self, model):
|
||||||
self.model = model
|
try:
|
||||||
|
model_version = model['__version__']
|
||||||
|
except:
|
||||||
|
model_version = 1
|
||||||
|
|
||||||
|
if model_version == 1:
|
||||||
|
self.model = model
|
||||||
|
else:
|
||||||
|
# The model is an unsupported version
|
||||||
|
try:
|
||||||
|
raw_model = model['__raw__']
|
||||||
|
except:
|
||||||
|
raise gptc.exceptions.UnsupportedModelError('this model is unsupported and does not contain a raw model for recompiling')
|
||||||
|
|
||||||
|
warnings.warn("model needed to be recompiled on-the-fly; please re-compile it and use the new compiled model in the future")
|
||||||
|
self.model = gptc.compiler.compile(raw_model)
|
||||||
|
|
||||||
def classify(self, text):
|
def classify(self, text):
|
||||||
"""Classify text.
|
"""Classify text.
|
||||||
|
@ -34,6 +50,7 @@ class Classifier:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
model = self.model
|
model = self.model
|
||||||
|
|
||||||
text = gptc.tokenizer.tokenize(text)
|
text = gptc.tokenizer.tokenize(text)
|
||||||
probs = {}
|
probs = {}
|
||||||
for word in text:
|
for word in text:
|
||||||
|
|
|
@ -42,4 +42,7 @@ def compile(raw_model):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
word_weights[word] = {category:value}
|
word_weights[word] = {category:value}
|
||||||
|
|
||||||
|
word_weights['__version__'] = 1
|
||||||
|
word_weights['__raw__'] = raw_model
|
||||||
|
|
||||||
return word_weights
|
return word_weights
|
||||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user