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