def todays_sentiment(): """Get a mean of sentiment for all of today's articles.""" t = time.time() sentiment = db.session.query(db.func.avg(Article.sent)).filter(Article.date==date.today()).all() timing(t, "article.todays_sentiment") if sentiment[0][0]: return round(sentiment[0][0]*100, 2) return 0
def rawwordcloud(text, scale, width=150, height=80, POS=POS): t = time.time() tokenized = nltk.word_tokenize(text) tokenized = nltk.pos_tag(tokenized) text = [] for token in tokenized: if token[1] in POS: text.append(token[0]) text = ' '.join(text) t = timing(t, "generating texts") # Make wordcloud wordcloud = WordCloud( width=width, height=height, background_color='white', stopwords=stopwords, scale=scale)\ .generate(text).to_array() t = timing(t, "generating wordcloud") # Make image img = Image.fromarray(wordcloud.astype('uint8')) timing(t, "generating image") return img
def status(): t = time.time() agencies = Agency.query.order_by(Agency.name).all() ids = [a.id for a in agencies] articles = Article.query.filter(Article.date == date.today(), Article.agency_id.in_(ids)).all() t = timing(t, "getting articles") for article in articles: if not hasattr(article.agency, 'todays_articles_speed'): article.agency.todays_articles_speed = [] article.agency.todays_articles_speed.append(article) t = timing(t, "hanging articles") for agency in agencies: try: agency.todays_sentiment_speed = speedround( [a.sentiment for a in agency.todays_articles_speed]) agency.todays_neutrality_speed = speedround( [a.neutrality for a in agency.todays_articles_speed]) except: agency.todays_sentiment_speed = 0.0 agency.todays_neutrality_speed = 0.0 agency.todays_articles_speed = [] t = timing(t, "Calculating todays stats") return render_template('status.html', agencies=agencies, title='Status')
def todays_count(self): """Count of today's articles for the agency""" t = time.time() count = self.articles.filter(Article.date==date.today()).count() timing(t, "agency.todays_count") return count
def todays_articles(self): t = time.time() articles = self.articles.filter(Article.date==date.today())\ .order_by(Article.sent.desc()) timing(t, "agency.todays_articles") return articles
def index(): """Generates an index of today's articles, sorted by sentiment, colored by sentiment. """ t = time.time() sort = request.args.get('sort', default='Name') sorts = { 'Name': Agency.query.order_by(Agency.name), 'Articles': (Agency.query.join(Article).filter( Article.date == date.today()).group_by(Agency.id).order_by( db.func.count(Article.id).desc())), 'Cumulative Sentiment': Agency.query.order_by(Agency.cum_sent.desc()), 'Cumulative Neutrality': Agency.query.order_by(Agency.cum_neut.desc()), } page = request.args.get('page', default=1) try: page = int(page) except: abort(404) PER_PAGE = 3 pages = list(range(1, math.ceil(Agency.query.count() / PER_PAGE) + 1)) t = timing(t, "getting sorts and pages") try: with open('/status.json', 'rt') as fin: jobs = json.load(fin) except Exception as e: app.logger.info(e) jobs = {'running': []} t = timing(t, "getting jobs") try: agencies = sorts[sort].paginate(page, PER_PAGE).items except KeyError: abort(404) t = timing(t, "getting agencies") ids = [a.id for a in agencies] articles = Article.query.filter(Article.date == date.today(), Article.agency_id.in_(ids)).all() t = timing(t, "getting articles") for article in articles: if not hasattr(article.agency, 'todays_articles_speed'): article.agency.todays_articles_speed = [] article.agency.todays_articles_speed.append(article) t = timing(t, "hanging articles") for agency in agencies: try: agency.todays_sentiment_speed = speedround( [a.sentiment for a in agency.todays_articles_speed]) agency.todays_neutrality_speed = speedround( [a.neutrality for a in agency.todays_articles_speed]) except: agency.todays_sentiment_speed = 0.0 agency.todays_neutrality_speed = 0.0 agency.todays_articles_speed = [] t = timing(t, "calculating stats") html = render_template( 'index.html', count=Article.query.filter(Article.date == date.today()).count(), dbcount=Article.query.count(), agencies=agencies, sorts=sorts, sort=sort, pages=pages, page=page, overall=Article.todays_sentiment(), running=jobs['running'], active='index') timing(t, "rendering template") return html