Comment utiliser la bibliothèque java Lucence dans le projet Android?


Bien que j'ai intégré des bibliothèques dans la construction.gradle, mais lorsque je l'exécute sur le périphérique, l'erreur suivante se produit.

11-16 12:59:10.097 14630-14630/sadboy.antix E/AndroidRuntime: FATAL EXCEPTION: main
  Process: sadboy.antix, PID: 14630
  java.lang.NoClassDefFoundError: org.apache.lucene.util.AttributeSource$2
      at org.apache.lucene.util.AttributeSource.<clinit>(AttributeSource.java:150)
      at sadboy.antix.text_processing.KeywordsExtractor.getKeywordsList(KeywordsExtractor.java:34)
      at sadboy.antix.activities.NewsArticle.onCreate(NewsArticle.java:51)
      at android.app.Activity.performCreate(Activity.java:6010)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
      at android.app.ActivityThread.access$800(ActivityThread.java:155)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5343)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
11-16 13:04:10.197 14630-14630/sadboy.antix I/Process: Sending signal. PID: 14630 SIG: 9

Mon code ressemble à ceci, buidl.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "sadboy.antix"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:25.0.0'

    //Library for web scraping
    compile 'org.jsoup:jsoup:1.10.1'

    //Library for HTTP request
    compile 'com.squareup.okhttp3:okhttp:3.4.2'

    //Savage UX/UI libraries
    compile 'com.mikhaellopez:circularfillableloaders:1.2.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.balysv:material-ripple:1.0.2'
    compile('com.mikepenz:materialdrawer:5.7.0@aar') {
        transitive = true
    }

    //Lucence libraries for text processing
    compile group: 'org.apache.lucene', name: 'lucene-core', version: '5.5.0'
    compile group: 'org.apache.lucene', name: 'lucene-analyzers-common', version: '5.5.0'

    //Essential google libraries
    compile 'com.android.support:cardview-v7:25.0.0'
    compile 'com.android.support:recyclerview-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
}

Classes où j'utilise Lucence

package sadboy.antix.text_processing;

/**
 * Created by Varun Kumar on 11/15/2016.
 */

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.en.PorterStemFilter;
import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter;
import org.apache.lucene.analysis.standard.ClassicFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

import java.io.IOException;
import java.io.StringReader;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class KeywordsExtractor {

    public static List<CardKeyword> getKeywordsList(String fullText) throws IOException {

        TokenStream tokenStream = null;

        try {

            fullText = fullText.replaceAll("-+", "-0");
            fullText = fullText.replaceAll("[\\p{Punct}&&[^'-]]+", " ");
            fullText = fullText.replaceAll("(?:'(?:[tdsm]|[vr]e|ll))+\\b", "");

            StandardTokenizer stdToken = new StandardTokenizer();
            stdToken.setReader(new StringReader(fullText));

            tokenStream = new StopFilter(new ASCIIFoldingFilter(new ClassicFilter(new LowerCaseFilter(stdToken))), EnglishAnalyzer.getDefaultStopSet());
            tokenStream.reset();

            List<CardKeyword> cardKeywords = new LinkedList<>();

            CharTermAttribute token = tokenStream.getAttribute(CharTermAttribute.class);

            while (tokenStream.incrementToken()) {

                String term = token.toString();
                String stem = getStemForm(term);

                if (stem != null) {
                    CardKeyword cardKeyword = find(cardKeywords, new CardKeyword(stem.replaceAll("-0", "-")));
                    cardKeyword.add(term.replaceAll("-0", "-"));
                }
            }

            Collections.sort(cardKeywords);

            return cardKeywords;
        } finally {
            if (tokenStream != null) {
                try {
                    tokenStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static String getStemForm(String term) throws IOException {

        TokenStream tokenStream = null;

        try {
            StandardTokenizer stdToken = new StandardTokenizer();
            stdToken.setReader(new StringReader(term));

            tokenStream = new PorterStemFilter(stdToken);
            tokenStream.reset();


            Set<String> stems = new HashSet<>();

            CharTermAttribute token = tokenStream.getAttribute(CharTermAttribute.class);

            while (tokenStream.incrementToken()) {
                stems.add(token.toString());
            }


            if (stems.size() != 1) {
                return null;
            }

            String stem = stems.iterator().next();

            if (!stem.matches("[a-zA-Z0-9-]+")) {
                return null;
            }

            return stem;
        } finally {
            if (tokenStream != null) {
                try {
                    tokenStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static <T> T find(Collection<T> collection, T sample) {

        for (T element : collection) {
            if (element.equals(sample)) {
                return element;
            }
        }

        collection.add(sample);

        return sample;
    }
}

La Classe Du Modèle

package sadboy.antix.text_processing;

/**
 * Created by Varun Kumar on 11/15/2016.
 */

import java.util.HashSet;
import java.util.Set;


public class CardKeyword implements Comparable<CardKeyword> {


    private final String stem;
    private final Set<String> terms = new HashSet<>();
    private int frequency;

    public CardKeyword(String stem) {
        this.stem = stem;
    }


    public void add(String term) {
        this.terms.add(term);
        this.frequency++;
    }

    @Override
    public int compareTo(CardKeyword keyword) {
        return Integer.valueOf(keyword.frequency).compareTo(this.frequency);
    }

    @Override
    public int hashCode() {
        return this.getStem().hashCode();
    }


    @Override
    public boolean equals(Object o) {

        if (this == o) return true;

        if (!(o instanceof CardKeyword)) return false;

        CardKeyword that = (CardKeyword) o;

        return this.getStem().equals(that.getStem());
    }


    public String getStem() {
        return this.stem;
    }


    public Set<String> getTerms() {
        return this.terms;
    }


    public int getFrequency() {
        return this.frequency;
    }
}

Activité où tout cela commence

package sadboy.antix.activities;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.IOException;
import java.util.List;

import sadboy.antix.R;
import sadboy.antix.text_processing.CardKeyword;
import sadboy.antix.text_processing.KeywordsExtractor;

public class NewsArticle extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_article);

    String input = "Pick Flick is your ultimate app to find information about Movies, TV shows and Celebrities fast and easily on the go\n" +
            "\n" +
            "• Quick access of popular, top rated, now playing, highest grossing and upcoming movies\n" +
            "• Quick access of popular, top rated, airing today and on air TV shows\n" +
            "• View the cast of movies and TV shows\n" +
            "• Similar Movies and TV shows to watch to explore more on the basis of your search\n" +
            "• Find the movies or TV shows done by a celebrity\n" +
            "• Set reminders and bookmarks so you never forget or miss anything you like\n" +
            "• Share everything with your friends and family to plan movies or just watch a TV show maybe\n" +
            "• List of trending celebrities\n" +
            "• All the information and rating of Movies, TV shows and Celebrities on the go\n" +
            "• Fast and easy search with keywords and suggestions\n" +
            "• Fluid and easy to use UI enabling for glitch free experience\n" +
            "\n" +
            "• Like Pick Flick on Facebook\n" +
            "• https://www.facebook.com/pickflickapp\n" +
            "\n" +
            "• Follow Pick Flick on Instagram\n" +
            "• https://www.instagram.com/pickflickapp/\n" +
            "\n" +
            "• Pick Flick uses data and images by TMDb licensed under CC BY-NC 4.0\n" +
            "• https://creativecommons.org/licenses/by-nc/4.0/\n" +
            "\n" +
            "• Pick Flick uses the TMDb API but is not endorsed or certified by TMDb\n" +
            "• https://www.themoviedb.org/documentation/api/terms-of-use";


    List<CardKeyword> keywordsList = null;
    try {
        keywordsList = KeywordsExtractor.getKeywordsList(input);
    } catch (IOException e) {
        e.printStackTrace();
    }

    int i;

    for(i = 0;i<keywordsList.size();i++)
    {
        Log.d("Keyword",i+" "+keywordsList.get(i).getStem());
    }
}

}

Author: jva, 2016-11-16

1 answers

Pls vérifier en ajoutant ceci:

// https://mvnrepository.com/artifact/org.apache.lucene/lucene-backward-codecs

compile group: 'org.apache.lucene', name: 'lucene-backward-codecs', version: '5.5.0'
 0
Author: Chandana Kumara, 2016-11-16 08:47:21