我有两个程序,一个在ReactJS中,一个在Java中,并且需要将一个2D数组从ReactJS传递到我的Java main中,但是没有意识到我无法修改public static void main(String[] args) {}
的主要签名接受它。
我也看到了类似的问题,但是它们通常是关于在方法内部设置的一般方法周围传递多维数组,但是需要传递类似以下内容的方法:
[["1", "string", "1.0"], ["2", "string", "2.0"]]
...但是不能确定我需要出去什么。它只会添加到中间的字符串元素中,因此将类似于:
[["1", "string", "string", "1.0"], ["2", "string", "string", "string", "2.0"]]
因此,仅重申一下,我如何传递2d数组(知道内部数组将有多大),然后返回2d数组(不知道内部数组将有多大?)?
我的Java类如下...
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* This class takes a parameter (review) through the main args and runs it through the sentiment analysis.
* Where necessary, the analysis breaks the review into shorter sentences and scores each one.
* If devMode is set to TRUE, the detailed output is displayed in the console.
* The average of the review sentiments are then returned as a type double.
*/
public class SentimentAnalysis {
private static ArrayList findSentiment(ArrayList<String[]> allReviews) {
// Set to TRUE to display the breakdown of the output to the console
boolean devMode = false;
ArrayList<List> scoredReviewsArray = new ArrayList<>();
double totalSentimentScore = 0;
int counter = 1;
// Setup Stanford Core NLP package
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
if (devMode) {
System.out.println("" +
"\n==============================================================================================================\n" +
"The output shows the sentiment analysis for the comment provided.\n" +
"The StanfordCoreNlp algorithm breaks apart the input and rates each section numerically as follows:\n" +
"\n" +
"\t1 = Negative\n" +
"\t2 = Neutral\n" +
"\t3 = Positive\n" +
"\n" +
"The returned value is the average of all the sentiment ratings for the provided comment.\n" +
"==============================================================================================================\n"
);
}
for (String[] singleReview : allReviews) {
ArrayList<String> review = new ArrayList<>();
String reviewId = singleReview[0];
review.add(reviewId);
double sentimentSectionScore;
String reviewText = singleReview[1];
if (reviewText != null && reviewText.length() > 0) {
Annotation annotation = pipeline.process(reviewText);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
sentimentSectionScore = RNNCoreAnnotations.getPredictedClass(tree);
totalSentimentScore += sentimentSectionScore;
if (devMode) {
System.out.println("\nSentence " + counter++ + ": " + sentence);
System.out.println("Sentiment score: " + sentimentSectionScore);
System.out.println("Total sentiment score: " + totalSentimentScore);
}
review.add(sentence.toString());
review.add(String.valueOf(sentimentSectionScore));
}
scoredReviewsArray.add(review);
}
}
System.out.println(scoredReviewsArray.toString());
return scoredReviewsArray;
}
public static void main(String[] args) {
// Test array of review arrays
String mockReview1[] = {"1", "String 1.1. String 1.2.", ""};
String mockReview2[] = {"2", "String 2", ""};
String mockReview3[] = {"3", "String 3", ""};
ArrayList<String[]> reviews = new ArrayList<>();
reviews.add(mockReview1);
reviews.add(mockReview2);
reviews.add(mockReview3);
findSentiment(reviews);
}
}
...并且我将Java应用程序包装到Jar中,以使用子进程传入并从ReactJS中检索数据,如下所示:
const exec = require('child_process').spawn('java', ['-jar', './StanfordNlp.jar', allReviews]);
exec.stdout.on('data', function (data) {
// Do stuff...
});
希望这一切都有道理,如果您需要我澄清任何事情,请问。
非常感谢您的帮助。
谢谢!