-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_analyzer.py
65 lines (55 loc) · 2.33 KB
/
input_analyzer.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
"""
To use, it's required that you:
1. Install git large-file-storage (https://git-lfs.github.com/)
2. Have pulled the model 'trained_model' down from remote using git lfs
"""
from entity_classifier import EntityClassifier
from intent_classifier import IntentClassifier
import numpy as np
class InputAnalyzer:
"""Wrapper class for intent & entity extraction."""
def __init__(self):
self.__entity_classifier = EntityClassifier()
self.__intent_classifier = IntentClassifier()
def analyze(self, subject: str):
"""
Analyzes input to determine/find intents and entities.
Returns dict of analysis in form:
{
"input": original subject input arg,
"primary_intent": string of primary intent derived from classifier
"entities": dict of entities derived from entity classifier
"other": other information extracted (adjectives, pronouns, etc)
}
"""
analysis = {
"input": subject, # original, unmodified subject
"intents": None, # string
"entities": None, # a dictionary of <entity, type> pairs
"other": None # other info we may extract (adjectives,pronouns,etc)
}
analysis['entities'] = self.extract_entities(subject)
analysis['primary_intent'] = self.extract_primary_intent(subject)
return analysis
def extract_entities(self, subject: str):
"""
Finds entities within the subject string.
Returns entities with prediction confidence % in a dictionary.
Entity Types
- PER: person
- TEAM: team or organization
"""
entity_dict = self.__entity_classifier.get_entities(subject)
# maybe process the dict here to avoid passing along unneccessary info?
return entity_dict
def extract_primary_intent(self, subject: str):
"""
Determine intent of subject string.
Here for intent people to write, unless unnecessary
"""
intent_dict = self.__intent_classifier.classify_intent(subject)
primary_intent = "" # default
max_score = np.argmax(np.array(intent_dict["scores"]))
if intent_dict["scores"][max_score] > 0.6:
primary_intent = intent_dict["labels"][max_score]
return primary_intent