-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotter.java
139 lines (121 loc) · 5.15 KB
/
Plotter.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package ngordnet;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.QuickChart;
import com.xeiam.xchart.SwingWrapper;
import com.xeiam.xchart.StyleManager.ChartTheme;
import com.xeiam.xchart.ChartBuilder;
import java.util.List;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Set;
/** Utility class for generating plots. I used the plotting class provided in the project slides */
public class Plotter {
/**
* Creates a plot of the TimeSeries TS. Labels the graph with the given
* TITLE, XLABEL, YLABEL, and LEGEND.
*/
public static void plotTS(TimeSeries ts, String title, String xlabel,
String ylabel, String legend) {
Collection years = ts.years();
Collection counts = ts.data();
// Create Chart
Chart chart = QuickChart.getChart(title, ylabel, xlabel, legend, years,
counts);
// Show it
new SwingWrapper(chart).displayChart();
}
/**
* Creates a plot of the absolute word counts for WORD from STARTYEAR to
* ENDYEAR, using NGM as a data source.
*/
public static void plotCountHistory(NGramMap ngm, String word,
int startYear, int endYear) {
TimeSeries countHistory = ngm.countHistory(word, startYear, endYear);
plotTS(countHistory, "Popularity", "year", "count", word);
}
/**
* Creates a plot of the normalized weight counts for WORD from STARTYEAR to
* ENDYEAR, using NGM as a data source.
*/
public static void plotWeightHistory(NGramMap ngm, String word,
int startYear, int endYear) {
TimeSeries weightHistory = ngm.weightHistory(word, startYear, endYear);
plotTS(weightHistory, "Popularity", "year", "weight", word);
}
/**
* Creates a plot of the processed history from STARTYEAR to ENDYEAR, using
* NGM as a data source, and the YRP as a yearly record processor.
*/
public static void plotProcessedHistory(NGramMap ngm, int startYear,
int endYear, YearlyRecordProcessor yrp) {
TimeSeries wordWeights = ngm.processedHistory(startYear, endYear, yrp);
plotTS(wordWeights, "Word Length", "year", "avg. length", "word length");
}
/**
* Creates a plot of the total normalized count of every word that is a
* hyponym of CATEGORYLABEL from STARTYEAR to ENDYEAR using NGM and WN as
* data sources.
*/
public static void plotCategoryWeights(NGramMap ngm, WordNet wn,
String categoryLabel, int startYear, int endYear) {
Set words = wn.hyponyms(categoryLabel);
TimeSeries summedWeightHistory = ngm.summedWeightHistory(words,
startYear, endYear);
plotTS(summedWeightHistory, "Popularity", "year", "weight",
categoryLabel);
}
/**
* Creates overlaid category weight plots for each category label in
* CATEGORYLABELS from STARTYEAR to ENDYEAR using NGM and WN as data
* sources.
*/
public static void plotCategoryWeights(NGramMap ngm, WordNet wn,
String[] categoryLabels, int startYear, int endYear) {
Chart chart = new ChartBuilder().width(800).height(600)
.xAxisTitle("years").yAxisTitle("data").build();
for (String categoryLabel : categoryLabels) {
Set words = wn.hyponyms(categoryLabel);
TimeSeries bundle = ngm.summedWeightHistory(words, startYear,
endYear);
chart.addSeries(categoryLabel, bundle.years(), bundle.data());
}
new SwingWrapper(chart).displayChart();
}
/**
* Makes a plot showing overlaid individual normalized count for every word
* in WORDS from STARTYEAR to ENDYEAR using NGM as a data source.
*/
public static void plotAllWords(NGramMap ngm, String[] words,
int startYear, int endYear) {
Chart chart = new ChartBuilder().width(800).height(600)
.xAxisTitle("years").yAxisTitle("data").build();
for (String word : words) {
TimeSeries bundle = ngm.weightHistory(word, startYear, endYear);
chart.addSeries(word, bundle.years(), bundle.data());
}
new SwingWrapper(chart).displayChart();
}
/** Returns the numbers from max to 1, inclusive in decreasing order. */
private static Collection downRange(int max) {
ArrayList ranks = new ArrayList();
for (int i = max; i >= 1; i -= 1) {
ranks.add(i);
}
return ranks;
}
/**
* Plots the normalized count of every word against the rank of every word
* on a log-log plot. Uses data from YEAR, using NGM as a data source.
*/
public static void plotZipfsLaw(NGramMap ngm, int year) {
YearlyRecord yr = ngm.getRecord(year);
Collection counts = yr.counts();
Collection ranks = downRange(counts.size());
Chart chart = new ChartBuilder().width(800).height(600)
.xAxisTitle("rank").yAxisTitle("count").build();
chart.getStyleManager().setYAxisLogarithmic(true);
chart.getStyleManager().setXAxisLogarithmic(true);
chart.addSeries("zipf", ranks, counts);
new SwingWrapper(chart).displayChart();
}
}