Java Code Examples for edu.stanford.nlp.util.StringUtils

The following examples show how to use edu.stanford.nlp.util.StringUtils. These examples are extracted from open source projects. 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
/**
 * A debugging method to try relation extraction from the console.
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
  Properties props = StringUtils.argsToProperties(args);
  props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,regexner,parse,mention,coref,kbp");
  props.setProperty("regexner.mapping", "ignorecase=true,validpospattern=^(NN|JJ).*,edu/stanford/nlp/models/kbp/regexner_caseless.tab;edu/stanford/nlp/models/kbp/regexner_cased.tab");

  StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
  IOUtils.console("sentence> ", line -> {
    Annotation ann = new Annotation(line);
    pipeline.annotate(ann);
    for (CoreMap sentence : ann.get(CoreAnnotations.SentencesAnnotation.class)) {
      sentence.get(CoreAnnotations.KBPTriplesAnnotation.class).forEach(System.err::println);
      System.out.println(sentence);
    }
  });
}
 
Example 2
public static void main(String[] args){
    try{
      Properties props = StringUtils.argsToProperties(args);
//      props.setProperty("annotators", "tokenize,ssplit,lemma,pos,parse,ner");
      StanfordCoreNLP pipeline = new StanfordCoreNLP();
      String sentence = "John Gerspach was named Chief Financial Officer of Citi in July 2009.";
      Annotation doc = new Annotation(sentence);
      pipeline.annotate(doc);
      RelationExtractorAnnotator r = new RelationExtractorAnnotator(props);
      r.annotate(doc);

      for(CoreMap s: doc.get(CoreAnnotations.SentencesAnnotation.class)){
        System.out.println("For sentence " + s.get(CoreAnnotations.TextAnnotation.class));
        List<RelationMention> rls  = s.get(RelationMentionsAnnotation.class);
        for(RelationMention rl: rls){
          System.out.println(rl.toString());
        }
      }
    }catch(Exception e){
      e.printStackTrace();
    }
  }
 
Example 3
public static void main(String[] args) throws IOException {
    Properties props = StringUtils.argsToProperties(args);
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,regexner,parse,mention,coref,kbp");
    props.setProperty("regexner.mapping", "ignorecase=true,validpospattern=^(NN|JJ).*,edu/stanford/nlp/models/kbp/regexner_caseless.tab;edu/stanford/nlp/models/kbp/regexner_cased.tab");
    Set<String> interested = Stream.of("per:title", "per:employee_of", "org:top_members/employees").collect(Collectors.toSet());
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    IOUtils.console("sentence> ", line -> {
        Annotation ann = new Annotation(line);
        pipeline.annotate(ann);
        for (CoreMap sentence : ann.get(CoreAnnotations.SentencesAnnotation.class)) {
            sentence.get(CoreAnnotations.KBPTriplesAnnotation.class).forEach(r -> {
                 String relation = r.relationGloss();
                if(interested.contains(relation)) {
                    System.err.println(r);
                }
            });
        }
    });
}
 
Example 4
Source Project: phrasal   Source File: Phrasal.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Run Phrasal from the command line.
 *
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
  final Properties options = StringUtils.argsToProperties(args);
  final String configFile = options.containsKey("") ? (String) options.get("") : null;
  options.remove("");
  if ((options.size() == 0 && configFile == null) || options.containsKey("help") || options.containsKey("h")) {
    System.err.println(usage());
    System.exit(-1);
  }

  // by default, exit on uncaught exception
  Thread.setDefaultUncaughtExceptionHandler((t, ex) -> {
    logger.fatal("Uncaught top-level exception", ex);
    System.exit(-1);
  });

  final Map<String, List<String>> configuration = getConfigurationFrom(configFile, options);
  final Phrasal p = Phrasal.loadDecoder(configuration);
  
  if (options.containsKey("text")) p.decode(new FileInputStream(new File(options.getProperty("text"))), true);
  else p.decode(System.in, true);
}
 
Example 5
Source Project: phrasal   Source File: GermanPostprocessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * A main method for training and evaluating the postprocessor.
 * 
 * @param args
 */
public static void main(String[] args) {
  // Strips off hyphens
  Properties options = StringUtils.argsToProperties(args, optionArgDefs());
  if (options.containsKey("help") || args.length == 0) {
    System.err.println(usage(GermanPostprocessor.class.getName()));
    System.exit(-1);
  }

  int nThreads = PropertiesUtils.getInt(options, "nthreads", 1);
  GermanPreprocessor preProcessor = new GermanPreprocessor();
  GermanPostprocessor postProcessor = new GermanPostprocessor(options);
  
  CRFPostprocessor.setup(postProcessor, preProcessor, options);
  CRFPostprocessor.execute(nThreads, preProcessor, postProcessor);    
}
 
Example 6
Source Project: phrasal   Source File: FrenchPostprocessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * A main method for training and evaluating the postprocessor.
 * 
 * @param args
 */
public static void main(String[] args) {
  // Strips off hyphens
  Properties options = StringUtils.argsToProperties(args, optionArgDefs());
  if (options.containsKey("help") || args.length == 0) {
    System.err.println(usage(FrenchPostprocessor.class.getName()));
    System.exit(-1);
  }

  int nThreads = PropertiesUtils.getInt(options, "nthreads", 1);
  FrenchPreprocessor preProcessor = new FrenchPreprocessor();
  FrenchPostprocessor postProcessor = new FrenchPostprocessor(options);
  
  CRFPostprocessor.setup(postProcessor, preProcessor, options);
  CRFPostprocessor.execute(nThreads, preProcessor, postProcessor);    
}
 
Example 7
Source Project: phrasal   Source File: EnglishPostprocessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * A main method for training and evaluating the postprocessor.
 * 
 * @param args
 */
public static void main(String[] args) {
  // Strips off hyphens
  Properties options = StringUtils.argsToProperties(args, optionArgDefs());
  if (options.containsKey("help") || args.length == 0) {
    System.err.println(usage(EnglishPostprocessor.class.getName()));
    System.exit(-1);
  }

  int nThreads = PropertiesUtils.getInt(options, "nthreads", 1);
  EnglishPreprocessor preProcessor = new EnglishPreprocessor();
  EnglishPostprocessor postProcessor = new EnglishPostprocessor(options);
  
  CRFPostprocessor.setup(postProcessor, preProcessor, options);
  CRFPostprocessor.execute(nThreads, preProcessor, postProcessor);    
}
 
Example 8
Source Project: phrasal   Source File: SpanishPostprocessor.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * A main method for training and evaluating the postprocessor.
 * 
 * @param args
 */
public static void main(String[] args) {
  // Strips off hyphens
  Properties options = StringUtils.argsToProperties(args, optionArgDefs());
  if (options.containsKey("help") || args.length == 0) {
    System.err.println(usage(SpanishPostprocessor.class.getName()));
    System.exit(-1);
  }

  int nThreads = PropertiesUtils.getInt(options, "nthreads", 1);
  SpanishPreprocessor preProcessor = new SpanishPreprocessor();
  SpanishPostprocessor postProcessor = new SpanishPostprocessor(options);
  
  CRFPostprocessor.setup(postProcessor, preProcessor, options);
  CRFPostprocessor.execute(nThreads, preProcessor, postProcessor);    
}
 
Example 9
Source Project: phrasal   Source File: SyntheticRules.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static <TK,FV> int srcCompoundCnt(IString srcToken, IString tgtToken, List<DynamicTranslationModel<FV>> tmList) {
  int srcSize = srcToken.length(); 
  int cnt = 0;
  // each compound should have at least 4 characters
  for(int k = 4; k < srcSize - 3 ; ++k) {
    IString[] preSuffixes = new IString[3];
    preSuffixes[0] = new IString(srcToken.subSequence(0, k).toString()); // prefix
    preSuffixes[1] = new IString(srcToken.subSequence(srcSize - k , srcSize).toString()); // suffix
    preSuffixes[2] = new IString(StringUtils.capitalize(srcToken.subSequence(srcSize - k, srcSize).toString())); // capitalized suffix
    
    int mMax = preSuffixes[1] == preSuffixes[2] ? 2 : 3;
    
    for(int m = 0; m < mMax; ++m) {
      IString src = preSuffixes[m];
      if(!srcGarbageCollection(src, tmList)) {
        cnt += tmList.stream().mapToInt(tm -> tm.getJointLexCount(src, tgtToken)).sum();
      }
    }
  }
  return cnt;
}
 
Example 10
Source Project: phrasal   Source File: SyntheticRules.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static <TK,FV> int tgtCompoundCnt(IString srcToken, IString tgtToken, List<DynamicTranslationModel<FV>> tmList) {
  int tgtSize = tgtToken.length(); 
  int cnt = 0;
  // each compound should have at least 4 characters
  for(int k = 4; k < tgtSize - 3 ; ++k) {
    IString[] preSuffixes = new IString[3];
    preSuffixes[0] = new IString(tgtToken.subSequence(0, k).toString()); // prefix
    preSuffixes[1] = new IString(tgtToken.subSequence(tgtSize - k , tgtSize).toString()); // suffix
    preSuffixes[2] = new IString(StringUtils.capitalize(tgtToken.subSequence(tgtSize - k, tgtSize).toString())); // capitalized suffix
    
    int mMax = preSuffixes[1] == preSuffixes[2] ? 2 : 3;
    
    for(int m = 0; m < mMax; ++m) {
      IString tgt = preSuffixes[m];
      if(!tgtGarbageCollection(tgt, tmList)) {
        cnt += tmList.stream().mapToInt(tm -> tm.getJointLexCount(srcToken, tgt)).sum();
      }
    }
  }
  return cnt;
}
 
Example 11
Source Project: JHazm   Source File: POSTaggerTest.java    License: MIT License 6 votes vote down vote up
@Test
public void batchTagTest() throws IOException {
    POSTagger tagger = new POSTagger();

    String[] input = new String[] { "من", "به", "مدرسه", "رفته بودم", "."};
    List<TaggedWord> expected = new ArrayList<>();
    expected.add(new TaggedWord("من","PR"));
    expected.add(new TaggedWord("به","PREP"));
    expected.add(new TaggedWord("مدرسه","N"));
    expected.add(new TaggedWord("رفته بودم","V"));
    expected.add(new TaggedWord(".","PUNC"));
    List<TaggedWord> actual = tagger.batchTag(Arrays.asList(input));

    assertEquals("Failed to tagged words of '" + StringUtils.join(input, " ") + "' sentence", expected.size(), actual.size());
    for (int i = 0; i < expected.size(); i++) {
        TaggedWord actualTaggedWord = actual.get(i);
        TaggedWord expectedTaggedWord = expected.get(i);
        if (!actualTaggedWord.tag().equals(expectedTaggedWord.tag()))
            assertEquals("Failed to tagged words of '" + StringUtils.join(input, " ") + "' sentence", expectedTaggedWord, actualTaggedWord);
    }
}
 
Example 12
/**
  * 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 13
Source Project: ambiverse-nlu   Source File: YAGO3DBReader.java    License: 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 14
Source Project: ambiverse-nlu   Source File: GenericDBReader.java    License: 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 15
public static void main(String[] args) {
    String path = IntelConfig.DEPARTMENT_TRAIN_PROPERTY;
    Properties props = StringUtils.propFileToProperties(path);

    SeqClassifierFlags flags = new SeqClassifierFlags(props);
    CRFClassifier<CoreLabel> crf = new CRFClassifier<CoreLabel>(flags);
    crf.train();
    String modelPath = props.getProperty("serializeTo");
    crf.serializeClassifier(modelPath);
    System.out.println("Build model to " + modelPath);
}
 
Example 16
Source Project: phrasal   Source File: MakeWordClasses.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
  Properties options = StringUtils.argsToProperties(args, optionArgDefs());
  String[] filenames = options.getProperty("","").split("\\s+");
  if (filenames.length < 1 || filenames[0].length() == 0 || options.containsKey("h")
      || options.containsKey("help")) {
    System.err.println(usage());
    System.exit(-1);
  }
  MakeWordClasses mkWordCls = new MakeWordClasses(options);
  mkWordCls.run(filenames);
  mkWordCls.writeResults(System.out);
}
 
Example 17
Source Project: phrasal   Source File: CRFPreprocessor.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static CRFClassifier<CoreLabel> loadClassifier(String options) throws IllegalArgumentException {
  String[] inputFlags = options.split(" ");
  Properties props = StringUtils.argsToProperties(inputFlags);
  SeqClassifierFlags flags = new SeqClassifierFlags(props);
  CRFClassifier<CoreLabel> crfSegmenter = new CRFClassifier<>(flags);
  if(flags.loadClassifier == null) {
    throw new IllegalArgumentException("missing -loadClassifier flag for CRF preprocessor.");
  }
  crfSegmenter.loadClassifierNoExceptions(flags.loadClassifier, props);
  crfSegmenter.loadTagIndex();
  return crfSegmenter;
}
 
Example 18
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 19
Source Project: phrasal   Source File: MinimumBayesRisk.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * 
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
  if (args.length < 1) {
    System.err.print(usage());
    System.exit(-1);
  }

  Properties options = StringUtils.argsToProperties(args, argDefs());
  final double scale = PropertiesUtils.getDouble(options, "s", DEFAULT_SCALE);
  final String orientation = options.getProperty("o", "utility");
  final boolean risk = "risk".equals(orientation);
  final String metricName = options.getProperty("m", DEFAULT_METRIC);

  final String filename = options.getProperty("");
  BasicNBestList nbestlists = new BasicNBestList(filename);
  MulticoreWrapper<List<BasicNBestEntry>, List<Pair<Double, String>>> wrapper = 
    new MulticoreWrapper<List<BasicNBestEntry>, List<Pair<Double, String>>>(0, new Processor(metricName, risk, scale), true);
  for (List<BasicNBestEntry> nbestlist : nbestlists) {
    wrapper.put(nbestlist);
    while (wrapper.peek()) {
      DumpRescored(wrapper.poll());
    }
  }
  wrapper.join();
  while (wrapper.peek()) {
    DumpRescored(wrapper.poll());
  }
}
 
Example 20
public static void main(String[] args) {

    Properties options = StringUtils.argsToProperties(args, optionArgDefs());
    String annotations = PropertiesUtils.get(options, "annotations", null, String.class);
    
    boolean changepreps = PropertiesUtils.getBool(options, "changepreps", false);
    
    int sentenceCount = CoreNLPCache.loadSerialized(annotations);
   
    
    CoreMap sentence;
    for (int i = 0; i < sentenceCount; i++) {
      try {  
        sentence = CoreNLPCache.get(i);
        if (sentence == null) {
          System.out.println();
          System.err.println("Empty sentence #" + i);
          continue;
        }
        printDependencies(sentence, changepreps);
        //System.err.println("---------------------------");
      } catch (Exception e) {
        System.err.println("SourceSentence #" + i);
        e.printStackTrace();
        return;
      }
    }
  }
 
Example 21
/**
 * 
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
  if (args.length < 1) {
    System.err.print(usage());
    System.exit(-1);
  }
  
  Properties options = StringUtils.argsToProperties(args, argDefs());
  int ngramOrder = PropertiesUtils.getInt(options, "order", BLEUMetric.DEFAULT_MAX_NGRAM_ORDER);
  boolean disableTokenization = PropertiesUtils.getBool(options, "no-nist", false);
  String metric = options.getProperty("metric", "bleu");

  String[] refs = options.getProperty("").split("\\s+");
  List<List<Sequence<IString>>> referencesList = MetricUtils.readReferences(refs, ! disableTokenization);
  System.err.printf("Metric: %s with %d references%n", metric, referencesList.get(0).size());
  
  LineNumberReader reader = new LineNumberReader(new InputStreamReader(
      System.in));
  int sourceInputId = 0;
  for (String line; (line = reader.readLine()) != null; ++sourceInputId) {
    line = disableTokenization ? line : NISTTokenizer.tokenize(line);
    Sequence<IString> translation = IStrings.tokenize(line);
    double score = getScore(translation, referencesList.get(sourceInputId), ngramOrder, metric);
    System.out.printf("%.4f%n", score);
  }
  System.err.printf("Scored %d input segments%n", sourceInputId);
}
 
Example 22
Source Project: phrasal   Source File: BasicNBestEntry.java    License: GNU General Public License v3.0 5 votes vote down vote up
public BasicNBestEntry(String input) {
  line = input;
  List<List<String>> fields = StringUtils.splitFieldsFast(input.trim(), "|||");
  sentenceNumber = Integer.valueOf(fields.get(0).get(0));
  tokens = IStrings.toIStringSequence(fields.get(1));
  score = Float.parseFloat(fields.get(3).get(0));
}
 
Example 23
Source Project: phrasal   Source File: PhraseViewer.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static boolean validateCommandLine(String[] args) {
  // Command line parsing
  Properties options = StringUtils.argsToProperties(args, argDefs());

  VERBOSE = options.containsKey("v");
  SRC_FILE = options.getProperty("s", null);
  OPTS_FILE = options.getProperty("o", null);
  XSD_FILE = options.getProperty("x", null);
  FIRST_ID = PropertiesUtils.getInt(options, "f", Integer.MIN_VALUE);
  LAST_ID = PropertiesUtils.getInt(options,"l",Integer.MAX_VALUE);

  return true;
}
 
Example 24
Source Project: QuestionAnsweringSystem   Source File: AskServlet.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json;charset=UTF-8"); 
    response.setCharacterEncoding("UTF-8"); 
    request.setCharacterEncoding("UTF-8");
    
    String questionStr = request.getParameter("q");
    String n = request.getParameter("n");
    int topN = -1;
    if(n != null && StringUtils.isNumeric(n)){
        topN = Integer.parseInt(n);
    }
    Question question = null;
    List<CandidateAnswer> candidateAnswers = null;
    if (questionStr != null && questionStr.trim().length() > 3) {
        question = SharedQuestionAnsweringSystem.getInstance().answerQuestion(questionStr);
        if (question != null) {
            candidateAnswers = question.getAllCandidateAnswer();
        }
    }
    LOG.info("问题:"+questionStr); 
    try (PrintWriter out = response.getWriter()) {
        String json = JsonGenerator.generate(candidateAnswers, topN);
        out.println(json);
        LOG.info("答案:"+json);
    }
}
 
Example 25
Source Project: winter   Source File: LodCsvTableWriter.java    License: Apache License 2.0 4 votes vote down vote up
protected void writeLine(List<String> values, BufferedWriter w) throws IOException {
	w.write(String.format("\"%s\"\n", StringUtils.join(values, "\",\"")));		
	values.clear();
}
 
Example 26
public String getSubjectText() {
    return StringUtils.join(sentence.originalTexts().subList(subjectSpan.start(), subjectSpan.end()).stream(), " ");
}
 
Example 27
public String getObjectText() {
    return StringUtils.join(sentence.originalTexts().subList(objectSpan.start(), objectSpan.end()).stream(), " ");
}
 
Example 28
public String getSubjectText() {
  return StringUtils.join(sentence.originalTexts().subList(subjectSpan.start(), subjectSpan.end()).stream(), " ");
}
 
Example 29
public String getObjectText() {
  return StringUtils.join(sentence.originalTexts().subList(objectSpan.start(), objectSpan.end()).stream(), " ");
}
 
Example 30
Source Project: bioasq   Source File: PassageCandidateProvider.java    License: Apache License 2.0 4 votes vote down vote up
private boolean match(String candidateText, String gsText) {
  return Math.max(candidateText.length(), gsText.length()) * 0.9 <
          StringUtils.longestCommonSubstring(candidateText, gsText);
}