2013-02-28 48 views
1

我有一個帶有這樣的行的文本文件(見下文),其中英文句子後面跟着西班牙語句子和由「{##}」分隔的等效轉換表。 (如果你知道它的輸出giza-pp如何將匹配字符串提取到defaultdict(set)? Python

您所要求的這部分會議期間就這一問題進行的未來 幾天中進行辯論。 {##} susseñoríashan solicitado un debate sobre el tema para lospróximosdías,en el curso de este períodode sesiones。 {##} 0-0 0-1 1-2 2-3 3-4 4-5 5-6 6-7 7-8 8-9 12-10 13-11 14-11 15-12 16-13 17-14 9-15 10-16 11-17 18-18 17-19 19-21 20-22

轉換表被理解爲這樣的,0-0 0-1指英文表示的第0字(即you )0級和1字在西班牙文(即sus señorías

匹配比方說,我想知道什麼是course在西班牙從句子翻譯,通常我會做這種方式:

from collections import defaultdict 
eng, spa, trans = x.split(" {##} ") 
tt = defaultdict(set) 
for s,t in [i.split("-") for i in trans.split(" ")]: 
    tt[s].add(t) 

query = 'course' 
for i in spa.split(" ")[tt[eng.index(query)]]: 
    print i 

有沒有簡單的方法來做到上述?可能是regexline.find()

一些嘗試,我必須這樣做是爲了覆蓋許多其他問題一樣MWE和失蹤的翻譯後:

def getTranslation(gizaline,query): 
    src, trg, trans = gizaline.split(" {##} ") 
    tt = defaultdict(set) 
    for s,t in [i.split("-") for i in trans.split(" ")]: 
     tt[int(s)].add(int(t)) 
    try: 
     query_translated =[trg.split(" ")[i] for i in tt[src.split(" ").index(query)]] 
    except ValueError: 
     for i in src.split(" "): 
      if "-"+query or query+"-" in i: 
       query = i 
       break 
     query_translated =[trg.split(" ")[i] for i in tt[src.split(" ").index(query)]] 

    if len(query_translated) > 0: 
     return ":".join(query_translated) 
    else: 
     return "#NULL" 
+0

看起來已經很簡單了。順便說一句,如果你使用set作爲defaultdict的工廠,你應該使用'add','append'用於列表。 – alecxe 2013-02-28 06:36:59

+0

感謝您關於錯誤的提示。可能有人可能有一個更簡單的方法或至少更快的方式=) – alvas 2013-02-28 10:07:33

+0

是否有任何特殊原因需要'defaultdict(set)'? – TyrantWave 2013-02-28 10:23:37

回答

1

這種方式工作得很好,但我會做到這一點略有不同,採用list代替的set所以我們可以正確排序的話(set將輸出單詞按字母順序,不完全是我們想要的):

文件:q_15125575.py

#-*- encoding: utf8 -*- 
from collections import defaultdict 

INPUT = """you have requested a debate on this subject in the course of the next few days , during this part-session . {##} sus señorías han solicitado un debate sobre el tema para los próximos días , en el curso de este período de sesiones . {##} 0-0 0-1 1-2 2-3 3-4 4-5 5-6 6-7 7-8 8-9 12-10 13-11 14-11 15-12 16-13 17-14 9-15 10-16 11-17 18-18 17-19 19-21 20-22""" 

if __name__ == "__main__": 
    english, spanish, trans = INPUT.split(" {##} ") 
    eng_words = english.split(' ') 
    spa_words = spanish.split(' ') 
    transtable = defaultdict(list) 
    for e, s in [i.split('-') for i in trans.split(' ')]: 
     transtable[eng_words[int(e)]].append(spa_words[int(s)]) 

    print(transtable['course']) 
    print(transtable['you']) 
    print(" ".join(transtable['course'])) 
    print(" ".join(transtable['you'])) 

輸出:
[ 'CURSO']
[ 'SUS', '本身\ XC3 \ xb1or \ XC3 \ xadas']
CURSO
SUSseñorías

它稍長代碼因爲我使用的是實際字詞而不是索引 - 但這可以讓您直接從transtable查找

但是,這兩種方法od和我的方法都在同一個問題上失敗:重複單詞。
print(" ".join(transtable['this'])
給出:
el este
它至少在這樣的詞語出現,雖然順序,所以這是可行的。想要第一次發生'this'翻譯?
transtable['this'][0]會給你第一個詞。

和使用代碼:

tt = defaultdict(set) 
for e, s in [i.split('-') for i in trans.split(' ')]: 
    tt[int(e)].add(int(s)) 

query = 'this' 
for i in tt[eng_words.index(query)]: 
    print i 

給出:
7

您的代碼將只打印第一出現一個詞的索引。

+0

編輯我的代碼,以便它輸出目標翻譯。 – alvas 2013-02-28 11:13:23

+0

雖然你的代碼在問題中不會起作用,但它有很多錯誤。例如,你將字符串添加到tt {}('tt [s] .add(t)'會給出'tt ['0'] ='1'')。然後當你得到索引時,你從原始字符串'eng'中抓取它,而不是分割的單詞。這個索引是一個數字,而不是字符串,所以它總是不會返回任何內容。如果將它改爲'tt [int(s)] .add(int(t))',接下來的問題是它會失敗,因爲'spanish.split(「」)'期望一個整數,而不是一組。最後(假設'eng'是分詞),'eng.index(query)'仍然只返回第一個結果。 – TyrantWave 2013-02-28 11:46:56

+0

感謝您注意到缺少的代碼,我將把我的完整代碼。 =) – alvas 2013-02-28 12:01:37