-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugPrinter.java
More file actions
98 lines (80 loc) · 3.54 KB
/
DebugPrinter.java
File metadata and controls
98 lines (80 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ie.atu.sw;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
/**
* Visualises word vectors in order to understand if loaded correctly
*
* Prints word vectors stored in a map in a formatted tabular way
*/
public class DebugPrinter {
private static final int WORD_COLUMN_WIDTH = 22;
/**
* Displays a message asking the user how many word vectors they want to print.
*
* @param size The size of the Map
* @param label The name of the Map
*/
private static void getLinesToPrint(int size, String label) { // Big O = O(1)
System.out.println("To verify the correct " + label + " loading,");
System.out.println("how many lines would you like to see? ");
System.out.println("from 1 to (max size) " + size);
}
/**
* Requests to specify number of lines to be printed
*
* @param size Dimension of the Map or Set
* @param label The label for the data
*/
private static int printLines(int size, String label) { // Big O = O(1)
System.out.print(ConsoleColour.BLACK_BOLD_BRIGHT);
int lines = MenuProcessInput.processInput(size, new Scanner(System.in));
System.out.println(ConsoleColour.GREEN);
System.out.println("Debug: First " + lines + " " + label + " loaded:");
return lines;
}
/**
* Prints words and vectors from the provided map.
*
* @param wordVectors the map containing word vectors
* @param label the label for the word vectors
*/
public static void printVectors(Map<String, double[]> wordVectors, String label) { // Big O = O(n)
int count = 0; // initialise a counter
int size = wordVectors.size(); // get total size
getLinesToPrint(size, label); // call a method to print
int lines = printLines(size, label); // get number of lines
for (Map.Entry<String, double[]> key : wordVectors.entrySet()) { // Big O = O(n)
System.out.printf("%-" + WORD_COLUMN_WIDTH + "s", "Word: " + key.getKey());
System.out.print(" Vector: ["); // Print Word and Key tabulated [%-22s]
double[] vector = key.getValue(); // get vector array of double
for (int i = 0; i < vector.length; i++) { // Big O = O(n)- loop and print vectors
System.out.printf("%.4f", vector[i]); // formatted 4 decimal places
if (i < vector.length - 1) System.out.print(", ");
}
System.out.println("]");
count++; // update counter
if (count == lines) break; // stop loop
}
System.out.println("");
}
/**
* Prints words from the provided set.
*
* @param words the set containing words
* @param label the label for the words
*/
public static void printWords(Set<String> words, String label) { // Big O = O(n)
int count = 0;
int size = words.size();
getLinesToPrint(size, label);
int lines = printLines(size, label);
for (String word : words) { // Big O = O(n)
System.out.printf("%-" + WORD_COLUMN_WIDTH + "s", "Word: " + word);
System.out.println();
count++;
if (count == lines) break;
}
System.out.println("");
}
}