-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis
More file actions
executable file
·169 lines (158 loc) · 5.62 KB
/
Copy pathanalysis
File metadata and controls
executable file
·169 lines (158 loc) · 5.62 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Author: <Ryan Chang>
# Script Name: analysis
# Description: Analyze a board game dataset (TSV format) to extract statistical insights.
# This script performs the following:
# - Finds the most popular game mechanic (column 13) and domain (column 14)
# - Calculates average values for publication year, rating, and complexity
# - Computes Pearson correlation coefficients between:
# • Year of publication and average rating
# • Game complexity and average rating
# It handles data cleaning, multiple comma-separated values, and reports
# errors for missing or insufficient data.
# Usage: ./analysis <file>
# ------------------------------------------------------------------------------
# Check the number of arguments
# If the number is not 1, print error message
if [[ $# != 1 ]]
then
echo "Error: The number of arguments does not match" >&2
exit 1
fi
file="$1"
# Check file existence and permissions
if [[ ! -f "$file" ]]; then
echo "Error: File '$file' is not found." >&2
exit 1
fi
if [[ ! -r "$file" ]]; then
echo "Error: File '$file' is not readable." >&2
exit 1
fi
# Find the most popular mechanic
# 1. Extract Mechanics field (column 13) and skip empty entries and header
# 2. Spread multiple mechanics (comma-separated) into individual lines
# 3. Remove leading and trailing spaces or tabs from each mechanic
# 4. Remove blank lines caused by empty or malformed entries
# 5. Replace internal spaces with underscores for reliable field separation
# 6. Sort all mechanics alphabetically
# 7. Count each unique mechanic
# 8. Sort mechanics by frequency in descending order
# 9. Select the most frequent mechanic
# 10. Print formatted output with mechanic name and count, restoring spaces
gawk -F '\t' 'NR > 1 && $13 !~ /^[ \t]*$/ { print $13 }' "$file" \
| tr ',' '\n' \
| sed 's/^[ \t]*//;s/[ \t]*$//' \
| grep -v '^$' \
| sed 's/ /_/g' \
| sort \
| uniq -c \
| sort -nr \
| head -n 1 \
| gawk '{print "The most popular game mechanics is " $2 " found in " $1 " games"}' \
| sed 's/_/ /g'
# Find the most popular domain
# 1. Extract Domains field (column 14) and skip empty entries and header
# 2. Spread multiple values (comma-separated) into individual lines
# 3. Remove leading and trailing spaces or tabs from each domain
# 4. Remove blank lines caused by empty or malformed entries
# 5. Replace internal spaces with underscores for reliable field parsing
# 6. Sort all domains alphabetically
# 7. Count each unique domain
# 8. Sort domains by frequency in descending order
# 9. Select the most frequent domain
# 10. Print formatted output with domain name and count, restoring spaces
gawk -F '\t' 'NR > 1 && $14 !~ /^[ \t]*$/ { print $14 }' "$file" \
| tr ',' '\n' \
| sed 's/^[ \t]*//;s/[ \t]*$//' \
| grep -v '^$' \
| sed 's/ /_/g' \
| sort \
| uniq -c \
| sort -nr \
| head -n 1 \
| gawk '{print "The most game domain is " $2 " found in " $1 " games"}' \
| sed 's/_/ /g'
# Count the average value of publication year, average rating and game complexity
# Year Published: $3, Rating Average: $9, # Complexity Average: $11
avg_list=($(gawk -F '\t' '
BEGIN {
total_year = total_rating = total_complexity = 0;
num_year = num_rating = num_complexity = 0;
}
NR > 1 {
if ($3 !~ /^[ \t]*$/) {
total_year += $3
num_year++
}
if($9 !~ /^[ \t]*$/){
total_rating += $9
num_rating++
}
if($11 !~ /^[ \t]*$/){
total_complexity += $11
num_complexity++
}
}
END {
if (num_year == 0 || num_rating == 0 || num_complexity == 0) {
print "Error: One or more required fields are missing data." > "/dev/stderr";
exit 1;
}
y = total_year / num_year;
r = total_rating / num_rating;
c = total_complexity / num_complexity;
print y, r, c;
}
' "$file"
))
avg_year=${avg_list[0]}
avg_rating=${avg_list[1]}
avg_complexity=${avg_list[2]}
# Calculate the correlation between publication year and average rating
awk -F '\t' -v avg_year="$avg_year" -v avg_rating="$avg_rating" '
BEGIN {
sum_xy = sum_x2 = sum_y2 = 0;
}
NR > 1 && $3 !~ /^[ \t]*$/ && $9 !~ /^[ \t]*$/ {
dx = $3 - avg_year
dy = $9 - avg_rating
sum_xy += dx * dy
sum_x2 += dx * dx
sum_y2 += dy * dy
}
END {
if (sum_x2 > 0 && sum_y2 > 0) {
r = sum_xy / sqrt(sum_x2 * sum_y2)
printf "The correlation between the year of publication and the average rating is %.3f\n", r
} else {
print "Error: Insufficient data to compute correlation (Year vs Rating)" > "/dev/stderr"
exit 1
}
}
' "$file"
# Calculate the correlation between game complexity and average rating
awk -F '\t' -v avg_complexity="$avg_complexity" -v avg_rating="$avg_rating" '
BEGIN {
sum_xy = sum_x2 = sum_y2 = 0;
}
NR > 1 && $11 !~ /^[ \t]*$/ && $9 !~ /^[ \t]*$/ {
dx = $11 - avg_complexity
dy = $9 - avg_rating
sum_xy += dx * dy
sum_x2 += dx * dx
sum_y2 += dy * dy
}
END {
if (sum_x2 > 0 && sum_y2 > 0) {
r = sum_xy / sqrt(sum_x2 * sum_y2)
printf "The correlation between the complexity of a game and its average rating is %.3f\n", r
} else {
print "Error: Insufficient data to compute correlation (Complexity vs Rating)" > "/dev/stderr"
exit 1
}
}
' "$file"
# Exit successfully
exit 0