Created
December 26, 2011 00:19
-
-
Save izacus/1520111 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=UTF-8 | |
from matplotlib import cm | |
import numpy | |
import sunburnt | |
import matplotlib.pyplot as plt | |
QUERY_STRING = "drevesa" | |
AUTHOR_COLLAPSE_NUM = 100 | |
if __name__ == "__main__": | |
solr = sunburnt.SolrInterface("http://localhost:8983/solr/") | |
buckets = {} | |
names = set() | |
results = solr.query(QUERY_STRING, tip="zapis").facet_by("datum").paginate(rows=0).execute() | |
for facet in results.facet_counts.facet_fields["datum"]: | |
date, num = facet | |
other_result = solr.query(datum=date, tip="zapis").facet_by("govorec").paginate(rows=1).field_limit("datum").execute() | |
parsed_date = other_result[0]["datum"] | |
for govorec_facet in other_result.facet_counts.facet_fields["govorec"]: | |
govorec, count = govorec_facet | |
if not buckets.has_key(parsed_date): | |
buckets[parsed_date] = {} | |
if count < AUTHOR_COLLAPSE_NUM: | |
govorec = "Ostali" | |
if buckets[parsed_date].has_key(govorec): | |
count = buckets[parsed_date][govorec] + count | |
buckets[parsed_date].update({govorec:count}) | |
names.add(govorec) | |
for bucket in buckets: | |
for name in names: | |
buckets[bucket].setdefault(name, 0) | |
sorted_names = sorted(names) | |
sorted_names.remove("Ostali") | |
sorted_names.append("Ostali") | |
# Now do the plot | |
plt.figure(num=0, figsize=(60, 20), dpi=200) | |
int = numpy.arange(len(buckets)) | |
width = 0.5 | |
print "Plotting..." | |
comulative = numpy.zeros(len(buckets)) | |
count = 0 | |
bucket_keys = sorted(buckets.keys()) | |
for name in sorted_names: | |
values = numpy.array([buckets[x][name] for x in bucket_keys]) | |
plt.bar(int, | |
values, | |
bottom=comulative, | |
color=cm.hsv(1.*count/len(sorted_names)), | |
linewidth=0, | |
width=width, | |
label=name) | |
comulative += values | |
print "%s/%s" % (count, len(sorted_names)) | |
count += 1 | |
plt.title("Rezultati za iskanje: %s" % (QUERY_STRING,), fontsize=45) | |
plt.xticks(int+width / 2, ["%s.%s.%s" % (date.day, date.month, date.year) for date in bucket_keys], rotation='vertical') | |
plt.xlabel("Datum", fontsize=36) | |
plt.ylabel(u"Število replik", fontsize=36) | |
plt.legend() | |
print "Saving..." | |
plt.savefig("terms.png", format="png") | |
print "Done." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment