javalambdaaho-corasick

Why doesn't the lambda example in hankcs/AhoCorasickDoubleArrayTrie work?


I'm just copying the example from this github project page without any change and it's giving me a compile error

To reproduce, add this dependency to your pom

<dependency>
  <groupId>com.hankcs</groupId>
  <artifactId>aho-corasick-double-array-trie</artifactId>
  <version>1.2.1</version>
</dependency>

Then try to run this:

    // Collect test data set
    TreeMap<String, String> map = new TreeMap<String, String>();
    String[] keyArray = new String[]
            {
                    "hers",
                    "his",
                    "she",
                    "he"
            };
    for (String key : keyArray)
    {
        map.put(key, key);
    }
    // Build an AhoCorasickDoubleArrayTrie
    AhoCorasickDoubleArrayTrie<String> acdat = new AhoCorasickDoubleArrayTrie<String>();
    acdat.build(map);
    // Test it
    final String text = "uhers";
    acdat.parseText(text, (begin, end, value) -> {
        System.out.printf("[%d:%d]=%s\n", begin, end, value);
    });

The compile error is

The method parseText(CharSequence, AhoCorasickDoubleArrayTrie.IHit<String>) is ambiguous for the type AhoCorasickDoubleArrayTrie<String>

Please let me know if you need anything to clarify. You should be able to reproduce this with what I have provided here though.

Also, it's been suggested this may be a duplicate question when I posted this previously, but I do not think that's the case as that question is not related to lambda functions. If I'm wrong, please help me understand how that question's answer can resolve what I'm experiencing


Solution

  • AhoCorasickDoubleArrayTrie has two methods called parseText, one with IHit, another with IHitCancellable as parameter. Both interfaces declare a method boolean hit(int begin, int end, V value), so by using a lambda, the compiler doesn't know what method you intend to call.

    I haven't found a quick solution by googling but what you can do is declare your own class extending AhoCorasickDoubleArrayTrie with an own method that is calling the intended method in the super class that has the interface you want to use, e.g.

    void myParseText(String text, IHit<V> hit) {
        super.parseText(text, hit);
    }