Skip to content

Instantly share code, notes, and snippets.

@AlexDel
Created January 15, 2012 15:46
Show Gist options
  • Save AlexDel/1616220 to your computer and use it in GitHub Desktop.
Save AlexDel/1616220 to your computer and use it in GitHub Desktop.
NLTk Ex 2.23.b Строим график по Закону Ципфа для случайной последовательности
import nltk
import matplotlib.pyplot as plt
import random
random_string = '' #инициализируем переменную
#превращаем ее в строку случайных символов
while len(random_string) < 9964284: #кол-во букв в брауновском корпусе
random_string += random.choice("abcdefgklmnopqrstuvwxyz ")
words = random_string.split() #разбиваем строку в список по пробелам
def zipf_law(words):
freq_dist = nltk.FreqDist(words)#считаем кол-во вхождений
xaxis = []
yaxis = []
for offset, word in enumerate(freq_dist.keys()):
xaxis.append(offset)
yaxis.append(freq_dist[word])
#сшиваем два списка
zipf_dist = (xaxis,yaxis)
return zipf_dist
zipf_dist = zipf_law(words)
#строим график
plt.plot(zipf_dist[0],zipf_dist[1])
plt.show()
#для большей наглядности лучше сократить кол-во искомых слов до 1000 первых
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment