java - 如何将 2d 数组从 ReactJS 传递到 Java Main?
问题描述
我有两个程序,一个在 ReactJS 中,一个在 Java 中,需要将 ReactJS 中的 2d 数组传递到我的 Java 主程序中,但没有意识到我无法修改主签名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"]]
所以,重申一下,我怎样才能传入一个二维数组(知道内部数组有多大),然后返回一个二维数组(不知道内部数组有多大)?
我的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...
});
希望这一切都有意义,如果您需要我澄清任何事情,请尽管问。
非常感谢您的帮助。
谢谢!
解决方案
推荐阅读
- python - 如何在 Python 中定义乌龟而不创建新乌龟?
- javascript - 使用requirejs在数据表上单击事件
- git - 如何从 TFVC 存储库中挑选到 Git 存储库?
- python - 没有主机密钥的气流 SSHHook
- python - 使用 python 客户端库从日志文件中导出 promethous 指标
- tcl - 如何从 TCL 的测试文件中获取测试文件的名称
- azure-active-directory - 是否可以验证 Azure Active Directory 用户的密码
- reactjs - 如何通过 onPress 将新值传递给 Redux 存储?
- hadoop - 了解 Hadoop 的不同元素
- node.js - 独立 websocket 服务器的 Socket.io“无效帧头”错误