Java Code Examples for edu.stanford.nlp.util.StringUtils#join()

The following examples show how to use edu.stanford.nlp.util.StringUtils#join() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiLangsStanfordCoreNLPClient.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
/**
  * The main constructor. Create a client from a properties file and a list of backends.
  * Note that this creates at least one Daemon thread.
  *
  * @param properties The properties file, as would be passed to {@link StanfordCoreNLP}.
  * @param backends The backends to run on.
  * @param apiKey The key to authenticate with as a username
  * @param apiSecret The key to authenticate with as a password
  */
 private MultiLangsStanfordCoreNLPClient(Properties properties, List<Backend> backends,
                               String apiKey, String apiSecret, String lang) { 
this.lang = lang;
   // Save the constructor variables
   this.properties = properties;
   Properties serverProperties = new Properties();
   for (String key : properties.stringPropertyNames()) {
     serverProperties.setProperty(key, properties.getProperty(key));
   }
   Collections.shuffle(backends, new Random(System.currentTimeMillis()));
   this.scheduler = new BackendScheduler(backends);
   this.apiKey = apiKey;
   this.apiSecret = apiSecret;

   // Set required serverProperties
   serverProperties.setProperty("inputFormat", "serialized");
   serverProperties.setProperty("outputFormat", "serialized");
   serverProperties.setProperty("inputSerializer", ProtobufAnnotationSerializer.class.getName());
   serverProperties.setProperty("outputSerializer", ProtobufAnnotationSerializer.class.getName());

   // Create a list of all the properties, as JSON map elements
   List<String> jsonProperties = serverProperties.stringPropertyNames().stream().map(key -> '"' + StringUtils.escapeJsonString(key) +
           "\": \"" + StringUtils.escapeJsonString(serverProperties.getProperty(key)) + '"')
       .collect(Collectors.toList());
   // Create the JSON object
   this.propsAsJSON = "{ " + StringUtils.join(jsonProperties, ", ") + " }";

   // Start 'er up
   this.scheduler.start();
 }
 
Example 2
Source File: YAGO3DBReader.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public List<Fact> getFacts(String subject, String relation, String object) throws SQLException {
  List<Fact> triples = new LinkedList<>();
  Statement stmt;
  ResultSet rs;

  Connection con = EntityLinkingManager.getConnectionForDatabase(EntityLinkingManager.DB_YAGO);

  List<String> queryCondition = new LinkedList<>();
  if (subject != null) {
    queryCondition.add("subject='" + StringEscapeUtils.escapeSql(subject) + "'");
  }
  if (relation != null) {
    queryCondition.add("predicate='" + StringEscapeUtils.escapeSql(relation) + "'");
  }

  if (object != null) {
    queryCondition.add("object='" + StringEscapeUtils.escapeSql(object) + "'");
  }

  String conditionString = StringUtils.join(queryCondition, " AND ");
  stmt = con.createStatement();
  String sql = "SELECT id, subject, object, predicate  FROM " + tableName + " WHERE " + conditionString;
  rs = stmt.executeQuery(sql);

  while (rs.next()) {
    triples.add(new Fact(rs.getString("id"), rs.getString("subject"), rs.getString("predicate"), rs.getString("object")));
  }
  rs.close();

  EntityLinkingManager.releaseConnection(con);

  return triples;
}
 
Example 3
Source File: GenericDBReader.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public List<Fact> getFacts(String subject, String relation, String object) throws SQLException {
	List<Fact> triples = new LinkedList<>();
	Statement stmt;
	ResultSet rs;

	Connection con = EntityLinkingManager.getConnectionForDatabase(EntityLinkingManager.DB_YAGO);

	List<String> queryCondition = new LinkedList<>();
	if (subject != null) {
		queryCondition.add("subject = '" + StringEscapeUtils.escapeSql(subject) + "'");
	}
	if (relation != null) {
		queryCondition.add("predicate = '" + StringEscapeUtils.escapeSql(relation) + "'");
	}

	if (object != null) {
		queryCondition.add("object = '" + StringEscapeUtils.escapeSql(object) + "'");
	}

	String conditionString = StringUtils.join(queryCondition, " AND ");
	stmt = con.createStatement();
	String sql = "SELECT id, subject, object, predicate  FROM " + tableName + " WHERE " + conditionString;
	rs = stmt.executeQuery(sql);

	while (rs.next()) {
		triples.add(new Fact(rs.getString("id"), rs.getString("subject"), rs.getString("predicate"),
				rs.getString("object")));
	}
	rs.close();

	EntityLinkingManager.releaseConnection(con);

	return triples;
}
 
Example 4
Source File: DependencyBnBPreorderer.java    From phrasal with GNU General Public License v3.0 5 votes vote down vote up
private static String preorder(Tree tree) {
  
  List<Tree> queue = new LinkedList<>();
  queue.add(tree);
  
  
  while ( ! queue.isEmpty()) {
    Tree currentNode = queue.remove(0);
    
    if (currentNode.isLeaf())
      continue;
    
    Tree children[] = currentNode.children();
    int childCount = children.length;
    IndexedWord hw = (IndexedWord) currentNode.label();
    List<FeatureNode> featureNodes = new ArrayList<>(childCount);
    for (int i = 0; i < childCount; i++) {
      featureNodes.add(new FeatureNode(children[i], hw));
      queue.add(children[i]);
    }
    if (childCount < 8) {
      Pair<Double, List<Integer>> result = search(featureNodes, new LinkedList<Integer>(), Double.NEGATIVE_INFINITY);
      if (result != null) {
        List<Integer> permutation = result.second;
        List<Tree> newChildren = new ArrayList<>(Arrays.asList(children));
        for (int i = 0; i < childCount; i++) {
          int idx = permutation.get(i);
          newChildren.set(idx, children[i]);
        }
        currentNode.setChildren(newChildren);
      } else {
        System.err.println("Warning: No path found.");
      }
    }
  }
  
  return StringUtils.join(tree.yieldWords());
}
 
Example 5
Source File: IntelKBPRelationExtractor.java    From InformationExtraction with GNU General Public License v3.0 4 votes vote down vote up
public String getSubjectText() {
    return StringUtils.join(sentence.originalTexts().subList(subjectSpan.start(), subjectSpan.end()).stream(), " ");
}
 
Example 6
Source File: IntelKBPRelationExtractor.java    From InformationExtraction with GNU General Public License v3.0 4 votes vote down vote up
public String getObjectText() {
    return StringUtils.join(sentence.originalTexts().subList(objectSpan.start(), objectSpan.end()).stream(), " ");
}
 
Example 7
Source File: KBPRelationExtractor.java    From InformationExtraction with GNU General Public License v3.0 4 votes vote down vote up
public String getSubjectText() {
  return StringUtils.join(sentence.originalTexts().subList(subjectSpan.start(), subjectSpan.end()).stream(), " ");
}
 
Example 8
Source File: KBPRelationExtractor.java    From InformationExtraction with GNU General Public License v3.0 4 votes vote down vote up
public String getObjectText() {
  return StringUtils.join(sentence.originalTexts().subList(objectSpan.start(), objectSpan.end()).stream(), " ");
}
 
Example 9
Source File: U.java    From tac2015-event-detection with GNU General Public License v3.0 4 votes vote down vote up
public static String sp(double[] x) {
	ArrayList<String> parts = new ArrayList<String>();
	for (int i=0; i < x.length; i++)
		parts.add(String.format("%.2g", x[i]));
	return "[" + StringUtils.join(parts) + "]";
}