source: main/trunk/model-sites-dev/mars/src/java/org/greenstone/mars/WekaApplyAVModels.java@ 35207

Last change on this file since 35207 was 34802, checked in by davidb, 3 years ago

Saving instances changed to a DataSink, and located in WekaUtil

File size: 2.3 KB
Line 
1package org.greenstone.mars;
2
3//
4// Predict both Arousal and Valence values
5//
6
7import weka.core.Instances;
8
9import weka.classifiers.Classifier;
10
11
12// Based on:
13// https://waikato.github.io/weka-wiki/use_weka_in_your_java_code/
14
15class WekaApplyAVModels
16{
17 public static void main(String[] args)
18 {
19 WekaUtil.checkUsageApplyAVModels(args);
20
21 String arousal_model_input_filename = args[0];
22 String valence_model_input_filename = args[1];
23 String unclassified_data_input_filename = args[2];
24 String classified_data_output_filename = args[3];
25
26 Classifier arousal_classifier = WekaUtil.loadClassifierModel(arousal_model_input_filename);
27 Classifier valence_classifier = WekaUtil.loadClassifierModel(valence_model_input_filename);
28
29 Instances unlabeled_instances= WekaUtil.loadInstancesForClassification(unclassified_data_input_filename);
30
31 Instances filtered_unlabeled_instances = WekaUtil.applyFilter(unlabeled_instances,null); // no additional top-up to remove
32
33 WekaUtil.appendUnclassifiedAttribute(filtered_unlabeled_instances,WekaUtil.AROUSAL_ATTRIBUTE_NAME);
34 System.out.println("Predicting arousal:");
35 Instances arousal_labeled_instances = WekaUtil.makePredictions(arousal_classifier, filtered_unlabeled_instances, null);
36
37 WekaUtil.appendUnclassifiedAttribute(arousal_labeled_instances,WekaUtil.VALENCE_ATTRIBUTE_NAME);
38 System.out.println("Predicting valence:");
39 Instances av_labeled_instances = WekaUtil.makePredictions(valence_classifier, arousal_labeled_instances, null);
40
41 /*
42 try {
43 // Save labeled data
44
45 System.out.println("Saving labeled instances: " + classified_data_output_filename);
46 FileWriter fw = new FileWriter(classified_data_output_filename);
47 BufferedWriter bw = new BufferedWriter(fw);
48
49 bw.write(av_labeled_instances.toString());
50 bw.newLine();
51 bw.flush();
52 bw.close();
53
54 }
55 catch (Exception e) {
56 e.printStackTrace();
57 }
58 */
59
60 /*
61 try {
62 System.out.println("Saving labeled instances: " + classified_data_output_filename);
63 DataSink.write(classified_data_output_filename, av_labeled_instances);
64 }
65 catch (Exception e) {
66 System.err.println("Failed to save data to: " + classified_data_output_filename);
67 e.printStackTrace();
68 }
69 */
70
71 WekaUtil.saveInstancesAsDataSink(av_labeled_instances,classified_data_output_filename);
72
73 }
74}
Note: See TracBrowser for help on using the repository browser.