1   package eu.fbk.dkm.premon.premonitor;
2   
3   import com.google.common.collect.Lists;
4   import eu.fbk.dkm.premon.premonitor.propbank.Inflection;
5   import eu.fbk.dkm.premon.premonitor.propbank.Role;
6   import eu.fbk.dkm.premon.premonitor.propbank.Roleset;
7   import eu.fbk.dkm.premon.vocab.NIF;
8   import eu.fbk.dkm.premon.vocab.PMONB;
9   import org.openrdf.model.URI;
10  import org.openrdf.rio.RDFHandler;
11  
12  import java.io.File;
13  import java.util.*;
14  import java.util.regex.Matcher;
15  
16  /**
17   * Created by alessio on 03/11/15.
18   */
19  
20  public class NombankConverter extends BankConverter {
21  
22      private static String LINK_PATTERN = "http://nlp.cs.nyu.edu/meyers/nombank/nombank.1.0/frames/%s.xml";
23  
24      public NombankConverter(File path, RDFHandler sink, Properties properties, Map<String, URI> wnInfo) {
25          super(path, properties.getProperty("source"), sink, properties, properties.getProperty("language"), wnInfo);
26  
27          this.nonVerbsToo = true;
28          this.isOntoNotes = false;
29          this.noDef = !properties.getProperty("extractdefinitions", "0").equals("1");
30          this.defaultType = "n";
31      }
32  
33      @Override protected URI addExampleArgToSink(Type argType, String argName, URI markableURI,
34              String f, String rolesetID, URI asURI) {
35          URI argumentURI = uriForArgument(rolesetID, argName);
36  
37          switch (argType) {
38          case NUMERIC:
39              addStatementToSink(markableURI, NIF.ANNOTATION_P, asURI, EXAMPLE_GRAPH);
40              Type fType = getType(f);
41              switch (fType) {
42              case M_FUNCTION:
43                  if (f.equals("mnr")) {
44                      break;
45                  }
46  
47                  if (f.equals("prd")) {
48                      addStatementToSink(asURI, PMONB.TAG_P, PMONB.mapO.get(f), EXAMPLE_GRAPH);
49                  } else {
50                      throw new IllegalArgumentException(String.format("String %s not found", f));
51                  }
52                  break;
53              case ADDITIONAL:
54                  addStatementToSink(asURI, PMONB.TAG_P, PMONB.mapO.get(f), EXAMPLE_GRAPH);
55                  break;
56              default:
57                  // FC: it happens, don't know if it is OK
58              }
59              break;
60          case M_FUNCTION:
61              addStatementToSink(markableURI, NIF.ANNOTATION_P, asURI, EXAMPLE_GRAPH);
62              break;
63          case ADDITIONAL:
64              addStatementToSink(asURI, PMONB.TAG_P, PMONB.mapO.get(argName), EXAMPLE_GRAPH);
65              break;
66          default:
67              //todo: should never happen, but it happens
68          }
69  
70          return argumentURI;
71      }
72  
73      @Override protected void addRelToSink(Type argType, String argName, URI markableURI) {
74          // in NomBank <rel> is always attributeless
75      }
76  
77      @Override URI getPredicate() {
78          return PMONB.ROLESET;
79      }
80  
81      @Override URI getSemanticArgument() {
82          return PMONB.SEMANTIC_ROLE;
83      }
84  
85      @Override URI getRoleToArgumentProperty() {
86          return PMONB.ARGUMENT_P;
87      }
88  
89      @Override URI getCoreProperty() {
90          return PMONB.CORE;
91      }
92  
93      @Override HashMap<String, URI> getFunctionMap() {
94          return PMONB.mapM;
95      }
96  
97      @Override void addInflectionToSink(URI exampleURI, Inflection inflection) {
98  
99      }
100 
101     @Override protected URI getExternalLink(String lemma, String type) {
102         return createURI(String.format(LINK_PATTERN, lemma));
103     }
104 
105     @Override void addArgumentToSink(URI argumentURI, String argName, String f, Type argType,
106             String lemma, String type, String rolesetID, URI lexicalEntryURI, Role role, Roleset roleset) {
107         // F is not present in NomBank
108 
109         String key;
110         URI keyURI;
111 
112         switch (argType) {
113         case NUMERIC:
114             key = argName;
115             keyURI = PMONB.mapF.get(argName);
116             break;
117         case AGENT:
118             key = "a";
119             keyURI = PMONB.ARGA;
120             break;
121         default:
122             return;
123         }
124 
125         List<String> vnLemmas = Lists.newArrayList();
126         for (Matcher matcher : getPropBankPredicates(roleset.getSource())) {
127             vnLemmas.add(getLemmaFromPredicateName(matcher.group(2)));
128         }
129 
130         addArgumentToSink(key, keyURI, argumentURI, lemma, type, rolesetID, lexicalEntryURI, role, vnLemmas);
131 
132         // todo: bad! this should be merged with the addExternalLinks method
133         // URI argConceptualizationURI = uriForConceptualization(lemma, type, rolesetID, key);
134         ArrayList<Matcher> matchers = getPropBankPredicates(roleset.getSource());
135 
136         URI rolesetURI = uriForRoleset(rolesetID);
137         URI conceptualizationURI = uriForConceptualization(lemma, type, rolesetID);
138 
139         for (Matcher matcher : matchers) {
140             String pbLemma = matcher.group(2);
141             String pbPredicate = matcher.group(1);
142 
143             //todo: really bad check
144             String source = role.getSource();
145             if (source != null && source.length() > 0) {
146                 key = source;
147             }
148 
149             for (String pbLink : pbLinks) {
150                 pbLemma = getLemmaFromPredicateName(pbLemma);
151                 // URI argPropBankConceptualizationURI = uriForConceptualizationWithPrefix(pbLemma, "v", pbPredicate, key, pbLink);
152                 // addSingleMapping(prefix, DEFAULT_ARG_SUFFIX, argConceptualizationURI, argPropBankConceptualizationURI);
153 
154                 URI pbRolesetURI = uriForRoleset(pbPredicate, pbLink);
155                 URI pbConceptualizationURI = uriForConceptualizationWithPrefix(pbLemma, "v", pbPredicate, pbLink);
156                 URI pbArgumentURI = uriForArgument(pbPredicate, key, pbLink);
157 
158                 addMappings(rolesetURI, pbRolesetURI, conceptualizationURI, pbConceptualizationURI, argumentURI, pbArgumentURI);
159             }
160         }
161     }
162 
163     @Override Type getType(String code) {
164         if (code != null) {
165             if (PMONB.mapM.containsKey(code)) {
166                 return Type.M_FUNCTION;
167             }
168             if (PMONB.mapO.containsKey(code)) {
169                 return Type.ADDITIONAL;
170             }
171 
172             Matcher matcher = ARG_NUM_PATTERN.matcher(code);
173             if (matcher.find()) {
174                 return Type.NUMERIC;
175             }
176 
177             if (code.equals("a")) {
178                 return Type.AGENT;
179             }
180 
181             //todo: what id MOD?
182             if (code.equals("mod")) {
183                 return Type.NULL;
184             }
185 
186             throw new IllegalArgumentException(String.format("String %s not found", code));
187         }
188         return Type.NULL;
189     }
190 
191     protected void addExternalLinks(ComplexLemmaWithMappings complexLemmaWithMappings, URI conceptualizationURI, String uriLemma, String type) {
192 
193         Set<String> lemmas = new HashSet<>();
194 
195         String rolesetID = complexLemmaWithMappings.getRolesetID();
196         URI rolesetURI = uriForRoleset(rolesetID);
197 
198         // PropBank
199         ArrayList<Matcher> matchers = getPropBankPredicates(complexLemmaWithMappings.getPbSource());
200         for (Matcher matcher : matchers) {
201             String pbLemma = matcher.group(2);
202             String lemma = getLemmaFromPredicateName(pbLemma);
203             lemmas.add(lemma);
204             String pbPredicate = matcher.group(1);
205 
206             for (String pbLink : pbLinks) {
207                 URI pbRolesetURI = uriForRoleset(pbPredicate, pbLink);
208                 URI pbConceptualizationURI = uriForConceptualizationWithPrefix(lemma, "v", pbPredicate, pbLink);
209                 addMappings(rolesetURI, pbRolesetURI, conceptualizationURI, pbConceptualizationURI);
210             }
211         }
212 
213         // VerbNet
214         List<String> vnClasses = getVnClasses(complexLemmaWithMappings.getVn());
215         for (String vnClass : vnClasses) {
216             for (String vnLink : vnLinks) {
217                 for (String lemma : lemmas) {
218                     URI vnClassURI = uriForRoleset(vnClass, vnLink);
219                     URI vnConceptualizationURI = uriForConceptualizationWithPrefix(lemma, "v", vnClass, vnLink);
220                     addMappings(rolesetURI, vnClassURI, conceptualizationURI, vnConceptualizationURI);
221                 }
222             }
223         }
224 
225     }
226 }