-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathapp.ddlog
244 lines (211 loc) · 7.03 KB
/
app.ddlog
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
## Random variable to predict #################################################
# This application's goal is to predict whether a given pair of person mention
# are indicating a spouse relationship or not.
@extraction
has_spouse?(
@key
@references(relation="person_mention", column="mention_id", alias="p1")
p1_id text,
@key
@references(relation="person_mention", column="mention_id", alias="p2")
p2_id text
).
## Input Data #################################################################
@source
articles(
@key
@distributed_by
id text,
@searchable
content text
).
## NLP markup #################################################################
@source
sentences(
@key
@distributed_by
# XXX This breaks the search index. @source should not be derived from another @source
#@references(relation="articles", column="id")
doc_id text,
@key
sentence_index int,
@search_type("text[]")
tokens json,
@search_type("text[]")
lemmas json,
@search_type("text[]")
pos_tags json,
@search_type("text[]")
ner_tags json,
@search_type("int[]")
doc_offsets json,
@search_type("text[]")
dep_types json,
@search_type("int[]")
dep_tokens json
).
function nlp_markup over (
doc_id text,
content text
) returns rows like sentences
implementation "udf/nlp_markup.sh" handles tsj lines.
sentences += nlp_markup(doc_id, content) :-
articles(doc_id, content).
## Candidate mapping ##########################################################
@extraction
person_mention(
@key
mention_id text,
@searchable
mention_text text,
@distributed_by
@references(relation="sentences", column="doc_id", alias="appears_in")
doc_id text,
@references(relation="sentences", column="sentence_index", alias="appears_in")
sentence_index int,
begin_index int,
end_index int
).
function map_person_mention over (
doc_id text,
sentence_index int,
tokens text[],
ner_tags text[]
) returns rows like person_mention
implementation "udf/map_person_mention.py" handles tsj lines.
person_mention += map_person_mention(
doc_id, sentence_index, tokens, ner_tags
) :-
sentences(doc_id, sentence_index, tokens, _, _, ner_tags, _, _, _).
spouse_candidate(
p1_id text,
p1_name text,
p2_id text,
p2_name text
).
num_people(doc_id, sentence_index, COUNT(p)) :-
person_mention(p, _, doc_id, sentence_index, _, _).
spouse_candidate(p1, p1_name, p2, p2_name) :-
num_people(same_doc, same_sentence, num_p),
person_mention(p1, p1_name, same_doc, same_sentence, p1_begin, _),
person_mention(p2, p2_name, same_doc, same_sentence, p2_begin, _),
num_p < 5,
p1 < p2,
p1_name != p2_name,
p1_begin != p2_begin.
## Feature Extraction #########################################################
# Feature extraction (using DDLIB via a UDF) at the relation level
@extraction
spouse_feature(
@key
@references(relation="has_spouse", column="p1_id", alias="has_spouse")
p1_id text,
@key
@references(relation="has_spouse", column="p2_id", alias="has_spouse")
p2_id text,
@key
feature text
).
function extract_spouse_features over (
p1_id text,
p2_id text,
p1_begin_index int,
p1_end_index int,
p2_begin_index int,
p2_end_index int,
doc_id text,
sent_index int,
tokens text[],
lemmas text[],
pos_tags text[],
ner_tags text[],
dep_types text[],
dep_tokens int[]
) returns rows like spouse_feature
implementation "udf/extract_spouse_features.py" handles tsj lines.
spouse_feature += extract_spouse_features(
p1_id, p2_id, p1_begin_index, p1_end_index, p2_begin_index, p2_end_index,
doc_id, sent_index, tokens, lemmas, pos_tags, ner_tags, dep_types, dep_tokens
) :-
person_mention(p1_id, _, doc_id, sent_index, p1_begin_index, p1_end_index),
person_mention(p2_id, _, doc_id, sent_index, p2_begin_index, p2_end_index),
sentences(doc_id, sent_index, tokens, lemmas, pos_tags, ner_tags, _, dep_types, dep_tokens).
## Distant Supervision ########################################################
@extraction
spouse_label(
@key
@references(relation="has_spouse", column="p1_id", alias="has_spouse")
p1_id text,
@key
@references(relation="has_spouse", column="p2_id", alias="has_spouse")
p2_id text,
@navigable
label int,
@navigable
rule_id text
).
# make sure all pairs in spouse_candidate are considered as unsupervised examples
spouse_label(p1,p2, 0, NULL) :- spouse_candidate(p1, _, p2, _).
# distant supervision using data from DBpedia
@source
spouses_dbpedia(
@key
person1_name text,
@key
person2_name text
).
spouse_label(p1,p2, 1, "from_dbpedia") :-
spouse_candidate(p1, p1_name, p2, p2_name),
spouses_dbpedia(n1, n2),
[ lower(n1) = lower(p1_name), lower(n2) = lower(p2_name) ;
lower(n2) = lower(p1_name), lower(n1) = lower(p2_name) ].
# supervision by heuristic rules in a UDF
function supervise over (
p1_id text, p1_begin int, p1_end int,
p2_id text, p2_begin int, p2_end int,
doc_id text,
sentence_index int,
sentence_text text,
tokens text[],
lemmas text[],
pos_tags text[],
ner_tags text[],
dep_types text[],
dep_tokens int[]
) returns (
p1_id text, p2_id text, label int, rule_id text
)
implementation "udf/supervise_spouse.py" handles tsj lines.
spouse_label += supervise(
p1_id, p1_begin, p1_end,
p2_id, p2_begin, p2_end,
doc_id, sentence_index,
tokens, lemmas, pos_tags, ner_tags, dep_types, dep_token_indexes
) :-
spouse_candidate(p1_id, _, p2_id, _),
person_mention(p1_id, p1_text, doc_id, sentence_index, p1_begin, p1_end),
person_mention(p2_id, p2_text, _, _, p2_begin, p2_end),
sentences(
doc_id, sentence_index,
tokens, lemmas, pos_tags, ner_tags, _, dep_types, dep_token_indexes
).
# resolve multiple labels by majority vote (summing the labels in {-1,0,1})
spouse_label_resolved(p1_id, p2_id, SUM(vote)) :- spouse_label(p1_id, p2_id, vote, rule_id).
# assign the resolved labels for the spouse relation
@materialize
has_spouse(p1_id, p2_id) = if l > 0 then TRUE
else if l < 0 then FALSE
else NULL end :- spouse_label_resolved(p1_id, p2_id, l).
## Inference Rules ############################################################
# Features
@weight(f)
has_spouse(p1_id, p2_id) :-
spouse_feature(p1_id, p2_id, f).
# Inference rule: Symmetry
@weight(3.0)
has_spouse(p1_id, p2_id) => has_spouse(p2_id, p1_id) :-
TRUE.
# Inference rule: Only one marriage
@weight(-1.0)
has_spouse(p1_id, p2_id) => has_spouse(p1_id, p3_id) :-
TRUE.