#!/usr/bin/tclsh # cutoff2forms - auxiliary corpus script # Copyright (C) 2004-2007 Richard Moot (Richard.Moot@labri.fr) # Copyright (C) 2004-2007 CNRS (http://www.cnrs.fr) # Copyright (C) 2004-2007 INRIA (http://www.inria.fr) # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # cutoff2forms transforms a file as produced by the treecounts script # into a tcl array "formula", which is required by several other scripts # as a file "formula.tcl" # typical call: cutoff2forms cutoff treecounts formula.tcl formula.pl # printform.tcl # note that a call without just a single argument will use these # default filenames # # formula.tcl will containing an array from formulas to integers # formula.pl will contain this same array but as Prolog facts # printform.tcl will contain the inverse array from integers # to formulas for the purpose of printing # a second array is used for the inverse array, so we can sort on the # numbers and have them in order in the output file. 20041231 RM # modified again to create the "formula.pl" file at the same time; the # new version asks a file as produced by the treecounts script as the # first argument, "formula.tcl" as the second and "formula.pl" as the # third. if {$argc <= 1} { set file1 "treecounts" set file2 "formula.tcl" set file3 "formula.pl" set file4 "printform.tcl" if {$argc == 1} { set cutoff [lindex $argv 0] } else { set cutoff 1 } } elseif {$argc == 4} { set cutoff 1 set file1 [lindex $argv 0] set file2 [lindex $argv 1] set file3 [lindex $argv 2] set file4 [lindex $argv 3] } elseif {$argc == 5} { set cutoff [lindex $argv 0] set file1 [lindex $argv 1] set file2 [lindex $argv 2] set file3 [lindex $argv 3] set file4 [lindex $argv 4] } else { puts stderr "Usage: cutoff2forms \[cutoff\] \[treecounts formula.tcl formula.pl printform.tcl\]" exit 1 } set total 0 set forms 0 set f1 [open $file1 r] set f2 [open $file2 w] set f3 [open $file3 w] set f4 [open $file4 w] while {[gets $f1 line] >= 0} { if {[regexp {^([0-9]+)\ (.*)$} $line all num form]} { if {$num>=$cutoff} { if {[catch {set formula($form)}]} { incr forms set number($forms) $form set formula($form) $forms } } else { set cut($form) 0 set formula($form) 0 } } else { puts $f2 "# $line" } } puts $f2 "" set cutforms 0 foreach i [lsort [array names cut]] { puts $f2 "set formula($i) 0" incr cutforms } puts $f2 "# Cut: $cutforms" puts $f4 "set printform(0) ???" foreach i [lsort -integer [array names number]] { puts $f2 "set formula($number($i)) $i" puts $f3 "formula($i, $number($i))." puts $f4 "set printform($i) $number($i)" } puts $f2 "# Total: $forms" close $f1 close $f2 close $f3 close $f4