这里我发布了一个perl程序,使用perl程序找到两个synset之间的相似性:
#! /usr/bin/perl -w
use strict;
use warnings;
use WordNet::QueryData;
use WordNet::Similarity::random;
use WordNet::Similarity::lesk;
use WordNet::Similarity::vector;
use WordNet::Similarity::vector_pairs;
# Get the concepts.
my $wps1 = shift;
my $wps2 = shift;
unless (defined $wps1 and defined $wps2) {
print STDERR "Undefined input\n";
print STDERR "Usage: sample.pl synset1 synset2\n";
print STDERR "\tSynsets must be in word#pos#sense format (ex., dog#n#1)\n";
exit 1;
}
print STDERR "Loading WordNet... ";
my $wn = WordNet::QueryData->new;
die "Unable to create WordNet object.\n" if(!$wn);
print STDERR "done.\n";
# Create an object for each of the measures of semantic relatedness.
print STDERR "Creating lesk object... ";
my $lesk = WordNet::Similarity::lesk->new($wn, "config-files/config-lesk.conf");
die "Unable to create lesk object.\n" if(!defined $lesk);
my ($error, $errString) = $lesk->getError();
die $errString if($error > 1);
print STDERR "done.\n";
# Find the relatedness of the concepts using each of the measures.
my $value = $lesk->getRelatedness($wps1, $wps2);
($error, $errString) = $lesk->getError();
die $errString if($error > 1);
print "LESK Similarity = $value\n";
print "LESK ErrorString = $errString\n" if $error;
__END__
在终端上我用它作为:
coep@coep:~/WordNet-Similarity-2.05/samples$ perl sample.pl church#n#1 temple#n#1
Loading WordNet... done.
Creating lesk object... done.
LESK Similarity = 77
coep@coep:~/WordNet-Similarity-2.05/samples$
有谁能告诉我如何为这个perl程序编写python包装器?
答案 0 :(得分:2)
Yo可以使用subprocess
模块从python调用perl脚本,管理它的标准输出然后分析它:
#!/usr/bin/env python
import subprocess
set1 = 'church#n#1'
set2 = 'temple#n#1'
cmd = ['perl', './sample.pl', set1, set2]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in proc.stdout:
if 'Similarity' in line:
similarity = int(line.split("=")[-1])
print similarity
答案 1 :(得分:1)