12

:) w = Word(printables) を使ってみましたが、うまくいきません。これの仕様をどのように指定すればよいですか。「w」はヒンディー語文字 (UTF-8) を処理するためのものです。

コードは文法を指定し、それに応じて解析します。

671.assess  :: अहसास  ::2
x=number + "." + src + "::" + w + "::" + number + "." + number

英語の文字しかない場合は機能しているため、コードは ascii 形式では正しいですが、コードは unicode 形式では機能していません。

つまり、671.assess :: ahsaas ::2 という形式のコードがある場合にコードが機能するということです。

つまり、単語を英語形式で解析しますが、Unicode 形式で文字を解析して出力する方法がわかりません。目的のために、英語のヒンディー語の単語の配置にこれが必要です。

Python コードは次のようになります。

# -*- coding: utf-8 -*-
from pyparsing import Literal, Word, Optional, nums, alphas, ZeroOrMore, printables , Group , alphas8bit , 
# grammar 
src = Word(printables)
trans =  Word(printables)
number = Word(nums)
x=number + "." + src + "::" + trans + "::" + number + "." + number
#parsing for eng-dict
efiledata = open('b1aop_or_not_word.txt').read()
eresults = x.parseString(efiledata)
edict1 = {}
edict2 = {}
counter=0
xx=list()
for result in eresults:
  trans=""#translation string
  ew=""#english word
  xx=result[0]
  ew=xx[2]
  trans=xx[4]   
  edict1 = { ew:trans }
  edict2.update(edict1)
print len(edict2) #no of entries in the english dictionary
print "edict2 has been created"
print "english dictionary" , edict2 

#parsing for hin-dict
hfiledata = open('b1aop_or_not_word.txt').read()
hresults = x.scanString(hfiledata)
hdict1 = {}
hdict2 = {}
counter=0
for result in hresults:
  trans=""#translation string
  hw=""#hin word
  xx=result[0]  
  hw=xx[2]
  trans=xx[4]
  #print trans
  hdict1 = { trans:hw }
  hdict2.update(hdict1)

print len(hdict2) #no of entries in the hindi dictionary
print"hdict2 has been created"
print "hindi dictionary" , hdict2
'''
#######################################################################################################################

def translate(d, ow, hinlist):
   if ow in d.keys():#ow=old word d=dict
    print ow , "exists in the dictionary keys"
        transes = d[ow]
    transes = transes.split()
        print "possible transes for" , ow , " = ", transes
        for word in transes:
            if word in hinlist:
        print "trans for" , ow , " = ", word
                return word
        return None
   else:
        print ow , "absent"
        return None

f = open('bidir','w')
#lines = ["'\
#5# 10 # and better performance in business in turn benefits consumers .  # 0 0 0 0 0 0 0 0 0 0 \
#5# 11 # vHyaapaar mEmn bEhtr kaam upbhOkHtaaomn kE lIe laabhpHrdd hOtaa hAI .  # 0 0 0 0 0 0 0 0 0 0 0 \
#'"]
data=open('bi_full_2','rb').read()
lines = data.split('!@#$%')
loc=0
for line in lines:
    eng, hin = [subline.split(' # ')
                for subline in line.strip('\n').split('\n')]

    for transdict, source, dest in [(edict2, eng, hin),
                                    (hdict2, hin, eng)]:
        sourcethings = source[2].split()
        for word in source[1].split():
            tl = dest[1].split()
            otherword = translate(transdict, word, tl)
            loc = source[1].split().index(word)
            if otherword is not None:
                otherword = otherword.strip()
                print word, ' <-> ', otherword, 'meaning=good'
                if otherword in dest[1].split():
                    print word, ' <-> ', otherword, 'trans=good'
                    sourcethings[loc] = str(
                        dest[1].split().index(otherword) + 1)

        source[2] = ' '.join(sourcethings)

    eng = ' # '.join(eng)
    hin = ' # '.join(hin)
    f.write(eng+'\n'+hin+'\n\n\n')
f.close()
'''

ソースファイルの入力文の例が次の場合:

1# 5 # modern markets : confident consumers  # 0 0 0 0 0 
1# 6 # AddhUnIk baajaar : AshHvsHt upbhOkHtaa .  # 0 0 0 0 0 0 
!@#$%

出力は次のようになります:-

1# 5 # modern markets : confident consumers  # 1 2 3 4 5 
1# 6 # AddhUnIk baajaar : AshHvsHt upbhOkHtaa .  # 1 2 3 4 5 0 
!@#$%

出力の説明:- これにより、双方向の位置合わせが実現されます。これは、英語の「modern」の最初の単語がヒンディー語の「AddhUnik」の最初の単語に対応し、その逆も同様であることを意味します。ここでは、文字も双方向マッピングの不可欠な部分であるため、単語として扱われます。したがって、ヒンディー語の WORD '.' を観察すると、null アラインメントがあり、終止符がないため、英語の文に関しては何もマッピングされません。出力の 3 行目は、基本的に、双方向マッピングを実現しようとしている多数の文に対して作業している場合の区切り記号を表します。

ヒンディー語の文章が Unicode(UTF-8) 形式の場合、どのように変更すれば機能しますか?

4

3 に答える 3

27

Pyparsingprintablesは、ASCII 範囲の文字列のみを扱います。次のように、完全な Unicode 範囲の印刷可能ファイルが必要です。

unicodePrintables = u''.join(unichr(c) for c in xrange(sys.maxunicode) 
                                        if not unichr(c).isspace())

transこれで、このより完全な非スペース文字セットを使用して定義できます。

trans = Word(unicodePrintables)

ヒンディー語のテスト文字列をテストできませんでしたが、これでうまくいくと思います。

(Python 3 を使用している場合、個別の unichr 関数はなく、xrange ジェネレーターもありません。以下を使用してください。

unicodePrintables = ''.join(chr(c) for c in range(sys.maxunicode) 
                                        if not chr(c).isspace())

編集:

pyparsing 2.3.0 の最近のリリースでは、さまざまな Unicode 言語範囲に対してprintablesalphasnums、およびを提供する新しい名前空間クラスが定義されています。alphanums

import pyparsing as pp
pp.Word(pp.pyparsing_unicode.printables)
pp.Word(pp.pyparsing_unicode.Devanagari.printables)
pp.Word(pp.pyparsing_unicode.देवनागरी.printables)
于 2010-02-26T09:43:50.433 に答える
7

原則として、エンコードされたバイト文字列を処理しないでください。できるだけ早く(メソッドを呼び出して)適切なUnicode文字列.decodeし、すべての処理を常にUnicode文字列で実行します。その後、I/Oの目的で必要な場合は.encode必要なバイト文字列エンコーディングに戻します。

あなたがリテラルについて話しているなら、あなたがあなたのコードにいるように見えるので、「できるだけ早く」はすぐにありますu'...':あなたのリテラルを表現するために使用してください。より一般的なケースでは、エンコードされた形式でI / Oを実行する必要がありますが、入力の直後です(特定のエンコードされた形式で出力を実行する必要がある場合は、出力の直前と同じです)。

于 2010-02-26T06:08:08.063 に答える