将文本集合转换为向量表示的Perl脚本
输入文件采用UTF8编码,每行具有以下结构:将文本集合转换为向量表示的Perl脚本,perl,Perl,输入文件采用UTF8编码,每行具有以下结构: C\tTEXT\n 其中C是一类文档(几个字符),\t是一个制表符,文本是一个字符序列,\n是一个换行字符 从每个文本中删除HTML标记和类似标记、实体、非字母的字符,并将每个文本转换为单词序列,其中顺序并不重要 然后从每个文本创建一个向量,其中向量的各个元素(属性)对应于文本集合中的单词和值向量的大小取决于单词在文本中的出现情况。这些价值观可以是两种类型: A - number of occurrences of words (1
C\tTEXT\n
其中C是一类文档(几个字符),\t
是一个制表符,文本是一个字符序列,\n
是一个换行字符
从每个文本中删除HTML标记和类似标记、实体、非字母的字符,并将每个文本转换为单词序列,其中顺序并不重要
然后从每个文本创建一个向量,其中向量的各个元素(属性)对应于文本集合中的单词和值向量的大小取决于单词在文本中的出现情况。这些价值观可以是两种类型:
A - number of occurrences of words (1 or 0)
B - number of occurrences of words (0 or more)
最后一个值向量是文档的类
如有必要,可从所有文本中删除频率较低(如一)的单词
也可以删除包含少量字符的单词
Example input file:
CLASS One Class One
CLASS One Two
2CLASS two three
CLAS12 three
示例输出文件:
这些是脚本的参数(最小字长=1,单词的最小出现次数=1,A)
输出:
one two three
CLASS 2 0 0
CLASS 1 1 0
2CLASS 0 1 1
CLAS12 0 0 1
我当前的代码:
请帮帮我
#!/usr/bin/perl
use strict;
use encoding 'UTF-8';
use Data::Dumper;
my %vector = ();
my @vectors = ();
my ($string,$word);
open SOURCE, "<:encoding(UTF-8)", "source.txt" or die "File does not exist $!\n";
my($class,$hodnota);
while (my $line = <SOURCE>) {
if($line=~ /^(\w+)\t(.+)\n/){
$string =$2; $class = $1;
$string=~ s/[^a-zA-Z ]//g;
for $word ( split " +", $string )
{
$vector{$word}++;
}
$vector{"class"} = $class;
push(@vectors, %vector)
}
}
close S;
print Dumper( \@vectors );
#/usr/bin/perl
严格使用;
使用编码“UTF-8”;
使用数据::转储程序;
我的%vector=();
我的@vectors=();
我的($string,$word);
开源,“我建议如下:
chomp($line);
if ($line =~ /^(\w+)\t(.+)/){
my $vector = {};
my ($class, $string) = ($1, $2);
for my $word (split /[^a-zA-Z]/, $string) {
next if length($word) < $some_treshold; # $word is too short
my $word_lc = lc($word);
$vector{$word_lc}++;
$all_words{$word_lc} = 1; # this has to be initialized before main loop, as $all_words = {};
}
$vector{"class"} = $class; # hopefully, no words will be "class"
push(@vectors, %vector)
}
chomp($line);
如果($line=~/^(\w+)\t(+.+)/){
我的$vector={};
我的($class,$string)=($1,$2);
对于我的$word(拆分/[^a-zA-Z]/,$string){
接下来,如果长度($word)<$some_#treshold;#$word太短
我的$word\u lc=lc($word);
$vector{$word_lc}++;
$all_words{$word_lc}=1;#这必须在主循环之前初始化,如$all_words={};
}
$vector{“class}=$class;#希望没有单词是“class”
推送(@vectors,%vector)
}
完成后,所有使用的单词都可以通过键%$all_words
找到。希望我能正确理解您的需要。使用严格;
use strict;
use warnings;
use Data::Dumper;
open my $in_data, shift(@ARGV);
my @array_of_hashes_of_hashes=();
#used array of hashes_of_hashes because you treated two instances of CLASS differently
#if they could be treated the same, a simple hash of hashes would work fine.
while (<$in_data>)
{
if ($_ =~ /^(\w+)\t(.+)\n/)
{
my %temp_hash=();
my @values=split(/ /,$2);
foreach (@values)
{
$temp_hash{lc($_)}+=1; #so that one and One map to the same key
}
push @array_of_hashes_of_hashes, {$1 => \%temp_hash};
}
}
print Dumper \@array_of_hashes_of_hashes; #just to show you what it looks like
使用警告;
使用数据::转储程序;
打开我的$in_数据,shift(@ARGV);
my@array\u of_hashes\u of_hashes=();
#因为您对类的两个实例的处理不同,所以使用了哈希数组
#如果可以对它们进行相同的处理,那么一个简单的散列就可以了。
而()
{
如果($\u=~/^(\ w+)\t(+)\n/)
{
我的%temp_哈希=();
my@values=拆分(/,$2);
foreach(@values)
{
$temp_hash{lc($)}+=1;#因此一个和一个映射到同一个键
}
push@array\u of \u hash\u of \u hash,{$1=>\%temp\u hash};
}
}
打印转储程序\@array\u of_hashes\u of_hashes;#只是为了向您展示它的外观
我注意到您没有从Class-One Class-One
打印Class
的值,因此如果您想在打印所有内容时过滤掉该值