/** * Returns the found command or throws an exception. * * @param cmp possible completions * @param par parent command * @param <E> token type * @return index * @throws QueryException query exception */ private <E extends Enum<E>> E consume(final Class<E> cmp, final Cmd par) throws QueryException { final String token = command(null); if (!suggest || token == null || !token.isEmpty()) { try { // return command reference; allow empty strings as input ("NULL") return Enum.valueOf(cmp, token == null ? "NULL" : token.toUpperCase(Locale.ENGLISH)); } catch (final IllegalArgumentException ignore) { } } final Enum<?>[] alt = startWith(cmp, token); // handle empty input if (token == null) { if (par != null) throw help(alt, par); if (suggest) throw error(alt, EXPECTING_CMD); return null; } // output error for similar commands final byte[] name = uc(token(token)); final Levenshtein ls = new Levenshtein(); for (final Enum<?> s : startWith(cmp, null)) { final byte[] sm = uc(token(s.name())); if (ls.similar(name, sm) && Cmd.class.isInstance(s)) { throw error(alt, UNKNOWN_SIMILAR_X, name, sm); } } // show unknown command error or available command extensions throw par == null ? error(alt, UNKNOWN_TRY_X, token) : help(alt, par); }
/** * Returns a key similar to the specified string, or {@code null}. * * @param key key to be found * @return similar key */ public final synchronized String similar(final String key) { final byte[] name = token(key); final Levenshtein ls = new Levenshtein(); for (final String prop : props.keySet()) { if (ls.similar(name, token(prop), 0)) return prop; } return null; }
public static void main(String[] args) { Levenshtein lt = new Levenshtein(); String str = "爱,从冬季开始"; String target = "爱,始于冬季"; System.out.println("similarityRatio=" + lt.getSimilarityRatio(str, target)); }
/** * Finds similar function names and throws an error message. * * @param name function name * @param ii input info * @throws QueryException query exception */ public void funError(final QNm name, final InputInfo ii) throws QueryException { // find global function Functions.get().error(name, ii); // find similar local function final Levenshtein ls = new Levenshtein(); final byte[] nm = lc(name.local()); for (final UserFunc f : funcs) { if (ls.similar(nm, lc(f.name.local()), 0)) { FUNSIMILAR.thrw(ii, name.string(), f.name.string()); } } }
/** * Throws an error if one of the pre-defined functions is similar to the specified function name. * * @param name function name * @param ii input info * @throws QueryException query exception */ public void error(final QNm name, final InputInfo ii) throws QueryException { // compare specified name with names of predefined functions final byte[] ln = name.local(); final Levenshtein ls = new Levenshtein(); for (int k = 1; k < size; ++k) { final int i = indexOf(keys[k], '}'); final byte[] u = substring(keys[k], 2, i); final byte[] l = substring(keys[k], i + 1); if (eq(ln, l)) { final byte[] ur = name.uri(); FUNSIMILAR.thrw( ii, new TokenBuilder(NSGlobal.prefix(ur)).add(':').add(l), new TokenBuilder(NSGlobal.prefix(u)).add(':').add(l)); } else if (ls.similar(ln, l, 0)) { FUNSIMILAR.thrw(ii, name.string(), l); } } }
/** * Performs a fuzzy search for the specified token with a maximum number of errors. * * @param token token to look for * @param k number of errors allowed * @return iterator */ private synchronized IndexIterator fuzzy(final byte[] token, final int k) { FTIndexIterator it = FTIndexIterator.FTEMPTY; final int tokl = token.length, tl = tp.length; final int e = Math.min(tl - 1, tokl + k); int s = Math.max(1, tokl - k) - 1; while (++s <= e) { int p = tp[s]; if (p == -1) continue; int t = s + 1, r = -1; while (t < tl && r == -1) r = tp[t++]; while (p < r) { if (ls.similar(inY.readBytes(p, s), token, k)) { it = FTIndexIterator.union(iter(pointer(p, s), size(p, s), inZ, token), it); } p += s + ENTRY; } } return it; }
/** * Returns an instance of a with the specified name and number of arguments, or {@code null}. * * @param name name of the function * @param args optional arguments * @param dyn compile-/run-time flag * @param ctx query context * @param ii input info * @return function instance * @throws QueryException query exception */ public static TypedFunc get( final QNm name, final Expr[] args, final boolean dyn, final QueryContext ctx, final InputInfo ii) throws QueryException { // get namespace and local name // parse data type constructors if (eq(name.uri(), XSURI)) { final byte[] ln = name.local(); final AtomType type = AtomType.find(name, false); if (type == null) { final Levenshtein ls = new Levenshtein(); for (final AtomType t : AtomType.values()) { if (t.par != null && t != AtomType.NOT && t != AtomType.AAT && t != AtomType.BIN && ls.similar(lc(ln), lc(t.string()), 0)) FUNSIMILAR.thrw(ii, name.string(), t.string()); } } // no constructor function found, or abstract type specified if (type == null || type == AtomType.NOT || type == AtomType.AAT) { FUNCUNKNOWN.thrw(ii, name.string()); } if (args.length != 1) FUNCTYPE.thrw(ii, name.string()); final SeqType to = SeqType.get(type, Occ.ZERO_ONE); return TypedFunc.constr(new Cast(ii, args[0], to), to); } // pre-defined functions final StandardFunc fun = Functions.get().get(name, args, ii); if (fun != null) { if (!ctx.sc.xquery3 && fun.xquery3()) FEATURE30.thrw(ii); for (final Function f : Function.UPDATING) { if (fun.sig == f) { ctx.updating(true); break; } } return new TypedFunc(fun, fun.sig.type(args.length)); } // user-defined function final TypedFunc tf = ctx.funcs.get(name, args, ii); if (tf != null) return tf; // Java function (only allowed with administrator permissions) final JavaMapping jf = JavaMapping.get(name, args, ctx, ii); if (jf != null) return TypedFunc.java(jf); // add user-defined function that has not been declared yet if (!dyn && FuncType.find(name) == null) return ctx.funcs.add(name, args, ii, ctx); // no function found return null; }
private void processCommand(String st, String lt, boolean isKeyboardLayout) { Levenshtein.getLevenshteinDistance(st, lt, isKeyboardLayout); }
private int tebakKitab(Book[] xkitab) { if (p_kitab == null) { return -1; } int res = -1; // 0. bikin cache semua judul kitab yang dibuang spasinya dan dikecilin semua dan 1 jadi I, 2 // jadi II, dst { Jumper.KitabRef[] refs = pendekCache.get(xkitab); if (refs == null) { ArrayList<Jumper.KitabRef> a = new ArrayList<Jumper.KitabRef>(); for (Book k : xkitab) { String judul = k.judul.replaceAll("(\\s|-|_)+", "").toLowerCase(); // $NON-NLS-1$ //$NON-NLS-2$ { Jumper.KitabRef ref = new KitabRef(); ref.pendek = judul; ref.pos = k.bookId; a.add(ref); } if (judul.contains("1") || judul.contains("2") || judul.contains("3")) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ judul = judul .replaceAll("1", "i") .replaceAll("2", "ii") .replaceAll( "3", "iii"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ //$NON-NLS-5$ // //$NON-NLS-6$ Jumper.KitabRef ref = new KitabRef(); ref.pendek = judul; ref.pos = k.bookId; a.add(ref); } } refs = a.toArray(new Jumper.KitabRef[0]); pendekCache.put(xkitab, refs); Log.d(TAG, "entri pendekCache baru: " + Arrays.toString(refs)); // $NON-NLS-1$ } } // 0 juga. bersihin p_kitab Jumper.KitabRef[] refs = pendekCache.get(xkitab); p_kitab = p_kitab.replaceAll("(\\s|-|_)", "").toLowerCase(); // $NON-NLS-1$ //$NON-NLS-2$ Log.d(TAG, "tebakKitab fase 0: p_kitab = " + p_kitab); // $NON-NLS-1$ // 1. coba cocokin keseluruhan (co: "kejadian", "yohanes") for (Jumper.KitabRef ref : refs) { if (ref.pendek.equals(p_kitab)) { Log.d(TAG, "tebakKitab fase 1 sukses: " + p_kitab); // $NON-NLS-1$ return ref.pos; } } // 2. coba cocokin depannya, kalo ada 1 doang yang lulus, sukses int pos_buatNanti = -1; { int lulus = 0; for (Jumper.KitabRef ref : refs) { if (ref.pendek.startsWith(p_kitab)) { lulus++; if (lulus == 1) pos_buatNanti = ref.pos; } } if (lulus == 1) { Log.d( TAG, "tebakKitab fase 2 sukses: " + pos_buatNanti + " untuk " + p_kitab); //$NON-NLS-1$ //$NON-NLS-2$ return pos_buatNanti; } else { Log.d(TAG, "tebakKitab fase 2: lulus = " + lulus); // $NON-NLS-1$ } } // 3. String matching hanya kalo p_kitab 2 huruf ato lebih if (p_kitab.length() >= 2) { int minSkor = 99999999; int pos = -1; for (Jumper.KitabRef ref : refs) { int skor = Levenshtein.distance(p_kitab, ref.pendek); if (p_kitab.charAt(0) != ref.pendek.charAt(0)) { skor += 150; // kira2 1.5 insertion } Log.d( TAG, "tebakKitab fase 3: dengan " + ref.pendek + ":" + ref.pos + " skor " + skor); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ if (skor < minSkor) { minSkor = skor; pos = ref.pos; } } if (pos != -1) { Log.d( TAG, "tebakKitab fase 3 sukses: " + pos + " dengan skor " + minSkor); //$NON-NLS-1$//$NON-NLS-2$ return pos; } } // 7. Keluarin yang pertama cocok kalo ada lebih dari 1 yang lulus fase 2 if (pos_buatNanti != -1) { Log.d( TAG, "tebakKitab fase 7 sukses: " + pos_buatNanti + " untuk " + p_kitab); //$NON-NLS-1$ //$NON-NLS-2$ return pos_buatNanti; } return res; }
/** * Returns the optimal path of transformation * * @param firstWordID The ID of the first word * @param secondWordID The ID of the second word * @return A map that contains HashPoints as keys. These are (x,y) coordinates that have unique * hash values for their x,y coordinates. The corresponding Integer will indicate the * transformation action done at this point to yield one string out of the other. (As * described by the <code>ACTION_*</code> constants x can be interpreted as a columns and y as * a row in the Levenshtein matrix. (starting at origin (0,0)) * @throws IndexOutOfBoundsException If at least one of the ID does not exist */ public Map<HashPoint, Integer> levenshteinPath(int firstWordID, int secondWordID) throws IndexOutOfBoundsException { return Levenshtein.getIdealPath(getLevenshteinMatrix(firstWordID, secondWordID)); }