ich hätte da mal eine frage bezüglich der funktion lexicon(). wir möchten ein programm so modifizieren, dass es anstelle von worttokens/worttypes zeichentokens/zeichentypes in ein lexikon einliest. wir sind bis hierher gekommen:
Code: Alles auswählen
# -*- coding: cp1252 -*-
in_file = open("glocke_test.txt", 'r')
out_file = open("output_glocke_test.txt", 'w')
number_of_signs = 0
lexicon = {}
zl = []
a = 0
# Read the input file, computing the lexicon
# as each line is read in
for line in in_file:
line = line.split()
for item in line:
zl_token = list(item)
zl = zl + zl_token
sign = item in zl
for item in zl:
a += 1
number_of_signs += 1
sign_number = lexicon.get(zl[a], 0)
lexicon[zl[a]] = sign_number + 1
print zl
in_file.close()
# Print the lexicon to the output file, sorted
# according to reversed frequency (from highest
# to lowest)
signs = lexicon.items()
print signs
signs.sort(key=lambda x:x[1], reverse=True)
for item in signs:
print>>out_file, token[0], token[1]
out_file.close()
print "Anzahl Zeichen: ", number_of_signs
print "Anzahl Zeichen-Types:", lexicon
vielen dank für eure hilfe,
gruß, sebastian