protobufでStanfordNLPの結果を出力したいのですが(そのサイズはずっと小さいので)、Pythonで結果を読み返しています。私はどうしたらいいですか?Protobuf PythonでのStanfordNLP出力のシリアル化
java -cp "stanford-corenlp-full-2015-12-09/*" \
edu.stanford.nlp.pipeline.StanfordCoreNLP \
-annotators tokenize,ssplit \
-file input.txt \
-outputFormat serialized \
-outputSerializer \
edu.stanford.nlp.pipeline.ProtobufAnnotationSerializer
は次に、このようなPythonモジュールにStanfordNLPのソースコードが付属していCoreNLP.proto
、コンパイルするprotoc
を使用:
Iは出力指示hereこのようProtobufAnnotationSerializer
でシリアル化結果を、続いて:
protoc --python_out=. CoreNLP.proto
はその後、pythonで私はこのようなファイルをリードバック:
import CoreNLP_pb2
doc = CoreNLP_pb2.Document()
doc.ParseFromString(open('input.txt.ser.gz', 'rb').read())
解析は、次のエラーメッセージで失敗し
---------------------------------------------------------------------------
DecodeError Traceback (most recent call last)
<ipython-input-213-d8eaeb9c2048> in <module>()
1 doc = CoreNLP_pb2.Document()
----> 2 doc.ParseFromString(open('imed/s5_tokenized/conv-00000.ser.gz', 'rb').read())
/usr/local/lib/python2.7/dist-packages/google/protobuf/message.pyc in ParseFromString(self, serialized)
183 """
184 self.Clear()
--> 185 self.MergeFromString(serialized)
186
187 def SerializeToString(self):
/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.pyc in MergeFromString(self, serialized)
1092 # The only reason _InternalParse would return early is if it
1093 # encountered an end-group tag.
-> 1094 raise message_mod.DecodeError('Unexpected end-group tag.')
1095 except (IndexError, TypeError):
1096 # Now ord(buf[p:p+1]) == ord('') gets TypeError.
DecodeError: Unexpected end-group tag.
UPDATE:
私はシリアライザガボール・アンジェリの作成者を尋ね、答えを得ました。 protobufオブジェクトはwriteDelimitedTo
のファイルにthis lineで書き込まれました。 writeTo
に変更すると、出力ファイルをPythonで読むことができます。
実行中のprotocのバージョンは何ですか? 'protoc --version' – sberry
@sberry:" libprotoc 3.0.0 "を出力します。 – shaoyl85
これはまた、問題(.javaファイルを生成するためにどのバージョンが使われたかわかりません)でも問題になるかもしれませんが、私の答えは最初です。 – sberry