package info.puzz.a10000sentences.importer;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordUtils {

    public static List<String> getWords(String str) {
        ArrayList<String> res = new ArrayList<>();
        StringBuilder currentWord = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (Character.isLetter(c) || Character.isDigit(c)) {
                currentWord.append(c);
            } else {
                if (currentWord.length() > 0) {
                    res.add(currentWord.toString());
                    currentWord = new StringBuilder();
                }
            }
        }
        if (currentWord.length() > 0) {
            res.add(currentWord.toString());
        }
        return res;
    }

    /**
     * Removes nonspacing chars. Those are, for example accents used in arabic, but <strong>not</strong>
     * accents used in Croatian (šđčćž) or Italian (éí).
     * @param s
     * @return
     */
    public static String removeNonspacingChars(String s) {
        StringBuilder res = new StringBuilder();
        Pattern p = Pattern.compile("[^\\p{Mn}]");
        Matcher m = p.matcher(s);
        while (m.find()) {
            res.append(m.group());
        }
        m.reset();
        return res.toString();
    }

    public static void main(String[] args) {
        System.out.println(removeNonspacingChars("عَسَى أَن يَهْدِيَنِ رَبِّي لِأَقْرَبَ مِنْ هَذَا رَشَدًا"));
        System.out.println(removeNonspacingChars("Što bi to sad bilo šđčćžŠĐČĆŽ"));
        System.out.println(removeNonspacingChars("éí"));
    }
}

@@ -950,22 +950,25 @@
 %0A     * 
-accent
+diacritic
 s used i
@@ -984,16 +984,19 @@
 n (%C5%A1%C4%91%C4%8D%C4%87%C5%BE
+...
 ) or Ita
@@ -1004,44 +1004,24 @@
 ian 
-(%C3%A9%C3%AD).%0A     * @param s%0A     * @return
+accents (%C3%A9%C3%AC...).
 %0A   

Comment fix