2

こんにちは、NLTK を使用して自然言語処理を学んでいます。本のbabelize_shell()の例を実装しようとしています。私がやっていることは、babelize_shell() を実行することです。その後、文字列を入力し、本に記載されているようにドイツ語を入力し、続いて実行します。

私が得ているエラーは次のとおりです。

Traceback (most recent call last):
  File "<pyshell#148>", line 1, in <module>
    babelize_shell()
  File "C:\Python27\lib\site-packages\nltk\misc\babelfish.py", line 175, in babelize_shell
    for count, new_phrase in enumerate(babelize(phrase, 'english', language)):
  File "C:\Python27\lib\site-packages\nltk\misc\babelfish.py", line 126, in babelize
    phrase = translate(phrase, next, flip[next])
  File "C:\Python27\lib\site-packages\nltk\misc\babelfish.py", line 106, in translate
    if not match: raise BabelfishChangedError("Can't recognize translated string.")
BabelfishChangedError: Can't recognize translated string.

セッションの例を次に示します。

>>> babelize_shell()
NLTK Babelizer: type 'help' for a list of commands.
Babel> how long before the next flight to Alice Springs?
Babel> german
Babel> run
0> how long before the next flight to Alice Springs?
1> wie lang vor dem folgenden Flug zu Alice Springs?
2> how long before the following flight to Alice jump?
3> wie lang vor dem folgenden Flug zu Alice springen Sie?
4> how long before the following flight to Alice do you jump?
5> wie lang, bevor der folgende Flug zu Alice tun, Sie springen?
6> how long, before the following flight to Alice does, do you jump?
7> wie lang bevor der folgende Flug zu Alice tut, tun Sie springen?
8> how long before the following flight to Alice does, do you jump?
9> wie lang, bevor der folgende Flug zu Alice tut, tun Sie springen?
10> how long, before the following flight does to Alice, do do you jump?
11> wie lang bevor der folgende Flug zu Alice tut, Sie tun Sprung?
12> how long before the following flight does leap to Alice, does you?
4

1 に答える 1

7

私は今同じ問題を抱えています。

私はこれを見つけました: http://nltk.googlecode.com/svn/trunk/doc/api/nltk.misc.babelfish-module.html

BabelfishChangedError babelfish.yahoo.com が HTML レイアウトの詳細を変更し、babelizer が正しい形式でデータを送信しなくなった場合、または結果を解析できなくなった場合にスローされます。

これを修正する方法があるかどうかを確認します。

私が今思いついたソリューションは、Microsoft Translator Web サービス (SOAP) を使用しています。簡単な解決策ではありませんが、コードを書くのは面白いです。

http://msdn.microsoft.com/en-us/library/hh454950の指示に従い、nltk/misc/babelfish.py にある babelfish.py を変更しました。

  1. Azure Marketplace で Microsoft Translator API にサブスクライブする

Azure Marketplace で Microsoft Translator API をサブスクライブします。無料のサブスクリプションを選択しました。

  1. アプリケーションを登録する Azure DataMarket

アプリケーションを Azure DataMarket に登録するには、手順 1 の LiveID 資格情報を使用して datamarket.azure.com/developer/applications/ にアクセスし、[登録] をクリックします。後で使用するために、クライアント ID とクライアント シークレットを書き留めます。

  1. Python 用の suds をインストールします。 fedorahosted.org/suds/

  2. babelfish.py を変更します (独自の cliend_id とシークレットを使用します)。

//追加するインポート

from suds.client import Client
import httplib
import ast

...

#added function
def soaped_babelfish(TextToTranslate,codeLangFrom, codeLangTo):

    #Oauth credentials
    params = urllib.urlencode({'client_id': 'babelfish_soaped', 'client_secret': '1IkIG3j0ujiSMkTueCZ46iAY4fB1Nzr+rHBciHDCdxw=', 'scope': 'http://api.microsofttranslator.com', 'grant_type': 'client_credentials'})


    headers = {"Content-type": "application/x-www-form-urlencoded"}
    conn = httplib.HTTPSConnection("datamarket.accesscontrol.windows.net")
    conn.request("POST", "/v2/OAuth2-13/", params, headers)
    response = conn.getresponse()
    #print response.status, response.reason

    data = response.read()


    #obtain access_token
    respondeDict = ast.literal_eval(data)
    access_token = respondeDict['access_token']
    conn.close()


    #use the webservice with the accesstoken
    client = Client('http://api.microsofttranslator.com/V2/Soap.svc')

    result = client.service.Translate('Bearer'+' '+access_token,TextToTranslate,codeLangFrom, codeLangTo, 'text/plain','general')

    return result

...

#modified translate method
def translate(phrase, source, target):
    phrase = clean(phrase)
    try:
        source_code = __languages[source]
        target_code = __languages[target]
    except KeyError, lang:
        raise ValueError, "Language %s not available " % lang

    return clean(soaped_babelfish(phrase,source_code,target_code))

以上で、SOAP 版は終了です。ある日、Web のみのソリューションを試してみます (現在の babelfish.py に似ていますが、変更に適応しています)。

于 2012-09-06T14:06:19.830 に答える