Skip to content

Commit

Permalink
Implemented new syntax for supervision rules
Browse files Browse the repository at this point in the history
  • Loading branch information
seojiwon committed Sep 1, 2015
1 parent 7413ed6 commit 5efe177
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 51 deletions.
3 changes: 1 addition & 2 deletions examples/chunking.ddlog
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ word_features +=
words(sent_id, word_id1, word1, pos1, tag1),
words(sent_id, word_id2, word2, pos2, tag2).

@label(tag)
tag(word_id) :- words(word_id, a, b, c, tag).
tag(word_id) = tag :- words(word_id, a, b, c, tag).

@weight(f)
tag(word_id) :- word_features(word_id, f).
8 changes: 3 additions & 5 deletions examples/ocr.ddlog
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ label2(
q1?(wid INT).
q2?(wid INT).

@label(val)
q1(wid) :- label1(wid, val).
@label(val)
q2(wid) :- label2(wid, val).
q1(wid) = val :- label1(wid, val).
q2(wid) = val :- label2(wid, val).

@weight(fid)
q1(wid) :- features(id, wid, fid, fval).
@weight(fid)
q2(wid) :- features(id, wid, fid, fval).
q2(wid) :- features(id, wid, fid, fval).
8 changes: 3 additions & 5 deletions examples/smoke.ddlog
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ cancer? (
person_id bigint
).

@label(l)
smoke(pid) :- person_smokes(pid, l).
smoke(pid) = l :- person_smokes(pid, l).

@label(l)
cancer(pid) :- person_has_cancer(pid, l).
cancer(pid) = l :- person_has_cancer(pid, l).

@weight(0.5)
smoke(pid) => cancer(pid) :- person_smokes(pid, l).

@weight(0.4)
smoke(pid1) => smoke(pid) :- friends(pid1, pid).
smoke(pid1) => smoke(pid) :- friends(pid1, pid).
3 changes: 1 addition & 2 deletions examples/spouse_example.ddlog
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ has_spouse_features +=
people_mentions(s, p1idx, p1len, _, person1_id),
people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
has_spouse(rid) :- has_spouse_candidates(_, _, _, _, rid, l).
has_spouse(rid) = l :- has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
has_spouse(rid) :-
Expand Down
13 changes: 10 additions & 3 deletions src/main/scala/org/deepdive/ddlog/DeepDiveLogParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,22 @@ class DeepDiveLogParser extends JavaTokenParsers {
ConjunctiveQuery(head, disjunctiveBodies, isDistinct != None, limit map (_.toInt))
}

def supervision = "=" ~> (variableName | "TRUE" | "FALSE")

def conjunctiveQueryWithSupervision = // returns Parser[String], Parser[ConjunctiveQuery]
cqHeadTerms ~ opt("*") ~ opt("|" ~> decimalNumber) ~ opt(supervision) ~ ":-" ~ rep1sep(cqConjunctiveBody, ";") ^^ {
case (head ~ isDistinct ~ limit ~ sup ~ ":-" ~ disjunctiveBodies) =>
(sup, ConjunctiveQuery(head, disjunctiveBodies, isDistinct != None, limit map (_.toInt)))
}

def functionCallRule : Parser[FunctionCallRule] =
relationName ~ "+=" ~ functionName ~ conjunctiveQuery ^^ {
case (out ~ _ ~ func ~ cq) => FunctionCallRule(out, func, cq)
}

def supervision = "@label" ~> "(" ~> variableName <~ ")"
def extractionRule =
opt(supervision) ~ relationName ~ conjunctiveQuery ^^ {
case (sup ~ head ~ cq) => ExtractionRule(head, cq, sup)
relationName ~ conjunctiveQueryWithSupervision ^^ {
case (head ~ cq) => ExtractionRule(head, cq._2, cq._1)
}

def factorWeight = "@weight" ~> "(" ~> rep1sep(expr, ",") <~ ")" ^^ { FactorWeight(_) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ object DeepDiveLogPrettyPrinter extends DeepDiveLogHandler {
case b: QuantifiedBody => print(b)
}

def print(cq: ConjunctiveQuery): String = {
def print(cq: ConjunctiveQuery, supervision: String = ""): String = {

def printBodyList(b: List[Body]) = {
s"${(b map print).mkString(",\n ")}"
Expand All @@ -145,7 +145,7 @@ object DeepDiveLogPrettyPrinter extends DeepDiveLogHandler {
val headStrTmp = cq.headTerms map print mkString(", ")
val headStr = if (headStrTmp isEmpty) "" else s"(${headStrTmp})"

headStr + distinctStr + limitStr + " :-\n " + bodyStr
headStr + distinctStr + limitStr + supervision + " :-\n " + bodyStr
}

def print(a: HeadAtom) : String = {
Expand All @@ -168,8 +168,8 @@ object DeepDiveLogPrettyPrinter extends DeepDiveLogHandler {
}

def print(stmt: ExtractionRule): String = {
( stmt.supervision map (s => s"@label(${s})\n") getOrElse("") ) +
stmt.headName + print(stmt.q) + ".\n"
var supervision = stmt.supervision map (s => s" = ${s}") getOrElse("");
stmt.headName + print(stmt.q, supervision) + ".\n"
}

def print(stmt: FunctionCallRule): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ dd_new_q2(wid) :-
q2(wid);
dd_delta_q2(wid).

@label(val)
dd_delta_q1(wid) :-
dd_delta_q1(wid) = val :-
dd_delta_label1(wid, val).

@label(val)
dd_delta_q2(wid) :-
dd_delta_q2(wid) = val :-
dd_delta_label2(wid, val).

@weight(fid)
Expand Down
6 changes: 2 additions & 4 deletions test/expected-output-test/ocr_example/print.expected
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ q1?(wid INT).

q2?(wid INT).

@label(val)
q1(wid) :-
q1(wid) = val :-
label1(wid, val).

@label(val)
q2(wid) :-
q2(wid) = val :-
label2(wid, val).

@weight(fid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ dd_new_cancer(person_id) :-
cancer(person_id);
dd_delta_cancer(person_id).

@label(l)
dd_delta_smoke(pid) :-
dd_delta_smoke(pid) = l :-
dd_delta_person_smokes(pid, l).

@label(l)
dd_delta_cancer(pid) :-
dd_delta_cancer(pid) = l:-
dd_delta_person_has_cancer(pid, l).

@weight(0.5)
Expand Down
6 changes: 2 additions & 4 deletions test/expected-output-test/smoke_example/print.expected
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ smoke?(person_id bigint).

cancer?(person_id bigint).

@label(l)
smoke(pid) :-
smoke(pid) = l :-
person_smokes(pid, l).

@label(l)
cancer(pid) :-
cancer(pid) = l :-
person_has_cancer(pid, l).

@weight(0.5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ dd_delta_has_spouse_features += ext_has_spouse_features(ARRAY_TO_STRING(words, "
dd_new_people_mentions(s, p1idx, p1len, _, person1_id),
dd_delta_people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
dd_delta_has_spouse(rid) :-
dd_delta_has_spouse(rid) = l :-
dd_delta_has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
Expand Down
3 changes: 1 addition & 2 deletions test/expected-output-test/spouse_example/print.expected
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ has_spouse_features += ext_has_spouse_features(ARRAY_TO_STRING(words, "~^~"), ri
people_mentions(s, p1idx, p1len, _, person1_id),
people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
has_spouse(rid) :-
has_spouse(rid) = l :-
has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ has_spouse_features +=
people_mentions(s, p1idx, p1len, _, person1_id),
people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
has_spouse(rid) :- has_spouse_candidates(_, _, _, _, rid, l).
has_spouse(rid) = l :- has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
has_spouse(rid) :-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ dd_delta_has_spouse_features += ext_has_spouse_features(ARRAY_TO_STRING(words, "
dd_new_people_mentions(s, p1idx, p1len, _, person1_id),
dd_delta_people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
dd_delta_has_spouse(rid) :-
dd_delta_has_spouse(rid) = l:-
dd_delta_has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ has_spouse_features +=
people_mentions(s, p1idx, p1len, _, person1_id),
people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
has_spouse(rid) :- has_spouse_candidates(_, _, _, _, rid, l).
has_spouse(rid) = l :- has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
has_spouse(rid) :-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ dd_delta_has_spouse_features += ext_has_spouse_features(ARRAY_TO_STRING(words, "
dd_new_people_mentions(s, p1idx, p1len, _, person1_id),
dd_delta_people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
dd_delta_has_spouse(rid) :-
dd_delta_has_spouse(rid) = l :-
dd_delta_has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ has_spouse_features += ext_has_spouse_features(ARRAY_TO_STRING(words, "~^~"), ri
people_mentions(s, p1idx, p1len, _, person1_id),
people_mentions(s, p2idx, p2len, _, person2_id).

@label(l)
has_spouse(rid) :-
has_spouse(rid) = l :-
has_spouse_candidates(_, _, _, _, rid, l).

@weight(f)
Expand Down

0 comments on commit 5efe177

Please sign in to comment.