2016-05-24 5 views
0

ドキュメントをStanfordNLPサーバーに送信するフィルターを実装しようとしました。しかし、どのようにデータを送信するかはわかりません。サーバーのデバッグ出力(data)は次のようになりますPythonでStanfordNLP Serverのクエリを作成するにはどうすればいいですか?

u'I own a dog but bought this fun and helpful book for two of my favorite people that own a cat for a gift. What a perrrrrfect gift item it is and both my friends just loved it. Hoping to have It\\'s a Dog\\'s LIfe soon.Linda Hannawalt' 

[/127.0.0.1:36926] API call w/annotators tokenize,ssplit,pos,depparse,lemma,ner,mention,coref,natlog,openie 
outputFormat=json&This+remote%2C+for+whatever+reason%2C+was+chosen+by+Time+Warner+to+replace+their+previous+silver+remote%2C+the+Time+Warner+Synergy+V+RC-U62CP-1.12S.++The+actual+function+of+this+CLIKR-5+is+OK%2C+but+the+ergonomic+design+sets+back+remotes+by+20+years.++The+buttons+are+all+the+same%2C+there%27s+no+separation+of+the+number+buttons%2C+the+volume+and+channel+buttons+are+the+same+shape+as+the+other+buttons+on+the+remote%2C+and+it+all+adds+up+to+a+crappy+user+experience.++Why+would+TWC+accept+this+as+a+replacement%3F++This+remote+is+virtually+impossible+to+pick+up+and+use+without+staring+at+it+to+make+sure+where+your+fingers+are.++Heck%2C+you+have+to+feel+around+just+to+figure+out+if+you%27ve+grabbed+it+by+the+top+or+bottom%2C+since+there%27s+no+articulation+in+the+body+of+the+thing+to+tell+you+which+end+is+up.++Horrible%2C+just+horrible+design.++I%27m+skipping+this+and+paying+double+for+a+refurbished+Synergy+V.=true&annotators=tokenize%2Cssplit%2Cpos&tokenize.whitespace=true 

しかし、これは、入力された文書については

values = { 
     paragraph: "true", 
     "tokenize.whitespace": "true", 
     "annotators": "tokenize,ssplit,pos", 
     "outputFormat": "json" 
    } 

    data  = urllib.urlencode(values) 
    req  = urllib2.Request(self.url, data) 
    response = urllib2.urlopen(req) 
    result = response.read() 

:現時点では私はこのようにそれをやっています受け取った出力:

<type 'list'>: [{'lemma': u'outputformat', 'originalText': u'outputFormat'}, {'lemma': u'json', 'originalText': u'json'}, {'lemma': u'annotator', 'originalText': u'annotators'}, {'lemma': u'%', 'originalText': u'%'}, {'lemma': u'%', 'originalText': u'%'}, {'lemma': u'2cpos', 'originalText': u'2Cpos'}, {'lemma': u'dog', 'originalText': u'dog'}, {'lemma': u'fun', 'originalText': u'fun'}, {'lemma': u'book', 'originalText': u'book'}, {'lemma': u'people', 'originalText': u'people'}, {'lemma': u'cat', 'originalText': u'cat'}, {'lemma': u'gift', 'originalText': u'gift'}, {'lemma': u'perrrrrfect', 'originalText': u'perrrrrfect'}, {'lemma': u'gift', 'originalText': u'gift'}, {'lemma': u'item', 'originalText': u'item'}, {'lemma': u'friend', 'originalText': u'friends'}, {'lemma': u'%', 'originalText': u'%'}, {'lemma': u'dog', 'originalText': u'Dog'}, {'lemma': u'%', 'originalText': u'%'}, {'lemma': u'life', 'originalText': u'LIfe'}, {'lemma': u'soon.linda', 'originalText': u'soon.Linda'}, {'lemma': u'hannawalt', 'originalText': u'Hannawalt'}, {'lemma': u'tokenize.whitespace', 'originalText': u'tokenize.whitespace'}] 

実際のテキストはそこにありますが、{'lemma': u'outputformat', 'originalText': u'outputFormat'}は明らかに間違っています。正しいリクエスト文字列はどのように見えますか?

私のフィルタのコード:

def filter(self, paragraph): 

    values = { 
     paragraph: "true", 
     "tokenize.whitespace": "true", 
     "annotators": "tokenize,ssplit,pos", 
     "outputFormat": "json" 
    } 

    data = urllib.urlencode(values) 

    req = urllib2.Request(self.url, data) 
    response = urllib2.urlopen(req) 
    result = response.read() 

    result = json.loads(result) 

    filtered_tokens = list() 

    for sentence in result["sentences"]: 

     for token in sentence["tokens"]: 

      pos = token["pos"] 

      if pos in self.whitelist: 
       filtered_tokens.append({ 
        "originalText": token["originalText"], 
        "lemma": token["lemma"] 
       }) 

    if self.debug is True: 
     print "Filtered Tokens: " 
     print filtered_tokens 

    return filtered_tokens 
+1

ここに適切なフォーマットを示すドキュメントがあります:http://stanfordnlp.github.io/CoreNLP/corenlp-server.html。また有用なPythonラッパーがあります:https://github.com/smilli/py-corenlp – StanfordNLPHelp

+1

このPythonラッパーは役に立ちます:github.com/smilli/py-corenlpそしてあなたはそのソースコードを見てどのように要求を出しているのか。たとえば、このファイルにはリクエストを行うコードがあります:https://github.com/smilli/py-corenlp/blob/master/pycorenlp/corenlp.py – StanfordNLPHelp

+1

この質問には、Pythonラッパーを使用している人の例があります。http:///stackoverflow.com/questions/37375137/stanford-corenlp-openie-annotator – StanfordNLPHelp

答えて

1

このPythonラッパーが好きな人はたくさんいます:https://github.com/smilli/py-corenlp。私たちは最終的に、人々がPythonからStanford CoreNLPを使用するのに役立つ独自のコードをリリースしようとしています。

2

私は問題を疑うあなたはPOSTリクエストのボディでプロパティを提出していることです。プロパティはURLパラメータとして渡す必要があります。 POSTの本文は、注釈を付ける文書にする必要があります。

関連する問題