JAR download https://github.com/watson-developer-cloud/java-sdk Download from "Download the jar with dependencies here."
code
		String versionDate = "2018-03-19";
		String username = "xxx";
		String password = "xxx";
		NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding(versionDate, username, password);
		SentimentOptions sentiment = new SentimentOptions.Builder().build();
		EmotionOptions emotion = new EmotionOptions.Builder().build();
		KeywordsOptions keywords = new KeywordsOptions.Builder().build();
		EntitiesOptions entities = new EntitiesOptions.Builder().build();
		CategoriesOptions categories = new CategoriesOptions();
		SemanticRolesOptions semanticRoles = new SemanticRolesOptions.Builder().build();
		Features features = new Features.Builder() //
				.sentiment(sentiment) //
				.emotion(emotion) // "emotion: unsupported text language: ja"
				.keywords(keywords) //
				.entities(entities) //
				.categories(categories) //
				.semanticRoles(semanticRoles) //
				.build();
		String language = "ja";
		String text = "US President Trump...He criticized the company for surrendering in a trade war.";
		AnalyzeOptions analyzeOptions = new AnalyzeOptions.Builder().features(features).language(language).text(text)
				.build();
		AnalysisResults results = service.analyze(analyzeOptions).execute();
		System.err.println(results); // Print JSON
		if (results.getCategories() != null) {
			for (CategoriesResult result : results.getCategories()) {
				System.err.println(result.getLabel() + " : " + result.getScore());
			}
		}
Below are the results. (A part of the news text part is "... (omitted) ...")
{
  "language": "ja",
  "usage": {
    "features": 5,
    "text_characters": 94,
    "text_units": 1
  },
  "entities": [
    {
      "type": "Person",
      "text": "Playing cards",
      "relevance": 0.954265,
      "count": 1
    },
    {
      "type": "Organization",
      "text": "Harley-Davidson",
      "relevance": 0.860373,
      "count": 1
    },
    {
      "type": "Organization",
      "text": "European Union",
      "relevance": 0.79402,
      "count": 1
    },
    {
      "type": "Location",
      "text": "Rice",
      "relevance": 0.636659,
      "count": 1
    },
    {
      "type": "Location",
      "text": "EU",
      "relevance": 0.115173,
      "count": 1
    }
  ],
  "keywords": [
    {
      "relevance": 0.809941,
      "text": "Trade war"
    },
    {
      "relevance": 0.733196,
      "text": "Outside the United States"
    },
    {
      "relevance": 0.710815,
      "text": "Between the EU"
    },
    {
      "relevance": 0.582804,
      "text": "The company"
    },
    {
      "relevance": 0.569772,
      "text": "European Union(EU"
    },
    {
      "relevance": 0.557057,
      "text": "EU"
    },
    {
      "relevance": 0.498835,
      "text": "war"
    },
    {
      "relevance": 0.498399,
      "text": "Trade"
    },
    {
      "relevance": 0.472507,
      "text": "production"
    },
    {
      "relevance": 0.462888,
      "text": "Overseas"
    },
    {
      "relevance": 0.461109,
      "text": "European Union(EU)Retaliation tariffs by"
    },
    {
      "relevance": 0.445569,
      "text": "US President Trump"
    },
    {
      "relevance": 0.430877,
      "text": "Tariffs"
    },
    {
      "relevance": 0.418106,
      "text": "Retaliation"
    },
    {
      "relevance": 0.417108,
      "text": "while"
    },
    {
      "relevance": 0.395505,
      "text": "Rice"
    },
    {
      "relevance": 0.384407,
      "text": "European Union"
    },
    {
      "relevance": 0.370795,
      "text": "Harley-Davidson"
    },
    {
      "relevance": 0.35972,
      "text": "US President"
    },
    {
      "relevance": 0.350696,
      "text": "Playing cards"
    }
  ],
  "categories": [
    {
      "label": "/society/unrest and war",
      "score": 0.288369
    },
    {
      "label": "/law, govt and politics/government",
      "score": 0.197268
    },
    {
      "label": "/law, govt and politics",
      "score": 0.155733
    }
  ],
  "semantic_roles": [
    {
      "sentence": "US President Trump...(Abbreviation)...I criticized the company.",
      "action": {
        "text": "To do",
        "normalized": "To do",
        "verb": {
          "text": "To do"
        }
      },
      "object": {
        "text": "Production"
      }
    },
    {
      "sentence": "US President Trump...(Abbreviation)...I criticized the company.",
      "subject": {
        "text": "Harley Davidson"
      },
      "action": {
        "text": "did",
        "normalized": "did",
        "verb": {
          "text": "did"
        }
      }
    },
    {
      "sentence": "US President Trump...(Abbreviation)...I criticized the company.",
      "action": {
        "text": "did",
        "normalized": "did",
        "verb": {
          "text": "did"
        }
      },
      "object": {
        "text": "The company"
      }
    }
  ],
  "sentiment": {
    "document": {
      "label": "neutral",
      "score": 0.0
    }
  }
}
null /society/unrest and war : 0.288369 /law, govt and politics/government : 0.197268 /law, govt and politics : 0.155733
Recommended Posts