Example usage for java.text Normalizer normalize

List of usage examples for java.text Normalizer normalize

Introduction

In this page you can find the example usage for java.text Normalizer normalize.

Prototype

public static String normalize(CharSequence src, Form form) 

Source Link

Document

Normalize a sequence of char values.

Usage

From source file:com.metinkale.prayerapp.hadis.Frag.java

private static String normalize(CharSequence str) {
    String string = Normalizer.normalize(str, Normalizer.Form.NFD);
    string = string.replaceAll("[^\\p{ASCII}]", "_");
    return string.toLowerCase(Locale.ENGLISH);
}

From source file:com.metinkale.prayerapp.names.Main.java

@SuppressLint("NewApi")
private static String normalize(CharSequence str) {
    String string = Normalizer.normalize(str, Normalizer.Form.NFD);
    string = string.replaceAll("[^\\p{ASCII}]", "");
    return string.toLowerCase(Locale.ENGLISH);
}

From source file:com.geecko.QuickLyric.lyrics.Genius.java

public static ArrayList<Lyrics> search(String query) {
    ArrayList<Lyrics> results = new ArrayList<>();
    query = Normalizer.normalize(query, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+",
            "");//  w  w  w  .  jav  a2s.  co m
    JSONObject response = null;
    try {
        URL queryURL = new URL(
                String.format("http://api.genius.com/search?q=%s", URLEncoder.encode(query, "UTF-8")));
        response = new JSONObject(Net.getUrlAsString(queryURL));
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }

    try {
        if (response == null || response.getJSONObject("meta").getInt("status") != 200)
            return results;
        JSONArray hits = response.getJSONObject("response").getJSONArray("hits");

        int processed = 0;
        while (processed < hits.length()) {
            JSONObject song = hits.getJSONObject(processed).getJSONObject("result");
            String artist = song.getJSONObject("primary_artist").getString("name");
            String title = song.getString("title");
            String url = "http://genius.com/songs/" + song.getInt("id");
            Lyrics l = new Lyrics(Lyrics.SEARCH_ITEM);
            l.setArtist(artist);
            l.setTitle(title);
            l.setURL(url);
            l.setSource("Genius");
            results.add(l);
            processed++;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return results;
}

From source file:com.trenako.utility.Slug.java

/**
 * Encodes the provided String as {@code slug}.
 *
 * @param input the String to be encoded
 * @return the slug/*from www .j ava 2 s  . co  m*/
 */
public static String encode(String input) {
    Assert.notNull(input, "The input string must be not null");

    String nowhitespace = WHITESPACE.matcher(input).replaceAll("-");
    String normalized = Normalizer.normalize(nowhitespace, Form.NFD);
    String slug = NONLATIN.matcher(normalized).replaceAll("");
    return slug.toLowerCase(Locale.ENGLISH);
}

From source file:learningresourcefinder.web.Slugify.java

private static String normalize(String input) {
    String ret = StringUtils.trim(input);
    if (StringUtils.isBlank(ret)) {
        return "";
    }//from ww  w. java 2 s. c om
    ret = ret.replace("", "ss");
    ret = Normalizer.normalize(ret, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "")
            .replaceAll("[^a-zA-Z0-9 ]", "");
    return ret;
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel pnl = new JPanel();
    pnl.add(new JLabel("Enter text"));
    final JTextField txtText;
    txtText = new JTextField("to be removed");
    pnl.add(txtText);/*from   w  w  w. j av  a2 s.c  o  m*/

    JButton btnRemove = new JButton("Remove");
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = txtText.getText();
            text = Normalizer.normalize(text, Normalizer.Form.NFD);
            txtText.setText(text.replaceAll("[^\\p{ASCII}]", ""));
        }
    };
    btnRemove.addActionListener(al);
    pnl.add(btnRemove);
    add(pnl);
    pack();
    setVisible(true);
}

From source file:Utils.StringOperations.java

public static String stripAccents(String s) {
    s = org.apache.commons.lang.StringUtils.replaceEachRepeatedly(s.toLowerCase(), InputReplace, OutputReplace);
    s = StringEscapeUtils.escapeSql(s);/*from www. j  a  v  a2 s .  c  o m*/
    s = Normalizer.normalize(s.toLowerCase(), Normalizer.Form.NFD);
    //s = s.replaceAll("''", "'");
    //LOG.info("after stripAccents: " + s);
    return s;
}

From source file:RemoveAccents.java

public RemoveAccents() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel pnl = new JPanel();
    pnl.add(new JLabel("Enter text"));
    final JTextField txtText;
    txtText = new JTextField("to be removed");
    pnl.add(txtText);//from ww w  .  jav  a2 s  .  c om

    JButton btnRemove = new JButton("Remove");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = txtText.getText();
            text = Normalizer.normalize(text, Normalizer.Form.NFD);
            txtText.setText(text.replaceAll("[^\\p{ASCII}]", ""));
        }
    };
    btnRemove.addActionListener(al);
    pnl.add(btnRemove);

    getContentPane().add(pnl);

    pack();
    setVisible(true);
}

From source file:utils.TextNormalizer.java

public String normalizeText(String text) {
    return text == null ? null
            : Normalizer.normalize(text, Normalizer.Form.NFD)
                    .replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
}

From source file:org.commcare.android.util.StringUtils.java

/**
 * @param input A non-null string/*  w w w .j a va  2 s. c om*/
 * @return a canonical version of the passed in string that is lower cased and has removed diacritical marks
 * like accents.
 */
@SuppressLint("NewApi")
public synchronized static String normalize(String input) {
    if (normalizationCache == null) {
        normalizationCache = new LruCache<String, String>(cacheSize);

        diacritics = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    }
    String cachedString = normalizationCache.get(input);
    if (cachedString != null) {
        return cachedString;
    }

    //Initialized the normalized string (If we can, we'll use the Normalizer API on it)
    String normalized = input;

    //If we're above gingerbread we'll normalize this in NFD form 
    //which helps a lot. Otherwise we won't be able to clear up some of those
    //issues, but we can at least still eliminate diacritics.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        normalized = Normalizer.normalize(input, Normalizer.Form.NFD);
    } else {
        //TODO: I doubt it's worth it, but in theory we could run
        //some other normalization for the minority of pre-API9
        //devices.
    }

    String output = diacritics.matcher(normalized).replaceAll("").toLowerCase();

    normalizationCache.put(input, output);

    return output;
}