-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2.run
More file actions
195 lines (163 loc) · 4.26 KB
/
Copy pathe2.run
File metadata and controls
195 lines (163 loc) · 4.26 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
PATH=.
usage() {
echo "Usage: $0 <text_file> <nouns_file>"
exit 1
}
if [ "$#" -ne 2 ]; then
usage
fi
if [ ! -r "$1" ]; then
echo "Error: Text file not found or not readable: $1"
usage
fi
if [ ! -r "$2" ]; then
echo "Error: Nouns file not found or not readable: $2"
usage
fi
all_text=""
while read -r line; do
all_text+="${line} "
done < "$1"
trim() {
local s="$1"
s="${s#"${s%%[![:space:]]*}"}"
s="${s%"${s##*[![:space:]]}"}"
echo "$s"
}
is_abbreviation() {
local token="$1"
token="${token%\"}"
token="${token%\'}"
if [[ "$token" =~ ^[A-Za-zĄČĘĖĮŠŲŪŽąčęėįšųūž]\.$ ]]; then
return 0
fi
return 1
}
extract_sentence() {
local txt="$1"
local s=""
local len=${#txt}
local i=0
while [ $i -lt $len ]; do
local char="${txt:$i:1}"
s="$s$char"
if [[ "$char" == "." || "$char" == "?" || "$char" == "!" ]]; then
if [[ "$char" == "." ]]; then
local token="${s##* }"
if is_abbreviation "$token"; then
i=$((i+1))
continue
fi
fi
if [ $((i+1)) -ge $len ]; then
echo "$s"
return
fi
local next="${txt:$((i+1)):1}"
if [[ "$next" == " " ]]; then
echo "$s"
return
fi
fi
i=$((i+1))
done
echo "$s"
}
declare -A seen
unique_sentences=()
while [[ -n "$all_text" ]]; do
sentence=$(extract_sentence "$all_text")
trimmed=$(trim "$sentence")
if [[ -n "$trimmed" ]]; then
if [[ -z "${seen["$trimmed"]}" ]]; then
seen["$trimmed"]=1
unique_sentences+=("$trimmed")
fi
fi
all_text="${all_text:${#sentence}}"
all_text=$(trim "$all_text")
done
declare -A noun_db
while IFS= read -r noun_line; do
noun=$(trim "$noun_line")
noun="${noun,,}"
if [[ -n "$noun" ]]; then
noun_db["$noun"]=1
fi
done < "$2"
count_nouns() {
local sentence="$1"
local count=0
for word in $sentence; do
clean_word="${word//\"/}"
clean_word="${clean_word//\'/}"
clean_word="${clean_word//[-.,!?;:()]/}"
clean_word="${clean_word,,}"
if [[ -n "${noun_db["$clean_word"]}" ]]; then
count=$((count+1))
fi
done
echo "$count"
}
calculate_avg_distance() {
local sentence="$1"
read -ra words <<< "$sentence"
local n=${#words[@]}
declare -A positions
local i
for (( i=0; i<n; i++ )); do
local word="${words[i]}"
word="${word//\"/}"
word="${word//\'/}"
word="${word//[-.,!?;:()]/}"
word="${word,,}"
if [ -z "${positions[$word]}" ]; then
positions[$word]="$i"
else
positions[$word]="${positions[$word]} $i"
fi
done
for w in "${!positions[@]}"; do
local pos_str="${positions[$w]}"
read -ra pos_arr <<< "$pos_str"
local sum=0
local count=0
local p j diff
for p in "${pos_arr[@]}"; do
for (( j=0; j<n; j++ )); do
if [ $j -ne $p ]; then
if [ $p -gt $j ]; then
diff=$(( p - j ))
else
diff=$(( j - p ))
fi
sum=$(( sum + diff ))
count=$(( count + 1 ))
fi
done
done
if [ $count -gt 0 ]; then
avg=$(( sum / count ))
# avg100=$(( sum * 100 / count ))
# integer_part=$(( avg100 / 100 ))
# decimal_part=$(( avg100 % 100 ))
decimal_part=0
else
integer_part=0
decimal_part=0
fi
if [ $decimal_part -lt 10 ]; then
printf "Word: '%s' Average distance: %d.0%d\n" "$w" "$avg" "$decimal_part"
else
printf "Word: '%s' Average distance: %d.%d\n" "$w" "$avg" "$decimal_part"
fi
done
}
for s in "${unique_sentences[@]}"; do
noun_count=$(count_nouns "$s")
echo "$s"
echo "Noun count: $noun_count"
calculate_avg_distance "$s"
echo "---------------------------"
done