<s> from __future__ import print_function <EOL> from typing import Any , Dict , List <EOL> import zulip <EOL> import re <EOL> import collections <EOL> def get_recent_messages ( client , narrow , count = <NUM_LIT:100> ) : <EOL> narrow = [ word . split ( '<STR_LIT::>' ) for word in narrow . split ( ) ] <EOL> req = { <EOL> '<STR_LIT>' : narrow , <EOL> '<STR_LIT>' : count , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : False <EOL> } <EOL> old_messages = client . do_api_query ( req , zulip . API_VERSTRING + '<STR_LIT>' , method = '<STR_LIT:GET>' ) <EOL> if '<STR_LIT>' not in old_messages : <EOL> return [ ] <EOL> return old_messages [ '<STR_LIT>' ] <EOL> def get_words ( content ) : <EOL> regex = "<STR_LIT>" <EOL> words = re . findall ( regex , content , re . M ) <EOL> words = [ w . lower ( ) for w in words ] <EOL> return words <EOL> def analyze_messages ( msgs , word_count , email_count ) : <EOL> for msg in msgs : <EOL> if False : <EOL> if '<STR_LIT>' in msg [ '<STR_LIT:content>' ] : <EOL> name = msg [ '<STR_LIT>' ] . split ( ) [ <NUM_LIT:0> ] <EOL> print ( '<STR_LIT>' , name ) <EOL> m = re . search ( '<STR_LIT>' , msg [ '<STR_LIT:content>' ] , re . M | re . S ) <EOL> if m : <EOL> ticket , email , req = m . groups ( ) <EOL> words = get_words ( req ) <EOL> for word in words : <EOL> word_count [ word ] += <NUM_LIT:1> <EOL> email_count [ email ] += <NUM_LIT:1> <EOL> if False : <EOL> print ( ) <EOL> for k , v in msg . items ( ) : <EOL> print ( '<STR_LIT>' % ( k , v ) ) <EOL> def generate_support_stats ( ) : <EOL> client = zulip . Client ( ) <EOL> narrow = '<STR_LIT>' <EOL> count = <NUM_LIT> <EOL> msgs = get_recent_messages ( client , narrow , count ) <EOL> msgs_by_topic = collections . defaultdict ( list ) <EOL> for msg in msgs : <EOL> topic = msg [ '<STR_LIT>' ] <EOL> msgs_by_topic [ topic ] . append ( msg ) <EOL> word_count = collections . defaultdict ( int ) <EOL> email_count = collections . defaultdict ( int ) <EOL> if False : <EOL> for topic in msgs_by_topic : <EOL> msgs = msgs_by_topic [ topic ] <EOL> analyze_messages ( msgs , word_count , email_count ) <EOL> if True : <EOL> words = [ w for w in word_count . keys ( ) if word_count [ w ] >= <NUM_LIT:10> and len ( w ) >= <NUM_LIT:5> ] <EOL> words = sorted ( words , key = lambda w : word_count [ w ] , reverse = True ) <EOL> for word in words : <EOL> print ( word , word_count [ word ] ) <EOL> if False : <EOL> emails = sorted ( list ( email_count . keys ( ) ) , <EOL> key = lambda w : email_count [ w ] , reverse = True ) <EOL> for email in emails : <EOL> print ( email , email_count [ email ] ) <EOL> generate_support_stats ( ) </s>
