Jave Code Examples for 'normalize'

60 Jave code examples are found related to "normalize". 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.
Example 1
Project: garmadon   Author: criteo   File: MXBeanHelper.java    License: Apache License 2.0 6 votes vote down vote up
public static String normalizeName(String poolName) {
    if (poolName == null) return null;
    if ("Code Cache".equals(poolName)) {
        return MEMORY_POOL_CODE_HEADER;
    }
    if ("Tenured Gen".equals(poolName) || poolName.endsWith("Old Gen") || "GenPauseless Old Gen".equals(poolName)) {
        return MEMORY_POOL_OLD_HEADER;
    }
    if (poolName.endsWith("Perm Gen")) {
        return MEMORY_POOL_PERM_HEADER;
    }
    if ("Metaspace".equals(poolName)) {
        return MEMORY_POOL_METASPACE_HEADER;
    }
    if ("Compressed Class Space".equals(poolName)) {
        return MEMORY_POOL_COMPRESSEDCLASSPACE_HEADER;
    }
    if (poolName.endsWith("Eden Space") || "GenPauseless New Gen".equals(poolName)) {
        return MEMORY_POOL_EDEN_HEADER;
    }
    if (poolName.endsWith("Survivor Space")) {
        return MEMORY_POOL_SURVIVOR_HEADER;
    }
    return poolName;
}
 
Example 2
Project: sepia-assist-server   Author: SEPIA-Framework   File: NormalizerLightTR.java    License: MIT License 6 votes vote down vote up
public String normalizeText(String text) {
			
	//special characters - that fail to convert to small case correctly
	text = text.replaceAll("İ", "i");
	text = text.replaceAll("I", "ı");
			
	text = text.replaceAll("(!(?!\\()|¿|¡|,(?!\\d))", "").toLowerCase().trim();
	//text = text.replaceAll("(?<![oO])'", "").trim();		//TODO: use it or not?
	//text = text.replaceAll("((?<!\\d)\\.$)", "").trim();
	text = text.replaceAll("(\\.|\\?)$", "").trim();
	
	//special characters
	//TODO: use it or not?
	text = text.replaceAll("ß","ss").replaceAll("ä","ae").replaceAll("ü","ue").replaceAll("ö","oe");
	text = text.replaceAll("é","e").replaceAll("è","e").replaceAll("ê","e");
	text = text.replaceAll("á","a").replaceAll("í","i").replaceAll("ó","o").replaceAll("ñ","n").replaceAll("ú","u");
	
	//clean up
	text = text.replaceAll("\\s+", " ").trim();
	
	return text;
}
 
Example 3
Project: javamail   Author: m-szalik   File: TransportTest.java    License: Apache License 2.0 6 votes vote down vote up
private static String normalizeContent(String input) {
    Pattern trimTailPattern = Pattern.compile("[ \t]+$");
    StringBuilder sb = new StringBuilder();
    for(String line : input.split("\n")) {
        line = trimTailPattern.matcher(line).replaceAll("");
        if (line.startsWith("Message-ID:")) {
            sb.append("Message-ID: testID\n");
            continue;
        }
        if (line.startsWith("\tboundary=")) {
            sb.append("\tboundary=\"tb\"\n");
            continue;
        }
        if (line.startsWith("------=_Part")) {
            sb.append("------=_Part_XYZ--\n");
            continue;
        }
        sb.append(line).append('\n');
    }
    return sb.toString().replaceAll("\r", "");
}
 
Example 4
Project: deeplearning4j   Author: eclipse   File: ReductionOpValidation.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNormalizeMomentsOp() {
    INDArray data = Nd4j.linspace(1, 100, 100, DataType.DOUBLE).reshape(10, 10);
    INDArray ssSum = data.sum(0);
    INDArray ssSqSum = data.mul(data).sum(0);

    INDArray meanExp = data.mean(0);
    INDArray varExp = data.var(false, 0);

    INDArray mean = Nd4j.createUninitialized(DataType.DOUBLE, meanExp.shape());
    INDArray var = Nd4j.createUninitialized(DataType.DOUBLE, varExp.shape());

    OpTestCase op = new OpTestCase(new NormalizeMoments(Nd4j.scalar(DataType.DOUBLE, 10), ssSum, ssSqSum, mean, var));
    op.expectedOutput(0, meanExp);
    op.expectedOutput(1, varExp);

    String err = OpValidation.validate(op);
    assertNull(err);
}
 
Example 5
Project: fenixedu-academic   Author: FenixEdu   File: Thesis.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
private String normalizeKeywords(String keywords) {
    StringBuilder builder = new StringBuilder();

    if (keywords == null) {
        return null;
    } else {
        for (String part : keywords.split(",")) {
            String trimmed = part.trim();

            if (trimmed.length() != 0) {
                if (builder.length() != 0) {
                    builder.append(", ");
                }

                builder.append(trimmed);
            }
        }
    }

    return builder.toString();
}
 
Example 6
Project: tassal   Author: mast-group   File: JavaASTExtractor.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Hacky way to compare snippets.
 *
 * @param snippet
 * @return
 */
private String normalizeCode(final char[] snippet) {
	final List<String> tokens = (new JavaTokenizer())
			.tokenListFromCode(snippet);

	final StringBuffer bf = new StringBuffer();
	for (final String token : tokens) {
		if (token.equals(ITokenizer.SENTENCE_START)
				|| token.equals(ITokenizer.SENTENCE_END)) {
			continue;
		} else {
			bf.append(token);
		}
		bf.append(" ");
	}
	return bf.toString();

}
 
Example 7
Project: incubator-heron   Author: apache   File: KryoSerializer.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private static Map<String, String> normalizeKryoRegister(Map<String, Object> config) {
  // TODO: de-duplicate this logic with the code in nimbus
  Object res = config.get(Config.TOPOLOGY_KRYO_REGISTER);
  if (res == null) {
    return new TreeMap<String, String>();
  }
  Map<String, String> ret = new HashMap<>();
  // Register config is a list. Each value can be either a String or a map
  for (Object o: (List) res) {
    if (o instanceof Map) {
      ret.putAll((Map) o);
    } else {
      ret.put((String) o, null);
    }
  }

  //ensure always same order for registrations with TreeMap
  return new TreeMap<String, String>(ret);
}
 
Example 8
Project: smithy   Author: awslabs   File: IdlTextParser.java    License: Apache License 2.0 6 votes vote down vote up
private static String normalizeLineEndings(String lexeme) {
    if (!containsCarriageReturn(lexeme)) {
        return lexeme;
    }

    StringBuilder builder = new StringBuilder(lexeme.length());
    for (int i = 0; i < lexeme.length(); i++) {
        char c = lexeme.charAt(i);
        if (c != '\r') {
            builder.append(c);
        } else {
            // Convert "\r\n" to "\n".
            if (i < lexeme.length() - 1 && lexeme.charAt(i + 1) == '\n') {
                i++;
            }
            builder.append('\n');
        }
    }

    return builder.toString();
}
 
Example 9
Project: nutch-htmlunit   Author: xautlx   File: WebGraph.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Normalizes and trims extra whitespace from the given url.
 * 
 * @param url The url to normalize.
 * 
 * @return The normalized url.
 */
private String normalizeUrl(String url) {

  if (!normalize) {
    return url;
  }

  String normalized = null;
  if (urlNormalizers != null) {
    try {

      // normalize and trim the url
      normalized = urlNormalizers.normalize(url,
        URLNormalizers.SCOPE_DEFAULT);
      normalized = normalized.trim();
    }
    catch (Exception e) {
      LOG.warn("Skipping " + url + ":" + e);
      normalized = null;
    }
  }
  return normalized;
}
 
Example 10
Project: Neural-Network-Programming-with-Java-SecondEdition   Author: PacktPublishing   File: DataNormalization.java    License: MIT License 6 votes vote down vote up
/**
 * Normalizes data matrix
 * 
 * @param data matrix to be normalized
 * @return normalized matrix
 */
public double[][] normalize(double[][] data) {

	int rows = data.length;
	int cols = data[0].length;

	if ((minValues == null) && (maxValues == null) && (meanValues == null) && (stdValues == null))
		calculateReference(data);

	double[][] normalizedData = new double[rows][cols];

	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			switch (TYPE) {
			case MIN_MAX:
				normalizedData[i][j] = (minNorm)
						+ ((data[i][j] - minValues[j]) / (maxValues[j] - minValues[j])) * (scaleNorm);
				break;
			case ZSCORE:
				normalizedData[i][j] = scaleNorm * (data[i][j] - meanValues[j]) / stdValues[j];
				break;
			}
		}
	}
	return normalizedData;
}
 
Example 11
Project: yauaa   Author: nielsbasjes   File: TestNormalize.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void checkBrandNormalizationExamples() {
    // At least 3 lowercase
    assertEquals("NielsBasjes",      Normalize.brand("NielsBasjes"));
    assertEquals("NielsBasjes",      Normalize.brand("NIelsBasJES"));
    assertEquals("BlackBerry",       Normalize.brand("BlackBerry"));

    // Less than 3 lowercase
    assertEquals("Nielsbasjes",      Normalize.brand("NIelSBasJES"));
    assertEquals("Blackberry",       Normalize.brand("BLACKBERRY"));

    // Multiple words. Short words (1,2,3 letters) go full uppercase
    assertEquals("Niels NBA Basjes", Normalize.brand("NIels NbA BasJES"));
    assertEquals("LG",               Normalize.brand("lG"));
    assertEquals("HTC",              Normalize.brand("hTc"));
    assertEquals("Sony",             Normalize.brand("sOnY"));
    assertEquals("Asus",             Normalize.brand("aSuS"));
}
 
Example 12
Project: gcs   Author: richardwilkes   File: List.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/** Makes sure all the items in the list have the same indentation. */
public void normalizeIndentation() {
	float max = 0;
	Element o;
	for (Iterator i = list.iterator(); i.hasNext();) {
		o = (Element) i.next();
		if (o instanceof ListItem) {
			max = Math.max(max, ((ListItem) o).getIndentationLeft());
		}
	}
	for (Iterator i = list.iterator(); i.hasNext();) {
		o = (Element) i.next();
		if (o instanceof ListItem) {
			((ListItem) o).setIndentationLeft(max);
		}
	}
}
 
Example 13
Project: gcs   Author: richardwilkes   File: PDFTextStripper.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Used within {@link #normalize(List)} to handle a {@link TextPosition}.
 * 
 * @return The StringBuilder that must be used when calling this method.
 */
private StringBuilder normalizeAdd(List<WordWithTextPositions> normalized,
        StringBuilder lineBuilder, List<TextPosition> wordPositions, LineItem item)
{
    if (item.isWordSeparator())
    {
        normalized.add(
                createWord(lineBuilder.toString(), new ArrayList<TextPosition>(wordPositions)));
        lineBuilder = new StringBuilder();
        wordPositions.clear();
    }
    else
    {
        TextPosition text = item.getTextPosition();
        lineBuilder.append(text.getUnicode());
        wordPositions.add(text);
    }
    return lineBuilder;
}
 
Example 14
Project: gcs   Author: richardwilkes   File: PDFTextStripper.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Normalize the given list of TextPositions.
 * 
 * @param line list of TextPositions
 * @return a list of strings, one string for every word
 */
private List<WordWithTextPositions> normalize(List<LineItem> line)
{
    List<WordWithTextPositions> normalized = new LinkedList<WordWithTextPositions>();
    StringBuilder lineBuilder = new StringBuilder();
    List<TextPosition> wordPositions = new ArrayList<TextPosition>();

    for (LineItem item : line)
    {
        lineBuilder = normalizeAdd(normalized, lineBuilder, wordPositions, item);
    }

    if (lineBuilder.length() > 0)
    {
        normalized.add(createWord(lineBuilder.toString(), wordPositions));
    }
    return normalized;
}
 
Example 15
Project: swellrt   Author: SwellRT   File: DocHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Normalizes a point so that it is biased towards text nodes, and node ends
 * rather than node start.
 *
 * @param <N>
 * @param <E>
 * @param <T>
 * @param point
 * @param doc
 */
public static <N, E extends N, T extends N> Point<N> normalizePoint(Point<N> point,
    ReadableDocument<N, E, T> doc) {
  N previous = null;
  if (!point.isInTextNode()) {
    previous = Point.nodeBefore(doc, point.asElementPoint());
    T nodeAfterAsText = doc.asText(point.getNodeAfter());
    if (nodeAfterAsText != null) {
      point = Point.<N>inText(nodeAfterAsText, 0);
    }
  } else if (point.getTextOffset() == 0) {
    previous = doc.getPreviousSibling(point.getContainer());
  }

  T previousAsText = doc.asText(previous);
  if (previous != null && previousAsText != null) {
    point = Point.inText(previous, doc.getLength(previousAsText));
  }

  return point;
}
 
Example 16
Project: swellrt   Author: SwellRT   File: Link.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @return best guess link annotation value from an arbitrary string. feeding
 *         the return value back through this method should always return the
 *         input.
 * @throws InvalidLinkException
 */
@SuppressWarnings("deprecation")
public static String normalizeLink(String rawLinkValue) throws InvalidLinkException {
  Preconditions.checkNotNull(rawLinkValue);
  rawLinkValue = rawLinkValue.trim();
  String[] parts = splitUri(rawLinkValue);
  String scheme = parts != null ? parts[0] : null;

  // Normal web url
  if (rawLinkValue.matches(QUERY_REGEX) || 
      (scheme != null && WEB_SCHEMES.contains(scheme))) {
    return rawLinkValue;
  }

  // default 
  return "http://"+rawLinkValue;

}
 
Example 17
Project: swellrt   Author: SwellRT   File: AnnotationRegistry.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param key,
 *          a probably short-format annotation key
 * @return a normalized annotation key
 */
@JsIgnore
public static ReadableStringSet normalizeKeys(ReadableStringSet keySet) {

  StringSet normalizedKeySet = CollectionUtils.createStringSet();

  keySet.each(new Proc() {

    @Override
    public void apply(String key) {
      normalizedKeySet.add(normalizeKey(key));
    }

  });

  return normalizedKeySet;
}
 
Example 18
Project: swellrt   Author: SwellRT   File: EditorAssert.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 'Normalises' an xml string to avoid fickleness in testing. Currently
 * we only perform a few normalisation steps:
 *
 * Self-close all empty elements, e.g., <p></p> -> <p/>
 * Replace " with '
 *
 * More can be added, e.g., to normalise whitespace, if + when needed.
 *
 * @param xml
 * @return normalised xml string
 */
private static String normalizeXml(String xml) {
  if (xml == null) {
    return null;
  }
  int index = 0;
  int last = 0;
  StringBuilder out = new StringBuilder();
  while ((index = xml.indexOf("></", index)) != -1) {
    if (xml.charAt(index - 1) == '/') {
      index += 3;
      // Already self-closing.
      continue;
    }
    int open = xml.lastIndexOf('<', index);
    if (open != -1 && xml.charAt(open + 1) != '/') {
      out.append(xml.subSequence(last, index)).append("/>");
      index = last = xml.indexOf('>', index + 1) + 1;
    } else {
      index += 3;
    }
  }
  out.append(xml.subSequence(last, xml.length()));
  return out.toString().replaceAll("\"", "'");
}
 
Example 19
Project: swellrt   Author: SwellRT   File: DocHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test normalize points between an element and a text node <a>stuff</a>"hi"
 */
public void testNormalizePointElementFollowedByTextNode() {
  MutableDocument<Node, Element, Text> doc = initializeMutableDoc();
  Element p = doc.asElement(doc.getFirstChild(doc.getDocumentElement()));
  assert p != null;

  Element aElement =
      doc.createElement(Point.start(doc, p), "a", Collections.<String, String> emptyMap());
  doc.insertText(Point.start(doc, aElement), "stuff");
  doc.insertText(Point.<Node> end(p), "hi");
  Text hi = doc.asText(doc.getLastChild(p));
  Text stuff = doc.asText(aElement.getFirstChild());

  assertEquals(Point.inText(hi, 0), DocHelper.normalizePoint(Point.<Node> inText(hi, 0), doc));
  assertEquals(Point.inText(hi, 0), DocHelper.normalizePoint(Point.<Node>inElement(p, hi), doc));
  // In the future, we might want to move the caret out from inline elements.
  assertEquals(Point.inText(stuff, stuff.getLength()), DocHelper.normalizePoint(Point
      .<Node> inText(stuff, stuff.getLength()), doc));
}
 
Example 20
Project: 4pdaClient-plus   Author: slartus   File: NewsFragment.java    License: Apache License 2.0 6 votes vote down vote up
private String normalizeCommentUrls(String body) {
    if (App.getInstance().getPreferences().getBoolean("loadNewsComment", false)) {
        body = body.replaceAll("(<div class=\"comment-box\" id=\"comments\">[\\s\\S]*?<ul class=\"page-nav box\">[\\s\\S]*?<\\/ul>)", "");
    }

    body = Pattern.compile("<iframe[^><]*?src=\"http://www.youtube.com/embed/([^\"/]*)\".*?(?:</iframe>|/>)", Pattern.CASE_INSENSITIVE)
            .matcher(body)
            .replaceAll("<a class=\"video-thumb-wrapper\" href=\"https?://www.youtube.com/watch?v=$1\"><img class=\"video-thumb\" width=\"480\" height=\"320\" src=\"https://img.youtube.com/vi/$1/0.jpg\"/></a>");
    return body
            // удаляем форму ответа
            .replaceAll("<form[^>]*action=\"/wp-comments-post.php\"[^>]*>[\\s\\S]*</form>",
                    "")
            // заменяем обрезанные ссылки
            .replace("href=\"/", "href=\"https://4pda.ru/")
            .replace("href=\"#commentform\"", "href=\"https://4pdaservice.org/#commentform");
}
 
Example 21
Project: nexus-public   Author: sonatype   File: RawUploadHandlerTest.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void testNormalizePath(String directory, String file, String expectedPath) throws IOException {
  reset(rawFacet);
  ComponentUpload component = new ComponentUpload();

  component.getFields().put("directory", directory);

  AssetUpload asset = new AssetUpload();
  asset.getFields().put("filename", file);
  asset.setPayload(jarPayload);
  component.getAssetUploads().add(asset);

  when(assetPayload.componentId()).thenReturn(new DetachedEntityId("foo"));
  when(attributesMap.get(Asset.class)).thenReturn(assetPayload);
  when(content.getAttributes()).thenReturn(attributesMap);
  when(rawFacet.put(any(), any())).thenReturn(content);
  underTest.handle(repository, component);

  ArgumentCaptor<String> pathCapture = ArgumentCaptor.forClass(String.class);
  verify(rawFacet).put(pathCapture.capture(), any(PartPayload.class));

  String path = pathCapture.getValue();
  assertNotNull(path);
  assertThat(path, is(expectedPath));
}
 
Example 22
Project: Eclipse-Postfix-Code-Completion   Author: trylimits   File: Bindings.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Normalizes the binding so that it can be used as a type inside a declaration (e.g. variable
 * declaration, method return type, parameter type, ...).
 * For null bindings, java.lang.Object is returned.
 * For void bindings, <code>null</code> is returned.
 * 
 * @param binding binding to normalize
 * @param ast current AST
 * @return the normalized type to be used in declarations, or <code>null</code>
 */
public static ITypeBinding normalizeForDeclarationUse(ITypeBinding binding, AST ast) {
	if (binding.isNullType())
		return ast.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
	if (binding.isPrimitive())
		return binding;
	binding= normalizeTypeBinding(binding);
	if (binding == null || !binding.isWildcardType())
		return binding;
	ITypeBinding bound= binding.getBound();
	if (bound == null || !binding.isUpperbound()) {
		ITypeBinding[] typeBounds= binding.getTypeBounds();
		if (typeBounds.length > 0) {
			return typeBounds[0];
		} else {
			return binding.getErasure();
		}
	} else {
		return bound;
	}
}
 
Example 23
Project: openhab-core   Author: openhab   File: ConfigurationNormalizer.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Normalizes the types of the configuration's parameters to the allowed ones. References are ignored. Null values
 * are replaced with the defaults and then normalized.
 *
 * @param configuration the configuration to normalize.
 * @param configDescriptionMap the meta-data of the configuration.
 */
public static void normalizeConfiguration(Configuration configuration,
        Map<String, ConfigDescriptionParameter> configDescriptionMap) {
    for (Entry<String, ConfigDescriptionParameter> entry : configDescriptionMap.entrySet()) {
        ConfigDescriptionParameter parameter = entry.getValue();
        if (parameter != null) {
            String parameterName = entry.getKey();
            final Object value = configuration.get(parameterName);
            if (value instanceof String && ((String) value).contains("${")) {
                continue; // It is a reference
            }
            if (value == null) {
                final Object defaultValue = parameter.getDefault();
                if (defaultValue == null) {
                    configuration.remove(parameterName);
                } else {
                    configuration.put(parameterName, ConfigUtil.normalizeType(defaultValue, parameter));
                }
            } else {
                configuration.put(parameterName, ConfigUtil.normalizeType(value, parameter));
            }
        }
    }
}
 
Example 24
Project: camel-cookbook-examples   Author: CamelCookbook   File: NormalizerSpringTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNormalizeJson() throws Exception {
    final InputStream resource = getClass().getClassLoader().getResourceAsStream("bookstore.json");
    final String request = context().getTypeConverter().convertTo(String.class, resource);

    getMockEndpoint("mock:unknown").setExpectedMessageCount(0);
    getMockEndpoint("mock:csv").setExpectedMessageCount(0);
    getMockEndpoint("mock:xml").setExpectedMessageCount(0);

    getMockEndpoint("mock:json").expectedBodiesReceived(getExpectedBookstore());
    getMockEndpoint("mock:normalized").expectedBodiesReceived(getExpectedBookstore());

    template.sendBodyAndHeader("direct:start", request, Exchange.FILE_NAME, "bookstore.json");

    assertMockEndpointsSatisfied();
}
 
Example 25
Project: Ngram-Graphs   Author: ayushoriginal   File: Distribution.java    License: Apache License 2.0 6 votes vote down vote up
/**Normalizes the values of the distribution to the sum of values, resolving to a probability
 *distribution, as the sum of the new distribution will amount to 1.
 */
public void normalizeToSum() {
    double dMax = 0;
    // 1st pass - Find sum
    Iterator iValIter = hDistro.values().iterator();
    while (iValIter.hasNext()) {
        dMax += (Double)iValIter.next();
    }
    
    Iterator<TKeyType> iKeyIter = hDistro.keySet().iterator();
    while (iKeyIter.hasNext()) {
        TKeyType oKey = iKeyIter.next();
        Double dVal = (Double)hDistro.get(oKey);
        dVal /= dMax; // Normalize
        hDistro.put(oKey, dVal);
    }
}
 
Example 26
Project: ant-ivy   Author: apache   File: AbstractURLHandler.java    License: Apache License 2.0 6 votes vote down vote up
protected String normalizeToString(URL url) throws IOException {
    if (!"http".equals(url.getProtocol()) && !"https".equals(url.getProtocol())) {
        return url.toExternalForm();
    }

    try {
        URI uri = new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(),
                url.getRef());

        // it is possible that the original url was already (partial) escaped,
        // so we must unescape all '%' followed by 2 hexadecimals...
        String uriString = uri.normalize().toASCIIString();

        // manually escape the '+' character
        uriString = uriString.replaceAll("\\+", "%2B");

        return ESCAPE_PATTERN.matcher(uriString).replaceAll("%$1");
    } catch (URISyntaxException e) {
        IOException ioe = new MalformedURLException("Couldn't convert '" + url.toString()
                + "' to a valid URI");
        ioe.initCause(e);
        throw ioe;
    }
}
 
Example 27
Project: ant-ivy   Author: apache   File: UpdateSiteLoader.java    License: Apache License 2.0 6 votes vote down vote up
private URI normalizeSiteUri(URI uri, URI defaultValue) {
    if (uri == null) {
        return defaultValue;
    }
    String uriString = uri.toString();
    if (uriString.endsWith("site.xml")) {
        try {
            return new URI(uriString.substring(0, uriString.length() - 8));
        } catch (URISyntaxException e) {
            throw new RuntimeException("Illegal uri", e);
        }
    }
    if (!uriString.endsWith("/")) {
        try {
            return new URI(uriString + "/");
        } catch (URISyntaxException e) {
            throw new RuntimeException("Illegal uri", e);
        }
    }
    return uri;
}
 
Example 28
Project: Java8CN   Author: Java8-CNAPI-Team   File: MutableBigInteger.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Ensure that the MutableBigInteger is in normal form, specifically
 * making sure that there are no leading zeros, and that if the
 * magnitude is zero, then intLen is zero.
 */
final void normalize() {
    if (intLen == 0) {
        offset = 0;
        return;
    }

    int index = offset;
    if (value[index] != 0)
        return;

    int indexBound = index+intLen;
    do {
        index++;
    } while(index < indexBound && value[index] == 0);

    int numZeros = index - offset;
    intLen -= numZeros;
    offset = (intLen == 0 ?  0 : offset+numZeros);
}
 
Example 29
Project: olat   Author: huihoo   File: InfoMessageDaoImpl.java    License: Apache License 2.0 6 votes vote down vote up
private String normalizeBusinessPath(String url) {
    if (url == null) {
        return null;
    }
    if (url.startsWith("ROOT")) {
        url = url.substring(4, url.length());
    }
    final List<String> tokens = new ArrayList<String>();
    for (final StringTokenizer tokenizer = new StringTokenizer(url, "[]"); tokenizer.hasMoreTokens();) {
        final String token = tokenizer.nextToken();
        if (!tokens.contains(token)) {
            tokens.add(token);
        }
    }

    final StringBuilder sb = new StringBuilder();
    for (final String token : tokens) {
        sb.append('[').append(token).append(']');
    }
    return sb.toString();
}
 
Example 30
Project: react-templates-plugin   Author: idok   File: DirectiveUtil.java    License: MIT License 6 votes vote down vote up
public static String normalizeAttributeName(String name) {
    if (name == null) return null;
    if (name.startsWith("data-")) {
        name = name.substring(5);
    } else if (name.startsWith("x-")) {
        name = name.substring(2);
    }
    name = name.replace(':', '-');
    name = name.replace('_', '-');
    if (name.endsWith("-start")) {
        name = name.substring(0, name.length() - 6);
    } else if (name.endsWith("-end")) {
        name = name.substring(0, name.length() - 4);
    }
    return name;
}
 
Example 31
Project: jatecs   Author: jatecs   File: MDMcomparable.java    License: GNU General Public License v3.0 6 votes vote down vote up
private void normalizeDocumentVectors(Matrix termByDocument){
	JatecsLogger.status().println("Normalizing document-vectors");
	int rows=termByDocument.getRowDimension();
	int cols=termByDocument.getColumnDimension();
	for(int c = 0; c < cols; c++){
		double norm=0.0;
		for(int f = 0; f < rows; f++){
			norm+=(termByDocument.get(f, c)*termByDocument.get(f, c));
		}
		norm = Math.sqrt(norm);
		for(int f = 0; f < rows; f++){
			double old = termByDocument.get(f, c);
			termByDocument.set(f, c, old / norm);
		}
	}
	JatecsLogger.status().println("[done].");
}
 
Example 32
Project: hottub   Author: dsrg-uoft   File: Arc2D.java    License: GNU General Public License v2.0 6 votes vote down vote up
static double normalizeDegrees(double angle) {
    if (angle > 180.0) {
        if (angle <= (180.0 + 360.0)) {
            angle = angle - 360.0;
        } else {
            angle = Math.IEEEremainder(angle, 360.0);
            // IEEEremainder can return -180 here for some input values...
            if (angle == -180.0) {
                angle = 180.0;
            }
        }
    } else if (angle <= -180.0) {
        if (angle > (-180.0 - 360.0)) {
            angle = angle + 360.0;
        } else {
            angle = Math.IEEEremainder(angle, 360.0);
            // IEEEremainder can return -180 here for some input values...
            if (angle == -180.0) {
                angle = 180.0;
            }
        }
    }
    return angle;
}
 
Example 33
Project: hottub   Author: dsrg-uoft   File: XMLGregorianCalendarImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
     * <p>Normalize this instance to UTC.</p>
     *
     * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
     * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
     */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {

    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();

    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new DurationImpl(minutes >= 0, // isPositive
            0, //years
            0, //months
            0, //days
            0, //hours
            minutes < 0 ? -minutes : minutes, // absolute
            0  //seconds
    );
    result.add(d);

    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
 
Example 34
Project: AML-Project   Author: AgreementMakerLight   File: StringParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param s: the name to normalize
 * @return the normalized name
 */
public static String normalizeProperty(String name)
{
	//First replace codes with their word equivalents 
	String parsed = name.replace("&amp","and");
	parsed = parsed.replace("&apos;","'");
	parsed = parsed.replace("&nbsp;"," ");
	//Remove dashes
	parsed = parsed.replaceAll("-","");
	//Then replace all other non-word characters with white spaces
	//except for apostrophes and brackets
	parsed = parsed.replaceAll(" *[^a-zA-Z0-9'()] *"," ");
	
	//Then remove multiple, leading and trailing spaces
	parsed = parsed.replaceAll(" {2,}"," ");
	parsed = parsed.trim();
	
	//Then normalize the case changes and return the result
	parsed = normalizeCaseChanges(parsed,true);
	return parsed;
}
 
Example 35
Project: agrest   Author: agrestio   File: ResourceParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Strips leading and trailing slash from the path for further resolution against the base
 */
// From @Path javadoc:
//   Paths are relative. For an annotated class the base URI is the application path, see {@link ApplicationPath}.
//   For an annotated method the base URI is the effective URI of the containing class. For the purposes of
//   absolutizing a path against the base URI , a leading '/' in a path is ignored and base URIs are treated as if
//   they ended in '/'.
private static String normalizePathSegment(String path) {

    if (path == null || path.length() == 0 || path.equals("/")) {
        return "";
    }

    if (path.startsWith("/")) {
        path = path.substring(1);
    }

    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }

    return path;
}
 
Example 36
Project: sensorhub   Author: sensiasoft   File: Quat4d.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Normalizes quaternion q and places the result into this quaternion
 * @param q other quaternion
 * @return reference to this quaternion for chaining other operations
 */
public final Quat4d normalize(final Quat4d q)
{
    double norm = (q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);

    if (norm > 0.0)
    {
        norm = 1.0 / Math.sqrt(norm);
        this.x = norm * q.x;
        this.y = norm * q.y;
        this.z = norm * q.z;
        this.w = norm * q.w;
    }
    else
    {
        this.x = 0.0;
        this.y = 0.0;
        this.z = 0.0;
        this.w = 0.0;
    }
    
    return this;
}
 
Example 37
Project: Pydev   Author: fabioz   File: RefactoringInfo.java    License: Eclipse Public License 1.0 6 votes vote down vote up
public String normalizeSourceSelection(ICoreTextSelection selection) {
    String selectedText = "";

    if (selection.getText() != null) {
        selectedText = selection.getText().trim();
    }
    if (selectedText.length() == 0) {
        return "";
    }

    try {
        return normalizeBlockIndentation(selection, selectedText);
    } catch (Throwable e) {
        /* TODO: uncommented empty exception catch all */
    }
    return selectedText;

}
 
Example 38
Project: azure-cosmosdb-java   Author: Azure   File: AddressInformation.java    License: MIT License 6 votes vote down vote up
private static String normalizePhysicalUri(String physicalUri) {
    if (StringUtils.isEmpty(physicalUri)) {
        return physicalUri;
    }

    // backend returns non normalized uri with "//" tail
    // e.g, https://cdb-ms-prod-westus2-fd2.documents.azure.com:15248/apps/4f5c042d-76fb-4ce6-bda3-517e6ef3984f/
    // services/cf4b9ab2-019c-45ca-ac88-25a92b66dddf/partitions/2078862a-d698-475b-a308-02598370d1d9/replicas/132077748219659199s//
    // we should trim the tail double "//"

    int i = physicalUri.length() -1;

    while(i >= 0 && physicalUri.charAt(i) == '/') {
        i--;
    }

    return physicalUri.substring(0, i + 1) + '/';
}
 
Example 39
Project: tm4e   Author: eclipse   File: TextUtils.java    License: Eclipse Public License 1.0 6 votes vote down vote up
private static String normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces) {
	int spacesCnt = 0;
	for (int i = 0; i < str.length(); i++) {
		if (str.charAt(i) == '\t') {
			spacesCnt += tabSize;
		} else {
			spacesCnt++;
		}
	}

	StringBuilder result = new StringBuilder();
	if (!insertSpaces) {
		long tabsCnt = Math.round(Math.floor(spacesCnt / tabSize));
		spacesCnt = spacesCnt % tabSize;
		for (int i = 0; i < tabsCnt; i++) {
			result.append('\t');
		}
	}

	for (int i = 0; i < spacesCnt; i++) {
		result.append(' ');
	}

	return result.toString();
}
 
Example 40
Project: sql-parser   Author: brunoribeiro   File: BooleanNormalizer.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/** Normalize a top-level boolean expression. */
public ValueNode normalizeExpression(ValueNode boolClause) throws StandardException {
    /* For each expression tree:
     *  o Eliminate NOTs (eliminateNots())
     *  o Ensure that there is an AndNode on top of every
     *      top level expression. (putAndsOnTop())
     *  o Finish the job (changeToCNF())
     */
    if (boolClause != null) {
        boolClause = eliminateNots(boolClause, false);
        assert verifyEliminateNots(boolClause);
        boolClause = putAndsOnTop(boolClause);
        assert verifyPutAndsOnTop(boolClause);
        boolClause = changeToCNF(boolClause, true);
        assert verifyChangeToCNF(boolClause, true);
    }
    return boolClause;
}
 
Example 41
private String normalizeColumnName(String columnName) {
    String invColumnName = columnName != null ? columnName.trim().toLowerCase(Locale.getDefault()) : null;

    if (invColumnName == null || columnName.length() == 0) {
        throw new IllegalArgumentException("Column name cannot be null or empty.");
    }

    if (invColumnName.length() > 128) {
        throw new IllegalArgumentException("Column name cannot be longer than 128 characters.");
    }

    if (invColumnName.matches("[a-zA-Z_]/w*")) {
        throw new IllegalArgumentException(
                "Column name must start with a letter or underscore, and can contain only alpha-numeric characters and underscores.");
    }

    if (invColumnName.matches("__/w*") && !isSystemProperty(invColumnName)) {
        throw new IllegalArgumentException("Column names prefixed with \"__\" are system reserved.");
    }

    return invColumnName;
}
 
Example 42
Project: azure-mobile-apps-android-client   Author: Azure   File: UriHelper.java    License: Apache License 2.0 6 votes vote down vote up
public static String normalizeAndUrlEncodeParameters(HashMap<String, String> parameters, String encoding) {
    String result = "";

    if (parameters != null && parameters.size() > 0) {
        for (Map.Entry<String, String> parameter : parameters.entrySet()) {

            if (result.isEmpty()) {
                result = "?";
            } else {
                result += "&";
            }

            try {
                result += URLEncoder.encode(parameter.getKey(), encoding) + "=" + URLEncoder.encode(parameter.getValue(), encoding);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    return result;
}
 
Example 43
Project: azure-mobile-apps-android-client   Author: Azure   File: UriHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Normalizes the parameters to add it to the Url
 *
 * @param parameters list of the parameters.
 * @return the parameters to add to the url.
 */
public static String normalizeParameters(HashMap<String, String> parameters) {

    String result = "";

    if (parameters != null && parameters.size() > 0) {
        for (Map.Entry<String, String> parameter : parameters.entrySet()) {

            if (result.isEmpty()) {
                result = "?";
            } else {
                result += "&";
            }

            result += parameter.getKey() + "=" + parameter.getValue();
        }
    }

    return result;
}
 
Example 44
Project: openjdk-8-source   Author: keerath   File: Arc2D.java    License: GNU General Public License v2.0 6 votes vote down vote up
static double normalizeDegrees(double angle) {
    if (angle > 180.0) {
        if (angle <= (180.0 + 360.0)) {
            angle = angle - 360.0;
        } else {
            angle = Math.IEEEremainder(angle, 360.0);
            // IEEEremainder can return -180 here for some input values...
            if (angle == -180.0) {
                angle = 180.0;
            }
        }
    } else if (angle <= -180.0) {
        if (angle > (-180.0 - 360.0)) {
            angle = angle + 360.0;
        } else {
            angle = Math.IEEEremainder(angle, 360.0);
            // IEEEremainder can return -180 here for some input values...
            if (angle == -180.0) {
                angle = 180.0;
            }
        }
    }
    return angle;
}
 
Example 45
Project: openjdk-8-source   Author: keerath   File: Util.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static String normalizeNewlines(String text) {
    StringBuilder sb = new StringBuilder();
    final int textLength = text.length();
    final String NL = DocletConstants.NL;
    int pos = 0;
    for (int i = 0; i < textLength; i++) {
        char ch = text.charAt(i);
        switch (ch) {
            case '\n':
                sb.append(text, pos, i);
                sb.append(NL);
                pos = i + 1;
                break;
            case '\r':
                sb.append(text, pos, i);
                sb.append(NL);
                if (i + 1 < textLength && text.charAt(i + 1) == '\n')
                    i++;
                pos = i + 1;
                break;
        }
    }
    sb.append(text, pos, textLength);
    return sb.toString();
}
 
Example 46
Project: openjdk-8-source   Author: keerath   File: URLPermission.java    License: GNU General Public License v2.0 6 votes vote down vote up
private List<String> normalizeMethods(String methods) {
    List<String> l = new ArrayList<>();
    StringBuilder b = new StringBuilder();
    for (int i=0; i<methods.length(); i++) {
        char c = methods.charAt(i);
        if (c == ',') {
            String s = b.toString();
            if (s.length() > 0)
                l.add(s);
            b = new StringBuilder();
        } else if (c == ' ' || c == '\t') {
            throw new IllegalArgumentException("white space not allowed");
        } else {
            if (c >= 'a' && c <= 'z') {
                c += 'A' - 'a';
            }
            b.append(c);
        }
    }
    String s = b.toString();
    if (s.length() > 0)
        l.add(s);
    return l;
}
 
Example 47
Project: openjdk-8-source   Author: keerath   File: BasisLibrary.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * XSLT Standard function normalize-space(string).
 */
public static String normalize_spaceF(String value) {
    int i = 0, n = value.length();
    StringBuilder result = threadLocalStringBuilder.get();
result.setLength(0);

    while (i < n && isWhiteSpace(value.charAt(i)))
        i++;

    while (true) {
        while (i < n && !isWhiteSpace(value.charAt(i))) {
            result.append(value.charAt(i++));
        }
        if (i == n)
            break;
        while (i < n && isWhiteSpace(value.charAt(i))) {
            i++;
        }
        if (i < n)
            result.append(' ');
    }
    return result.toString();
}
 
Example 48
Project: openjdk-8-source   Author: keerath   File: BaseCalendar.java    License: GNU General Public License v2.0 6 votes vote down vote up
void normalizeMonth(CalendarDate date) {
    Date bdate = (Date) date;
    int year = bdate.getNormalizedYear();
    long month = bdate.getMonth();
    if (month <= 0) {
        long xm = 1L - month;
        year -= (int)((xm / 12) + 1);
        month = 13 - (xm % 12);
        bdate.setNormalizedYear(year);
        bdate.setMonth((int) month);
    } else if (month > DECEMBER) {
        year += (int)((month - 1) / 12);
        month = ((month - 1)) % 12 + 1;
        bdate.setNormalizedYear(year);
        bdate.setMonth((int) month);
    }
}
 
Example 49
Project: joshua   Author: apache   File: DeNormalize.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Scanning from left-to-right, a comma or period preceded by a space will become just the
 * comma/period.
 * 
 * @param line The single-line input string
 * @return The input string modified as described above
 */
public static String joinPunctuationMarks(String line) {
  String result = line;
  result = result.replace(" ,", ",");
  result = result.replace(" ;", ";");
  result = result.replace(" :", ":");
  result = result.replace(" .", ".");
  result = result.replace(" !", "!");
  result = result.replace("¡ ", "¡");
  result = result.replace(" ?", "?");
  result = result.replace("¿ ", "¿");
  result = result.replace(" )", ")");
  result = result.replace(" ]", "]");
  result = result.replace(" }", "}");
  result = result.replace("( ", "(");
  result = result.replace("[ ", "[");
  result = result.replace("{ ", "{");
  return result;
}
 
Example 50
Project: Mindustry   Author: Anuken   File: Placement.java    License: GNU General Public License v3.0 6 votes vote down vote up
/** Normalize two points into one straight line, no diagonals. */
public static Seq<Point2> normalizeLine(int startX, int startY, int endX, int endY){
    Pools.freeAll(points);
    points.clear();
    if(Math.abs(startX - endX) > Math.abs(startY - endY)){
        //go width
        for(int i = 0; i <= Math.abs(startX - endX); i++){
            points.add(Pools.obtain(Point2.class, Point2::new).set(startX + i * Mathf.sign(endX - startX), startY));
        }
    }else{
        //go height
        for(int i = 0; i <= Math.abs(startY - endY); i++){
            points.add(Pools.obtain(Point2.class, Point2::new).set(startX, startY + i * Mathf.sign(endY - startY)));
        }
    }
    return points;
}
 
Example 51
Project: Juicebox   Author: aidenlab   File: MatrixTools.java    License: MIT License 6 votes vote down vote up
public static int[][] normalizeMatrixUsingColumnSum(int[][] matrix) {
    int[][] newMatrix = new int[matrix.length][matrix[0].length];
    int[] columnSum = new int[matrix[0].length];
    for (int[] row : matrix) {
        for (int i = 0; i < row.length; i++) {
            columnSum[i] += row[i];
        }
    }

    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++) {
            newMatrix[i][j] = matrix[i][j] / columnSum[j];
        }
    }

    return newMatrix;
}
 
Example 52
Project: bidder   Author: RTB4FREE   File: BidRequest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Convert iso2 country codes to iso3. C1X and bidswitch need this. The database
 * key device.geo.country shadows the json object. So we do a lookup and replace
 * it. But, then, we also need to replace the textnode of the bid request,
 * becaause it is still 2 character. This means that you need to know the 2 and
 * 3 char codes for the countries to do meaningful queries.
 *
 * So, we must overrwite the bid request country field to normalize it.
 */
protected void normalizeCountryCode() {
	Object o = this.database.get("device.geo.country");
	if (o instanceof MissingNode)
		return;

	TextNode country = (TextNode) o;
	TextNode test = null;
	if (country != null) {
		test = cache.get(country.asText());
		if (test == null) {
			String iso3 = isoMap.query(country.asText());
			test = new TextNode(iso3);
		}
		if (test != country) {
			database.put("device.geo.country", test);
			JsonNode device = rootNode.get("device");
			ObjectNode geo = (ObjectNode) device.get("geo");
			geo.set("country", test);

		}
	}
}
 
Example 53
public static void normalizeMap(THashMap<String, THashMap<String, Double>> map) {
	Set<String> keys = map.keySet();
	for (String key: keys) {
		THashMap<String, Double> val = map.get(key);
		double total = 0.0;
		Set<String> keys1 = val.keySet();
		for (String key1: keys1) {
			total += val.get(key1);
		}
		for (String key1: keys1) {
			double v = val.get(key1) / total;
			val.put(key1, v);
		}
		map.put(key, val);
	}
}
 
Example 54
public static TObjectDoubleHashMap<String> add(TObjectDoubleHashMap<String> vector, 
		TObjectDoubleHashMap<String> oldVector, int count) {
	TObjectDoubleHashMap<String> res = new TObjectDoubleHashMap<String>();
	if (oldVector == null) {
		oldVector = new TObjectDoubleHashMap<String>();
	}
	String[] vKeys = new String[vector.size()];
	vector.keys(vKeys);
	for (int i = 0; i < vKeys.length; i++) {
		double value = vector.get(vKeys[i]) / (double)count;
		if (oldVector.contains(vKeys[i])) {
			res.put(vKeys[i], value + oldVector.get(vKeys[i]));
			oldVector.remove(vKeys[i]);
		} else {
			res.put(vKeys[i], value);
		}
	}			
	String[] oldKeys = new String[oldVector.size()];
	oldVector.keys(oldKeys);
	for (int i = 0; i < oldKeys.length; i++) {
		res.put(oldKeys[i], oldVector.get(oldKeys[i]));
	}
	return res;
}
 
Example 55
/**
 * Return the given path with backslashes ("\") converted to slashes ("/).
 * Slashes are the normalized CrawlableDatset path seperator.
 * This method can be used on absolute or relative paths.
 * <p/>
 *
 * @param path the path to be normalized.
 * @return the normalized path.
 * @throws NullPointerException if path is null.
 */
private String normalizePath(String path) {
  // Replace any occurance of a backslash ("\") with a slash ("/").
  // NOTE: Both String and Pattern escape backslash, so need four backslashes to find one.
  // NOTE: No longer replace multiple backslashes with one slash, which allows for UNC pathnames (Windows LAN
  // addresses).
  // Was path.replaceAll( "\\\\+", "/");
  return path.replaceAll("\\\\", "/");

  // String newPath = path.replaceAll( "\\\\", "/" );
  // Note: No longer remove trailing slashes as new File() removes slashes for us.
  // // Remove trailing slashes.
  // while ( newPath.endsWith( "/" ) && ! newPath.equals( "/" ) )
  // newPath = newPath.substring( 0, newPath.length() - 1 );
  //
  // return newPath;
}
 
Example 56
Project: DeviceConnect-Android   Author: DeviceConnect   File: MediaType.java    License: MIT License 6 votes vote down vote up
/**
 * Normalizes the specified token.
 * 
 * @param token
 *            Token to normalize.
 * @return The normalized token.
 * @throws IllegalArgumentException
 *             if <code>token</code> is not legal.
 */
private static String normalizeToken(String token) {
    int length;
    char c;

    // Makes sure we're not dealing with a "*" token.
    token = token.trim();
    if ("".equals(token) || "*".equals(token))
        return "*";

    // Makes sure the token is RFC compliant.
    length = token.length();
    for (int i = 0; i < length; i++) {
        c = token.charAt(i);
        if (c <= 32 || c >= 127 || _TSPECIALS.indexOf(c) != -1)
            throw new IllegalArgumentException("Illegal token: " + token);
    }

    return token;
}
 
Example 57
Project: lwbd   Author: qlyoung   File: BeatDetector.java    License: MIT License 6 votes vote down vote up
/**
 * Normalizes all values in this hash map to [0, 1]. Does not normalize
 * keys.
 * @param map the map to normalize. This map is not modified.
 * @return The normalized map.
 */
public static LinkedHashMap<Long, Float> normalizeValues(final LinkedHashMap<Long, Float> map) {
    // normalize values to range [0, 1]
    LinkedHashMap<Long, Float> newMap = new LinkedHashMap<Long, Float>();

    // find max value
    float max = 0;
    for (Float f : map.values())
        if (f > max)
            max = f;

    // divide all values by max value
    float value;
    for (Long l : map.keySet()) {
        value = map.get(l);
        value /= max;
        newMap.put(l, value);
    }

    return newMap;
}
 
Example 58
Project: The-5zig-Mod   Author: 5zig-reborn   File: ComparableVersion.java    License: GNU General Public License v3.0 6 votes vote down vote up
void normalize()
{
    for ( int i = size() - 1; i >= 0; i-- )
    {
        Item lastItem = get( i );

        if ( lastItem.isNull() )
        {
            // remove null trailing items: 0, "", empty list
            remove( i );
        }
        else if ( !( lastItem instanceof ListItem ) )
        {
            break;
        }
    }
}
 
Example 59
Project: olingo-odata2   Author: apache   File: JPAEdmKey.java    License: Apache License 2.0 6 votes vote down vote up
public void normalizeComplexKey(final ComplexType complexType, final List<PropertyRef> propertyRefList) {
  for (Property property : complexType.getProperties()) {
    try {

      SimpleProperty simpleProperty = (SimpleProperty) property;
      Facets facets = (Facets) simpleProperty.getFacets();
      if (facets == null) {
        simpleProperty.setFacets(new Facets().setNullable(false));
      } else {
        facets.setNullable(false);
      }
      PropertyRef propertyRef = new PropertyRef();
      propertyRef.setName(simpleProperty.getName());
      propertyRefList.add(propertyRef);

    } catch (ClassCastException e) {
      ComplexProperty complexProperty = (ComplexProperty) property;
      normalizeComplexKey(complexTypeView.searchEdmComplexType(complexProperty.getType()), propertyRefList);
    }

  }
}
 
Example 60
Project: short-text-search   Author: ysc   File: ShortTextSearcher.java    License: Apache License 2.0 6 votes vote down vote up
public String normalize(String text){
    StringBuilder normal = new StringBuilder();
    for(char c : text.toCharArray()){
        if(isEnglish(c)
                || isNumber(c)
                || isChinese(c)){
            normal.append(c);
        }
    }
    if(text.length() != normal.length()){
        if(LOGGER.isDebugEnabled()){
            LOGGER.debug("移除非英语数字和中文字符, 移除之前: {}, 移除之后: {}", text, normal.toString());
        }
    }
    return normal.toString().toLowerCase();
}