Python - pyparsing unicode字符

标签 python unicode nlp pyparsing

:) 我尝试使用 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 
!@#$%

输出说明:- 这样就实现了双向对齐。 这意味着英语“现代”的第一个单词映射到印地语“AddhUnIk”的第一个单词,反之亦然。这里甚至字符也被视为单词,因为它们也是双向映射的组成部分。因此,如果您观察印地语单词“。”有一个空对齐并且它映射到关于英语句子的任何东西,因为它没有句号。 当我们处理您尝试实现双向映射的多个句子时,输出的第 3 行基本上代表一个分隔符。

如果我有 Unicode(UTF-8) 格式的印地语句子,我应该做些什么修改才能让它工作。

最佳答案

Pyparsing 的 printables 只处理 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,定义了新的命名空间类以提供 printablesalphasnumsalphanums 用于各种 Unicode 语言范围。

import pyparsing as pp
pp.Word(pp.pyparsing_unicode.printables)
pp.Word(pp.pyparsing_unicode.Devanagari.printables)
pp.Word(pp.pyparsing_unicode.देवनागरी.printables)

关于Python - pyparsing unicode字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2339386/

相关文章:

c++ - Boost.Regex 支持 icu/unicode

python - 如何计算大型文本语料库中双字母组的每次出现

python - Visual Studio Code新功能连接到Jupyter Notebook,是否也可以连接到JupyterHub?

Python:如何检查值列表是否包含在某个范围内

Python 按值降序对嵌套字典进行排序

用于表示过滤的漏斗的 Unicode 字符

c++ Unicode 字符串与易混淆字符的比较。例如(U 0054)应该是==(U03A4)等

nlp - Gensim word2vec中的most_similar和similar_by_vector之间的区别?

java - 斯坦福 Java NLP 选区标签缩写

python - 启用 3D 透明度 - pyopengl