I'm trying to avoid hard-coding file names in my Java class and want to pass them as the command line arguments in Eclipse, e.g. "JSONDataStore_complex.json topicdatanew.txt".
my code is as follows:
public class B4TopicModelling {
public static void main(String[] args) {
if (args.length == 2) {
B4TopicModelling loader = new B4TopicModelling();
String filePath = args[0]; // e.g. "JSONDataStore_complex.json"
String TMFlatFile = args[1]; // e.g. "topicdatanew.txt"
System.out.println("Start Topic Modelling :" + filePath);
loader.StartTopicModellingProcess(filePath);
int numTopics = 10; // the number of topics (or groups we'll cluster the documents into)
int numThreads = 2; // how many threads it will use (helping speed up the process on machines with multiple cores)
int numIterations = 500; // how many times the process is run
int numTopWords = 10; // the top ten words per topic
loader.RunTopicModelling(TMFlatFile, numTopics, numThreads, numIterations, numTopWords);
} else {
System.err.println("Error: No valid arguments have been entered.");
}
}
private void StartTopicModellingProcess(String filePath) {
JSONIOHelper jsonIO = new JSONIOHelper(); // create an object of the JSONIOHelper class
jsonIO.LoadJSON(filePath); // call the LoadJSON method
ConcurrentHashMap<String, String> lemmas = jsonIO.GetDocumentsFromJSONStructure();
// String TMFlatFile = "topicdata_new.txt";
SaveLemmaDataToFile(TMFlatFile, lemmas);
}
private void SaveLemmaDataToFile(String TMFlatFile, ConcurrentHashMap<String, String> lemmas) {
// create a for loop which saves each entry in the ConcurrentHashMap called
// lemmas into the file with the name TMFlatFile.
try (FileWriter writer = new FileWriter(TMFlatFile)) {
// Write our string to our file
for (Entry<String, String> entry : lemmas.entrySet()) {
// Saves the keys then tab then 'en' and tab and the lemmas entries
writer.write(entry.getKey() + "\ten\t" + entry.getValue() + "\r\n");
}
System.out.println("TMFlatFile File saved successfully...");
}
catch (Exception e) {
System.out.println("Saving TMFlatFile to text file failed...");
}
}
private void RunTopicModelling(String TMFlatFile, int numTopics, int numThreads, int numIterations, int numTopWords) {
ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
// Pipes: tokenise, map to features
pipeList.add(new CharSequence2TokenSequence(Pattern.compile("\\p{L}[\\p{L}\\p{P}]+\\p{L}")));
// pipeList.add(new TokenSequenceRemoveStopwords(new File("stoplist_en.txt"), "UTF-8", false, false, false));
pipeList.add(new TokenSequence2FeatureSequence());
InstanceList instances = new InstanceList(new SerialPipes(pipeList));
InputStreamReader fileReader = null;
// loads the file passed in via the TMFlatFile variable into the fileReader
// variable
try {
File inFile = new File(TMFlatFile);
fileReader = new InputStreamReader(new FileInputStream(inFile));
} catch (Exception e) {
System.out.println("File Load failed");
System.exit(1);
}
// linking data to the pipeline
instances
.addThruPipe(new CsvIterator(fileReader, Pattern.compile("^(\\S*)[\\s,]*(\\S*)[\\s,]*(.*)$"), 3, 2, 1));
// Setting up the Parallel Topic Model
ParallelTopicModel model = new ParallelTopicModel(numTopics, 1.0, 0.01);
model.addInstances(instances);
model.setNumThreads(numThreads);
model.setNumIterations(numIterations);
try {
model.estimate();
} catch (IOException e) {
System.out.println("Model estimation failed...");
}
}
}
The line with "SaveLemmaDataToFile(TMFlatFile, lemmas)" gives me an error,
TMFlatFile cannot be resolved to a variable.
If I uncomment "String TMFlatFile = "topicdata_new.txt";" this will work. however, I do not understand how to supply this filename as a command line argument in Eclipse instead? Is it possible?