-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
3,723 additions
and
146 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"directory": "app/static/js/vendor/bower" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ config.py | |
bower/ | ||
index.css | ||
config.py | ||
data/ | ||
db/ |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from flask_wtf import Form | ||
from wtforms import TextField | ||
from wtforms.validators import Required | ||
|
||
class CloneForm(Form): | ||
username = TextField('Twitter Username', validators=[Required()]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,76 @@ | ||
import datetime | ||
from app import db | ||
import random | ||
import re | ||
import config | ||
|
||
class Verb(db.Document): | ||
from textblob import TextBlob | ||
from app import db, twitter | ||
|
||
class Clone(db.Document): | ||
created_at = db.DateTimeField(default=datetime.datetime.now, required=True) | ||
body = db.StringField(required=True, unique=True) | ||
username = db.StringField(required=True, unique=True) | ||
patterns = db.ListField(db.StringField(), required=True) | ||
vocabulary = db.DictField(required=True) | ||
|
||
meta = { | ||
'allow_inheritance': True, | ||
'indexes': ['-created_at'], | ||
'indexes': ['-created_at', 'username'], | ||
'ordering': ['-created_at'] | ||
} | ||
|
||
class Noun(db.Document): | ||
created_at = db.DateTimeField(default=datetime.datetime.now, required=True) | ||
body = db.StringField(required=True, unique=True) | ||
def imprint(self, username): | ||
""" | ||
Generate a clone for a given Twitter user. | ||
""" | ||
self.username = username | ||
user_tweets = twitter.tweets(username, count=2000) | ||
|
||
meta = { | ||
'allow_inheritance': True, | ||
'indexes': ['-created_at'], | ||
'ordering': ['-created_at'] | ||
} | ||
# { POS tag: words } | ||
self.vocabulary = {} | ||
|
||
class Adjective(db.Document): | ||
created_at = db.DateTimeField(default=datetime.datetime.now, required=True) | ||
body = db.StringField(required=True, unique=True) | ||
# Mad-lib Tweet patterns. | ||
self.patterns = [] | ||
|
||
meta = { | ||
'allow_inheritance': True, | ||
'indexes': ['-created_at'], | ||
'ordering': ['-created_at'] | ||
} | ||
for tweet in user_tweets: | ||
text = tweet['body'] | ||
if '@' not in text: # Trying without any @mentions. | ||
|
||
class Adverb(db.Document): | ||
created_at = db.DateTimeField(default=datetime.datetime.now, required=True) | ||
body = db.StringField(required=True, unique=True) | ||
# Remove urls and @mentions | ||
text = re.sub(r'(?:\@|https?\://)\S+', '', text) | ||
pattern = text | ||
|
||
meta = { | ||
'allow_inheritance': True, | ||
'indexes': ['-created_at'], | ||
'ordering': ['-created_at'] | ||
} | ||
# Extract parts of speech. | ||
for t in TextBlob(text).pos_tags: | ||
token = t[0] | ||
tag = t[1] | ||
|
||
# Preserve hashtags. | ||
# Skip untagged tokens. | ||
# Skip tokens which are too short. | ||
if token[0] == '#' or tag == '-NONE-' or len(token) <= 2: | ||
continue | ||
|
||
if tag in config.ELIGIBLE_TAGS: | ||
# Build the pattern. | ||
pattern = pattern.replace(token, '{{{{ {0} }}}}'.format(tag)) | ||
|
||
# Add new tokens to the vocabulary. | ||
if tag not in self.vocabulary: | ||
self.vocabulary[tag] = [] | ||
self.vocabulary[tag].append(token.lower()) | ||
|
||
self.patterns.append(pattern) | ||
|
||
def speak(self): | ||
pattern = random.choice(self.patterns) | ||
tweet = pattern | ||
|
||
# Extract the tags to be replaced. | ||
p = re.compile(r'\{\{\s*([A-Za-z]+)\s*\}\}') | ||
tags = p.findall(pattern) | ||
|
||
# Replace the tags with selections from the vocabulary. | ||
for tag in tags: | ||
token = random.choice(self.vocabulary[tag]) | ||
tweet = re.sub(r'(\{\{\s*' + re.escape(tag) + r'\s*\}\})', token, tweet, 1) | ||
return tweet |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import clones |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from flask import Blueprint, request, render_template, redirect, url_for, flash | ||
from app.models import Clone | ||
from app.forms import CloneForm | ||
import re | ||
|
||
bp = Blueprint('clones', __name__, url_prefix = '/clones') | ||
|
||
@bp.route('/<string:username>') | ||
def clone(username): | ||
clone = Clone.objects.get_or_404(username=username) | ||
return render_template('clones/member.jade', clone=clone) | ||
|
||
@bp.route('/', methods=['GET', 'POST']) | ||
def clones(): | ||
form = CloneForm() | ||
if form.validate_on_submit(): | ||
username = form.username.data | ||
if not Clone.objects.get(username): | ||
clone = Clone() | ||
clone.imprint(username) | ||
clone.save() | ||
return redirect(url_for('clones.clone', username=username)) | ||
return render_template('clones/create.jade', form=form) | ||
|
||
|
||
@bp.app_template_filter() | ||
def highlight_pattern(pattern): | ||
""" | ||
A template filter for highlighting | ||
fill-in spots in patterns. | ||
Example usage (in `pyjade`):: | ||
div= pattern|highlight_pattern | ||
""" | ||
p = re.compile(r'\{\{\s*([A-Za-z]+)\s*\}\}') | ||
tags = p.findall(pattern) | ||
for tag in tags: | ||
pattern = re.sub(r'(\{\{\s*' + re.escape(tag) + r'\s*\}\})', '<span style="color:#ddd;">'+tag+'</span>', pattern, 1) | ||
return pattern |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// ========================================== CORE | ||
@import "normalize" | ||
@import "h5bp" | ||
@import "mixins" | ||
@import "type" | ||
@import "lists" | ||
@import "images" |
Oops, something went wrong.