Java - 如何导入外部包?

标签 java package

我对 Java 还是比较陌生。我已经尝试寻找解决方案,但我认为我对 Java 的了解还不够多,甚至不知道我应该寻找什么。

我想试试语音识别,所以我下载了 CMU 的 Sphinx-4 源代码并编译了它。一旦一切正常,我就可以很好地运行包含的演示。接下来,我在包含 Sphinx-4 目录的目录中为我的代码创建了一个目录 (/Jarvis/)。在 Sphinx-4 源目录中,有一长串目录指向我玩过的演示 (/Sphinx4-1.0beta6/src/apps/edu/cmu/sphinx/demo/)。在演示文件夹中是一个 HelloWorld 目录,其中包含一个使用 Sphinx-4 功能的简单程序的源代码。我的第一个目标是在我为自己的代码设置的目录中获取此演示的副本。所以我制作了 HelloWorld 文件的副本并将它们放在我的 Jarvis 目录中。重命名并更改了代码,以便以前是 HelloWorld 的所有内容现在都是 Jarvis。不幸的是,我遇到的问题并不像命名问题那么简单,而是找到包的路径。

对于 Java,过去我大多只使用简单的“javac example.java”命令来编译代码。我猜在这种情况下我可能需要更多东西,但我不确定到底是什么。尝试以这种方式编译时出现错误:

Jarvis.java:15: error: package edu.cmu.sphinx.frontend.util does not exist
import edu.cmu.sphinx.frontend.util.Microphone;
                                   ^
Jarvis.java:16: error: package edu.cmu.sphinx.recognizer does not exist
import edu.cmu.sphinx.recognizer.Recognizer;
                                ^
Jarvis.java:17: error: package edu.cmu.sphinx.result does not exist
import edu.cmu.sphinx.result.Result;
                            ^
Jarvis.java:18: error: package edu.cmu.sphinx.util.props does not exist
import edu.cmu.sphinx.util.props.ConfigurationManager;
                                ^
Jarvis.java:28: error: cannot find symbol
        ConfigurationManager cm;
        ^
  symbol:   class ConfigurationManager
  location: class Jarvis
Jarvis.java:31: error: cannot find symbol
            cm = new ConfigurationManager(args[0]);
                     ^
  symbol:   class ConfigurationManager
  location: class Jarvis
Jarvis.java:33: error: cannot find symbol
            cm = new ConfigurationManager(Jarvis.class.getResource("jarvis.config.xml"));
                     ^
  symbol:   class ConfigurationManager
  location: class Jarvis
Jarvis.java:36: error: cannot find symbol
        Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
        ^
  symbol:   class Recognizer
  location: class Jarvis
Jarvis.java:36: error: cannot find symbol
        Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
                                 ^
  symbol:   class Recognizer
  location: class Jarvis
Jarvis.java:40: error: cannot find symbol
        Microphone microphone = (Microphone) cm.lookup("microphone");
        ^
  symbol:   class Microphone
  location: class Jarvis
Jarvis.java:40: error: cannot find symbol
        Microphone microphone = (Microphone) cm.lookup("microphone");
                                 ^
  symbol:   class Microphone
  location: class Jarvis
Jarvis.java:53: error: cannot find symbol
            Result result = recognizer.recognize();
            ^
  symbol:   class Result
  location: class Jarvis
12 errors

所以我希望包括那些丢失的包。我知道它与代码中路径的定义方式有关,但我对 Java 的了解还不够,无法确切知道如何定义这些路径。我是否应该在代码中更改更多内容?我应该以不同的方式进行编译吗?只要朝着正确的方向插入就会很有用。

下面我包含了最初来自 HelloWorld 演示的 4 个文件,我对其进行了轻微的命名修改。

感谢您的宝贵时间!

贾维斯.java:

package jarvis.jarvis;

import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;


/**
 * A simple HelloWorld demo showing a simple speech application built using Sphinx-4. This application uses the Sphinx-4
 * endpointer, which automatically segments incoming audio into utterances and silences.
 */
public class Jarvis {

    public static void main(String[] args) {
            ConfigurationManager cm;

        if (args.length > 0) {
            cm = new ConfigurationManager(args[0]);
        } else {
            cm = new ConfigurationManager(Jarvis.class.getResource("jarvis.config.xml"));
        }

        Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
        recognizer.allocate();

        // start the microphone or exit if the programm if this is not possible
        Microphone microphone = (Microphone) cm.lookup("microphone");
        if (!microphone.startRecording()) {
            System.out.println("Cannot start microphone.");
            recognizer.deallocate();
            System.exit(1);
        }

        System.out.println("Say: (Good morning | Hello) ( Bhiksha | Evandro | Paul | Philip | Rita | Will )");

        // loop the recognition until the programm exits.
        while (true) {
            System.out.println("Start speaking. Press Ctrl-C to quit.\n");

            Result result = recognizer.recognize();

            if (result != null) {
                String resultText = result.getBestFinalResultNoFiller();
                System.out.println("You said: " + resultText + '\n');
            } else {
                System.out.println("I can't hear what you said.\n");
            }
        }
    }
}

jarvis.config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!--
   Sphinx-4 Configuration file
-->

<!-- ******************************************************** -->
<!--  an4 configuration file                             -->
<!-- ******************************************************** -->

<config>

    <!-- ******************************************************** -->
    <!-- frequently tuned properties                              -->
    <!-- ******************************************************** -->

    <property name="logLevel" value="WARNING"/>

    <property name="absoluteBeamWidth"  value="-1"/>
    <property name="relativeBeamWidth"  value="1E-80"/>
    <property name="wordInsertionProbability" value="1E-36"/>
    <property name="languageWeight"     value="8"/>

    <property name="frontend" value="epFrontEnd"/>
    <property name="recognizer" value="recognizer"/>
    <property name="showCreations" value="false"/>


    <!-- ******************************************************** -->
    <!-- word recognizer configuration                            -->
    <!-- ******************************************************** -->

    <component name="recognizer" type="edu.cmu.sphinx.recognizer.Recognizer">
        <property name="decoder" value="decoder"/>
        <propertylist name="monitors">
            <item>accuracyTracker </item>
            <item>speedTracker </item>
            <item>memoryTracker </item>
        </propertylist>
    </component>

    <!-- ******************************************************** -->
    <!-- The Decoder   configuration                              -->
    <!-- ******************************************************** -->

    <component name="decoder" type="edu.cmu.sphinx.decoder.Decoder">
        <property name="searchManager" value="searchManager"/>
    </component>

    <component name="searchManager"
        type="edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager">
        <property name="logMath" value="logMath"/>
        <property name="linguist" value="flatLinguist"/>
        <property name="pruner" value="trivialPruner"/>
        <property name="scorer" value="threadedScorer"/>
        <property name="activeListFactory" value="activeList"/>
    </component>


    <component name="activeList"
             type="edu.cmu.sphinx.decoder.search.PartitionActiveListFactory">
        <property name="logMath" value="logMath"/>
        <property name="absoluteBeamWidth" value="${absoluteBeamWidth}"/>
        <property name="relativeBeamWidth" value="${relativeBeamWidth}"/>
    </component>

    <component name="trivialPruner"
                type="edu.cmu.sphinx.decoder.pruner.SimplePruner"/>

    <component name="threadedScorer"
                type="edu.cmu.sphinx.decoder.scorer.ThreadedAcousticScorer">
        <property name="frontend" value="${frontend}"/>
    </component>

    <!-- ******************************************************** -->
    <!-- The linguist  configuration                              -->
    <!-- ******************************************************** -->

    <component name="flatLinguist"
                type="edu.cmu.sphinx.linguist.flat.FlatLinguist">
        <property name="logMath" value="logMath"/>
        <property name="grammar" value="jsgfGrammar"/>
        <property name="acousticModel" value="wsj"/>
        <property name="wordInsertionProbability"
                value="${wordInsertionProbability}"/>
        <property name="languageWeight" value="${languageWeight}"/>
        <property name="unitManager" value="unitManager"/>
    </component>


    <!-- ******************************************************** -->
    <!-- The Grammar  configuration                               -->
    <!-- ******************************************************** -->

    <component name="jsgfGrammar" type="edu.cmu.sphinx.jsgf.JSGFGrammar">
        <property name="dictionary" value="dictionary"/>
        <property name="grammarLocation"
             value="resource:/edu/cmu/sphinx/demo/jarvis/"/>
        <property name="grammarName" value="jarvis"/>
    <property name="logMath" value="logMath"/>
    </component>


    <!-- ******************************************************** -->
    <!-- The Dictionary configuration                            -->
    <!-- ******************************************************** -->

    <component name="dictionary"
        type="edu.cmu.sphinx.linguist.dictionary.FastDictionary">
        <property name="dictionaryPath"
     value="resource:/WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/dict/cmudict.0.6d"/>
        <property name="fillerPath"
     value="resource:/WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/noisedict"/>
        <property name="addSilEndingPronunciation" value="false"/>
        <property name="allowMissingWords" value="false"/>
        <property name="unitManager" value="unitManager"/>
    </component>

    <!-- ******************************************************** -->
    <!-- The acoustic model configuration                         -->
    <!-- ******************************************************** -->
    <component name="wsj"
               type="edu.cmu.sphinx.linguist.acoustic.tiedstate.TiedStateAcousticModel">
        <property name="loader" value="wsjLoader"/>
        <property name="unitManager" value="unitManager"/>
    </component>

    <component name="wsjLoader" type="edu.cmu.sphinx.linguist.acoustic.tiedstate.Sphinx3Loader">
        <property name="logMath" value="logMath"/>
        <property name="unitManager" value="unitManager"/>
        <property name="location" value="resource:/WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz"/>
    </component>


    <!-- ******************************************************** -->
    <!-- The unit manager configuration                           -->
    <!-- ******************************************************** -->

    <component name="unitManager"
        type="edu.cmu.sphinx.linguist.acoustic.UnitManager"/>

    <!-- ******************************************************** -->
    <!-- The frontend configuration                               -->
    <!-- ******************************************************** -->

    <component name="frontEnd" type="edu.cmu.sphinx.frontend.FrontEnd">
        <propertylist name="pipeline">
            <item>microphone </item>
            <item>preemphasizer </item>
            <item>windower </item>
            <item>fft </item>
            <item>melFilterBank </item>
            <item>dct </item>
            <item>liveCMN </item>
            <item>featureExtraction </item>
        </propertylist>
    </component>

    <!-- ******************************************************** -->
    <!-- The live frontend configuration                          -->
    <!-- ******************************************************** -->
    <component name="epFrontEnd" type="edu.cmu.sphinx.frontend.FrontEnd">
        <propertylist name="pipeline">
            <item>microphone </item>
            <item>dataBlocker </item>
            <item>speechClassifier </item>
            <item>speechMarker </item>
            <item>nonSpeechDataFilter </item>
            <item>preemphasizer </item>
            <item>windower </item>
            <item>fft </item>
            <item>melFilterBank </item>
            <item>dct </item>
            <item>liveCMN </item>
            <item>featureExtraction </item>
        </propertylist>
    </component>

    <!-- ******************************************************** -->
    <!-- The frontend pipelines                                   -->
    <!-- ******************************************************** -->

    <component name="dataBlocker" type="edu.cmu.sphinx.frontend.DataBlocker">
        <!--<property name="blockSizeMs" value="10"/>-->
    </component>

    <component name="speechClassifier"
               type="edu.cmu.sphinx.frontend.endpoint.SpeechClassifier">
        <property name="threshold" value="13"/>
    </component>

    <component name="nonSpeechDataFilter"
               type="edu.cmu.sphinx.frontend.endpoint.NonSpeechDataFilter"/>

    <component name="speechMarker"
               type="edu.cmu.sphinx.frontend.endpoint.SpeechMarker" >
        <property name="speechTrailer" value="50"/>
    </component>


    <component name="preemphasizer"
               type="edu.cmu.sphinx.frontend.filter.Preemphasizer"/>

    <component name="windower"
               type="edu.cmu.sphinx.frontend.window.RaisedCosineWindower">
    </component>

    <component name="fft"
            type="edu.cmu.sphinx.frontend.transform.DiscreteFourierTransform">
    </component>

    <component name="melFilterBank"
        type="edu.cmu.sphinx.frontend.frequencywarp.MelFrequencyFilterBank">
    </component>

    <component name="dct"
            type="edu.cmu.sphinx.frontend.transform.DiscreteCosineTransform"/>

    <component name="liveCMN"
               type="edu.cmu.sphinx.frontend.feature.LiveCMN"/>

    <component name="featureExtraction"
               type="edu.cmu.sphinx.frontend.feature.DeltasFeatureExtractor"/>

    <component name="microphone"
               type="edu.cmu.sphinx.frontend.util.Microphone">
        <property name="closeBetweenUtterances" value="false"/>
    </component>


    <!-- ******************************************************* -->
    <!--  monitors                                               -->
    <!-- ******************************************************* -->

    <component name="accuracyTracker"
                type="edu.cmu.sphinx.instrumentation.BestPathAccuracyTracker">
        <property name="recognizer" value="${recognizer}"/>
        <property name="showAlignedResults" value="false"/>
        <property name="showRawResults" value="false"/>
    </component>

    <component name="memoryTracker"
                type="edu.cmu.sphinx.instrumentation.MemoryTracker">
        <property name="recognizer" value="${recognizer}"/>
    <property name="showSummary" value="false"/>
    <property name="showDetails" value="false"/>
    </component>

    <component name="speedTracker"
                type="edu.cmu.sphinx.instrumentation.SpeedTracker">
        <property name="recognizer" value="${recognizer}"/>
        <property name="frontend" value="${frontend}"/>
    <property name="showSummary" value="true"/>
    <property name="showDetails" value="false"/>
    </component>


    <!-- ******************************************************* -->
    <!--  Miscellaneous components                               -->
    <!-- ******************************************************* -->

    <component name="logMath" type="edu.cmu.sphinx.util.LogMath">
        <property name="logBase" value="1.0001"/>
        <property name="useAddTable" value="true"/>
    </component>

</config>

贾维斯.克:

#JSGF V1.0;

/**
 * JSGF Grammar for Hello World example
 */

grammar jarvis;

public <greet> = (Good morning | Hello) ( Bhiksha | Evandro | Paul | Philip | Rita | Will );

贾维斯. list

Main-Class: jarvis.Jarvis
Class-Path: ../sphinx4-1.0beta6/lib/sphinx4.jar ../sphinx4-1.0beta6/lib/WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar

最佳答案

编译器使用“类路径”来查找所需的库。设置类路径的方法有很多种:一种是使用CLASSPATH环境变量,另一种是使用-classpath命令行开关。总之,您可以编译:

javac -classpath path/to/sphinx.jar jarvis/Jarvis.java

您可以在 http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html 中找到完整的文档

另一件事:您的 Jarvis 类声明它位于名为 jarvis.jarvis 的包中,因此该类的完全限定名称将是 jarvis.jarvis.Jarvis 。 .. 不确定这是否是您的意图。

关于Java - 如何导入外部包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9153261/

相关文章:

java - 使用 null 值调用具有相同 arity 的两个方法

java - 什么是 Java bean?

java - Struts 2 中同一页面上传的文件详细信息

git - 如何将 Go 与私有(private) GitLab 存储库一起使用

java - Maven 禁止循环包依赖

java - 将数组保存到硬盘

java - 在android中的自定义数组适配器中获取过滤器选项?

java - 多个 Java 项目和重构

android - 厂商时钟/闹钟包及类名列表,请加

android - 这样我就可以在 Intent 中发送 Toast 了吗?