2016-06-12 6 views
4

グーグルSyntaxNetは、私が読んで、この出力(文字列データ)を解析するためのpythonを使用したい出力 のような..解析のpython asciitree出力と印刷「ラベルブラケット表記は」

saw VBD ROOT 
+-- Alice NNP nsubj 
| +-- , , punct 
| +-- reading VBG rcmod 
|  +-- who WP nsubj 
|  +-- had VBD aux 
|  +-- been VBN aux 
|  +-- about IN prep 
|   +-- SyntaxNet NNP pobj 
+-- , , punct 
+-- Bob NNP dobj 
+-- in IN prep 
| +-- hallway NN pobj 
|  +-- the DT det 
+-- yesterday NN tmod 
+-- . . punct 

を与えます。 と LIKE 'ラベルブラケット記法' でプリントアウト - あなたは私を助けることができる

>
[saw [Alice [,] [reading [who][had][been][about [SyntaxNet]]]][,][Bob][in [hallway [the]]][yesterday][.]] 

答えて

6

ツリーを解析する代わりに、構文解析しやすいすべての形式でSyntaxNetに出力することができます。

1  Alice _  NOUN NNP  _  10  nsubj _  _ 
2  ,  _  .  ,  _  1  punct _  _ 
3  who  _  PRON WP  _  6  nsubj _  _ 
4  had  _  VERB VBD  _  6  aux  _  _ 
5  been _  VERB VBN  _  6  aux  _  _ 
6  reading _  VERB VBG  _  1  rcmod _  _ 
7  about _  ADP  IN  _  6  prep _  _ 
8  SyntaxNet  _  NOUN NNP  _  7  pobj _  _ 
9  ,  _  .  ,  _  10  punct _  _ 
10  saw  _  VERB VBD  _  0  ROOT _  _ 
11  Bob  _  NOUN NNP  _  10  dobj _  _ 
12  in  _  ADP  IN  _  10  prep _  _ 
13  the  _  DET  DT  _  14  det  _  _ 
14  hallway _  NOUN NN  _  12  pobj _  _ 
15  yesterday  _  NOUN NN  _  10  tmod _  _ 
16  .  _  .  .  _  10  punct _  _ 

各列の意味はhere見つけることができます:あなたの文章のためconll形式は、次のようになります。現時点で懸念されるのは、最初の列(単語のID)、2番目の列(単語そのもの)、7番目の列(つまり頭、つまり親)です。ルートノードはconll形式は、私たちは(私はあなたがあなたの出力を得るために使用されると仮定)demo.shの最後の数行をコメントアウトする必要が取得するために0

の親を持っています

$PARSER_EVAL \ 
    --input=$INPUT_FORMAT \ 
    --output=stdout-conll \ 
    --hidden_layer_sizes=64 \ 
    --arg_prefix=brain_tagger \ 
    --graph_builder=structured \ 
    --task_context=$MODEL_DIR/context.pbtxt \ 
    --model_path=$MODEL_DIR/tagger-params \ 
    --slim_model \ 
    --batch_size=1024 \ 
    --alsologtostderr \ 
    | \ 
    $PARSER_EVAL \ 
    --input=stdin-conll \ 
    --output=stdout-conll \ 
    --hidden_layer_sizes=512,512 \ 
    --arg_prefix=brain_parser \ 
    --graph_builder=structured \ 
    --task_context=$MODEL_DIR/context.pbtxt \ 
    --model_path=$MODEL_DIR/parser-params \ 
    --slim_model \ 
    --batch_size=1024 \ 
    --alsologtostderr #\ 
# | \ 
# bazel-bin/syntaxnet/conll2tree \ 
# --task_context=$MODEL_DIR/context.pbtxt \ 
# --alsologtostderr 

(前の行のバックスラッシュをコメントアウトすることを忘れないでください)

where I got this trick from, see the comment

私は自分自身をdemo.sh実行すると、私は必要としない多くの情報を取得します。どのように私はあなたが把握するために残すことを取り除くことができます(私に知らせる:))。 私は関連する部分をファイルにcoppiedしましたので、私はそれを私が書くつもりのPythonプログラムにパイプすることができます。情報を取り除くことができれば、demo.shをPythonプログラムに直接パイプすることができるはずです。

注:私はかなりPythonに新しいので、私のコードを改善するために自由に感じてください。

まず、入力からconllファイルを読み込みたいだけです。私は素敵なクラスに各単語を置くのが好きです。

#!/usr/bin/env python 

import sys 

# Conll data format: 
# http://ilk.uvt.nl/conll/#dataformat 
# 
# The only parts we need: 
# 1: ID 
# 2: FORM (The original word) 
# 7: HEAD (The ID of its parent) 

class Word: 
    "A class containing the information of a single line from a conll file." 

    def __init__(self, columns): 
     self.id = int(columns[0]) 
     self.form = columns[1] 
     self.head = int(columns[6]) 
     self.children = [] 

# Read the conll input and put it in a list of words. 
words = [] 
for line in sys.stdin: 
    # Remove newline character, split on spaces and remove empty columns. 
    line = filter(None, line.rstrip().split(" ")) 

    words.append(Word(line)) 

ニースですが、まだツリー構造ではありません。我々はもう少し仕事をしなければならない。

すべての単語についてすべての子供を調べるために、リスト全体を数回見てもらえますが、これは非効率的です。私は代わりに親によってそれらを並べ替えると、それはちょうど与えられた親のすべての子を取得するために迅速な検索する必要があります。

# Sort the words by their head (parent). 
lookup = [[] for _ in range(len(words) + 1)] 
for word in words: 
    lookup[word.head].append(word) 

ツリー構造を作成します。

# Build a tree 
def buildTree(head): 
    "Find the children for the given head in the lookup, recursively" 

    # Get all the children of this parent. 
    children = lookup[head] 

    # Get the children of the children. 
    for child in children: 
     child.children = buildTree(child.id) 

    return children 

# Get the root's child. There should only be one child. The function returns an 
# array of children so just get the first one. 
tree = buildTree(0)[0] # Start with head = 0 (which is the ROOT node) 

は、Wordクラスにいくつかのメソッドのオーバーロードを追加することができ、新しい形式でツリーを印刷できるようにするには:

def __str__(self): 
    if len(self.children) == 0: 
     return "[" + self.form + "]" 
    else: 
     return "[" + self.form + " " + "".join(str(child) for child in self.children) + "]" 

def __repr__(self): 
    return self.__str__() 

今、あなたをちょうどこれを行うことができます:

print tree 

パイプそれはとても好き:syntaxnetから

cat input.conll | ./my_parser.py 

またはdirectylyを:

echo "Alice, who had been reading about SyntaxNet, saw Bob in the hallway yesterday." | syntaxnet/demo.sh | ./my_parser.py 
+0

どうもありがとうございました!私は考えを得た ツリーの構築を含むsyntaxnet/conll2tree.pyを修正しました。 私はあなたの関数 "def __str __(self):"ロジックを使用しています。 いいですよ!ありがとう – CPUU

+0

私はサーバークライアントの設計モデルを使用します。 クライアントが文を送信すると、Serverが実行されています(多くの情報が表示されますが、必要はありません)。 – CPUU