Compare commits

...

3 Commits

3 changed files with 20 additions and 29 deletions

View File

@ -41,39 +41,30 @@ def compile(
word_counts: Dict[int, Dict[str, int]] = {} word_counts: Dict[int, Dict[str, int]] = {}
names = [] names = tuple(categories.keys())
for category, text in categories.items(): for category, text in categories.items():
if not category in names:
names.append(category)
for word in text: for word in text:
try: if word in word_counts:
counts_for_word = word_counts[word] try:
except KeyError: word_counts[word][category] += 1
counts_for_word = {} except KeyError:
word_counts[word] = counts_for_word word_counts[word][category] = 1
else:
word_counts[word] = {category: 1}
try: category_lengths = {
word_counts[word][category] += 1 category: len(text) for category, text in categories.items()
except KeyError:
word_counts[word][category] = 1
word_counts = {
word: counts
for word, counts in word_counts.items()
if sum(counts.values()) >= min_count
} }
word_weights: Dict[int, Dict[str, float]] = {} word_weights: Dict[int, Dict[str, float]] = {
for word, values in word_counts.items(): word: {
for category, value in values.items(): category: value / category_lengths[category]
try: for category, value in values.items()
word_weights[word][category] = value / len(categories[category]) }
except KeyError: for word, values in word_counts.items()
word_weights[word] = { if sum(values.values()) >= min_count
category: value / len(categories[category]) }
}
model: Dict[int, List[int]] = {} model: Dict[int, List[int]] = {}
for word, weights in word_weights.items(): for word, weights in word_weights.items():

View File

@ -62,7 +62,7 @@ class Model:
} }
return probs return probs
def get(self, token): def get(self, token: str) -> Dict[str, float]:
try: try:
weights = self.weights[ weights = self.weights[
gptc.tokenizer.hash_single(gptc.tokenizer.normalize(token)) gptc.tokenizer.hash_single(gptc.tokenizer.normalize(token))

View File

@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "gptc" name = "gptc"
version = "3.1.0" version = "3.1.1"
description = "General-purpose text classifier" description = "General-purpose text classifier"
readme = "README.md" readme = "README.md"
authors = [{ name = "Samuel Sloniker", email = "sam@kj7rrv.com"}] authors = [{ name = "Samuel Sloniker", email = "sam@kj7rrv.com"}]