Anlässlich einer Programmierhausaufgabe, die ich schon zu 60% erledigt habe, muss ich mich mit dem Programmcode mal an Euch wenden.
Es geht darum, dass der Nutzer ein Wort angibt, das in einem Korpus, das als Datei reingeladen wird, gesucht wird. Dieses Wort, auch Token genannt, soll zusammen mit den fünf vorhergehenden und den fünf folgenden Wörtern (=Kollokation) ausgegeben werden.
Kommt das Wort aber häufiger vor, so soll das nächste Auftreten des Wortes im Korpus unter dem vorherigen mit einem Zeilenumbruch dazwischen ausgegeben werden.
Ich habe das dementsprechend programmiert und meine While-Schleife scheint die weiteren Befehle auch auszuführen (bis die Anzahl Zeilenumbrüche im abschließenden String, der ausgegeben wird, gleich der Vorkommen des gesuchten Wortes ist), aber wenn ein Wort mehrmals auftritt, wird es nicht an den String angefügt, der String enthält trotzdem nur die allererste Kollokation. Warum?
Das hier ist mein Programmcode und einen Ausschnitt aus der Textdatei, die das Korpus darstellt, habe ich angehängt (Wenn man für das zu suchende Wort 'supervision' eingibt, hat man nur 3 Treffer, daher ist das ganz passend).
Code: Alles auswählen
# coding=utf-8
from __future__ import division
import codecs
Korpus_original = codecs.open("poe.txt","r").read().split() #eventuell nach "poe.txt" noch ,"utf-8", anfügen
Korpus=Korpus_original[:]
suchwort=raw_input(u"Welches Wort soll im Korpus gesucht werden?\n")
Kollokationen=""
Fuenfvor=[]
Fuenfhinter=[]
list_suchwoerter= []
while (Kollokationen.count("\n"))<=Korpus.count(suchwort):
#print "Anzahl Suchwörter im Korpus: ",Korpus.count(suchwort)
# print "Nummer von Zeilenumbrüchen in Kollokationen",Kollokationen.count("\n")
suchwortPos=Korpus.index(suchwort.lower())
list_suchwoerter.append(suchwortPos)
print list_suchwoerter
#print Korpus[suchwortPos-5:suchwortPos]
for i in range (suchwortPos-5,suchwortPos):
if Korpus[i] not in Fuenfvor and len(Fuenfvor)<5:
Fuenfvor.append(Korpus[i])
# print "i", Korpus[i], Fuenfvor
for i in range(suchwortPos,suchwortPos+5):
if Korpus[i] not in Fuenfhinter and len(Fuenfhinter)<6:
Fuenfhinter.append(Korpus[i])
# print Fuenfhinter
if suchwortPos in list_suchwoerter:
del Korpus[suchwortPos]
#print "EIN SUCHWORT GELÖSCHT"
Kollokationen=" ".join(Fuenfvor)+" "+suchwort+" "+" ".join(Fuenfhinter[1:])+"\n"
#print Fuenfvor, suchwort, Fuenfhinter
#Kollokationen=" ".join(Kollokationen)
print Kollokationen
Vielen Dank im Voraus
Poe.txt (Das Korpus):
In the inmost recesses of this coppice not far from the eastern
or more remote end of the island Legrand had built himself a small
hut which he occupied when I first by mere accident made his
acquaintance This soon ripened into friendship for there was much
in the recluse to excite interest and esteem I found him well
educated with unusual powers of mind but infected with misanthropy
and subject to perverse moods of alternate enthusiasm and melancholy
He had with him many books but rarely employed them His chief
amusements were gunning and fishing or sauntering along the beach
and through the myrtles in quest of shells or entomological
specimens his collection of the latter might have been envied by a
Swammerdamm In these excursions he was usually accompanied by an old
negro called Jupiter who had been manumitted before the reverses of
the family but who could be induced neither by threats nor by
promises to abandon what he considered his right of attendance upon
the footsteps of his young Massa Will It is not improbable that
the relatives of Legrand conceiving him to be somewhat unsettled in
intellect had contrived to instil this obstinacy into Jupiter with
a view to the supervision and guardianship of the wanderer
The winters in the latitude of Sullivan 's Island are seldom very
severe and in the fall of the year it is a rare event indeed when a
fire is considered necessary About the middle of October 18- there
occurred however a day of remarkable chilliness Just before sunset
I scrambled my way through the evergreens to the hut of my friend
whom I had not visited for several weeks my residence being at
that time in Charleston a distance of nine miles from the Island
while the facilities of passage and re-passage were very far behind
those of the present day Upon reaching the hut I rapped as was my
custom and getting no reply sought for the key where I knew it was
secreted unlocked the door and went in A fine fire was blazing upon
the hearth It was a novelty and by no means an ungrateful one I
threw off an overcoat took an arm-chair by the crackling
logs and awaited patiently the arrival of my hosts