Java by API/java.text/RuleBasedCollator
new RuleBasedCollator(String rules)
import java.text.Collator;
import java.text.ParseException;
import java.text.RuleBasedCollator;
import java.util.Collections;
import java.util.Locale;
import java.util.Vector;
public class SpanishSort {
public SpanishSort() {
Vector v = new Vector();
v.add("musa");
v.add("chic");
v.add("llama");
v.add("dela");
v.add("chocolate");
v.add("banana");
v.add("crispa");
v.add("luzca");
Collator esCollator = Collator.getInstance(new Locale("es", "ES"));
String spanishRules = ((RuleBasedCollator) esCollator).getRules();
try {
String traditionalRules = "& C < ch, cH, Ch, CH " + "& L < ll, lL, Ll, LL";
RuleBasedCollator collate = new RuleBasedCollator(spanishRules + traditionalRules);
Collections.sort(v, collate);
} catch (ParseException e) {
System.out.println("Error parsing rules " + e.toString());
}
StringBuffer result = new StringBuffer();
for (int i = 0; i < v.size(); i++) {
result.append(v.elementAt(i));
result.append("\n");
}
System.out.println(result);
}
public static void main(String[] args) {
new SpanishSort();
}
}
RuleBasedCollator: getCollationElementIterator(String source)
import java.text.CollationElementIterator;
import java.text.Collator;
import java.text.RuleBasedCollator;
public class Search {
public static int indexOf(String source, String pattern) {
RuleBasedCollator rbc = (RuleBasedCollator) Collator.getInstance();
rbc.setStrength(Collator.SECONDARY);
CollationElementIterator textCEI;
CollationElementIterator patCEI;
textCEI = rbc.getCollationElementIterator(source);
patCEI = rbc.getCollationElementIterator(pattern);
// e1 will contain the collation element for the source
// e2 will contain the collation element for the pattern
int e1, e2;
int startMatch = -1;
// initialize e2 with the first collation element in the pattern
e2 = patCEI.next();
while ((e1 = textCEI.next()) != CollationElementIterator.NULLORDER) {
if (e1 == e2) { // if the elements match
if (startMatch == -1)
startMatch = textCEI.getOffset();
e2 = patCEI.next(); // increment to the next element
if (e2 == CollationElementIterator.NULLORDER)
break;
} else { // elements do not match
if (startMatch != -1) {
patCEI.reset();
e2 = patCEI.next();
startMatch = -1;
}
}
}
return startMatch;
}
public static void main(String[] args) {
String text = "Wie hei\u00DFen Sie?"; // Wie heißen Sie?
String pattern = "heissen";
int index = indexOf(text, pattern);
if (index != -1)
System.out.println("Found a match at: " + index);
else
System.out.println("No match found!");
}
}
RuleBasedCollator: getCollationKey(String source)
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.CollationKey;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Locale;
import java.util.Vector;
class CollateApp {
public static void main(String args[]) {
if (args.length != 1) {
System.out.println("Usage: java CollateApp file");
System.exit(0);
}
Locale defaultLocale = Locale.getDefault();
RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(defaultLocale);
Vector<Object> keyVector = new Vector<Object>();
try {
BufferedReader in = new BufferedReader(new FileReader(args[0]));
String line;
while ((line = in.readLine()) != null)
keyVector.addElement(collator.getCollationKey(line));
in.close();
} catch (Exception ex) {
System.out.println(ex);
System.exit(0);
}
CollationKey keys[] = new CollationKey[keyVector.size()];
for (int i = 0; i < keys.length; ++i)
keys[i] = (CollationKey) keyVector.elementAt(i);
}
}