2016-01-03 11 views
5

Ben 2 dosyaları var:Dizgeciklerin bir listesini gerçekleştirmekten ziyade, jeneratörler tokenizasyon için nasıl çalışır?

hyp.txt

It is a guide to action which ensures that the military always obeys the commands of the party 
he read the book because he was interested in world history 

ref.txt

It is a guide to action that ensures that the military will forever heed Party commands 
he was interested in world history because he read the book 

Ve satırları karşılaştırmak için bazı hesaplamalar bir işlevi var metin, örneğin hip.txt satır 1, ref.txt satır 1 ile.

def scorer(list_of_tokenized_hyp, list_of_tokenized_ref): 
    """ 
    :type list_of_tokenized_hyp: iter(iter(str)) 
    :type list_of_tokenized_ref: iter(iter(str)) 
    """ 
    for hypline, refline in zip(list_of_tokenized_hyp, list_of_tokenized_ref): 
     # do something with the iter(str) 
    return score 

Ve bu işlev değiştirilemez. Bununla birlikte, fonksiyona beslediğim şeyi manipüle edebilirim.

with open('hyp.txt', 'r') as hypfin, open('ref.txt', 'r') as reffin: 
    hyp = [line.split() for line in hypfin] 
    ref = [line.split() for line in reffin] 
    scorer(hypfin, reffin) 

Ama böyle yaparak ben scorer() içine beslemeden önce belleğe tüm dosya ve bölünmüş dize yükleyen: Yani şu anda böyle işlevi dosyayı besliyorum.

scorer() dosyaları satır satır işliyor Bil ki, scorer() fonksiyonunu değiştirmeden fonksiyonu beslemeden önce bölünmüş dize hayata değil bir yolu var mı?

Bunun yerine bir tür jeneratörde beslemenin bir yolu var mı?

with open('hyp.txt', 'r') as hypfin, open('ref1.txt', 'r') as ref1fin, open('ref2.txt', 'r') as ref2fin: 
    hyp = (h.split() for h in hypline) 
    ref = (r.split() for r in hypline) 
    scorer(hypfin, reffin) 

ama h.split() gerçekleşmiş olup olmadığını emin değilim:

Bu denedim. Eğer maddeleştirilmişse, neden? Değilse neden? Ben scorer() işlevini değiştirmek olsaydı

, o zaman ben kolayca for sonra bu satırı ekleyebilirsiniz:

def scorer(list_of_tokenized_hyp, list_of_tokenized_ref): 
    for hypline, refline in zip(list_of_tokenized_hyp, list_of_tokenized_ref): 
     hypline = hypline.split() 
     refline = refline.split() 
     # do something with the iter(str) 
    return score 

Ama bu işlevi değiştiremezsiniz çünkü bu, benim durumumda mümkün değildir.

+2

Python 2 veya Python 3 kullanıyor musunuz? Python 2'de 'zip' bir liste döndürür, bu yüzden 'skorer'de' zip 'çağrısı,' scorer'ı çağırmadan önce zeki olmaya çalışsanız bile bir liste yapar. – BrenBarn

+0

Python 3 kullanıyorum – alvas

cevap

2

Kişisel jeneratör ifadeleri bunlar tek seferde Bölünmüş listeleri oluşturmak için dosyanın tamamını okumaz yani sizin istediğiniz şekilde davranmaya (Python 2'de itertools.izip() ile değiştirin).

Sen str.split() bir günlük versiyonunu ikame edilmesiyle oluyor ne kadar bazı fikir edinebilirsiniz

: Eğer hyp ve ref girişi tüketmek yok jeneratörler gerçekten olduğunu görebilirsiniz my_split() çıkışında itibaren

def my_split(s): 
    print('my_split(): {!r}'.format(s)) 
    return s.split() 

>>> hypfin = open('hyp.txt', 'r') 
>>> reffin = open('ref.txt', 'r') 
>>> hyp = (my_split(h) for h in hypfin) # N.B. my_split() not called here 
>>> hyp 
<generator object <genexpr> at 0x7fa89ad16b40> 
>>> ref = (my_split(r) for r in reffin) # N.B. my_split() not called here 
>>> ref 
<generator object <genexpr> at 0x7fa89ad16bd0> 

>>> z = zip(hyp, ref) # N.B. my_split() not called here 
>>> z 
<zip object at 0x7fa89ad15cc8> 

>>> hypline, refline = next(z) 
my_split(): 'It is a guide to action which ensures that the military always obeys the commands of the party\n' 
my_split(): 'It is a guide to action that ensures that the military will forever heed Party commands\n' 
>>> hypline, refline = next(z) 
my_split(): 'he read the book because he was interested in world history\n' 
my_split(): 'he was interested in world history because he read the book\n' 
>>> hypline, refline = next(z) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
StopIteration 

gerekli olana kadar.z, erişilinceye kadar hiçbir girdi tüketmeyen bir zip nesnesidir. for döngüsü, her bir yineleme için her dosyadan yalnızca bir giriş satırının tükendiğini göstermek üzere next() ile simüle edilir.

3

Evet, örnek, iki jeneratörü

with open('hyp.txt', 'r') as hypfin, open('ref1.txt', 'r') as reffin: 
    hyp = (h.split() for h in hypfin) 
    ref = (r.split() for r in reffin) 
    scorer(hyp, ref) 

ve split

tanımlar ve bir sonraki satırın karşılık gelen okuma, her biri için döngü-yineleme için yapılır. Python 3 en zip() ile kombine

İlgili konular