2017-05-22 6 views
0

Ich habe zwei Listen in eine Textdatei schreiben, wie unten dargestellt:zwei Listen zusammenfassen und in CONLL Datenformat

pos_tag(word_tokenize('This shoe is of Blue color.')) 

[('This', 'DT'), 
('shoe', 'NN'), 
('is', 'BEZ'), 
('of', 'IN'), 
('Blue', 'JJ-TL'), 
('color', 'NN'), 
('.', '.')] 

custom_tags('This shoe is of Blue color.') 
Out[125]: 
[('This', 'Other'), 
('shoe', 'Product'), 
('is', 'Other'), 
('of', 'Other'), 
('Blue', 'Color'), 
('color', 'Other'), 
('.', 'Other')] 

, die durch zwei Funktionen zurückgegeben werden. Jetzt möchte ich sie zu einem verschmelzen und schließlich in eine Textdatei im Format von CONLL zu schreiben, wie unten dargestellt:

LEICESTERSHIRE NNP I-NP I-ORG 
TAKE NNP I-NP O 
OVER IN I-PP O 
AT NNP I-NP O 
TOP NNP I-NP O 
AFTER NNP I-NP O 
INNINGS NNP I-NP O 
VICTORY NN I-NP O 

Nur in meinem Fall wird der Ausgang sein:

This DT Other 
shoe NN Product 
is BEZ Other 
of IN Other 
Blue JJ-TL Color 
Color NN Other 

Ich habe Verwendung versucht, dies zu tun:

list(zip(pos_tag(word_tokenize(sentence)),custom_tags(sentence))) 

Aber das gibt mir:

[(('This', 'DT'), ('This', 'Other')), 
(('footwear', 'NN'), ('footwear', 'Product')), 
(('is', 'BEZ'), ('is', 'Other')), 
(('of', 'IN'), ('of', 'Other')), 
(('blue', 'JJ'), ('blue', 'Color')), 
(('color', 'NN-HL'), ('color', 'Other'))] 

Kann mir bitte jemand helfen, die gewünschte Ausgabe zu erhalten, und ich muss auch jede Ausgabe in eine Textdatei mit einem Trennzeichen zwischen den Zeilen schreiben.

Antwort

0

Mit einem Verständnis

l1=[('This', 'DT'), ('shoe', 'NN'), ('is', 'BEZ'), ('of', 'IN'), ('Blue', 'JJ-TL'), ('color', 'NN'), ('.', '.')] 
l2=[('This', 'Other'), ('shoe', 'Product'), ('is', 'Other'), ('of', 'Other'), ('Blue', 'Color'), ('color', 'Other'), ('.', 'Other')] 

l3=[(x[0][0],x[0][1],x[1][1]) for x in zip(l1, l2)] 
0

Warum versuchen Sie nicht append zu verwenden, obwohl es nicht der eleganteste Weg ist?

A = 

    [('This', 'DT'), 
    ('shoe', 'NN'), 
    ('is', 'BEZ'), 
    ('of', 'IN'), 
    ('Blue', 'JJ-TL'), 
    ('color', 'NN'), 
    ('.', '.')] 

    B = 
    [('This', 'Other'), 
    ('shoe', 'Product'), 
    ('is', 'Other'), 
    ('of', 'Other'), 
    ('Blue', 'Color'), 
    ('color', 'Other'), 
    ('.', 'Other')] 

    Title = 
    [('This',), 
    ('shoe',), 
    ('is',), 
    ('of',), 
    ('Blue',), 
    ('color',), 
    ('.',)] 

    for j, item in enumerate(A): 
     Title[j].append(item) 
     Title[j].append(B[j][1]) 

    for tuple in Title: 
     line = '{0[0]} {0[1]} {0[2]}'.format(tuple) 

Für Write-Dateien verwenden open() Zum Beispiel

f = open('This/is/your/destination/file.txt', 'w') 
    # Here you do something 

    f.write() 
    f.close() 
Verwandte Themen