-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateQuestionStats.m
More file actions
97 lines (90 loc) · 3.58 KB
/
generateQuestionStats.m
File metadata and controls
97 lines (90 loc) · 3.58 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
function [questionStats] = generateQuestionStats(chatFile)
% Question Statistics Generator
% This function takes a .txt file of a Pokémon Showdown chat log as
% input, then generates a list of usernames ranked by number of lines
% sent to the chat room which contained at least one question mark.
questionLines = 0;
totalLines = 0;
fid = fopen(['logs/' chatFile]);
while feof(fid) == 0
line = fgetl(fid);
verticalBars = strfind(line, '|');
if length(verticalBars) >= 3
if strcmp(line(10:12), '|c|') && ~strcmp(line(14), '|') && length(line) >= verticalBars(3) + 1
message = line((verticalBars(3) + 1):length(line));
questionMarks = strfind(message, '?');
if length(questionMarks) >= 1
questionLines = questionLines + 1;
end
end
end
totalLines = totalLines + 1;
end
fclose(fid);
fprintf('The chat log has been scanned.\n');
fprintf('There are %d lines with question marks and %d total lines.\n', questionLines, totalLines);
if questionLines > 0
usernames = java_array('java.lang.String', questionLines);
currentChatLine = 1;
fid = fopen(['logs/' chatFile]);
while feof(fid) == 0
line = fgetl(fid);
verticalBars = strfind(line, '|');
if length(verticalBars) >= 3
if strcmp(line(10:12), '|c|') && ~strcmp(line(14), '|') && length(line) >= verticalBars(3) + 1
message = line((verticalBars(3) + 1):length(line));
questionMarks = strfind(message, '?');
if length(questionMarks) >= 1
fullUsername = line((verticalBars(2) + 2):(verticalBars(3) - 1));
alphanumericUsername = lower(regexprep(fullUsername, '[^a-zA-Z0-9]', ''));
usernames(currentChatLine) = java.lang.String(alphanumericUsername);
currentChatLine = currentChatLine + 1;
end
end
end
end
fclose(fid);
fprintf('The username database has been generated.\n');
uniqueUsernames = java_array('java.lang.String', 1);
questionCount = double.empty(0, questionLines);
fprintf('Scanning the username database...\n');
reverse = '';
for i = 1:questionLines
for j = 1:length(uniqueUsernames)
if usernames(i) == uniqueUsernames(j)
questionCount(j) = questionCount(j) + 1;
unique = false;
break
else
unique = true;
end
end
if unique
uniqueUsernames(length(uniqueUsernames)+1) = usernames(i);
questionCount(length(uniqueUsernames)) = 1;
end
progress = sprintf('%d/%d lines scanned\n', i, questionLines);
fprintf([reverse, progress]);
reverse = repmat(sprintf('\b'), 1, length(progress));
end
questionPercent = double.empty(0, questionLines);
for i = 1:length(questionCount)
questionPercent(i) = 100 * (questionCount(i) / questionLines);
end
c1 = cell(uniqueUsernames);
c2 = num2cell(transpose(questionCount));
c3 = num2cell(transpose(questionPercent));
dataArray = [c1 c2 c3];
sortedData = sortrows(dataArray, -2);
sortedData(size(sortedData, 1),:) = [];
rank = cell(size(sortedData, 1), 1);
for i = 1:size(sortedData, 1)
rank(i) = num2cell(i);
end
header = {'Rank' 'Username' 'Questions' 'Percent Total'};
questionStats = [header; [rank sortedData]];
else
fprintf('There were no lines containing question marks during this period.\n');
questionStats = {'Rank' 'Username' 'Questions' 'Percent Total'};
end
end