Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 从CSV文件创建GATE文档_Perl_Csv_Gate - Fatal编程技术网

Perl 从CSV文件创建GATE文档

Perl 从CSV文件创建GATE文档,perl,csv,gate,Perl,Csv,Gate,我需要转换如下结构的csv文档: i love iphone \t positive i hate iphone \t negative 到包含相关类的gate文档: 最好的方法是什么?jape,groovy?可能不是更简单的答案,但它可以与以下perl脚本一起工作: use strict; use locale; use HTML::Entities; open (IN,$ARGV[0]) or die "file doesn't exist ! : $!\n"; my $i

我需要转换如下结构的csv文档:

i love iphone \t positive
i hate iphone \t negative
到包含相关类的gate文档:


最好的方法是什么?jape,groovy?

可能不是更简单的答案,但它可以与以下perl脚本一起工作:

use strict;
use locale;
use HTML::Entities;

open (IN,$ARGV[0])
    or die "file doesn't exist ! : $!\n";

my $i = 0;

while (my $form = <FICHIER>) {

    if ($form =~ /^((.+)\t(.+))$/)

    {   
        my $file = "tweet_".$i.".xml";
        # Use the open() function to create the file.
        unless(open FILE, '>'.$file) {
        # Die with error message 
        # if we can't open it.
        die "nUnable to create $file";
        }           

        my $sentence =$2;
        my $encoded_sent = encode_entities($sentence);

        my $class = $3;
        my $length_sent = length($sentence);

        ##head xml
        print FILE "<?xml version='1.0' encoding='UTF-8'?>"."\n";
        print FILE '<GateDocument version="3">'."\n";
        print FILE '<GateDocumentFeatures>'."\n";
        print FILE '<Feature>'."\n";
        print FILE '<Name className="java.lang.String">gate.SourceURL</Name>'."\n";
        print FILE '<Value className="java.lang.String">created from String</Value>'."\n";
        print FILE '</Feature>'."\n";
        print FILE '</GateDocumentFeatures>'."\n";

        ##create xml for each line  -- here is the content
        print FILE '<TextWithNodes><Node id="0"/>'.$encoded_sent.'<Node id="'.$length_sent.'"/></TextWithNodes>'."\n";

        print FILE '<AnnotationSet Name="Key">'."\n";
        print FILE '<Annotation Id="1" Type="Tweet" StartNode="0" EndNode="'.$length_sent.'">'."\n";

        print FILE '<Feature>'."\n";
        print FILE '<Name className="java.lang.String">class</Name>'."\n";
        print FILE '<Value className="java.lang.String">'.$class.'</Value>'."\n";
        print FILE '</Feature>'."\n";
        print FILE '</Annotation>'."\n";
        print FILE '</AnnotationSet>'."\n";

        ##end of the document
        print FILE '</GateDocument>'."\n";
        $i++;
    }
    close FILE;
}    
close IN;
使用严格;
使用区域设置;
使用HTML::实体;
打开(在$ARGV[0]中)
或死“文件不存在!:$!\n”;
我的$i=0;
while(我的$form=){
如果($form=~/^(.+)\t(.+)$/)
{   
my$file=“tweet”.$i.“.xml”;
#使用open()函数创建文件。
除非(打开文件“>”.$FILE){
#死机时显示错误消息
#如果我们不能打开它。
死“无法创建$file”;
}           
我的$句子=$2;
我的$encoded_sent=编码_实体($句子);
我的$class=$3;
我的$length\u sent=长度(句子);
##头部xml
打印文件“”。“\n”;
打印文件“”。“\n”;
打印文件“”。“\n”;
打印文件“”。“\n”;
打印文件“gate.SourceURL”。“\n”;
打印文件“根据字符串创建”。“\n”;
打印文件“”。“\n”;
打印文件“”。“\n”;
##为每一行创建xml——以下是内容
打印文件“”。$encoded\u已发送。“”。“\n”;
打印文件“”。“\n”;
打印文件“”。“\n”;
打印文件“”。“\n”;
打印文件“类”。“\n”;
打印文件'.$class'.''。“\n”;
打印文件“”。“\n”;
打印文件“”。“\n”;
打印文件“”。“\n”;
##文件结束
打印文件“”。“\n”;
$i++;
}
关闭文件;
}    
接近;

基本上,您必须处理CSV和GATE文档。如果您在CPAN上搜索,您会发现可以轻松处理此类文档的模块

因此,您可以使用Text::CSV从CSV文件中获取文本,并使用NLP::GATE::Document模块的
setText
setAnnotationSet
方法来创建、设置文本和注释GATE文档


试一试,如果遇到任何问题,请再次询问您迄今为止为实现目标而尝试的代码。

您的代码中有几个问题,但只添加了一条注释:您是否检查了如何处理GATE文档和注释?