1 | package org.greenstone.mars;
|
---|
2 |
|
---|
3 | //import java.util.Random;
|
---|
4 |
|
---|
5 | import java.io.BufferedInputStream;
|
---|
6 | import java.io.FileInputStream;
|
---|
7 |
|
---|
8 | import java.io.BufferedReader;
|
---|
9 | import java.io.BufferedWriter;
|
---|
10 | import java.io.FileReader;
|
---|
11 | import java.io.FileWriter;
|
---|
12 |
|
---|
13 | import weka.core.converters.ConverterUtils.DataSource;
|
---|
14 | import weka.core.Attribute;
|
---|
15 | import weka.core.Instance;
|
---|
16 | import weka.core.Instances;
|
---|
17 | import weka.core.SerializationHelper;
|
---|
18 |
|
---|
19 | import weka.filters.Filter;
|
---|
20 | import weka.filters.unsupervised.attribute.Remove;
|
---|
21 |
|
---|
22 | import weka.classifiers.Classifier;
|
---|
23 |
|
---|
24 |
|
---|
25 | // Based on:
|
---|
26 | // https://waikato.github.io/weka-wiki/use_weka_in_your_java_code/
|
---|
27 |
|
---|
28 | class WekaApplyValenceModel
|
---|
29 | {
|
---|
30 |
|
---|
31 | public static void main(String[] args)
|
---|
32 | {
|
---|
33 | WekaUtil.checkUsageApplyModel(args);
|
---|
34 |
|
---|
35 | String classifier_input_filename = args[0];
|
---|
36 | String unclassified_data_input_filename = args[1];
|
---|
37 | String classified_data_output_filename = args[2];
|
---|
38 |
|
---|
39 | Classifier classifier = WekaUtil.loadClassifierModel(classifier_input_filename);
|
---|
40 |
|
---|
41 | Instances unlabeled_instances= WekaUtil.loadInstancesForClassification(unclassified_data_input_filename);
|
---|
42 |
|
---|
43 | // It is permissible to run this code and supply it with a data file that includes groundtruth in it.
|
---|
44 | // In this situation, the 'unlabeled' instances:
|
---|
45 | // (i) need to be massaged to be in the same form as truly unlabeled data
|
---|
46 | // (ii) we also set up 'groundtruth_instances' as an alias (reference) to 'filtered_unlabeled_instanced'
|
---|
47 | // to trigger calculating the error on the predicted vaues
|
---|
48 |
|
---|
49 | boolean has_groundtruth_data = WekaUtil.instancesHavePredictAttribute(unlabeled_instances,WekaUtil.VALENCE_ATTRIBUTE_NAME);
|
---|
50 |
|
---|
51 | // The following deals with (i) internally, ensuring that what is returned is suitable for making predictions on
|
---|
52 | Instances filtered_unlabeled_instances
|
---|
53 | = WekaUtil.filterInstancesForApplying(unlabeled_instances,has_groundtruth_data,
|
---|
54 | WekaUtil.VALENCE_ATTRIBUTE_NAME,"471");
|
---|
55 |
|
---|
56 | // The following deals with (ii)
|
---|
57 | Instances groundtruth_instances = (has_groundtruth_data) ? filtered_unlabeled_instances : null;
|
---|
58 |
|
---|
59 | Instances labeled_instances = WekaUtil.makePredictions(classifier, filtered_unlabeled_instances, groundtruth_instances);
|
---|
60 |
|
---|
61 | try {
|
---|
62 | // Save labeled data
|
---|
63 |
|
---|
64 | System.out.println("Saving labeled instances: " + classified_data_output_filename);
|
---|
65 | FileWriter fw = new FileWriter(classified_data_output_filename);
|
---|
66 | BufferedWriter bw = new BufferedWriter(fw);
|
---|
67 |
|
---|
68 | bw.write(labeled_instances.toString());
|
---|
69 | bw.newLine();
|
---|
70 | bw.flush();
|
---|
71 | bw.close();
|
---|
72 |
|
---|
73 | }
|
---|
74 | catch (Exception e) {
|
---|
75 | e.printStackTrace();
|
---|
76 | }
|
---|
77 |
|
---|
78 | }
|
---|
79 | }
|
---|