2015-09-07 41 views
1

我該如何反思一棵樹的Treex::PML::Node對象來找出每個節點包含的數據?我可以調用根節點的方法:例如,$root->firstson()是另一個Node對象。但是,我如何檢查樹節點的數據字段?我對Perl對象知之甚少以破解這個螺母。如何檢查Perl對象(PML節點)?

背景:我正在與支持Perl腳本的應用程序搏鬥,並試圖操作一個暴露爲PML節點樹的解析句子。 (每個節點代表一個單詞及其各種註釋,這正是我試圖訪問的內容。)不幸的是,我無法訪問PML模式 - 我擁有的是$root變量的句柄。

回答

2

使用節點對象的attribute_paths方法來獲取可用數據的列表。

轉到Prague Markup Language Documentation並下載所有的示例架構的(他們稱對方...)

example1.xml 
example1_schema.xml 
example2.xml 
example2_schema.xml 
... 

在這個例子中,我使用example7.xml得到的路徑列表,您可以使用獲取數據。存儲在PML節點的數據來自以下 句子:

約翰愛瑪麗。他星期五告訴她。

下面是代碼:

#!/usr/bin/env perl 

use strict; 
use warnings; 

use Treex::PML; 

my $document = Treex::PML::Factory->createDocumentFromFile('example7.xml'); 

foreach my $tree ($document->trees) { 
    my $node = $tree; 
    while ($node) { 
     $node = $node->following; # depth-first traversal 
     my @paths = $node->attribute_paths(); 
     print "Can call the following:\n"; 
     for (@paths) { 
      print '$node->all("' . $_ . '");' . "\n"; 
      my ($value) = $node->all($_); 
      print " ==> $value\n"; 
     } 
     exit 0; 
    } 
} 

輸出:

Can call the following: 
$node->all("label"); 
==> NP 
$node->all("w/id"); 
==> t#s1w1 
$node->all("w/#content"); 
==> John 
+0

謝謝!現在只是看到這個,出於某種原因。 – alexis