谷歌自然语言情感分析存在问题

时间:2019-04-26 14:51:29

标签: android google-cloud-platform

我有使用Google Cloud Natural Language API对语句或一组语句执行情感分析的代码。但是,当项目运行时,即使我的代码没有错误,我也没有得到任何输出。

我也尝试实现由android提供的示例android应用程序,以实现情感分析,尽管代码运行得很好,但我听不懂代码。这是该链接,对于理解代码的任何帮助将不胜感激-https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/nl/Language

These are my dependencies:

implementation 'com.google.cloud:google-cloud-language:1.70.0'

我的应用程序级别gradle文件中的android部分如下所示:

android {     编译版本28     defaultConfig {         applicationId“ com.example.googlesentiment”         minSdkVersion 19         targetSdkVersion 28         版本代码1         versionName“ 1.0”         testInstrumentationRunner     “ android.support.test.runner.AndroidJUnitRunner”     }     buildTypes {         发布 {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android-              Optimiz.txt'),'proguard-rules.pro'         }     }     configuration.all {         resolutionStrategy.force'com.google.code.findbugs:jsr305:1.3.9'     }     包装选项{         排除“ META-INF / DEPENDENCIES”         排除“ META-INF /许可”         排除“ META-INF / LICENSE.txt”         排除“ META-INF / license.txt”         排除“ META-INF / NOTICE”         排除“ META-INF / NOTICE.txt”         排除“ META-INF / notice.txt”         排除“ META-INF / ASL2.0”         排除“ META-INF / INDEX.LIST”     } }

I have a class named MyAnalyser where the main code for sentiment analysis lies . It is as follows:

package com.example.googlesentiment;

import com.google.cloud.language.v1.AnalyzeSentimentResponse;

import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;

import com.google.cloud.language.v1.LanguageServiceClient;
import com.google.cloud.language.v1.Sentiment;

public class MyAnalyser {

    String magnitude,score;
    private final static String TAG = "My Tag";

    public  Sentiment analyzeSentimentText(String text) throws Exception {
        // [START language_sentiment_text]
        // Instantiate The Languageclient 
           com.google.cloud.language.v1.LanguageServiceClient
        try (LanguageServiceClient language = LanguageServiceClient.create()) 
         {
            Document doc = Document.newBuilder()
                    .setContent(text)
                    .setType(Type.PLAIN_TEXT)
                    .build();
            AnalyzeSentimentResponse response = 
                           language.analyzeSentiment(doc);
            Sentiment sentiment = response.getDocumentSentiment();
            if (sentiment == null) {
                System.out.println("No sentiment found");
            } else {
                magnitude = Float.toString(sentiment.getMagnitude());
                score = Float.toString(sentiment.getMagnitude());
                Log.d(TAG,Float.toString(sentiment.getMagnitude()));
                Log.d(TAG,Float.toString(sentiment.getScore()));
            }

            Log.d(TAG,sentiment.toString());
            return sentiment;
        }
        // [END language_sentiment_text]
    }


    public String getMagnitude(){
        return magnitude;
    }
    public String getScore(){
        return score;
    }


}

这是我的mainActivity.java文件的create方法:单击按钮时,将执行情感分析,但是当我单击按钮时,什么也没发生:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    enterSample = this.findViewById(R.id.enter_sample_text);
    button = this.findViewById(R.id.button_analyse);
    textView = this.findViewById(R.id.get_sentiment_and_entity);

    sampleText = enterSample.getText().toString();
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG,"this is starting");
            MyAnalyser analyser = new MyAnalyser();
            try {
                Log.d(TAG,"this is starting");
                analyser.analyzeSentimentText(sampleText);
                Log.d(TAG,"this is starting");
                textView.setText(analyser.getMagnitude() + "\n" + 
                analyser.getScore());
                Log.d(TAG,"this is starting");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });


}

我希望情绪和幅度能达到浮动类型(转换为字符串)。但没有输出

0 个答案:

没有答案