List of usage examples for org.apache.commons.lang StringUtils abbreviate
public static String abbreviate(String str, int maxWidth)
Abbreviates a String using ellipses.
From source file:com.sfs.ucm.controller.HelpContentAction.java
/** * Extract help content//from w ww . j a v a 2s . c om * * @param buf * byte array representing help content file * @throws UCMException */ private void extractHelpContent(byte[] buf) throws UCMException { try { InputStream inp = new ByteArrayInputStream(buf); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0); Iterator<Row> iter = sheet.iterator(); Cell cell = null; Row row = null; // header rows if (iter.hasNext()) { row = iter.next(); // table title row = iter.next(); // column headers } if (iter.hasNext()) { while (iter.hasNext()) { // process records row = iter.next(); // help key cell = row.getCell(0); if (cell != null) { String key = cell.getStringCellValue(); // help content cell = row.getCell(1); String contents = cell.getStringCellValue(); // log it Object[] values = new Object[3]; values[0] = row.getRowNum() + 1; // display as one-based values[1] = key; values[2] = StringUtils.abbreviate(contents, 20); logger.info("Processing row {}; contents: {};{}", values); // construct the help content object Help theHelpItem = new Help(key, contents); // if help item already exists then just update its contents otherwise add record int ndx = this.helpItems.indexOf(theHelpItem); if (ndx == -1) { this.helpItems.add(theHelpItem); logger.info("Added Help Item {}", theHelpItem.getKeyword()); // persist the object em.persist(theHelpItem); } else { Help tmp = this.helpItems.get(ndx); tmp.setContent(contents); // persist the object em.persist(tmp); logger.info("Updated Help Item {}", tmp.getKeyword()); } } } } // done inp.close(); } catch (InvalidFormatException e) { logger.error(e.getMessage()); throw new UCMException(e); } catch (IOException e) { logger.error(e.getMessage()); throw new UCMException(e); } }
From source file:com.haulmont.cuba.web.app.loginwindow.AppLoginWindow.java
protected void doLogin() { String login = loginField.getValue(); String password = passwordField.getValue() != null ? passwordField.getValue() : ""; if (StringUtils.isEmpty(login) || StringUtils.isEmpty(password)) { showNotification(messages.getMainMessage("loginWindow.emptyLoginOrPassword"), NotificationType.WARNING); return;/* w ww .j a v a 2 s. c o m*/ } try { Locale selectedLocale = localesSelect.getValue(); app.setLocale(selectedLocale); if (loginByRememberMe && webConfig.getRememberMeEnabled()) { doLogin(new RememberMeCredentials(login, password, selectedLocale)); } else { doLogin(new LoginPasswordCredentials(login, password, selectedLocale)); } // locale could be set on the server if (connection.getSession() != null) { Locale loggedInLocale = connection.getSession().getLocale(); if (globalConfig.getLocaleSelectVisible()) { app.addCookie(App.COOKIE_LOCALE, loggedInLocale.toLanguageTag()); } } } catch (InternalAuthenticationException e) { log.error("Internal error during login", e); showUnhandledExceptionOnLogin(e); } catch (LoginException e) { log.info("Login failed: {}", e.toString()); String message = StringUtils.abbreviate(e.getMessage(), 1000); showLoginException(message); } catch (Exception e) { log.warn("Unable to login", e); showUnhandledExceptionOnLogin(e); } }
From source file:com.sfs.captor.controller.RequirementAction.java
/** * Validate requirement/*from w ww. j a v a 2 s .c o m*/ * <ul> * <li>If new requirement check for duplicate</li> * </ul> * * @return flag true if validation is successful */ private boolean validate() { boolean isvalid = true; if (this.requirement.getId() == null) { if (this.requirements.contains(this.requirement)) { this.facesContextMessage.errorMessage("{0} already exists", StringUtils.abbreviate(this.requirement.getName(), 25)); logger.error("{} already exists", this.requirement.getName()); isvalid = false; RequestContext requestContext = RequestContext.getCurrentInstance(); requestContext.addCallbackParam("validationFailed", !isvalid); } } return isvalid; }
From source file:de.tudarmstadt.lt.lm.app.SentPerp.java
void run(Reader r) { long l = 0;//from w w w . ja va2 s .c om for (LineIterator liter = new LineIterator(r); liter.hasNext();) { if (++l % 5000 == 0) LOG.info("{}: processing line {}.", _rmi_string, l); String line = liter.next(); if (line.trim().isEmpty()) continue; List<String> sentences; try { sentences = _lm_prvdr.splitSentences(line); } catch (Exception e) { LOG.error("{}: Could not split sentences from line {}: '{}'.", _rmi_string, l, StringUtils.abbreviate(line, 100), e); continue; } for (String sentence : sentences) { double p_log10_sent = 0d; double num_words = 0d; List<String> tokens; List<String>[] ngrams; try { tokens = _lm_prvdr.tokenizeSentence(sentence); if (tokens == null || tokens.isEmpty()) continue; ngrams = _lm_prvdr.getNgramSequence(tokens); if (ngrams == null || ngrams.length == 0) continue; } catch (Exception e) { LOG.error("{}: Could not get ngrams from line {}: '{}'.", _rmi_string, l, StringUtils.abbreviate(line, 100), e); continue; } for (List<String> ngram : ngrams) { if (ngram.isEmpty()) continue; _num_ngrams++; try { if (_lm_prvdr_oovref.ngramContainsOOV(ngram)) { _oov_ngrams++; if (_lm_prvdr_oovref.ngramEndsWithOOV(ngram)) { _oov_terms++; if (_no_oov) continue; } } double log10prob = _lm_prvdr.getNgramLog10Probability(ngram); p_log10_sent += log10prob; num_words++; if (log10prob < _min_prob) { _min_prob = log10prob; _min_ngram = ngram; } if (log10prob > _max_prob) { _max_prob = log10prob; _max_ngram = ngram; } } catch (Exception e) { LOG.error("{}: Could not add ngram '{}' to perplexity.", _rmi_string, ngram); continue; } } if (num_words == 0) continue; p_log10_sent = p_log10_sent / num_words;// (double)tokens.size(); _num_sents++; _sum_log10_prob_sents += p_log10_sent; } } }
From source file:de.tudarmstadt.lt.ltbot.postprocessor.DecesiveValueProducerPerplexity.java
double getPerplexity(CrawlURI uri) { assert _lmprvdr != null : "String provider service must not be null here. This should have been checked before."; String cleaned_plaintext = _textExtractorInstance.getCleanedUtf8PlainText(uri).trim(); String cleaned_plaintext_abbr = MULTIPLE_SPACES_PATTERN .matcher(StringUtils.abbreviate(cleaned_plaintext, 50)).replaceAll(" "); addExtraInfo(uri, EXTRA_INFO_PLAINTEXT_ABBREVIATED, cleaned_plaintext_abbr); if (cleaned_plaintext.isEmpty()) return Double.POSITIVE_INFINITY; double perplexity = Double.POSITIVE_INFINITY; try {//from ww w . j a v a 2 s . c om String docid = "#" + Integer.toHexString(cleaned_plaintext.hashCode()); LOG.finest(String.format("Sending text with id '%s' to StringProvider: '%s' (length %d).", docid, cleaned_plaintext_abbr, cleaned_plaintext.length())); perplexity = computePerplexity(cleaned_plaintext); // if (Double.isNaN(perplexity)) { // double perplexity_new = -1d; // LOG.log(Level.WARNING, String.format("[%s '%s'] failed to get meaningful perplexity: %g. Setting perplexity to %g.", uri.toString(), cleaned_plaintext_abbr, perplexity, perplexity_new)); // perplexity = perplexity_new; // } LOG.finest(String.format("[%s, '%s'] perplexity: %g.", uri.toString(), cleaned_plaintext_abbr, perplexity)); } catch (Throwable t) { for (int i = 1; t != null && i < 10; i++) { LOG.log(Level.SEVERE, String.format("Could not compute perplexity for URI '%s' and text: '%s'. (%d %s:%s)", uri.toString(), cleaned_plaintext_abbr, i, t.getClass().getSimpleName(), t.getMessage()), t); t = t.getCause(); LOG.log(Level.SEVERE, "Requesting to pause crawl."); getCrawlController().requestCrawlPause(); _paused_due_to_error = true; } } if (!Double.isFinite(perplexity) || perplexity <= 1) { LOG.log(Level.FINE, String.format( "[%s '%s'] resetting infinite perplexity to predefined maximum perplexity value (-1).", uri.toString(), cleaned_plaintext_abbr)); perplexity = -1; } return perplexity; }
From source file:com.intuit.tank.common.ScriptUtil.java
/** * @param step//w ww . j a va2 s. c o m * @return */ private static String getStepLabel(ScriptStep step) { StringBuilder label = new StringBuilder(); if (step.getType().equalsIgnoreCase(ScriptConstants.REQUEST)) { label.append(step.getProtocol()).append("://").append(step.getHostname()).append(step.getSimplePath()); int qsCount = 0; if (step.getQueryStrings() != null) { for (RequestData qs : step.getQueryStrings()) { label.append(qsCount == 0 ? "?" : "&"); label.append(qs.getKey()).append("=").append(qs.getValue()); qsCount++; } } } else if (step.getType().equalsIgnoreCase(ScriptConstants.VARIABLE)) { Set<RequestData> setData = step.getData(); if (null != setData) { Iterator<RequestData> iter = setData.iterator(); while (iter.hasNext()) { RequestData d = iter.next(); label.append("Variable definition " + d.getKey() + "=>" + d.getValue()); } } } else if (step.getType().equalsIgnoreCase(ScriptConstants.AUTHENTICATION)) { Set<RequestData> setData = step.getData(); if (null != setData) { String scheme = "ALL"; String host = ""; String user = ""; Iterator<RequestData> iter = setData.iterator(); while (iter.hasNext()) { RequestData d = iter.next(); if (d.getKey().equals(ScriptConstants.AUTH_SCHEME)) { scheme = d.getValue(); } else if (d.getKey().equals(ScriptConstants.AUTH_HOST)) { host = d.getValue(); } else if (d.getKey().equals(ScriptConstants.AUTH_USER_NAME)) { user = d.getValue(); } } label.append("Authentication " + scheme + " [host: " + host + " user: " + user + "]"); } } else if (step.getType().equalsIgnoreCase(ScriptConstants.THINK_TIME)) { String min = "0"; String max = "0"; Set<RequestData> setData = step.getData(); if (null != setData) { Iterator<RequestData> iter = setData.iterator(); while (iter.hasNext()) { RequestData d = iter.next(); if (d.getKey().contains("min")) min = d.getValue(); else if (d.getKey().contains("max")) max = d.getValue(); } } label.append("Think time " + min + "-" + max); } else if (step.getType().equalsIgnoreCase(ScriptConstants.LOGIC)) { label.append("Logic Step: " + step.getName()); } else if (step.getType().equalsIgnoreCase(ScriptConstants.COOKIE)) { String name = ""; String value = ""; for (RequestData requestData : step.getData()) { if (ScriptConstants.COOKIE_NAME.equals(requestData.getKey())) { name = requestData.getValue(); } if (ScriptConstants.COOKIE_VALUE.equals(requestData.getKey())) { value = requestData.getValue(); } // if (ScriptConstants.COOKIE_DOMAIN.equals(requestData.getKey())) { // domain = requestData.getValue(); // } } label.append("Set Cookie: ").append(name).append(" = ").append(value).toString(); } else if (step.getType().equalsIgnoreCase(ScriptConstants.SLEEP)) { Set<RequestData> setData = step.getData(); if (null != setData) { Iterator<RequestData> iter = setData.iterator(); while (iter.hasNext()) { RequestData d = iter.next(); label.append("Sleep for " + d.getValue()); } } } else if (step.getType().equalsIgnoreCase(ScriptConstants.CLEAR)) { label.append("Clear session"); } else if (step.getType().equalsIgnoreCase(ScriptConstants.TIMER)) { String name = null; String timerAction = null; for (RequestData rd : step.getData()) { if (rd.getKey().equalsIgnoreCase(ScriptConstants.IS_START)) { timerAction = rd.getValue(); } if (rd.getKey().equalsIgnoreCase(ScriptConstants.LOGGING_KEY)) { name = rd.getValue(); } } label.append(name).append(":").append(timerAction); } return StringUtils.abbreviate(label.toString(), 1024); }
From source file:de.tudarmstadt.lt.n2n.annotators.RelationAnnotator.java
private void searchForPath(JCas aJCas) { List<Relation> relations_to_remove = new LinkedList<Relation>(); for (Relation relation : JCasUtil.select(aJCas, Relation.class)) { Entity e1 = relation.getE1(); Entity e2 = relation.getE2(); Sentence covering_sentence = relation.getCoveringSentence(); LOG.trace(/*from ww w .ja v a 2 s .c om*/ "searching for a shortest path in the dependency graph for relation ({}-{},{}-{}) in sentence [{}].", e1.getCoveredText(), e1.getBegin(), e2.getCoveredText(), e2.getBegin(), StringUtils.abbreviate(covering_sentence.getCoveredText(), 50)); List<Dependency> dependency_path = null; try { dependency_path = find_path(e1, e2, JCasUtil.selectCovered(Dependency.class, covering_sentence)); if (dependency_path == null) { // no path found relations_to_remove.add(relation); LOG.debug("Removing relation ({}-{},{}-{}) in sentence [{}]. No dependency path was found.", e1.getCoveredText(), e1.getBegin(), e2.getCoveredText(), e2.getBegin(), StringUtils.abbreviate(covering_sentence.getCoveredText(), 50)); continue; } } catch (IllegalStateException e) { relations_to_remove.add(relation); LOG.warn(String.format("%s: '%s.'", e.getClass().getSimpleName(), e.getMessage())); continue; } int path_length = dependency_path.size(); // dependencies are edges thus the path length is len(edges) LOG.trace("Found shortest path with length {} for relation ({}-{},{}-{}) in sentence [{}].", path_length, e1.getCoveredText(), e1.getBegin(), e2.getCoveredText(), e2.getBegin(), StringUtils.abbreviate(covering_sentence.getCoveredText(), 50)); // if the direct neighbor of either entities representative tokens is connected via 'nn' relation and the representative token is not the governor then ignore this edge if (_remove_immediate_nn_relations) { if (path_length > 0 && "nn".equals(dependency_path.get(0).getDependencyType()) && dependency_path.get(0).getDependent().equals(e1.getRepresentativeToken())) { // check first edge dependency_path.remove(0); path_length--; LOG.debug( "The immediate connecting edge for e1 ({}-{}) in sentence [{}] is an 'nn' relation and e1 is the dependent. It is removed from the path. New path length is {}.", e1.getCoveredText(), e1.getBegin(), StringUtils.abbreviate(covering_sentence.getCoveredText(), 50), path_length); } if (path_length > 0 && "nn".equals(dependency_path.get(path_length - 1).getDependencyType()) && dependency_path.get(path_length - 1).getDependent() .equals(e2.getRepresentativeToken())) { // check last edge dependency_path.remove(path_length - 1); path_length--; LOG.debug( "The immediate connecting edge for e2 ({}-{}) in sentence [{}] is an 'nn' relation and e1 is the dependent. It is removed from the path. New path length is {}.", e2.getCoveredText(), e2.getBegin(), StringUtils.abbreviate(covering_sentence.getCoveredText(), 50), path_length); } } // if path is too short skip it if (path_length < _min_dependency_path_length) { LOG.debug( "Path for relation ({}-{},{}-{}) in sentence [{}] is shorter than minlength={} ({}). Removing relation from cas index.", e1.getCoveredText(), e1.getBegin(), e2.getCoveredText(), e2.getBegin(), StringUtils.abbreviate(covering_sentence.getCoveredText(), 50), _min_dependency_path_length, path_length); relations_to_remove.add(relation); continue; } // if path is too long skip it if (path_length > _max_dependency_path_length) { LOG.debug( "Path for relation ({}-{},{}-{}) in sentence [{}] is longer than maxlength={} ({}). Removing relation from cas index.", e1.getCoveredText(), e1.getBegin(), e2.getCoveredText(), e2.getBegin(), StringUtils.abbreviate(covering_sentence.getCoveredText(), 50), _max_dependency_path_length, path_length); relations_to_remove.add(relation); continue; } relation.setDependencyPath((FSArray) aJCas.getCas().createArrayFS(dependency_path.size())); for (int i = 0; i < dependency_path.size(); i++) relation.setDependencyPath(i, dependency_path.get(i)); } for (Relation relation : relations_to_remove) relation.removeFromIndexes(aJCas); }
From source file:de.tudarmstadt.lt.lm.app.PerpDoc.java
void run(Reader r) { String docid = null;// www.j a va 2 s .c o m _perplexity_doc.reset(); _oov_ngrams = 0; _oov_terms = 0; _num_ngrams = 0; long l = 0; String ts = null; String s = null; for (LineIterator liter = new LineIterator(r); liter.hasNext();) { if (++l % 5000 == 0) LOG.info("{}: processing line {}.", _rmi_string, l); String line = liter.next(); if (line.trim().isEmpty()) continue; // time <tab> sentence <tab> url // url is document id String[] splits = line.split("\t"); if (splits.length < 3) continue; if (docid == null) { docid = splits[2]; ts = splits[0]; } if (!splits[2].equals(docid)) { double perplexity = _perplexity_doc.get(); if (perplexity > _max_perp) _max_perp = perplexity; if (perplexity < _min_perp) _min_perp = perplexity; String o = String.format( "%s\t%s\t%s\tPerplexity: %6.3e \tMax: %6.3e \tMin: %6.3e \tngrams: %d \tOov-terms: %d \tOov-ngrams: %d", _rmi_string, ts, docid, perplexity, _max_perp, _min_perp, _num_ngrams, _oov_terms, _oov_ngrams); LOG.info(o); if (!_quiet) write(String.format("%s%n", o)); else write(String.format("%s\t%s\t%s\t%6.3e%n", _rmi_string, ts, docid, perplexity)); _perplexity_doc.reset(); docid = splits[2]; ts = splits[0]; } s = splits[1]; List<String>[] ngrams; try { ngrams = _lm_prvdr.getNgrams(s); if (ngrams == null || ngrams.length == 0) continue; } catch (Exception e) { LOG.error("{}: Could not get ngrams from line {}: '{}'.", _rmi_string, l, StringUtils.abbreviate(line, 100), e); continue; } for (List<String> ngram : ngrams) { if (ngram.isEmpty()) continue; _num_ngrams++; try { if (_lm_prvdr_oovref.ngramContainsOOV(ngram)) { _oov_ngrams++; if (_lm_prvdr_oovref.ngramEndsWithOOV(ngram)) { _oov_terms++; if (_no_oov) continue; } } _perplexity_doc.addLog10Prob(ngram); } catch (Exception e) { LOG.error("{}: Could not add ngram '{}' to perplexity.", _rmi_string, ngram); continue; } } } if (docid != null) { double perplexity = _perplexity_doc.get(); if (perplexity > _max_perp) _max_perp = perplexity; if (perplexity < _min_perp) _min_perp = perplexity; String o = String.format( "%s\t%s\t%s\tPerplexity: %6.3e \tMax: %6.3e \tMin: %6.3e \tngrams: %d \tOov-terms: %d \tOov-ngrams: %d", _rmi_string, ts, docid, perplexity, _max_perp, _min_perp, _num_ngrams, _oov_terms, _oov_ngrams); LOG.info(o); if (!_quiet) write(String.format("%s%n", o)); else write(String.format("%s\t%s\t%s\t%6.3e%n", _rmi_string, ts, docid, _perplexity_doc.get())); } }
From source file:de.tudarmstadt.lt.lm.app.Ngrams.java
public void run_across_sentences(Reader r, String f) { LineIterator liter = new LineIterator(r); for (long lc = 0; liter.hasNext();) { if (++lc % 1000 == 0) LOG.info("Processing line {}:{}", f, lc); try {/*from w w w.ja v a 2 s .co m*/ String line = liter.next(); if (line.trim().isEmpty()) continue; List<String> sentences = _prvdr.splitSentences(line); if (sentences == null || sentences.isEmpty()) continue; for (String sentence : sentences) { if (sentence == null || sentence.isEmpty()) continue; List<String> tokens = null; try { tokens = _prvdr.tokenizeSentence(sentence); if (tokens == null || tokens.isEmpty()) continue; } catch (Exception e) { LOG.warn("Could not get tokens from from String '{}' in line '{}' from file '{}'.", StringUtils.abbreviate(line, 100), lc, f); continue; } for (String word : tokens) { if (word == null || word.trim().isEmpty()) continue; _ngram.add(word); for (int n = Math.max(_ngram.size() - _order_to, 0); n <= Math .min(_ngram.size() - _order_from, _ngram.size() - 1); n++) _pout.println(StringUtils.join(_ngram.subList(n, _ngram.size()), " ")); _num_ngrams++; } _pout.flush(); } } catch (Exception e) { LOG.warn("Could not process line '{}' in file '{}'.", lc, f); } } }
From source file:com.fiveamsolutions.nci.commons.audit.DefaultProcessor.java
private String getValueString(Collection<?> value) { if (value == null) { return null; }//from ww w . ja v a 2 s . c o m String sep = ""; StringBuffer sb = new StringBuffer(); for (Object a : value) { if (sb.length() > AuditLogDetail.VALUE_LENGTH) { break; } sb.append(sep); sep = ","; if (isPersistent(a)) { sb.append(getId(a)); } else { escape(sb, ObjectUtils.toString(decomposeIi(a), null)); } } return StringUtils.abbreviate(sb.toString(), AuditLogDetail.VALUE_LENGTH); }