/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.tallison.lucene.syns.contextifiers; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ContextParser { public static void main(String[] args) { ContextParser p = new ContextParser(); Contextifier c = new PreContextifier(); String[] strings = {"the_quick_brown_dog", "a_quick_brown_dog", "the_quick_brown", "the_quick_green", "the_quick_brown_fox"}; List<String> converts = new ArrayList<String>(); for (String it : strings) { converts.add(c.convert(it, 10)); } Collections.sort(converts); Context context = new Context(); for (String s : converts) { System.out.println(s + " : " + p.parse(s, context).toString()); } } public Context parse(String s, Context c) { c.reset(); int start = s.lastIndexOf(Contextifier.START_MARKER); int field = s.lastIndexOf(Contextifier.FIELD_MARKER); int cnt = s.lastIndexOf(Contextifier.COUNT_MARKER); if (start < 0 || field < 0 || cnt < 0) { return c; } try { c.setCount(Integer.parseInt(s.substring(cnt + 1))); } catch (NumberFormatException e) { return c; } c.setField(s.substring(start + 1, field)); c.setContext(s.substring(field + 1, cnt)); c.setKey(s.substring(0, start)); return c; } public enum ATTRS { KEY, COUNT, FIELD, CONTEXT } }