
proc equals {vec x} {
## returns the indices of vec in which x occurs
set n {}
set l [expr [llength $vec] - 1]
for {set i 0} {$i <= $l} {incr i} {
if {[lindex $vec $i] == $x} {
lappend n $i
}
}
return $n
}


proc Linklevels {hierarchy plevel clevel} {

# list the elements at the parent level
set int [$hierarchy segments $plevel]

## list the elements at the child level
set wd [$hierarchy segments $clevel]

## list their parents element by element
set x {}
foreach j $wd {
set num [$hierarchy seginfo $j parents $plevel]
if {[llength $num] == 0} {
set num -1
}
lappend x $num
}

# which elements of $clevel are dominated by $int
# this has to be made into a for loop because there
# may be more than one value in $int

foreach p $int {
set e [equals $x $p]

# if the 1st element of $wd and the 5th element of $wd
# are dominated by int, and if the 5th element of $wd
# is the last element (from left to right) to be
# dominated by int, then this will extract from $wd
# elements 1 thru 5 and store them in $clevels
set clevels [lrange $wd [lindex $e 0] [lindex $e end]]

# make $clevels the children of $int
$hierarchy seginfo $p children $clevel $clevels
}
}



proc HierLabel {hierarchy plevel clevel lablevel {ltype1 U} {ltype2 A}} {

# list the elements at the parent level
set wd [$hierarchy segments $plevel]

# if an element at the plevel does not dominate
# an element at the clevel, then make
# the corresponding element at lablevel U
# otherwise make it A. 
# example: Hierlabel h Word Tone Accent
# makes all Accent elements U if Word doesn't
# dominate Tone, otherwise A
# Hierlabel h Word Tone Accent un ac
# as above, but instead of U and A you get un and ac
foreach j $wd {
set num [$hierarchy seginfo $j children $clevel]
if {[llength $num] == 0} {
$hierarchy seginfo $j label $lablevel $ltype1
} else {
$hierarchy seginfo $j label $lablevel $ltype2
}
}
}


