Skip to content

Commit

Permalink
Get rid of string constants in code
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiefer committed Jul 19, 2024
1 parent 15bae02 commit 2a233c9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

package de.dfki.mlt.rudimant.compiler.tree;
import static de.dfki.mlt.rudimant.agent.nlp.DialogueAct.*;

import static de.dfki.mlt.rudimant.compiler.tree.TestHelper.*;
import static de.dfki.mlt.rudimant.compiler.tree.TestUtilities.*;
Expand Down Expand Up @@ -97,11 +98,11 @@ public void testToStringDA() {

@Test
public void testFromRdf() {
Rdf da = _proxy.getClass("<dial:Accept>").getNewInstance("dial:");
Rdf da = _proxy.getClass("<dial:Accept>").getNewInstance(DIAL_NS);
da.setValue("<dial:sender>", "someone");
Rdf frame = _proxy.getClass("<sem:Answering>").getNewInstance("dial:");
Rdf frame = _proxy.getClass("<sem:Answering>").getNewInstance(DIAL_NS);
da.setValue("<sem:agent>", "someone else");
da.setValue("<dial:frame>", frame);
da.setValue(FRAME_PROPERTY, frame);

DialogueAct transformed = DialogueAct.fromRdfProper(da.getURI(), _proxy);

Expand All @@ -115,11 +116,11 @@ public void testFromRdf() {

@Test
public void testFromRdf2() {
Rdf da = _proxy.getClass("<dial:Accept>").getNewInstance("dial:");
Rdf da = _proxy.getClass("<dial:Accept>").getNewInstance(DIAL_NS);
da.setValue("<dial:sender>", "someone");
Rdf frame = _proxy.getClass("<sem:Frame>").getNewInstance("dial:");
Rdf frame = _proxy.getClass(FRAME_RDFCLASS).getNewInstance(DIAL_NS);
da.setValue("<sem:agent>", "someone else");
frame.setValue("<sem:label>", "Foo");
frame.setValue("<rdfs:label>", "Foo");
da.setValue("<dial:frame>", frame);

DialogueAct transformed = DialogueAct.fromRdfProper(da.getURI(), _proxy);
Expand All @@ -134,7 +135,7 @@ public void testFromRdf2() {

@Test
public void testToRdf2() {
Rdf sender = _proxy.getClass("<dom:Child>").getNewInstance("dial:");
Rdf sender = _proxy.getClass("<dom:Child>").getNewInstance(DIAL_NS);
DialogueAct da = new DialogueAct("@raw:ReturnGreeting(Meeting"
+ " ^ <Time>afternoon ^ <addressee>\"<dial:Child_8>\""
+ " ^ <sender>\"" + sender.getURI() + "\")");
Expand All @@ -148,8 +149,8 @@ public void testToRdf2() {
public void testToRdf1() {
// use information about entry time to make sure we do not only find an old entry
long now = System.currentTimeMillis();
Rdf sender = _proxy.getClass("<dom:Child>").getNewInstance("dial:");
_proxy.getClass("<dial:Confirm>").getNewInstance("dial:");
Rdf sender = _proxy.getClass("<dom:Child>").getNewInstance(DIAL_NS);
_proxy.getClass("<dial:Confirm>").getNewInstance(DIAL_NS);
DialogueAct da =
new DialogueAct("Inform", "Answer", "what", "solution", "sender", sender.toString() );
da.toRdf(_proxy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@
<http://www.dfki.de/lt/onto/common/dialogue.owl#follows> <http://www.w3.org/2000/01/rdf-schema#comment> "follows links the current dialogue act with its _immediate_ predecessors (a transitive property)"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#follows> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.dfki.de/lt/onto/common/dialogue.owl#DialogueAct> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#follows> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.dfki.de/lt/onto/common/dialogue.owl#DialogueAct> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#Frame> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#frame> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#FunctionalProperty> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#frame> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#Frame> <http://www.w3.org/2000/01/rdf-schema#comment> "Should be identified with sem:Frame, if available" .
<http://www.dfki.de/lt/onto/common/dialogue.owl#frame> <http://www.w3.org/2000/01/rdf-schema#comment> "the frame which encodes the (shallow) semantics/the semantic content of \n the dialogue act; in principle, this property should be part of a \n domain-specific ontology which interfaces dialogue acts and FrameNet \n frames through the property frame, by defining a domain (viz., \n dial:DialogueAct) and a range (viz., sem:Frame) restriction"^^<http://www.w3.org/2001/XMLSchema#string> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#frame> <http://www.w3.org/2000/01/rdf-schema#domain> <http://www.dfki.de/lt/onto/common/dialogue.owl#DialogueAct> .
<http://www.dfki.de/lt/onto/common/dialogue.owl#fromTime> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#DatatypeProperty> .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ sometimes called &apos;speaker&apos;; not used at the moment !!</rdfs:comment>
<owl:ObjectProperty rdf:about="http://www.dfki.de/lt/onto/common/dialogue.owl#frame">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#FunctionalProperty"/>
<rdfs:domain rdf:resource="http://www.dfki.de/lt/onto/common/dialogue.owl#DialogueAct"/>
<rdfs:range rdf:resource="http://www.dfki.de/lt/onto/common/dialogue.owl#Frame"/>
<rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string">the frame which encodes the (shallow) semantics/the semantic content of
the dialogue act; in principle, this property should be part of a
domain-specific ontology which interfaces dialogue acts and FrameNet
Expand Down Expand Up @@ -644,6 +645,14 @@ called &apos;side-participants&apos;, &apos;bystanders&apos;, and &apos;overhear



<!-- http://www.dfki.de/lt/onto/common/dialogue.owl#Frame -->

<owl:Class rdf:about="http://www.dfki.de/lt/onto/common/dialogue.owl#Frame">
<rdfs:comment>Should be identified with sem:Frame, if available</rdfs:comment>
</owl:Class>



<!-- http://www.dfki.de/lt/onto/common/dialogue.owl#GeneralPurposeFunction -->

<owl:Class rdf:about="http://www.dfki.de/lt/onto/common/dialogue.owl#GeneralPurposeFunction">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public class DialogueAct {

public static String DIAL_NS = "dial:";

public static final String DIALACT_RDFCLASS = "<dial:DialogueAct>";
public static final String FRAME_RDFCLASS = "<dial:Frame>";
public static final String PAIR_RDFCLASS = "<dial:Pair>";

public static final String FRAME_PROPERTY = "<dial:frame>";
public static final String SENDER_PROPERTY = "<dial:sender>";
public static final String ADRESSEE_PROPERTY = "<dial:addressee>";
public static final String FROMTIME_PROPERTY = "<dial:fromTime>";
public static final String TOTIME_PROPERTY = "<dial:toTime>";

public static final String NONRDFPROPS_PROPERTY = "<dial:repr>";

public static final String ARGS_PROPERTY = "<dial:hasArgs>";
public static final String FIRST_PROPERTY = "<dial:first>";
public static final String SECOND_PROPERTY = "<dial:second>";

private static Logger logger = LoggerFactory.getLogger(DialogueAct.class);

public long timeStamp;
Expand Down Expand Up @@ -206,10 +222,10 @@ private static Set<Object> addRdfArguments(StringBuilder sb, Rdf rdf, RdfProxy p
"select ?p ?o where {} ?p ?o ?_", rdf.getURI()).getTable();
for (List<String> propVal: propsValues.getRows()) {
String prop = propVal.get(0);
if (! "<dial:frame>".equals(prop) && !"<rdf:type>".equals(prop)) {
if ("<dial:hasArgs>".equals(prop)) {
if (! FRAME_PROPERTY.equals(prop) && !"<rdf:type>".equals(prop)) {
if (ARGS_PROPERTY.equals(prop)) {
result = rdf.getValue(prop);
} else if (! "<dial:repr>".equals(prop)) {
} else if (! NONRDFPROPS_PROPERTY.equals(prop)) {
Set<Object> valSet = rdf.getValue(prop);
assert(valSet.size() == 1);
Object val = valSet.iterator().next();
Expand All @@ -223,12 +239,12 @@ private static Set<Object> addRdfArguments(StringBuilder sb, Rdf rdf, RdfProxy p
/** RDF --> dag: Add initial Intent and Frame */
private static Rdf addIntentAndFrame(StringBuilder sb, String uri, RdfProxy proxy) {
Rdf da = proxy.getRdf(uri);
Rdf frame = (Rdf) da.getSingleValue("<dial:frame>");
if (! da.getClazz().isSubclassOf(proxy.getClass("<dial:DialogueAct>"))) {
Rdf frame = (Rdf) da.getSingleValue(FRAME_PROPERTY);
if (! da.getClazz().isSubclassOf(proxy.getClass(DIALACT_RDFCLASS))) {
logger.error("URI does not point to a DialogueAct: {}", uri);
}
sb.append("@raw:").append(uriToName(da.getClazz().toString()));
String frameName = (String)frame.getSingleValue("<sem:label>");
String frameName = (String)frame.getSingleValue(Rdf.RDFS_LABEL);
if (frameName == null)
frameName = uriToName(frame.getClazz().toString());
sb.append("(").append(frameName);
Expand All @@ -250,8 +266,8 @@ public static DialogueAct fromRdfProper(String uri, RdfProxy proxy) {
for(Object rdfPair: nonRdfProps) {
Rdf pair = (Rdf)rdfPair;
sb.append(" ^ <")
.append(pair.getString("<dial:first>")).append(">\"")
.append(pair.getString("<dial:second>")).append('"');
.append(pair.getString(FIRST_PROPERTY)).append(">\"")
.append(pair.getString(SECOND_PROPERTY)).append('"');
}
//extractArguments(rawDA, frame, proxy);
sb.append(')');
Expand All @@ -263,19 +279,19 @@ private Rdf storeIntentAndFrame(RdfProxy proxy) {
RdfClass diaClass = proxy.getRdfClass(getDialogueActType());
if (diaClass == null) {
logger.error("No Subclass of DialougeAct: {}", getDialogueActType());
diaClass = proxy.getClass("<dial:DialogueAct>");
diaClass = proxy.getClass(DIALACT_RDFCLASS);
}
Rdf rdfDialAct = diaClass.getNewInstance(DIAL_NS);
RdfClass frameClass = proxy.getRdfClass(getProposition());
boolean syntheticFrame = frameClass == null;
if (syntheticFrame) {
logger.warn("No Subclass of Frame: {}", getProposition());
frameClass = proxy.getClass("<sem:Frame>");
frameClass = proxy.getClass(FRAME_RDFCLASS);
}
Rdf frame = frameClass.getNewInstance(DIAL_NS);
if (syntheticFrame)
frame.setValue("<sem:label>", getProposition());
rdfDialAct.setValue("<dial:frame>", frame);
frame.setValue(Rdf.RDFS_LABEL, getProposition());
rdfDialAct.setValue(FRAME_PROPERTY, frame);
return rdfDialAct;
}

Expand Down Expand Up @@ -308,11 +324,11 @@ public Rdf toRdfProper(RdfProxy proxy) {
Rdf rdfDialAct = storeIntentAndFrame(proxy);
Set<String> props = storeRdfProperies(rdfDialAct, proxy);
for (String prop : props) {
RdfClass pairClass = proxy.getClass("<dial:Pair>");
Rdf pair = pairClass.getNewInstance("dial:");
pair.setValue("<dial:first>", prop);
pair.setValue("<dial:second>", getValue(prop));
rdfDialAct.add("<dial:hasArgs>", pair);
RdfClass pairClass = proxy.getClass(PAIR_RDFCLASS);
Rdf pair = pairClass.getNewInstance(DIAL_NS);
pair.setValue(FIRST_PROPERTY, prop);
pair.setValue(SECOND_PROPERTY, getValue(prop));
rdfDialAct.add(ARGS_PROPERTY, pair);
}
return rdfDialAct;
}
Expand All @@ -322,14 +338,14 @@ public static DialogueAct fromRdf(String uri, RdfProxy proxy) {
StringBuilder sb = new StringBuilder();
Rdf da = addIntentAndFrame(sb, uri, proxy);
addRdfArguments(sb, da, proxy);
String rep = (String)da.getSingleValue("<dial:repr>");
String rep = (String)da.getSingleValue(NONRDFPROPS_PROPERTY);
sb.append(rep).append(')');
return new DialogueAct(sb.toString());
}

/** dag --> RDF: use repr feature for non-RDF properties */
public Rdf toRdf(RdfProxy proxy) {
RdfClass dialClass = proxy.getClass("<dial:DialogueAct>");
RdfClass dialClass = proxy.getClass(DIALACT_RDFCLASS);
Rdf dial = storeIntentAndFrame(proxy);
Set<String> props = storeRdfProperies(dial, proxy);
StringBuilder sb = new StringBuilder();
Expand All @@ -342,7 +358,7 @@ public Rdf toRdf(RdfProxy proxy) {
sb.append(" ^ <").append(name).append(">\"").append(val).append('"');
}
}
dial.setValue("<dial:repr>", sb.toString());
dial.setValue(NONRDFPROPS_PROPERTY, sb.toString());
return dial;
}

Expand Down

0 comments on commit 2a233c9

Please sign in to comment.