GeometrizeTwitterBot  1.0
Python Twitter bot for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables
Functions | Variables
tweet_parser Namespace Reference

This module contains code that reads tweet text and figures out how the bot should do in response. More...

Functions

def _represents_int
 Returns true if the given string represents an integer value. More...
 
def _clamp
 Clamps the given number within the range smallest, largest. More...
 
def _add_shape_type_for_keywords
 Adds the given items to the given dictionary, mapping each keyword to the given shape name. More...
 
def _make_shape_keyword_dictionary
 Creates a dictionary mapping shape keywords to canonical shape names used by ChaiScript scripts Provides a convenient way to map words in tweets like "rects" to the shape type "RECTANGLE". More...
 
def _max_quantity_for_shape_type
 Gets the maximum number of shapes allowed for the given shape type This to to set a sensible limit on the length of time the bot will spend on a single image. More...
 
def _plural_name_for_shape_type
 Gets a human-readable name for the given shape type. More...
 
def _make_loop_body
 Helper for constructing a ChaiScript script for geometrizing shapes. More...
 
def _make_for_loop
 Helper for constructing a ChaiScript script for geometrizing shapes. More...
 
def _make_shape_code
 Helper for constructing a ChaiScript script for geometrizing shapes. More...
 
def make_shape_quantity_dictionary
 Parses a tweet message and returns a dictionary of the shape types and quantities that were requested. More...
 
def make_code_for_shape_tweet
 Parses the given tweet, returning the ChaiScript code for Geometrize to run based on the contents of the tweet. More...
 
def make_message_for_shape_dictionary
 Creates a message for the given shape dictionary, suitable for tweeting along with the result of running the script. More...
 

Variables

list _rect_keywords = [ "rect", "rects", "rectangle", "rectangles" ]
 
list _rotated_rect_keywords = [ "rotated_rect", "rotated_rects", "rotated_rectangle", "rotated_rectangles" ]
 
list _triangle_keywords = [ "tri", "tris", "triangle", "triangles" ]
 
list _ellipse_keywords = [ "ellipse", "ellipses" ]
 
list _rotated_ellipse_keywords = [ "rotated_ellipse", "rotated_ellipses" ]
 
list _circle_keywords = [ "circ", "circs", "circle", "circles" ]
 
list _line_keywords = [ "line", "lines" ]
 
list _quadratic_bezier_keywords = [ "bezier", "beziers", "quadratic_bezier", "quadratic_beziers" ]
 
list _polyline_keywords = [ "polyline", "polylines" ]
 

Detailed Description

This module contains code that reads tweet text and figures out how the bot should do in response.

Function Documentation

def tweet_parser._add_shape_type_for_keywords (   dict,
  shape_keywords,
  shape_name 
)
private

Adds the given items to the given dictionary, mapping each keyword to the given shape name.

26 
27 def _add_shape_type_for_keywords(dict, shape_keywords, shape_name):
28  for shape_keyword in shape_keywords:
29  dict[shape_keyword] = shape_name
def _add_shape_type_for_keywords
Adds the given items to the given dictionary, mapping each keyword to the given shape name...
Definition: tweet_parser.py:26

Here is the caller graph for this function:

def tweet_parser._clamp (   n,
  smallest,
  largest 
)
private

Clamps the given number within the range smallest, largest.

22 
23 def _clamp(n, smallest, largest):
24  return max(smallest, min(n, largest))
def _clamp
Clamps the given number within the range smallest, largest.
Definition: tweet_parser.py:22
def tweet_parser._make_for_loop (   step_count,
  shape_type 
)
private

Helper for constructing a ChaiScript script for geometrizing shapes.

81 
82 def _make_for_loop(step_count, shape_type):
83  return "for(var i = 0; i < " + str(step_count) + "; ++i) { " + _make_loop_body(shape_type) + " }";
def _make_loop_body
Helper for constructing a ChaiScript script for geometrizing shapes.
Definition: tweet_parser.py:77
def _make_for_loop
Helper for constructing a ChaiScript script for geometrizing shapes.
Definition: tweet_parser.py:81

Here is the call graph for this function:

Here is the caller graph for this function:

def tweet_parser._make_loop_body (   shape_type)
private

Helper for constructing a ChaiScript script for geometrizing shapes.

77 
78 def _make_loop_body(shape_type):
79  return "var prefs = task.getPreferences(); prefs.setShapeTypes(" + shape_type + "); task.setPreferences(prefs); task.stepModel();"
def _make_loop_body
Helper for constructing a ChaiScript script for geometrizing shapes.
Definition: tweet_parser.py:77

Here is the caller graph for this function:

def tweet_parser._make_shape_code (   dict)
private

Helper for constructing a ChaiScript script for geometrizing shapes.

85 
86 def _make_shape_code(dict):
87  code = ""
88  for key, value in dict.items():
89  code += _make_for_loop(value, key)
90  return code
def _make_shape_code
Helper for constructing a ChaiScript script for geometrizing shapes.
Definition: tweet_parser.py:85
def _make_for_loop
Helper for constructing a ChaiScript script for geometrizing shapes.
Definition: tweet_parser.py:81

Here is the call graph for this function:

Here is the caller graph for this function:

def tweet_parser._make_shape_keyword_dictionary ( )
private

Creates a dictionary mapping shape keywords to canonical shape names used by ChaiScript scripts Provides a convenient way to map words in tweets like "rects" to the shape type "RECTANGLE".

32 
34  dict = {}
35  _add_shape_type_for_keywords(dict, _rect_keywords, "RECTANGLE")
36  _add_shape_type_for_keywords(dict, _rotated_rect_keywords, "ROTATED_RECTANGLE")
37  _add_shape_type_for_keywords(dict, _triangle_keywords, "TRIANGLE")
38  _add_shape_type_for_keywords(dict, _ellipse_keywords, "ELLIPSE")
39  _add_shape_type_for_keywords(dict, _rotated_ellipse_keywords, "ROTATED_ELLIPSE")
40  _add_shape_type_for_keywords(dict, _circle_keywords, "CIRCLE")
41  _add_shape_type_for_keywords(dict, _line_keywords, "LINE")
42  _add_shape_type_for_keywords(dict, _quadratic_bezier_keywords, "QUADRATIC_BEZIER")
43  _add_shape_type_for_keywords(dict, _polyline_keywords, "POLYLINE")
44  return dict
def _add_shape_type_for_keywords
Adds the given items to the given dictionary, mapping each keyword to the given shape name...
Definition: tweet_parser.py:26
def _make_shape_keyword_dictionary
Creates a dictionary mapping shape keywords to canonical shape names used by ChaiScript scripts Provi...
Definition: tweet_parser.py:32

Here is the call graph for this function:

Here is the caller graph for this function:

def tweet_parser._max_quantity_for_shape_type (   shape_type)
private

Gets the maximum number of shapes allowed for the given shape type This to to set a sensible limit on the length of time the bot will spend on a single image.

47 
48 def _max_quantity_for_shape_type(shape_type):
49  dict = {}
50  dict["RECTANGLE"] = 500
51  dict["ROTATED_RECTANGLE"] = 750
52  dict["TRIANGLE"] = 750
53  dict["ELLIPSE"] = 750
54  dict["ROTATED_ELLIPSE"] = 750
55  dict["CIRCLE"] = 750
56  dict["LINE"] = 4000
57  dict["QUADRATIC_BEZIER"] = 4000
58  dict["POLYLINE"] = 4000
59 
60  return dict[shape_type]
def _max_quantity_for_shape_type
Gets the maximum number of shapes allowed for the given shape type This to to set a sensible limit on...
Definition: tweet_parser.py:47

Here is the caller graph for this function:

def tweet_parser._plural_name_for_shape_type (   shape_type)
private

Gets a human-readable name for the given shape type.

62 
63 def _plural_name_for_shape_type(shape_type):
64  dict = {}
65  dict["RECTANGLE"] = "Rectangles"
66  dict["ROTATED_RECTANGLE"] = "Rotated Rectangles"
67  dict["TRIANGLE"] = "Triangles"
68  dict["ELLIPSE"] = "Ellipses"
69  dict["ROTATED_ELLIPSE"] = "Rotated Ellipses"
70  dict["CIRCLE"] = "Circles"
71  dict["LINE"] = "Lines"
72  dict["QUADRATIC_BEZIER"] = "Beziers"
73  dict["POLYLINE"] = "Polylines"
74 
75  return dict[shape_type]
def _plural_name_for_shape_type
Gets a human-readable name for the given shape type.
Definition: tweet_parser.py:62

Here is the caller graph for this function:

def tweet_parser._represents_int (   s)
private

Returns true if the given string represents an integer value.

18 
19 def _represents_int(s):
20  return re.match(r"[-+]?\d+$", s) is not None
def _represents_int
Returns true if the given string represents an integer value.
Definition: tweet_parser.py:18

Here is the caller graph for this function:

def tweet_parser.make_code_for_shape_tweet (   message)

Parses the given tweet, returning the ChaiScript code for Geometrize to run based on the contents of the tweet.

The shapes are added in the order they are specified in the message. Message examples: "@Geometrizer triangles=30 and rectangles=20, thanks bot!" - produces an image with 30 triangles, then 20 rectangles. Or: "@Geometrizer triangles=30 circles=500, triangles=20, nice bot!" - produces an image with 30 triangles, 500 circles, 20 triangles. Or: "@Geometrizer tris=300 circs=200" - produces an image with 300 triangles, 200 circles. Or: "@Geometrize rotated_rects=500" - produces an image with 500 rotated rectangles. Or: "@Geometrizer this is a really cool bot!" - produces an image with a random selection and quantity of shapes. :return A chunk of ChaiScript code that adds the shapes that the tweet requests (must be combined with a larger script to work).

135 def make_code_for_shape_tweet(message):
136 
137  shape_quantity_dictionary = make_shape_quantity_dictionary(message)
138  if shape_quantity_dictionary:
139  # Try to match shapeTypeN=shapeQuantityN patterns
140  code = _make_shape_code(shape_quantity_dictionary)
141  print("Creating specific shapes and quantities code for tweet")
142  return code, shape_quantity_dictionary
143 
144  # Failed, so use a random shape type instead
145  print("Creating random shapes code for tweet")
146 
147  # Use a subset of possible shapes since random numbers of others don't always look good
148  shape_types = ['ROTATED_RECTANGLE', 'ROTATED_ELLIPSE', 'TRIANGLE', 'CIRCLE', 'ELLIPSE']
149  shape_type = random.choice(shape_types)
150  shape_quantity = random.randint(200, 500)
151  shape_quantity_dictionary[shape_type] = shape_quantity
152  code = _make_shape_code(shape_quantity_dictionary)
153 
154  return code, shape_quantity_dictionary
def make_code_for_shape_tweet
Parses the given tweet, returning the ChaiScript code for Geometrize to run based on the contents of ...
Definition: tweet_parser.py:134
def _make_shape_code
Helper for constructing a ChaiScript script for geometrizing shapes.
Definition: tweet_parser.py:85
def make_shape_quantity_dictionary
Parses a tweet message and returns a dictionary of the shape types and quantities that were requested...
Definition: tweet_parser.py:92

Here is the call graph for this function:

Here is the caller graph for this function:

def tweet_parser.make_message_for_shape_dictionary (   shape_quantity_dictionary)

Creates a message for the given shape dictionary, suitable for tweeting along with the result of running the script.

For example [ "ROTATED_RECTANGLES" => 250 ], "250 Rotated Rectangles" :return A message describing the shapes and quantities.

159 def make_message_for_shape_dictionary(shape_quantity_dictionary):
160  message = "Geometrized - "
161 
162  for shape, quantity in shape_quantity_dictionary.items():
163  message += str(quantity)
164  message += " "
165  message += _plural_name_for_shape_type(shape)
166  message += " "
167 
return message
def _plural_name_for_shape_type
Gets a human-readable name for the given shape type.
Definition: tweet_parser.py:62
def make_message_for_shape_dictionary
Creates a message for the given shape dictionary, suitable for tweeting along with the result of runn...
Definition: tweet_parser.py:158

Here is the call graph for this function:

Here is the caller graph for this function:

def tweet_parser.make_shape_quantity_dictionary (   message)

Parses a tweet message and returns a dictionary of the shape types and quantities that were requested.

92 
94  symbols = message.split(" ")
95  shape_keyword_dictionary = _make_shape_keyword_dictionary()
96  shape_quantity_dictionary = {}
97 
98  for symbol in symbols:
99  pair = symbol.split("=")
100 
101  if len(pair) != 2:
102  continue
103 
104  shape_type_key = pair[0].strip()
105  if shape_type_key not in shape_keyword_dictionary:
106  continue
107 
108  if not _represents_int(pair[1].strip()):
109  continue
110 
111  shape_type = shape_keyword_dictionary[shape_type_key]
112  shape_count = int(pair[1].strip())
113  shape_count_max = _max_quantity_for_shape_type(shape_type)
114 
115  if shape_count <= 0:
116  print("Got bad shape count request, must be positive")
117  continue
118 
119  if shape_count >= shape_count_max:
120  print("Requested shape count was too high, clamping it down")
121  shape_count = shape_count_max
122 
123  shape_quantity_dictionary[shape_type] = shape_count
124 
125  return shape_quantity_dictionary
def _represents_int
Returns true if the given string represents an integer value.
Definition: tweet_parser.py:18
def _max_quantity_for_shape_type
Gets the maximum number of shapes allowed for the given shape type This to to set a sensible limit on...
Definition: tweet_parser.py:47
def make_shape_quantity_dictionary
Parses a tweet message and returns a dictionary of the shape types and quantities that were requested...
Definition: tweet_parser.py:92
def _make_shape_keyword_dictionary
Creates a dictionary mapping shape keywords to canonical shape names used by ChaiScript scripts Provi...
Definition: tweet_parser.py:32

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

list tweet_parser._circle_keywords = [ "circ", "circs", "circle", "circles" ]
list tweet_parser._ellipse_keywords = [ "ellipse", "ellipses" ]
list tweet_parser._line_keywords = [ "line", "lines" ]
list tweet_parser._polyline_keywords = [ "polyline", "polylines" ]
list tweet_parser._quadratic_bezier_keywords = [ "bezier", "beziers", "quadratic_bezier", "quadratic_beziers" ]
list tweet_parser._rect_keywords = [ "rect", "rects", "rectangle", "rectangles" ]
list tweet_parser._rotated_ellipse_keywords = [ "rotated_ellipse", "rotated_ellipses" ]
list tweet_parser._rotated_rect_keywords = [ "rotated_rect", "rotated_rects", "rotated_rectangle", "rotated_rectangles" ]
list tweet_parser._triangle_keywords = [ "tri", "tris", "triangle", "triangles" ]