Matplotlib
Verfasst: Dienstag 31. Oktober 2017, 22:55
Hallo zusammen,
kann mir jemand helfen? Ich versuche zu verstehen, wie man mit Hilfe von Matplotlib sprachliche Daten abbilden kann. Ich benutze das Buch Natural Language Processing with Python http://www.nltk.org/book/ch04.html und dort ist im Kapitel 4 ein Beispiel benannt. Es wird nämlich die Sequenz/das Vorkommen von Modalverben aus einem Sprachkorpus ausgewertet und graphisch abgebildet.
Die Datei sieht nämlich so aus:
from numpy import arange
from matplotlib import pyplot
colors = 'rgbcmyk' # red, green, blue, cyan, magenta, yellow, black
def bar_chart(categories, words, counts):
"Plot a bar chart showing counts for each word by category"
ind = arange(len(words))
width = 1 / (len(categories) + 1)
bar_groups = []
for c in range(len(categories)):
bars = pyplot.bar(ind+c*width, counts[categories[c]], width,
color=colors[c % len(colors)])
bar_groups.append(bars)
pyplot.xticks(ind+width, words)
pyplot.legend([b[0] for b in bar_groups], categories, loc='upper left')
pyplot.ylabel('Frequency')
pyplot.title('Frequency of Six Modal Verbs by Genre')
pyplot.show()
und angeblich soll man im IDLE folgendes eingeben:
>>> genres = ['news', 'religion', 'hobbies', 'government', 'adventure']
>>> modals = ['can', 'could', 'may', 'might', 'must', 'will']
>>> cfdist = nltk.ConditionalFreqDist(
... (genre, word)
... for genre in genres
... for word in nltk.corpus.brown.words(categories=genre)
... if word in modals)
...
>>> counts = {}
>>> for genre in genres:
... counts[genre] = [cfdist[genre][word] for word in modals]
>>> bar_chart(genres, modals, counts)
Kennt sich jemand damit aus? Bei mir funktioniert es nicht, egal was ich versuche.
kann mir jemand helfen? Ich versuche zu verstehen, wie man mit Hilfe von Matplotlib sprachliche Daten abbilden kann. Ich benutze das Buch Natural Language Processing with Python http://www.nltk.org/book/ch04.html und dort ist im Kapitel 4 ein Beispiel benannt. Es wird nämlich die Sequenz/das Vorkommen von Modalverben aus einem Sprachkorpus ausgewertet und graphisch abgebildet.
Die Datei sieht nämlich so aus:
from numpy import arange
from matplotlib import pyplot
colors = 'rgbcmyk' # red, green, blue, cyan, magenta, yellow, black
def bar_chart(categories, words, counts):
"Plot a bar chart showing counts for each word by category"
ind = arange(len(words))
width = 1 / (len(categories) + 1)
bar_groups = []
for c in range(len(categories)):
bars = pyplot.bar(ind+c*width, counts[categories[c]], width,
color=colors[c % len(colors)])
bar_groups.append(bars)
pyplot.xticks(ind+width, words)
pyplot.legend([b[0] for b in bar_groups], categories, loc='upper left')
pyplot.ylabel('Frequency')
pyplot.title('Frequency of Six Modal Verbs by Genre')
pyplot.show()
und angeblich soll man im IDLE folgendes eingeben:
>>> genres = ['news', 'religion', 'hobbies', 'government', 'adventure']
>>> modals = ['can', 'could', 'may', 'might', 'must', 'will']
>>> cfdist = nltk.ConditionalFreqDist(
... (genre, word)
... for genre in genres
... for word in nltk.corpus.brown.words(categories=genre)
... if word in modals)
...
>>> counts = {}
>>> for genre in genres:
... counts[genre] = [cfdist[genre][word] for word in modals]
>>> bar_chart(genres, modals, counts)
Kennt sich jemand damit aus? Bei mir funktioniert es nicht, egal was ich versuche.