Example usage for java.lang Character isDigit

List of usage examples for java.lang Character isDigit

Introduction

In this page you can find the example usage for java.lang Character isDigit.

Prototype

public static boolean isDigit(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is a digit.

Usage

From source file:com.easemob.chatuidemo.activity.MainActivity.java

/**
 * hearder?header??ABCD...????/*from  www .  j ava 2 s  . c o m*/
 * 
 * @param username
 * @param user
 */
private static void setUserHearder(String username, User user) {
    String headerName = null;
    if (!TextUtils.isEmpty(user.getNick())) {
        headerName = user.getNick();
    } else {
        headerName = user.getUsername();
    }
    if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
        user.setHeader("");
    } else if (Character.isDigit(headerName.charAt(0))) {
        user.setHeader("#");
    } else {
        user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1)).get(0).target.substring(0, 1)
                .toUpperCase());
        char header = user.getHeader().toLowerCase().charAt(0);
        if (header < 'a' || header > 'z') {
            user.setHeader("#");
        }
    }
}

From source file:com.google.dart.engine.services.util.DartDocUtilities.java

private static String convertListItems(String[] lines) {
    StringBuffer buf = new StringBuffer();

    boolean wasCode = false;
    for (String line : lines) {
        // code block
        if (line.startsWith("    ")) {
            buf.append("<pre>");
            if (wasCode) {
                buf.append("\n");
            }/*from ww  w  .j  ava 2  s.  c  o m*/
            buf.append(line);
            buf.append("</pre>");
            wasCode = true;
            continue;
        }
        wasCode = false;
        // empty line
        if (line.isEmpty()) {
            buf.append("\n\n");
            continue;
        }
        // unordered list item
        if (line.startsWith("* ")) {
            buf.append("<li>" + line.substring(2) + "</li>\n");
            continue;
        }
        // ordered list
        {
            line = line.trim();
            int i = 0;
            while (i < line.length() && Character.isDigit(line.charAt(i))) {
                i++;
            }
            if (i < line.length() && line.charAt(i) == '.') {
                buf.append("<pre>    </pre>" + line + "<br>");
                continue;
            }
        }
        // text line
        buf.append(line);
        buf.append("\n");
    }
    return buf.toString();
}

From source file:ca.mcgill.cs.swevo.qualyzer.editors.RTFDocumentProvider2.java

private boolean isUnicodeCount(String control) {
    return control.length() > 2 && control.startsWith(UNICODE_COUNT_FULL)
            && Character.isDigit(control.charAt(2));
}

From source file:com.imalu.alyou.activity.LoginActivity.java

/**
 * hearder?header??ABCD...????/*from  ww w .j  ava 2  s .  co  m*/
 * 
 * @param username
 * @param user
 */
protected void setUserHearder(String username, HXUser user) {
    String headerName = null;
    if (!TextUtils.isEmpty(user.getNick())) {
        headerName = user.getNick();
    } else {
        headerName = user.getUsername();
    }
    if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
        user.setHeader("");
    } else if (Character.isDigit(headerName.charAt(0))) {
        user.setHeader("#");
    } else {
        user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1)).get(0).target.substring(0, 1)
                .toUpperCase());
        char header = user.getHeader().toLowerCase().charAt(0);
        if (header < 'a' || header > 'z') {
            user.setHeader("#");
        }
    }
}

From source file:ca.mcgill.cs.swevo.qualyzer.editors.RTFDocumentProvider2.java

private ParserPair parseUnicode(String unicodeStr) {
    StringBuilder number = new StringBuilder();
    int size = unicodeStr.length();
    for (int i = 0; i < size; i++) {
        char ch = unicodeStr.charAt(i);
        if (Character.isDigit(ch)) {
            number.append(ch);/*from   ww  w .  jav a  2s  .c  om*/
        } else {
            // For now, we don't care about the replacement...
            break;
        }
    }

    return new ParserPair(-1, number.toString());
}

From source file:de.tudarmstadt.ukp.dkpro.spelling.experiments.errormining.SpellingErrorFilter.java

@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
    // RevisionSentencePair annotations are added to the second view
    try {/* w ww .ja va  2 s .  c o m*/
        revView1 = jcas.getView(WikipediaRevisionPairReader.REVISION_1);
        revView2 = jcas.getView(WikipediaRevisionPairReader.REVISION_2);
    } catch (CASException e) {
        throw new AnalysisEngineProcessException(e);
    }

    DocumentMetaData dmd = DocumentMetaData.get(revView1);

    // heuristics in this loop should be loosely ordered by their computation time
    // "heavy" computing should come last
    for (RevisionSentencePair pair : JCasUtil.select(revView2, RevisionSentencePair.class)) {
        Sentence s1 = (Sentence) pair.getSentence1();
        Sentence s2 = (Sentence) pair.getSentence2();

        // do not consider very long sentences (usually parsing errors)
        if (s1.getCoveredText().length() > maxSentenceLength) {
            continue;
        }

        List<ChangedToken> changedList1 = JCasUtil.selectCovered(revView1, ChangedToken.class, s1);
        List<ChangedToken> changedList2 = JCasUtil.selectCovered(revView2, ChangedToken.class, s2);

        // only consider changes where there was a single token change
        if (changedList1.size() != 1 || changedList2.size() != 1) {
            continue;
        }

        ChangedToken changedToken1 = changedList1.iterator().next();
        ChangedToken changedToken2 = changedList2.iterator().next();

        String token1 = changedToken1.getCoveredText();
        String token2 = changedToken2.getCoveredText();

        // same token is definitely not what we want
        if (token1.toLowerCase().equals(token2.toLowerCase())) {
            continue;
        }

        // sentence may not contains line breaks -> this indicates wrong sentence splitting
        if (containsLinebreak(s1.getCoveredText()) || containsLinebreak(s2.getCoveredText())) {
            continue;
        }

        // check that the changed token is in the same position in the sentence
        // this avoids situations where "a b c" is changed into "a c b"
        // which often also leads to low distance / high similarity
        if (changedToken1.getPosition() != changedToken2.getPosition()) {
            continue;
        }

        // should not start with a number
        // (we are not looking for numbers)
        if (Character.isDigit(token1.charAt(0)) || Character.isDigit(token2.charAt(0))) {
            continue;
        }

        // should not be all uppercase letters
        if (token1.toUpperCase().equals(token1) || token2.toUpperCase().equals(token2)) {
            continue;
        }

        // should not change case
        if (!haveFirstLettersSameCase(token1.charAt(0), token2.charAt(0))) {
            continue;
        }

        // certain words should not be target words
        if (blackList.contains(token1.toLowerCase()) || blackList.contains(token2.toLowerCase())) {
            continue;
        }

        //deactivated as this check also removes a lot of interesting cases             
        //        // quick check for substitutions of whole words
        //        // if letter at the beginning an letter at the end is different
        //        if (text1.charAt(0) != text2.charAt(0) &&
        //            text1.charAt(text1.length()-1) != text2.charAt(text2.length()-1))
        //        {
        //            continue;
        //        }

        long freq1;
        long freq2;
        double distance;
        double ratio;

        try {
            freq1 = frequencyProvider.getFrequency(token1);
            freq2 = frequencyProvider.getFrequency(token2);
            ratio = (double) freq1 / freq2;
            distance = levenshteinComparator.getSimilarity(token1, token2);
        } catch (Exception e) {
            throw new AnalysisEngineProcessException(e);
        }

        // zero frequencies - definitely not a word
        if (freq1 == 0 || freq2 == 0) {
            continue;
        }

        // low frequencies - probably not a word
        if (freq1 < lowFreq || freq2 < lowFreq) {
            continue;
        }

        // distance should not be greater than a threshold
        if (distance > levenshteinDistance) {
            continue;
        }

        // low ratio and low levenshtein indicate normal spelling mistake
        // FIXME no magic numbers
        if (ratio < 0.0001 && distance < 4) {
            continue;
        }

        // if both token share the same lemma - probably not an error
        List<Lemma> lemmas1 = JCasUtil.selectCovered(revView1, Lemma.class, changedToken1);
        List<Lemma> lemmas2 = JCasUtil.selectCovered(revView2, Lemma.class, changedToken2);

        if (lemmas1.size() == 0 || lemmas2.size() == 0) {
            throw new AnalysisEngineProcessException(new Throwable("could not get lemma for token"));
        }

        Lemma lemma1 = lemmas1.get(0);
        Lemma lemma2 = lemmas2.get(0);

        if (lemma1.getValue().equals(lemma2.getValue())) {
            System.out.println("SAME LEMMA");
            System.out.println(token1 + " - " + token2);
            System.out.println();
            continue;
        }

        List<POS> tags1 = JCasUtil.selectCovered(revView1, POS.class, changedToken1);
        List<POS> tags2 = JCasUtil.selectCovered(revView2, POS.class, changedToken2);

        if (tags1.size() == 0 || tags2.size() == 0) {
            throw new AnalysisEngineProcessException(new Throwable("could not get lemma for token"));
        }

        POS tag1 = tags1.get(0);
        POS tag2 = tags2.get(0);

        //#################################
        // check for POS class
        if (!hasAllowableTag(tag2)) {
            System.out.println("Unallowed Tag");
            System.out.println(token1 + " - " + token2);
            System.out.println();
            continue;
        }
        //#################################

        // Is currently covered by the more general allowed tags rule 
        // changes in Named Entities are most likely to be due to semantic errors
        if (isNamedEntity(tag1) && isNamedEntity(tag2)) {
            System.out.println("NAMED ENTITY");
            System.out.println(token1 + " - " + token2);
            System.out.println();
            continue;
        }

        // whether the probably wrong token1 would be detected by a spell checker
        boolean detectable;
        try {
            detectable = isDetectableSpellingError(changedToken1, changedToken2, s1,
                    JCasUtil.selectCovered(revView1, Token.class, s1));
        } catch (ResourceInitializationException e) {
            throw new AnalysisEngineProcessException(e);
        } catch (UIMAException e) {
            throw new AnalysisEngineProcessException(e);
        }

        if (detectable) {
            System.out.println("DETECTED SPELLING ERROR");
            System.out.println(token1 + " - " + token2);
            System.out.println();
            continue;
        }

        try {
            if (isInCloseRelation(lemma1.getValue(), lemma2.getValue())) {
                System.out.println("CLOSE RELATION");
                System.out.println(token1 + " - " + token2);
                System.out.println();
                continue;
            }
        } catch (LexicalSemanticResourceException e) {
            throw new AnalysisEngineProcessException(e);
        }

        System.out.println(token1 + " - " + token2);
        System.out.println(freq1 + " - " + freq2);
        System.out.println(s1.getCoveredText());
        System.out.println(s2.getCoveredText());
        System.out.println();

        String leftContext = getContextSentences(revView1, s1, NR_OF_CONTEXT_SENTENCES, true);
        String rightContext = getContextSentences(revView1, s1, NR_OF_CONTEXT_SENTENCES, false);

        // write remaining errors in file using correct formatting
        DatasetItem item = new DatasetItem(token1, token2,
                leftContext.length() + changedToken1.getBegin() - s1.getBegin(),
                leftContext + s1.getCoveredText() + rightContext, new Integer(dmd.getCollectionId()).intValue(),
                new Integer(dmd.getDocumentId()).intValue());

        // both token should have the same POS, otherwise it is more likely a grammatical error
        if (tag1.getClass().getName().equals(tag2.getClass().getName())) {
            if (!alreadySeenErrors.contains(item.getWrong() + "-" + item.getCorrect())) {
                alreadySeenErrors.add(item.getWrong() + "-" + item.getCorrect());
                writeItem(semanticErrorWriter, item);
            }
        } else {
            if (!alreadySeenErrors.contains(item.getWrong() + "-" + item.getCorrect())) {
                alreadySeenErrors.add(item.getWrong() + "-" + item.getCorrect());
                writeItem(syntacticErrorWriter, item);
            }
        }
    }
}

From source file:com.s3d.webapps.util.time.DateUtils.java

/**
 * Reformat the timezone in a date string.
 *
 * @param str The input string/*from   ww  w .  ja v  a  2 s . c o m*/
 * @param signIdx The index position of the sign characters
 * @return The reformatted string
 */
private static String reformatTimezone(String str, int signIdx) {
    String str2 = str;
    if (signIdx >= 0 && signIdx + 5 < str.length() && Character.isDigit(str.charAt(signIdx + 1))
            && Character.isDigit(str.charAt(signIdx + 2)) && str.charAt(signIdx + 3) == ':'
            && Character.isDigit(str.charAt(signIdx + 4)) && Character.isDigit(str.charAt(signIdx + 5))) {
        str2 = str.substring(0, signIdx + 3) + str.substring(signIdx + 4);
    }
    return str2;
}

From source file:net.rim.ejde.internal.util.PackagingUtils.java

/**
 * Replaces special characters in the given string to conform to Java standard.
 *
 * @param val/*w ww . ja v a 2  s.co m*/
 * @return
 */
public static String replaceSpecialChars(String val) {
    // replace special characters
    final char replaceChr = getProjectOutputReplaceChar();
    for (char chr : BBPropertiesValidator.CHARS_WARN) {
        if (val.indexOf(chr) >= 0 && replaceChr != 0) {
            val = val.replace(chr, replaceChr);
        }
    }
    // append "_" in front if the first character is digit
    if (!val.isEmpty() && Character.isDigit(val.charAt(0))) {
        val = IConstants.UNDERSCORE_STRING + val;
    }
    return val;
}

From source file:org.codehaus.griffon.commons.GriffonClassUtils.java

/**
 * Converts a property name into its natural language equivalent eg ('firstName' becomes 'First Name')
 * @param name The property name to convert
 * @return The converted property name// ww w  .ja  va 2 s .c  o m
 */
public static String getNaturalName(String name) {
    List words = new ArrayList();
    int i = 0;
    char[] chars = name.toCharArray();
    for (int j = 0; j < chars.length; j++) {
        char c = chars[j];
        String w;
        if (i >= words.size()) {
            w = "";
            words.add(i, w);
        } else {
            w = (String) words.get(i);
        }

        if (Character.isLowerCase(c) || Character.isDigit(c)) {
            if (Character.isLowerCase(c) && w.length() == 0)
                c = Character.toUpperCase(c);
            else if (w.length() > 1 && Character.isUpperCase(w.charAt(w.length() - 1))) {
                w = "";
                words.add(++i, w);
            }

            words.set(i, w + c);
        } else if (Character.isUpperCase(c)) {
            if ((i == 0 && w.length() == 0) || Character.isUpperCase(w.charAt(w.length() - 1))) {
                words.set(i, w + c);
            } else {
                words.add(++i, String.valueOf(c));
            }
        }

    }

    StringBuffer buf = new StringBuffer();

    for (Iterator j = words.iterator(); j.hasNext();) {
        String word = (String) j.next();
        buf.append(word);
        if (j.hasNext())
            buf.append(' ');
    }
    return buf.toString();
}