com.googlecode.concurrenttrees.suffix.ConcurrentSuffixTree Java Examples

The following examples show how to use com.googlecode.concurrenttrees.suffix.ConcurrentSuffixTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BuildShakespeareTragediesSuffixTree.java    From concurrent-trees with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ConcurrentSuffixTree<String> tree = new ConcurrentSuffixTree<String>(new DefaultCharSequenceNodeFactory());
    for (String file : files) {
        String manuscript = IOUtil.loadTextFileFromClasspath(file, true, true, true); // true = convert to lowercase
        String manuscriptName = file.replaceAll("/.*/.*/", "").replace(".txt", "");
        tree.put(manuscript, manuscriptName);
        System.out.println("Added " + manuscriptName);
    }
    System.out.println("Built Suffix Tree. Estimating size on disk...");
    Thread.sleep(30000);
    DummyAppendable dummyAppendable = new DummyAppendable();
    PrettyPrinter.prettyPrint(tree, dummyAppendable);
    System.out.println("Done. Size on disk estimate:");
    System.out.println("Lines: " + dummyAppendable.lineCount);
    System.out.println("Characters: " + dummyAppendable.charCount);
}
 
Example #2
Source File: SuffixTreeUsage.java    From concurrent-trees with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    System.out.println("Suffixes for 'TEST': " + Iterables.toString(CharSequences.generateSuffixes("TEST")));
    System.out.println("Suffixes for 'TOAST': " + Iterables.toString(CharSequences.generateSuffixes("TOAST")));
    System.out.println("Suffixes for 'TEAM': " + Iterables.toString(CharSequences.generateSuffixes("TEAM")));

    SuffixTree<Integer> tree = new ConcurrentSuffixTree<Integer>(new DefaultCharArrayNodeFactory());

    tree.put("TEST", 1);
    tree.put("TOAST", 2);
    tree.put("TEAM", 3);

    System.out.println();
    System.out.println("Tree structure:");
    // PrettyPrintable is a non-public API for testing, prints semi-graphical representations of trees...
    PrettyPrinter.prettyPrint((PrettyPrintable) tree, System.out);

    System.out.println();
    System.out.println("Value for 'TEST' (exact match): " + tree.getValueForExactKey("TEST"));
    System.out.println("Value for 'TOAST' (exact match): " + tree.getValueForExactKey("TOAST"));
    System.out.println();
    System.out.println("Keys ending with 'ST': " + Iterables.toString(tree.getKeysEndingWith("ST")));
    System.out.println("Keys ending with 'M': " + Iterables.toString(tree.getKeysEndingWith("M")));
    System.out.println("Values for keys ending with 'ST': " + Iterables.toString(tree.getValuesForKeysEndingWith("ST")));
    System.out.println("Key-Value pairs for keys ending with 'ST': " + Iterables.toString(tree.getKeyValuePairsForKeysEndingWith("ST")));
    System.out.println();
    System.out.println("Keys containing 'TE': " + Iterables.toString(tree.getKeysContaining("TE")));
    System.out.println("Keys containing 'A': " + Iterables.toString(tree.getKeysContaining("A")));
    System.out.println("Values for keys containing 'A': " + Iterables.toString(tree.getValuesForKeysContaining("A")));
    System.out.println("Key-Value pairs for keys containing 'A': " + Iterables.toString(tree.getKeyValuePairsForKeysContaining("A")));
}
 
Example #3
Source File: BuildShakespeareSinglePlaySuffixTree.java    From concurrent-trees with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ConcurrentSuffixTree<String> tree = new ConcurrentSuffixTree<String>(new DefaultCharSequenceNodeFactory());
    for (String file : files) {
        String manuscript = IOUtil.loadTextFileFromClasspath(file, true, true, true); // true = convert to lowercase
        String manuscriptName = file.replaceAll("/.*/.*/", "").replace(".txt", "");
        tree.put(manuscript, manuscriptName);
        System.out.println("Added " + manuscriptName);
    }
    System.out.println("Built Suffix Tree. Estimating size on disk...");
    DummyAppendable dummyAppendable = new DummyAppendable();
    PrettyPrinter.prettyPrint(tree, dummyAppendable);
    System.out.println("Done. Size on disk estimate:");
    System.out.println("Lines: " + dummyAppendable.lineCount);
    System.out.println("Characters: " + dummyAppendable.charCount);
}
 
Example #4
Source File: BuildShakespeareWordSuffixTree.java    From concurrent-trees with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ConcurrentSuffixTree<WordValue> tree = new ConcurrentSuffixTree<WordValue>(new DefaultCharArrayNodeFactory());
    for (String file : files) {
        Set<String> wordsInFile = IOUtil.loadWordsFromTextFileOnClasspath(file, true); // true = convert to lowercase
        for (String word : wordsInFile) {
            WordValue wordValue = tree.getValueForExactKey(word);
            if (wordValue == null) {
                wordValue = new WordValue(word);
                tree.put(word, wordValue); // not using concurrency support here
            }
            wordValue.manuscriptsContainingWord.add(file.replaceAll("/.*/.*/", "").replace(".txt", ""));
        }
    }

    final String radixTreePrinted = PrettyPrinter.prettyPrint(tree);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JTextArea textArea = new JTextArea();
            textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
            textArea.setText(radixTreePrinted);
            JScrollPane scrollPane = new JScrollPane(textArea);
            textArea.setEditable(false);
            JFrame frame = new JFrame("Shakespeare Suffix Tree");
            frame.add(scrollPane);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(640, 480);
            frame.setVisible(true);
        }
    });

}
 
Example #5
Source File: SuffixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected SuffixTreeIndex(Attribute<O, A> attribute, NodeFactory nodeFactory) {
    super(attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(StringEndsWith.class);
        add(StringContains.class);
    }});
    this.nodeFactory = nodeFactory;
    this.tree = new ConcurrentSuffixTree<StoredResultSet<O>>(nodeFactory);
}
 
Example #6
Source File: TypeUtils.java    From baleen with Apache License 2.0 4 votes vote down vote up
/**
 * For a given type name, look through the type system and return the matching class. If two types
 * of the same name (but different packages) exist, then null will be returned and the package
 * will need to be included in the typeName.
 *
 * @param typeName The name of the type, optionally including the package
 * @param typeSystem The type system to search
 * @return The class associated with that type
 */
@SuppressWarnings("unchecked")
public static Class<AnnotationBase> getType(String typeName, TypeSystem typeSystem) {
  SuffixTree<Class<AnnotationBase>> types =
      new ConcurrentSuffixTree<>(new DefaultByteArrayNodeFactory());

  Iterator<Type> itTypes = typeSystem.getTypeIterator();
  while (itTypes.hasNext()) {
    Type t = itTypes.next();

    Class<AnnotationBase> c;
    try {
      String clazz = t.getName();
      if (clazz.startsWith("uima.")) {
        continue;
      } else if (clazz.endsWith("[]")) {
        clazz = clazz.substring(0, clazz.length() - 2);
      }

      Class<?> unchecked = Class.forName(clazz);

      if (AnnotationBase.class.isAssignableFrom(unchecked)) {
        c = (Class<AnnotationBase>) unchecked;
        types.put(t.getName(), c);
      } else {
        LOGGER.debug("Skipping class {} that doesn't inherit from AnnotationBase", clazz);
      }
    } catch (ClassNotFoundException e) {
      LOGGER.warn("Unable to load class {} from type system", t.getName(), e);
    }
  }

  Class<AnnotationBase> ret = getClassFromType("." + typeName, types);
  if (ret == null) {
    ret = getClassFromType(typeName, types);
  }

  if (ret == null) {
    LOGGER.warn("No uniquely matching class found for type {}", typeName);
  }

  return ret;
}
 
Example #7
Source File: SuffixTreeIndex.java    From cqengine with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void clear(QueryOptions queryOptions) {
    this.tree = new ConcurrentSuffixTree<StoredResultSet<O>>(new DefaultCharArrayNodeFactory());
}
 
Example #8
Source File: TrieMemIndex.java    From sasi with Apache License 2.0 4 votes vote down vote up
private ConcurrentSuffixTrie(ColumnDefinition column)
{
    super(column);
    trie = new ConcurrentSuffixTree<>(new SmartArrayBasedNodeFactory());
}