-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsdnn.py
More file actions
44 lines (35 loc) · 1.28 KB
/
sdnn.py
File metadata and controls
44 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
# License: MIT
# author: Luis Rei < me@luisrei.com >
import dnnhelper
class SilkNOWmodel():
def __init__(self):
self.loaded = False
self.model = None
self.labels = None
self.vocab = None
def load(self, model_path):
"""Load a SilkNOW text classification model."""
self.model, self.labels, self.vocab = dnnhelper.load_model(model_path)
self.loaded = True
def classify_text(self, text, lang):
"""Classify a single text-lang pair."""
ds = [{'txt': text, 'lang': lang}]
r = dnnhelper.classify_text(self.model,
self.labels,
self.vocab,
ds)
return r[0]
def classify_texts(self, text_lang_pairs):
"""Classify a list of text-lang pairs."""
ds = [{'txt': p[0], 'lang': p[1]} for p in text_lang_pairs]
r = dnnhelper.classify_text(self.model,
self.labels,
self.vocab,
ds)
return r
def load_model(model_load_path):
"""Load a SilkNOW text classification model."""
model = SilkNOWmodel()
model.load(model_load_path)
return model