| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use utf8; | ||
| 5 | use open qw(:utf8 :std); | ||
| 6 | |||
| 7 | use Getopt::Long; | ||
| 8 | use Pod::Usage; | ||
| 9 | |||
| 10 | use Brat::Handler; | ||
| 11 | |||
| 12 | binmode(STDIN, ':utf8'); | ||
| 13 | binmode(STDOUT, ':utf8'); | ||
| 14 | binmode(STDERR, ':utf8'); | ||
| 15 | |||
| 16 | my $verbose; | ||
| 17 | my $help; | ||
| 18 | my $man; | ||
| 19 | my @inputDirs; | ||
| 20 | my $inputDir; | ||
| 21 | my @inputFiles; | ||
| 22 | my $inputFile; | ||
| 23 | my $outputFile = "-"; | ||
| 24 | my $fileList; | ||
| 25 | |||
| 26 | if (scalar(@ARGV) ==0) { | ||
| 27 | $help = 1; | ||
| 28 | } | ||
| 29 | |||
| 30 | Getopt::Long::Configure ("bundling"); | ||
| 31 | |||
| 32 | GetOptions('help|?' => \$help, | ||
| 33 | 'man' => \$man, | ||
| 34 | 'verbose|v' => \$verbose, | ||
| 35 | 'input-dir|d=s' => \@inputDirs, | ||
| 36 | 'input-file|i=s' => \@inputFiles, | ||
| 37 | 'output-file|o=s' => \$outputFile, | ||
| 38 | 'file-list|l=s' => \$fileList, | ||
| 39 | ); | ||
| 40 | |||
| 41 | pod2usage(1) if $help; | ||
| 42 | pod2usage(-exitstatus => 0, -verbose => 2) if $man; | ||
| 43 | |||
| 44 | my $brat = Brat::Handler->new(); | ||
| 45 | |||
| 46 | foreach $inputFile (@inputFiles) { | ||
| 47 | $brat->_bratAnnotations($brat->loadFile($inputFile)); | ||
| 48 | } | ||
| 49 | |||
| 50 | foreach $inputDir (@inputDirs) { | ||
| 51 | # $brat->_inputDir($inputDir); | ||
| 52 | $brat->loadDir($inputDir); | ||
| 53 | } | ||
| 54 | |||
| 55 | # if (defined $inputDir) { | ||
| 56 | # # $brat->_inputDir($inputDir); | ||
| 57 | # $brat->loadDir($inputDir); | ||
| 58 | # } | ||
| 59 | |||
| 60 | if (defined $fileList) { | ||
| 61 | $brat->loadList($fileList); | ||
| 62 | } | ||
| 63 | |||
| 64 | my $concatAnn = $brat->concat(); | ||
| 65 | |||
| 66 | $concatAnn->print($outputFile); | ||
| 67 | |||
| 68 | ######################################################################## | ||
| 69 | |||
| 70 | =head1 NAME | ||
| 71 | |||
| 72 | concatBratFiles.pl - Perl script for concatenating Brat files | ||
| 73 | |||
| 74 | |||
| 75 | =head1 SYNOPSIS | ||
| 76 | |||
| 77 | concatBratFiles.pl [options] | ||
| 78 | |||
| 79 | where option can be --help --man --verbose | ||
| 80 | |||
| 81 | =head1 OPTIONS AND ARGUMENTS | ||
| 82 | |||
| 83 | =over 4 | ||
| 84 | |||
| 85 | =item --input-dir <filename>, -d <filename> | ||
| 86 | |||
| 87 | Specification of the name of the directory containing the input files. | ||
| 88 | Several directories can be specified. | ||
| 89 | |||
| 90 | =item --input-file <filename>, -i <filename> | ||
| 91 | |||
| 92 | Specification of the name of an input file (either the text or | ||
| 93 | annotation file). Several input files can be specified. | ||
| 94 | |||
| 95 | =item --output-file <filename>, -o <filename> | ||
| 96 | |||
| 97 | Specification of the name of the output files (either the text or | ||
| 98 | annotation file). The filename can be C<-> to print the results on the | ||
| 99 | standard output. | ||
| 100 | |||
| 101 | =item --file-list <filename>, -l <filename> | ||
| 102 | |||
| 103 | Specification of the list of files to concatenate. Each line contains | ||
| 104 | one file name. | ||
| 105 | |||
| 106 | =item --help | ||
| 107 | |||
| 108 | print help message for using concatBratFiles.pl | ||
| 109 | |||
| 110 | =item --man | ||
| 111 | |||
| 112 | print man page of concatBratFiles.pl | ||
| 113 | |||
| 114 | =item --verbose | ||
| 115 | |||
| 116 | Go into the verbose mode | ||
| 117 | |||
| 118 | =back | ||
| 119 | |||
| 120 | =head1 DESCRIPTION | ||
| 121 | |||
| 122 | This script concatenates several files which have been annotated with | ||
| 123 | Brat (<http://brat.nlplab.org/>). Two output files are created: a text | ||
| 124 | file which contains all the input text files, and a annotation file | ||
| 125 | which contains all the annotations of the input files. Annotations are | ||
| 126 | merged and their offset are shift. | ||
| 127 | |||
| 128 | It is required the name of the output file has the C<txt> or C<ann> | ||
| 129 | extension. | ||
| 130 | |||
| 131 | If no output files are specified, the concatenated files (text and | ||
| 132 | annotations) are printed on the standard output. | ||
| 133 | |||
| 134 | |||
| 135 | =head1 EXAMPLES | ||
| 136 | |||
| 137 | Concatenation of all the files in the directory C<examples> and print | ||
| 138 | text in the file C<concat1.txt> and the annotations in the file | ||
| 139 | C<concat1.ann>. | ||
| 140 | |||
| 141 | concatBratFiles.pl -d examples -o concat1.ann | ||
| 142 | |||
| 143 | |||
| 144 | Concatenation of all the three files and print | ||
| 145 | text in the file C<concatFile.txt> and the annotations in the file | ||
| 146 | C<concatFile.ann>. | ||
| 147 | |||
| 148 | concatBratFiles.pl -i examples/taln-2012-long-001-resume.txt -i examples/taln-2012-long-002-resume.ann -i examples/taln-2012-long-003-resume.txt -o concatFile.txt | ||
| 149 | |||
| 150 | |||
| 151 | Concatenation of all the files indicated in C<examples/list.txt> and | ||
| 152 | print text in the file C<concat2.txt> and the annotations in the file | ||
| 153 | C<concat2.ann>. | ||
| 154 | |||
| 155 | concatBratFiles.pl -l examples/list.txt -o concat2.ann | ||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | =head1 SEE ALSO | ||
| 160 | |||
| 161 | http://brat.nlplab.org/ | ||
| 162 | |||
| 163 | =head1 AUTHOR | ||
| 164 | |||
| 165 | Thierry Hamon, E<lt>hamon@limsi.frE<gt> | ||
| 166 | |||
| 167 | =head1 COPYRIGHT AND LICENSE | ||
| 168 | |||
| 169 | Copyright (C) 2015 Thierry Hamon | ||
| 170 | |||
| 171 | This is free software; you can redistribute it and/or modify it under | ||
| 172 | the same terms as Perl itself, either Perl version 5.8.4 or, at your | ||
| 173 | option, any later version of Perl 5 you may have available. | ||
| 174 | |||
| 175 | =cut | ||
| 176 |