107 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| from eca import *
 | |
| from eca.generators import start_offline_tweets
 | |
| 
 | |
| import random
 | |
| import re
 | |
| 
 | |
| ## You might have to update the root path to point to the correct path
 | |
| ## (by default, it points to <rules>_static)
 | |
| root_content_path = 'dashboard_static'
 | |
| 
 | |
| 
 | |
| # binds the 'setup' function as the action for the 'init' event
 | |
| # the action will be called with the context and the event
 | |
| @event('init')
 | |
| def setup(ctx, e):
 | |
|     start_offline_tweets('sports1.txt', event_name="tweet", time_factor=0.1)
 | |
|     ctx.count = 0
 | |
|     # fire('sample', {'previous': 0.0})
 | |
| 
 | |
| 
 | |
| # define a normal Python function
 | |
| def clip(lower, value, upper):
 | |
|     return max(lower, min(value, upper))
 | |
| 
 | |
| 
 | |
| # simple word splitter
 | |
| pattern = re.compile('\W+')
 | |
| 
 | |
| # sample stopword list, needs to be much more sophisticated
 | |
| stopwords = ["volleyball", "football", "basketball", "baseball", "tennis", "cricket", "soccer", "rugby"]
 | |
| 
 | |
| 
 | |
| def words(message):
 | |
|     result = pattern.split(message)
 | |
|     result = map(lambda w: w.lower(), result)
 | |
|     result = filter(lambda w: w in stopwords, result)
 | |
|     result = filter(lambda w: len(w) > 2, result)
 | |
|     return result
 | |
| 
 | |
| 
 | |
| @event('tweet')
 | |
| def generate_tweet(ctx, e):
 | |
|     tweet = dict(e.data)
 | |
|     try:
 | |
|         try:
 | |
|             tweet.pop("id_str")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("display_text_range")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("in_reply_to_status_id")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("in_reply_to_status_id_str")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("in_reply_to_user_id")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("in_reply_to_user_id_str")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("in_reply_to_screen_name")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("geo")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("coordinates")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("place")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("contributors")
 | |
|         except KeyError:
 | |
|             pass
 | |
|         try:
 | |
|             tweet.pop("is_quote_status")
 | |
|         except KeyError:
 | |
|             pass
 | |
|     except:
 | |
|         pass
 | |
|     # base sample on previous one
 | |
|     # sample = clip(-100, e.data['previous'] + random.uniform(+5.0, -5.0), 100)
 | |
|     # emit to outside world
 | |
|     emit('tweet', tweet)
 | |
|     for w in words(tweet['text']):
 | |
|         emit('pie', {
 | |
|             'action': 'add',
 | |
|             'value': (str(w), 1)
 | |
|         })
 | |
|     # # chain event
 | |
|     # fire('tweet', {'previous': tweet}, delay=0.05)
 | |
| 
 |