forked from FieldDB/Praat-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_distribution_bar_from_data_file.praat
185 lines (164 loc) · 4.46 KB
/
draw_distribution_bar_from_data_file.praat
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
# This script will take data from a simple text file, convert them into
# a Praat TextGrid object, and draw a horizontal bar in the Picture window
# with divisions correponding to the values of the data file.
#
# This script is distributed under the GNU General Public License.
# Copyright 24.4.2002 Mietta Lennes
#
form Draw distribution bar from data file
comment Full path of the data:
text path /home/lennes/tmp/phoneme_frequencies2.txt
boolean The_text_file_contains_a_title_row 0
positive Group_together_if_smaller_than_(%) 3
positive Picture_width_(inches) 12
positive Picture_height_(inches) 4
integer Font_size 24
text title Distribution of categories
endform
if fileReadable (path$) = 0
exit The file 'path$' does not exist! Give the correct path to the file and try again.
endif
Read Strings from raw text file... 'path$'
Rename... temp
numberOfStrings = Get number of strings
end = 0
sum = 0
long = 0
Line width... 3
# first, check the strings, calculate total sum, etc.
for string from 1 to numberOfStrings
string$ = Get string... string
if index (string$, " ") > 0
number$ = left$ (string$, (index (string$, " ") - 1))
if length (right$ (string$, (length (string$) - (index (string$, " "))))) > 2
long = 1
endif
elsif index (string$, " ") > 0
number$ = left$ (string$, (index (string$, " ") - 1))
if length (right$ (string$, (length (string$) - (index (string$, " "))))) > 2
long = 1
endif
elsif string$ <> ""
number$ = string$
endif
if number$ <> ""
number = 'number$'
sum = sum + number
elsif end = 0
printline Data ended in line 'string'.
end = 'string'
endif
endfor
if end > 0
numberOfStrings = end
endif
# now, create the TextGrid:
Create TextGrid... 0 sum textline
Rename... temp
boundary = 0
interval = 0
minor = 0
minorpoint = sum
minor$ = ""
count = 0
for i from 1 to numberOfStrings
select Strings temp
string$ = Get string... i
if index (string$, " ") > 0
number$ = left$ (string$, (index (string$, " ") - 1))
label$ = right$ (string$, (length (string$) - index (string$, " ")))
elsif index (string$, " ") > 0
number$ = left$ (string$, (index (string$, " ") - 1))
label$ = right$ (string$, (length (string$) - index (string$, " ")))
elsif string$ <> ""
number$ = string$
label$ = ""
endif
number = 'number$'
percent = (number / sum) * 100
if percent > group_together_if_smaller_than
boundary = boundary + number
else
if minor$ = ""
minor$ = minor$ + "'label$'"
else
minor$ = minor$ + ", 'label$'"
endif
minorpoint = minorpoint - (number / 2)
endif
select TextGrid temp
if number > 0 and boundary < sum and percent > group_together_if_smaller_than
interval = interval + 1
Insert boundary... 1 boundary
call CheckLabel
if long = 0
Set interval text... 1 interval 'label$'
else
text'interval'$ = label$
point'interval' = boundary - (number / 2)
count = count + 1
endif
elsif number > 0 and boundary = sum
interval = interval + 1
if percent > group_together_if_smaller_than
call CheckLabel
if long = 0
Set interval text... 1 interval 'label$'
else
text'interval'$ = label$
point'interval' = boundary - (number / 2)
count = count + 1
endif
endif
endif
endfor
if minor$ <> ""
interval = interval + 1
if length (minor$) > 14
label$ = "Others"
else
label$ = minor$
endif
call CheckLabel
if long = 0
Set interval text... 1 interval 'label$'
endif
endif
# and finally, draw the grid to the Picture window:
select TextGrid temp
Erase all
Viewport... 0 picture_width 0 picture_height
Draw... 0.0 0.0 no no no
Line width... 2
Marks bottom... 11 no yes no
tenpercent = sum / 20
One mark bottom... tenpercent no no no 10 \%
Times
Font size... font_size
Text... sum Centre -2.3 Bottom Total: 'sum'
if long = 1
for label from 1 to count
text$ = text'label'$
Text special... point'label' Left -0.9 Half Times font_size 45 'text$'
endfor
endif
if title$ <> ""
middle = sum / 2
if long = 0
Text... middle Centre 0 Top 'title$'
else
Text special... middle Centre 0.3 Half Helvetica 22 0 'title$'
Text special... minorpoint Left -0.9 Half Times font_size 45 'label$'
endif
endif
select Strings temp
plus TextGrid temp
Remove
procedure CheckLabel
while index (label$, "ä") > 0
label$ = left$ (label$, (index (label$, "ä") - 1)) + "\a""" + right$ (label$, (length (label$) - index (label$, "ä")))
endwhile
while index (label$, "ö") > 0
label$ = left$ (label$, (index (label$, "ö") - 1)) + "\o""" + right$ (label$, (length (label$) - index (label$, "ö")))
endwhile
endproc