javawatson

IBM Watson Tone Analyser


Trying to work the sample code to work. I am using Java 7.

I get an error on this line

service.setUsernameAndPassword("<username>", "<password>");

The notification is asking me to search repo for "service". This shouldnt be the case as service is defined on the line above. Does anyone know what could be wrong here?

import com.ibm.watson.developer_cloud.tone_analyzer.v3.*;
import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis;
import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions;

public class test {

    ToneAnalyzer service = new ToneAnalyzer("2017-09-21");
service.setUsernameAndPassword("<username>", "<password>");

String text =
  "I know the times are difficult! Our sales have been "
      + "disappointing for the past three quarters for our data analytics "
      + "product suite. We have a competitive data analytics product "
      + "suite in the industry. But we need to do our job selling it! "
      + "We need to acknowledge and fix our sales challenges. "
      + "We can’t blame the economy for our lack of execution! "
      + "We are missing critical sales opportunities. "
      + "Our product is in no way inferior to the competitor products. "
      + "Our clients are hungry for analytical tools to improve their "
      + "business outcomes. Economy has nothing to do with it.";

// Call the service and get the tone
ToneOptions toneOptions = new ToneOptions.Builder()
  .html(text)
  .build();

ToneAnalysis tone = service.tone(toneOptions).execute();
System.out.println(tone);

}

I also get an error for the System.out.println saying it cannot find symbol "tone".

pom file:

    <dependency>
        <groupId>com.ibm.watson.developer_cloud</groupId>
        <artifactId>java-sdk</artifactId>
        <version>6.1.0</version>
    </dependency>  

Solution

  • service.setUsernameAndPassword("<username>", "<password>");
    

    This is a call statement and should be inside a method.

    May be you want to rewrite you class like this:

    import com.ibm.watson.developer_cloud.tone_analyzer.v3.*;
    import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneAnalysis;
    import com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneOptions;
    
    public class Test {
    
    public static void main(String...s) {
        ToneAnalyzer service = new ToneAnalyzer("2017-09-21");
    service.setUsernameAndPassword("<username>", "<password>");
    
    String text =
      "I know the times are difficult! Our sales have been "
          + "disappointing for the past three quarters for our data analytics "
          + "product suite. We have a competitive data analytics product "
          + "suite in the industry. But we need to do our job selling it! "
          + "We need to acknowledge and fix our sales challenges. "
          + "We can’t blame the economy for our lack of execution! "
          + "We are missing critical sales opportunities. "
          + "Our product is in no way inferior to the competitor products. "
          + "Our clients are hungry for analytical tools to improve their "
          + "business outcomes. Economy has nothing to do with it.";
    
    // Call the service and get the tone
    ToneOptions toneOptions = new ToneOptions.Builder()
      .html(text)
      .build();
    
    ToneAnalysis tone = service.tone(toneOptions).execute();
    System.out.println(tone);
    
    }
    
    }
    

    PS: Its a good coding standard to start class name with a capital letter.