当前位置: 首页 > 知识库问答 >
问题:

如何在android项目中使用java Lucence库?

苏星宇
2023-03-14

虽然我在构建中集成了库。但当我在设备上运行它时,会发生以下错误。

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

我的代码是这样的,buidl。格拉德尔

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'
}

我正在使用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;
    }
}

模范班

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;
    }
}

这一切从哪里开始

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());
    }
}

}

共有1个答案

高慈
2023-03-14

请检查添加这个:

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

compile group: 'org.apache.lucene', name: 'lucene-backward-codecs', version: '5.5.0'
 类似资料:
  • 我之所以问这个问题,是因为我对Java和Android还不熟悉,我花了好几个小时想弄清楚这个问题。答案来自相关答案的组合,所以我想我会把我学到的东西记录下来,给其他正在挣扎的人。参见答案。 我使用的是Android Studio2.1.2,我的Java设置如下:

  • 我是Kotlin的新手,正在尝试使用coroutine学习异步编程。我在跟踪Kotlin的官方文件。但是当我试图编译代码时,它显示了错误:“未解析的引用:Kotlinx”。所以我只想知道,如何在非android项目中使用Coroutine? 我正在用Ubuntu终端编译代码。 代码段

  • 我想在我的应用程序中实现autolinktextview。github中有一个库,现在我想在我的应用程序中使用,但库语言是kotlin,我的应用程序项目语言是java。下面给出的库Url和我使用的依赖项。 https://github.com/armcha/AutoLinkTextViewV2

  • 我创建了一个TextView库项目,其中我使用了自定义字体。我必须创建这个库,因为这个也将在其他三个项目中使用。我发现android库项目不支持资产/字体文件夹。如果我以这种方式使用,它会给我错误消息 这里是否有解决此问题的方法。另外,我必须在三个不同的应用程序中使用重复视图。还有我用来从library assets文件夹中获取字体的示例代码。

  • 我对分级建造系统和智能是新的。 那么我如何创建一个Android库项目(例如com.myapp.lib1)和应用程序项目(例如com.myapp.app),并使构建系统在应用程序项目上包含com.myapp.lib1呢? 我转到Project Structure->Modules->我的App项目,并向lib项目添加了一个依赖项。IntelliJ现在在应用程序项目中使用时可以从lib项目中识别类,