-
Notifications
You must be signed in to change notification settings - Fork 0
/
plygrdQuery.m
86 lines (76 loc) · 2.63 KB
/
plygrdQuery.m
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
function cleanResponse = plygrdQuery(queries, datasets, authToken)
%PLYGRDQUERY Query the History Playground for the given terms in the
%specified datasets.
%
% response = PLYGRDQUERY(queries,datasets,authToken) returns a
% response structure containing each of the time series from the History
% Playground. The structure for each series can be found in response(1),
% response(2), etc.
%
% Example:
% authToken = plygrdLogin('email','password');
% queries = {'dog','cat','bird'};
% datasets = {'bna','caa'};
% response = plygrdQuery(queries,datasets,authToken);
% plygrdPlot(response);
%
% See also PLYGRD, PLYGRDLOGIN, PLYGRDPLOT
url = 'http://playground.enm.bris.ac.uk/ngram';
header = weboptions(...
'MediaType','application/json',...
'HeaderFields',{
'Authorization' authToken;...
'Content-Type' 'application/json; charset=UTF-8'...
}...
);
data = loadDefaults(queries,datasets);
data = setOptions(data, queries, datasets);
response = webwrite(url,data, header);
cleanResponse = cleanUp(data,response);
end
function data = loadDefaults(queries,datasets)
data = struct();
data.changepoints = 0;
data.multiterm = 0;
data.zscore = 0;
data.diff = 0;
data.detrend = 0;
data.bestFit = 0;
data.confidence = 0;
data.smooth = 0;
data.display = {'rank'};
data.maxDate = '';
data.minDate = '';
data.lang = cell(size(datasets,2),1);
data.dateFormat = cell(size(datasets,2),1);
data.interval = cell(size(datasets,2),1);
data.resolution = cell(size(datasets,2),1);
data.terms = cell(size(datasets,2)*size(queries,2),1);
end
function data = setOptions(data, queries, datasets)
idx = 1;
for i = 1: size(datasets,2)
data.lang{i,1} = "english";
data.dateFormat{i,1} = "YYYY";
data.interval{i,1} = "1";
data.resolution{i,1} = "years";
for j = 1 : size(queries,2)
data.terms{idx,1} = strcat(strcat(queries{1,j},':'),datasets{1,i});
idx = idx+1;
end
end
data.corpora = datasets;
end
function response = cleanUp(data,response)
response = rmfield(response, 'orig');
response = rmfield(response, 'dateFormat');
response = rmfield(response, 'interval');
response = rmfield(response, 'lang');
response = rmfield(response, 'resolution');
switch data.display{1}
case 'counts'
response = rmfield(response,'rank');
case 'rank'
response = rmfield(response,'counts');
end
end