2

I am writing a game that revolves around guessing what words in other languages mean. For example, guessing what "hola" means in English. For this game, I am creating a scoring system that uses the frequency of the word (from Google N Gram) being guessed to assign points. Basically, less times showing up = more points. However, when trying to send over the Java String that holds the word to R, I came to the realization that I have no idea what to do.

So far I have experimented with rJava to send over the word, but I am having difficulty making it work. I would greatly appreciate if someone who has experience with R could help me with my problem.

Java Class that generates word:

import java.util.Scanner;
import java.util.Random;
import java.io.File;
import java.io.IOException;

//this program gathers the words to be translated
//for game 1
public class dictionary {
    private String line;

    public dictionary() {
        this.line = "";
}

    public dictionary (String line) {   
    //getting word
    try {
        this.line = line;
        Random random = new Random();
        Scanner in = new Scanner (System.in);
        int count = 0;
        int rand = 1 + random.nextInt(8110);
        //read file name from standard input
        File file = new File("words.txt");
        //overwrite scanner to read file
        in = new Scanner(file);
        while (in.hasNextLine() && count != rand) {
            this.line = in.nextLine();
            count = count + 1;
        }
        in.close();
    }
    catch(IOException e) {
        System.out.println("File does not exist.");
    }
}

   public String getLine() {
    return this.line;
    }
}

R code that I came up with. Goal is to ingest the word from Java word (line):

library("ngramr")
library("rJava")
.jinit(parameters = getOption("java.parameters")
.jcall("java/lang/String", returnSig = "S", method = "getLine", evalString = TRUE)
 word <- .jclassString("string", "line")
 ngram(word, year_start = 1800, smoothing = 0)

Bonus: Also, if anyone is familiar with Google Ngram and knows how to record the amount of instances of a word in R and send it to Java, please let me know as it is a crucial part of the scoring system.

turt1edman
  • 41
  • 7

1 Answers1

0

It’s been a few weeks since I originally posted this question. Since then I have found a solution to both problems listed by using Python sockets instead of R. If anyone is interested in how I did that, it’s in my GitHub. I am going to leave this question here however, in the hopes someone will answer it for others with a similar problem.

turt1edman
  • 41
  • 7