-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPOS_NER_Example.py
More file actions
117 lines (95 loc) · 4.6 KB
/
Copy pathPOS_NER_Example.py
File metadata and controls
117 lines (95 loc) · 4.6 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
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
from emora_stdm import DialogueFlow
from enum import Enum, auto
####################################################################################
# Demonstrates how to use the NER and POS macros in Natex Expressions.
#
# You MUST be using v1.29 of emora_stdm.
# To upgrade to latest version:
# pip install --upgrade emora_stdm
#
# If that doesn't work, try
# pip uninstall emora_stdm
# pip install emora_stdm==1.29
#
# You use these macros similar to the ONT macro, except you do
# not need to build the dictionary of information.
#
# ----------
# NER
# ----------
# The NER macro uses the SpaCy package to identify named
# entities in a piece of text. Named entities are things like
# people, companies, locations, and so on.
#
# A list of possible categories of named entities that can be recognized
# can be found at https://spacy.io/api/annotation#named-entities
#
# ----------
# POS
# ----------
# The POS macro uses the SpaCy package to identify words
# in a piece of text based on its part of speech (noun, verb, adjective, etc.)
#
# A list of possible types of parts of speech that can be recognized can
# be found at https://spacy.io/api/annotation#pos-universal
#
# ----------
# Sample Conversation:
# ----------
# S: Who is your favorite celebrity?
# U: Loading wordnet.json... done in 6.014761924743652schris evans
# S: I really like chris evans too! Where do you think they live?
# U: hawaii
# S: I seem to not know that location. What activities do you think they like to do?
# U: reading
# S: Ok, reading , interesting. How would you describe that activity or your opinion on it?
# U: it is fun
# S: Yeah for sure. It is fun . So, what other celebrity do you like?
# U: miley cyrus
# S: I really like miley cyrus too! Where do you think they live?
# U: hollywood
# S: Yes, their home is likely to be hollywood . What activities do you think they like to do?
# U: running or walking on the beach
# S: Ok, running , interesting. How would you describe that activity or your opinion on it?
# U: it is difficult
# S: Yeah for sure. It is difficult . So, what other celebrity do you like?
####################################################################################
class State(Enum):
START = auto()
ASK_CELEB = auto()
REC_CELEB_ANS = auto()
COMMENT_CELEB = auto()
UNKNOWN_CELEB = auto()
UNKNOWN_LOC = auto()
CELEB_LOCATION = auto()
ACK = auto()
ACT = auto()
REC_ACT= auto()
UNKNOWN_ACT= auto()
DESC= auto()
REC_DESC= auto()
UNKNOWN_DESC= auto()
df = DialogueFlow(State.START, initial_speaker=DialogueFlow.Speaker.SYSTEM)
df.add_system_transition(State.START, State.ASK_CELEB, '"Who is your favorite celebrity?"')
# NER to find people names that are mentioned
df.add_user_transition(State.ASK_CELEB, State.REC_CELEB_ANS, '[$fave_celeb=#NER(person)]')
df.set_error_successor(State.ASK_CELEB, State.UNKNOWN_CELEB)
df.add_system_transition(State.REC_CELEB_ANS, State.CELEB_LOCATION, '[!"I really like" $fave_celeb "too! Where do you think they live?"]')
df.add_system_transition(State.UNKNOWN_CELEB, State.CELEB_LOCATION, '"Hmm... I do not know them. Where do you think they live?"')
# NER to find locations that are mentioned
df.add_user_transition(State.CELEB_LOCATION, State.ACK, '[$loc=#NER(gpe)]')
df.set_error_successor(State.CELEB_LOCATION, State.UNKNOWN_LOC)
df.add_system_transition(State.ACK, State.ACT, '[!"Yes, their home is likely to be" $loc ". What activities do you think they like to do?"]')
df.add_system_transition(State.UNKNOWN_LOC, State.ACT, '"I seem to not know that location. What activities do you think they like to do?"')
# POS to find nouns or verbs
df.add_user_transition(State.ACT, State.REC_ACT, '[$activity=#POS(noun,verb)]')
df.set_error_successor(State.ACT, State.UNKNOWN_ACT)
df.add_system_transition(State.REC_ACT, State.DESC, '[!"Ok, " $activity ", interesting. How would you describe that activity or your opinion on it?"]')
df.add_system_transition(State.UNKNOWN_ACT, State.DESC, '"I have never done that activity before. How would you describe that activity or your opinion on it?"')
# POS to find adjectives
df.add_user_transition(State.DESC, State.REC_DESC, '[$description=#POS(adj)]')
df.set_error_successor(State.DESC, State.UNKNOWN_DESC)
df.add_system_transition(State.REC_DESC, State.ASK_CELEB, '[!"Yeah for sure. It is " $description ". So, what other celebrity do you like?"]')
df.add_system_transition(State.UNKNOWN_DESC, State.ASK_CELEB, '"You have very interesting ideas about that. So, what other celebrity do you like?"')
if __name__ == "__main__":
df.run(debugging=True)