-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcustom_extractors.py
207 lines (167 loc) · 6.88 KB
/
custom_extractors.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import logging
import os
import typing
from typing import Any, Dict, List, Optional, Text, Tuple
import numpy as np
from indic_transliteration import sanscript
from indic_transliteration.sanscript import SCHEMES, SchemeMap, transliterate
from rasa.nlu import utils
from rasa.nlu.components import Component
from rasa.nlu.config import InvalidConfigError, RasaNLUModelConfig
from rasa.nlu.model import Metadata
from rasa.nlu.training_data import Message, TrainingData
class ScriptExtractor(Component):
"""Does not require any kind of featurization."""
provides = ["script"]
def __init__(self, component_config: Dict[Text, Any] = None) -> None:
super(ScriptExtractor, self).__init__(component_config)
def get_script(self, text: Text) -> Text:
"""
Very stOOpid logic to extract devanagari.
Detect script from the text message.
If ascii characters than 50%, then latin else devanagari script.
"""
# assume latin to be the default script
script = "latin"
count = 0
for ch in text:
if ord(ch) < 128:
count += 1
len_text = len(text) or 1 # avoid zero division
if count/len_text < 0.5:
script = "devanagari"
return script
def train(self, training_data: TrainingData, cfg: RasaNLUModelConfig, **kwargs: Any) -> None:
"""Not a trainable component."""
for example in training_data.intent_examples:
script = self.get_script(example.text)
example.set("script", script, add_to_output=True)
return None
def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:
return None
@classmethod
def load(cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional[Metadata] = None,
cached_component: Optional["ScriptExtractor"] = None,
**kwargs: Any) -> "ScriptExtractor":
return cls(meta)
def process(self, message: Message, **kwargs: Any) -> None:
"""Return script of the text for a message."""
script = self.get_script(message.text)
message.set("script", script, add_to_output=True)
class LatinTextExtractor(Component):
"""Does not require any kind of featurization."""
requires = [
"script"
]
provides = [
"latin_text"
]
@classmethod
def required_packages(cls):
return ["indic_transliteration"]
def __init__(self, component_config: Dict[Text, Any] = None) -> None:
super(LatinTextExtractor, self).__init__(component_config)
def train(self, training_data: TrainingData, cfg: RasaNLUModelConfig, **kwargs: Any) -> None:
"""Not a trainable component."""
for example in training_data.intent_examples:
latin_text = self.get_latin(example)
example.set("latin_text", latin_text, add_to_output=True)
return None
def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:
"""Nothing to persist here."""
return None
@classmethod
def load(cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional[Metadata] = None,
cached_component: Optional["ScriptExtractor"] = None,
**kwargs: Any) -> "ScriptExtractor":
return cls(meta)
def get_latin(self, message: Message) -> Text:
text = message.text
script = message.get("script")
latin_text = text
if script == "devanagari":
latin_text = transliterate(
text, sanscript.DEVANAGARI, sanscript.OPTITRANS).lower()
return latin_text
def process(self, message: Message, **kwargs: Any) -> None:
"""Convert text into latin from devanagari."""
latin_text = self.get_latin(message)
message.set("latin_text", latin_text, add_to_output=True)
class LanguageExtractor(Component):
"""Requires features."""
requires = [
"latin_text_features",
# "script" -> bad idea!
]
provides = [
"language"
]
@classmethod
def required_packages(cls):
return ["sklearn"]
def __init__(self,
component_config: Dict[Text, Any] = None,
clf: "LanguageExtractor" = None
) -> None:
super(LanguageExtractor, self).__init__(component_config)
self.clf = clf
def train(self, training_data: TrainingData, cfg: RasaNLUModelConfig, **kwargs: Any) -> None:
"""Train language classifier."""
X = []
y = []
for example in training_data.intent_examples:
latin_text_features = example.get("latin_text_features")
# Well this is hacked!!
intent = example.get("intent")
language = "hi" if intent.startswith("hi_") else "en"
example.set("language", {"name": language, "confidence": 1.0})
X.append(latin_text_features)
y.append(language)
X = np.array(X)
y = np.array(y)
# from sklearn.svm import SVC
# from sklearn.linear_model import LogisticRegression
# clf = LogisticRegression(penalty="l1")
from sklearn.svm import SVC
clf = SVC(kernel="linear", probability=True, C=10)
clf.fit(X, y)
logging.info("classification score: {}".format(clf.score(X, y)))
self.clf = clf
def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:
"""Persist this model into the passed directory."""
classifier_file_name = file_name+"_classifier.pkl"
if self.clf:
utils.json_pickle(os.path.join(
model_dir, classifier_file_name), self.clf)
return {"classifier": classifier_file_name}
@classmethod
def load(
cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional[Metadata] = None,
cached_component: Optional["LanguageExtractor"] = None,
**kwargs: Any
) -> "LanguageExtractor":
from sklearn.svm import SVC
# from sklearn.linear_model import LogisticRegression
classifier_file = os.path.join(model_dir, meta.get("classifier"))
if os.path.exists(classifier_file):
classifier = utils.json_unpickle(classifier_file)
return cls(meta, classifier)
return cls(meta)
def process(self, message: Message, **kwargs: Any) -> None:
"""Return language of the latin text for a message."""
latin_text_features = message.get("latin_text_features")
probs = self.clf.predict_proba([latin_text_features])[0]
index = np.argmax(probs)
confidence = probs[index]
language = self.clf.classes_[index]
language = {"name": language, "confidence": confidence}
message.set("language", language, add_to_output=True)