private LabeledSentence generateSupersenseTaggingInput(Tree sentence) { LabeledSentence res = new LabeledSentence(); List<Tree> leaves = sentence.getLeaves(); for (int i = 0; i < leaves.size(); i++) { String word = leaves.get(i).label().toString(); Tree preterm = leaves.get(i).parent(sentence); String pos = preterm.label().toString(); String stem = AnalysisUtilities.getInstance().getLemma(word, pos); res.addToken(word, stem, pos, "0"); } return res; }
public List<String> annotateSentenceWithSupersenses(Tree sentence) { List<String> result = new ArrayList<String>(); int numleaves = sentence.getLeaves().size(); if (numleaves <= 1) { return result; } LabeledSentence labeled = generateSupersenseTaggingInput(sentence); // see if a NER socket server is available int port = new Integer(ARKref.getProperties().getProperty("supersenseServerPort", "5557")); String host = "127.0.0.1"; Socket client; PrintWriter pw; BufferedReader br; String line; try { client = new Socket(host, port); pw = new PrintWriter(client.getOutputStream()); br = new BufferedReader(new InputStreamReader(client.getInputStream())); String inputStr = ""; for (int i = 0; i < labeled.length(); i++) { String token = labeled.getTokens().get(i); String stem = labeled.getStems().get(i); String pos = labeled.getPOS().get(i); inputStr += token + "\t" + stem + "\t" + pos + "\n"; } pw.println(inputStr); pw.flush(); // flush to complete the transmission while ((line = br.readLine()) != null) { String[] parts = line.split("\\t"); result.add(parts[2]); } br.close(); pw.close(); client.close(); } catch (Exception ex) { if (ARKref.Opts.debug) System.err.println("Could not connect to SST server."); // ex.printStackTrace(); } // if socket server not available, then use a local NER object if (result.size() == 0) { try { if (sst == null) { DiscriminativeTagger.loadProperties(ARKref.getPropertiesPath()); sst = DiscriminativeTagger.loadModel( ARKref.getProperties() .getProperty("supersenseModelFile", "config/supersenseModel.ser.gz")); } sst.findBestLabelSequenceViterbi(labeled, sst.getWeights()); for (String pred : labeled.getPredictions()) { result.add(pred); } } catch (Exception e) { e.printStackTrace(); } } // add a bunch of blanks if necessary while (result.size() < numleaves) result.add("0"); if (ARKref.Opts.debug) System.err.println("annotateSentenceSST: " + result); return result; }