Skip to content

Instantly share code, notes, and snippets.

@dribnet
Last active July 26, 2017 02:01
Show Gist options
  • Save dribnet/92eb8f286fc4804a9f8fc22b23ff89a5 to your computer and use it in GitHub Desktop.
Save dribnet/92eb8f286fc4804a9f8fc22b23ff89a5 to your computer and use it in GitHub Desktop.
Tracery + Data Example jupyter notebook
.ipynb_checkpoints
Type Date Description Total Balance
10/07/2017 WCC Parking -$3.25 $16.59
07/06/2017 Go Wellington -$1.66 $19.84
07/06/2017 Retail Top-up $19.75 $21.50
03/04/2017 Go Wellington -$1.66 $1.75
09/01/2017 WCC Parking -$1.75 $3.41
06/01/2017 WCC Parking -$6.25 $5.16
22/12/2016 WCC Parking -$1.75 $11.41
22/12/2016 Go Wellington -$1.66 $13.16
22/12/2016 Go Wellington -$1.66 $14.82
21/12/2016 WCC Parking -$3.25 $16.48
21/12/2016 WCC Parking -$4.75 $19.73
02/12/2016 Go Wellington -$1.66 $24.48
01/12/2016 Go Wellington -$1.66 $26.14
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example of using Tracery with CSV and JSON data\n",
"\n",
"Combing personal and public data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### First let's just make sure that tracery is working"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Howdy, world!\n",
"Hello, solar system!\n",
"Hey, solar system!\n",
"Hello, galaxy!\n",
"Greetings, solar system!\n"
]
}
],
"source": [
"import tracery\n",
"from tracery.modifiers import base_english\n",
"\n",
"# put your grammar here as the value assigned to \"rules\"\n",
"rules = {\n",
" \"origin\": \"#hello.capitalize#, #location#!\",\n",
" \"hello\": [\"hello\", \"greetings\", \"howdy\", \"hey\"],\n",
" \"location\": [\"world\", \"solar system\", \"galaxy\", \"universe\"]\n",
"}\n",
"\n",
"grammar = tracery.Grammar(rules) # create a grammar object from the rules\n",
"grammar.add_modifiers(base_english) # add pre-programmed modifiers\n",
"\n",
"for i in range(5):\n",
" print(grammar.flatten(\"#origin#\")) # and flatten, starting with origin rule"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"### Grab some personal data - here's some of my own snapper history"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import csv\n",
"import random\n",
"\n",
"rows = []\n",
"\n",
"for row in csv.DictReader(open(\"snapper.csv\")):\n",
" rows.append(row)\n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"53\n",
"OrderedDict([('\\ufeff\"Type\"', ''), ('Date', '10/07/2016'), ('Description', 'Go Wellington'), ('Total', '-$1.66'), ('Balance', '$19.83')])\n"
]
}
],
"source": [
"# look at the data - see how many records we have and also look at one at random\n",
"print(len(rows))\n",
"print(random.choice(rows))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Warmup - write a simple sentence just using the print command"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remember that day 15/07/2016 when you spent -$1.66 to go to Go Wellington\n"
]
}
],
"source": [
"event = random.choice(rows)\n",
"print(\"Remember that day {} when you spent {} to go to {}\".format(event['Date'], event['Total'], event['Description']))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"### OK, that worked good. Now try again, pretending we went to more interesting places"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"places_not_gone = [\n",
" \"Beach\",\n",
" \"California\",\n",
" \"North Pole\"\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remember that day 29/07/2016 when you spent -$1.66 to go to North Pole\n"
]
}
],
"source": [
"event = random.choice(rows)\n",
"place = random.choice(places_not_gone)\n",
"print(\"Remember that day {} when you spent {} to go to {}\".format(event['Date'], event['Total'], place))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### OK, let's now convert that to a tracery grammar.\n",
"\n",
"First we'll pull out all the dollar amounts and dates into separate lists"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"all_dollar_amounts = [x[\"Total\"] for x in rows]\n",
"all_dates = [x['Date'] for x in rows]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And here is the equivalent tracery grammar. Note the `{}` variables are now `#named variables#`."
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"trip_rules = {\n",
" \"place\": places_not_gone,\n",
" \"date\": all_dates,\n",
" \"price\": all_dollar_amounts,\n",
" \"origin\": [\n",
" \"Remember that day #date# when you spent #price# to go to #place#\"\n",
" ]\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To convince ourselves this works, we generate 5 sentences using the grammar."
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remember that day 12/08/2016 when you spent -$1.66 to go to North Pole\n",
"Remember that day 01/07/2016 when you spent -$1.66 to go to California\n",
"Remember that day 12/08/2016 when you spent -$1.66 to go to Beach\n",
"Remember that day 14/07/2016 when you spent $19.75 to go to North Pole\n",
"Remember that day 29/07/2016 when you spent -$1.66 to go to North Pole\n"
]
}
],
"source": [
"grammar = tracery.Grammar(trip_rules) # create a grammar object from the rules\n",
"grammar.add_modifiers(base_english) # add pre-programmed modifiers\n",
"\n",
"for i in range(5):\n",
" print(grammar.flatten(\"#origin#\")) # and flatten, starting with origin rule"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Add more Data\n",
"\n",
"That was good, but it would be more interesting to have more variety in places that I've been.\n",
"\n",
"So let's jump on [Darius Kazemi's copora](https://github.com/dariusk/corpora) project to find other data we can use. Browsing there under geography, one possiblilty is to use english towns and cities. So we'll download the raw version of that json."
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2017-07-26 13:51:50-- https://raw.githubusercontent.com/dariusk/corpora/master/data/geography/english_towns_cities.json\n",
"Resolving raw.githubusercontent.com... 151.101.164.133\n",
"Connecting to raw.githubusercontent.com|151.101.164.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 17582 (17K) [text/plain]\n",
"Saving to: ‘english_towns_cities.json.1’\n",
"\n",
"english_towns_citie 100%[===================>] 17.17K --.-KB/s in 0.009s \n",
"\n",
"2017-07-26 13:51:51 (1.92 MB/s) - ‘english_towns_cities.json.1’ saved [17582/17582]\n",
"\n"
]
}
],
"source": [
"!wget https://raw.githubusercontent.com/dariusk/corpora/master/data/geography/english_towns_cities.json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is how you open the file."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import json\n",
"\n",
"towns = json.loads(open(\"english_towns_cities.json\").read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I'm not sure what data is in the file. Let's just print it out and take a peek.\n",
"\n",
"In jupyter, if you enter in a variable by itself, it just prints it out."
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'cities': ['Bath',\n",
" 'Birmingham',\n",
" 'Bradford',\n",
" 'Brighton & Hove',\n",
" 'Bristol',\n",
" 'Cambridge',\n",
" 'Cantebury',\n",
" 'Carlisle',\n",
" 'Chelmsford',\n",
" 'Chester',\n",
" 'Chichester',\n",
" 'City of London',\n",
" 'City of Westminster',\n",
" 'Coventry',\n",
" 'Derby',\n",
" 'Durham',\n",
" 'Ely',\n",
" 'Exeter',\n",
" 'Gloucester',\n",
" 'Hereford',\n",
" 'Kingston upon Hull',\n",
" 'Lancaster',\n",
" 'Leeds',\n",
" 'Leicester',\n",
" 'Lichfield',\n",
" 'Lincoln',\n",
" 'Liverpool',\n",
" 'Manchester',\n",
" 'Newcastle upon Tyne',\n",
" 'Norwich',\n",
" 'Nottingham',\n",
" 'Oxford',\n",
" 'Peterborough',\n",
" 'Plymouth',\n",
" 'Portsmouth',\n",
" 'Preston',\n",
" 'Ripon',\n",
" 'Salford',\n",
" 'Salisbury',\n",
" 'Sheffield',\n",
" 'Southampton',\n",
" 'St Albans',\n",
" 'Stoke-on-Trent',\n",
" 'Sunderland',\n",
" 'Truro',\n",
" 'Wakefield',\n",
" 'Wells',\n",
" 'Winchester',\n",
" 'Worcester',\n",
" 'York'],\n",
" 'description': 'Two lists: one for English towns, one for English cities.',\n",
" 'sources': {'cities': 'https://en.wikipedia.org/wiki/List_of_cities_in_the_United_Kingdom',\n",
" 'towns': 'https://en.wikipedia.org/wiki/List_of_towns_in_England'},\n",
" 'towns': ['Abingdon',\n",
" 'Accrington',\n",
" 'Acle',\n",
" 'Acton',\n",
" 'Adlington',\n",
" 'Alcester',\n",
" 'Aldeburgh',\n",
" 'Aldershot',\n",
" 'Alford',\n",
" 'Alfreton',\n",
" 'Alnwick',\n",
" 'Alsager',\n",
" 'Alston',\n",
" 'Alton',\n",
" 'Altrincham',\n",
" 'Amble',\n",
" 'Ambleside',\n",
" 'Amersham',\n",
" 'Amesbury',\n",
" 'Ampthill',\n",
" 'Andover',\n",
" 'Appleby-in-Westmorland',\n",
" 'Arlesey',\n",
" 'Arundel',\n",
" 'Ashbourne',\n",
" 'Ashburton',\n",
" 'Ashby Woulds',\n",
" 'Ashby-de-la-Zouch',\n",
" 'Ashford',\n",
" 'Ashington',\n",
" 'Ashton-under-Lyne',\n",
" 'Askern',\n",
" 'Aspatria',\n",
" 'Atherstone',\n",
" 'Attleborough',\n",
" 'Axbridge',\n",
" 'Axminster',\n",
" 'Aylesbury',\n",
" 'Aylsham',\n",
" 'Bacup',\n",
" 'Bakewell',\n",
" 'Banbury',\n",
" 'Barking',\n",
" 'Barnard Castle',\n",
" 'Barnes',\n",
" 'Barnet',\n",
" 'Barnoldswick',\n",
" 'Barnsley',\n",
" 'Barnstaple',\n",
" 'Barrow-in-Furness',\n",
" 'Barton-upon-Humber',\n",
" 'Basildon',\n",
" 'Basingstoke',\n",
" 'Batley',\n",
" 'Battle',\n",
" 'Bawtry',\n",
" 'Beaconsfield',\n",
" 'Beaminster',\n",
" 'Bebington',\n",
" 'Beccles',\n",
" 'Beckenham',\n",
" 'Bedale',\n",
" 'Bedford',\n",
" 'Bedworth',\n",
" 'Belper',\n",
" 'Bentham',\n",
" 'Berkeley',\n",
" 'Berkhamsted',\n",
" 'Berwick-upon-Tweed',\n",
" 'Beverley',\n",
" 'Bewdley',\n",
" 'Bexhill-on-Sea',\n",
" 'Bexley',\n",
" 'Bicester',\n",
" 'Biddulph',\n",
" 'Bideford',\n",
" 'Biggleswade',\n",
" 'Billericay',\n",
" 'Billingham',\n",
" 'Bilston',\n",
" 'Bingham',\n",
" 'Bingley',\n",
" 'Birchwood',\n",
" 'Birkenhead',\n",
" 'Bishop Auckland',\n",
" \"Bishop's Castle\",\n",
" \"Bishop's Stortford\",\n",
" \"Bishop's Waltham\",\n",
" 'Blackburn',\n",
" 'Blackpool',\n",
" 'Blackrod',\n",
" 'Blackwater and Hawley',\n",
" 'Blandford Forum',\n",
" 'Bletchley and Fenny Stratford',\n",
" 'Blyth',\n",
" 'Bodmin',\n",
" 'Bognor Regis',\n",
" 'Bollington',\n",
" 'Bolsover',\n",
" 'Bolton',\n",
" 'Bootle',\n",
" 'Boroughbridge',\n",
" 'Boston',\n",
" 'Bottesford',\n",
" 'Bourne',\n",
" 'Bournemouth',\n",
" 'Bovey Tracey',\n",
" 'Brackley',\n",
" 'Bracknell',\n",
" 'Bradford-on-Avon',\n",
" 'Brading',\n",
" 'Bradley Stoke',\n",
" 'Bradninch',\n",
" 'Braintree',\n",
" 'Brampton',\n",
" 'Brandon',\n",
" 'Braunstone Town',\n",
" 'Brentford',\n",
" 'Brentwood',\n",
" 'Bridgnorth',\n",
" 'Bridgwater',\n",
" 'Bridlington',\n",
" 'Bridport',\n",
" 'Brierfield',\n",
" 'Brierley',\n",
" 'Brigg',\n",
" 'Brighouse',\n",
" 'Brightlingsea',\n",
" 'Brixham',\n",
" \"Broadstairs and St Peter's\",\n",
" 'Bromborough',\n",
" 'Bromley',\n",
" 'Bromsgrove',\n",
" 'Bromyard and Winslow',\n",
" 'Broseley',\n",
" 'Broughton-in-Furness',\n",
" 'Broughton',\n",
" 'Bruton',\n",
" 'Buckfastleigh',\n",
" 'Buckingham',\n",
" 'Bude-Stratton',\n",
" 'Budleigh Salterton',\n",
" 'Bulwell',\n",
" 'Bungay',\n",
" 'Buntingford',\n",
" 'Burford',\n",
" 'Burgess Hill',\n",
" 'Burgh-le-Marsh',\n",
" 'Burnham-on-Crouch',\n",
" 'Burnham-on-Sea and Highbridge',\n",
" 'Burnley',\n",
" 'Burntwood',\n",
" 'Burslem',\n",
" 'Burton Latimer',\n",
" 'Burton upon Trent',\n",
" 'Bury St Edmunds',\n",
" 'Bury',\n",
" 'Bushey',\n",
" 'Buxton',\n",
" 'Caistor',\n",
" 'Callington',\n",
" 'Calne',\n",
" 'Camborne',\n",
" 'Camelford',\n",
" 'Cannock',\n",
" 'Canvey Island',\n",
" 'Carlton Colville',\n",
" 'Carnforth',\n",
" 'Carshalton',\n",
" 'Carterton',\n",
" 'Castle Cary',\n",
" 'Castleford',\n",
" 'Chagford',\n",
" 'Chapel-en-le-Frith',\n",
" 'Chard',\n",
" 'Charlbury',\n",
" 'Chatham',\n",
" 'Chatteris',\n",
" 'Cheadle',\n",
" 'Cheltenham',\n",
" 'Chertsey',\n",
" 'Chesham',\n",
" 'Cheshunt',\n",
" 'Chester-le-Street',\n",
" 'Chesterfield',\n",
" 'Chickerell',\n",
" 'Chingford',\n",
" 'Chippenham',\n",
" 'Chipping Campden',\n",
" 'Chipping Norton',\n",
" 'Chipping Sodbury',\n",
" 'Chorley',\n",
" 'Chorleywood',\n",
" 'Christchurch',\n",
" 'Chudleigh',\n",
" 'Chulmleigh',\n",
" 'Church Stretton',\n",
" 'Cinderford',\n",
" 'Cirencester',\n",
" 'Clare',\n",
" 'Clay Cross',\n",
" 'Cleator Moor',\n",
" 'Cleethorpes',\n",
" 'Cleobury Mortimer',\n",
" 'Clevedon',\n",
" 'Clitheroe',\n",
" 'Clun',\n",
" 'Cockermouth',\n",
" 'Coggeshall',\n",
" 'Colburn',\n",
" 'Colchester',\n",
" 'Coleford',\n",
" 'Coleshill',\n",
" 'Colne',\n",
" 'Colyton',\n",
" 'Congleton',\n",
" 'Conisbrough',\n",
" 'Corbridge',\n",
" 'Corby',\n",
" 'Corringham',\n",
" 'Corsham',\n",
" 'Cotgrave',\n",
" 'Coulsdon',\n",
" 'Cowes',\n",
" 'Cramlington',\n",
" 'Cranbrook',\n",
" 'Craven Arms',\n",
" 'Crawley',\n",
" 'Crediton',\n",
" 'Crewe',\n",
" 'Crewkerne',\n",
" 'Cricklade',\n",
" 'Cromer',\n",
" 'Crosby',\n",
" 'Crowborough',\n",
" 'Crowland',\n",
" 'Crowle',\n",
" 'Croydon',\n",
" 'Cullompton',\n",
" 'Dagenham',\n",
" 'Dalton Town with Newton',\n",
" 'Darley Dale',\n",
" 'Darlington',\n",
" 'Dartford',\n",
" 'Dartmouth',\n",
" 'Darwen',\n",
" 'Daventry',\n",
" 'Dawley',\n",
" 'Dawlish',\n",
" 'Deal',\n",
" 'Dereham',\n",
" 'Desborough',\n",
" 'Devizes',\n",
" 'Dewsbury',\n",
" 'Didcot',\n",
" \"Dinnington St John's\",\n",
" 'Diss',\n",
" 'Doncaster',\n",
" 'Dorchester',\n",
" 'Dorking',\n",
" 'Dover',\n",
" 'Dovercourt',\n",
" 'Downham Market',\n",
" 'Driffield',\n",
" 'Droitwich Spa',\n",
" 'Dronfield',\n",
" 'Dudley',\n",
" 'Dukinfield',\n",
" 'Dulverton',\n",
" 'Dunstable',\n",
" 'Dunwich',\n",
" 'Dursley',\n",
" 'Ealing',\n",
" 'Earl Shilton',\n",
" 'Earley',\n",
" 'Easingwold',\n",
" 'East Cowes',\n",
" 'East Grinstead',\n",
" 'East Ham',\n",
" 'East Retford',\n",
" 'Eastbourne',\n",
" 'Eastleigh',\n",
" 'Eastwood',\n",
" 'Eccles',\n",
" 'Eccleshall',\n",
" 'Edenbridge',\n",
" 'Edgware',\n",
" 'Edmonton',\n",
" 'Egremont',\n",
" 'Elland',\n",
" 'Ellesmere Port',\n",
" 'Ellesmere',\n",
" 'Elstree and Borehamwood',\n",
" 'Emsworth',\n",
" 'Enfield',\n",
" 'Epping',\n",
" 'Epworth',\n",
" 'Erith',\n",
" 'Eton',\n",
" 'Evesham',\n",
" 'Exmouth',\n",
" 'Eye',\n",
" 'Fairford',\n",
" 'Fakenham',\n",
" 'Falmouth',\n",
" 'Fareham',\n",
" 'Faringdon',\n",
" 'Farnham',\n",
" 'Faversham',\n",
" 'Fazeley',\n",
" 'Featherstone',\n",
" 'Felixstowe',\n",
" 'Ferndown',\n",
" 'Ferryhill',\n",
" 'Filey',\n",
" 'Filton',\n",
" 'Finchley',\n",
" 'Fleet',\n",
" 'Fleetwood',\n",
" 'Flitwick',\n",
" 'Folkestone',\n",
" 'Fordbridge',\n",
" 'Fordingbridge',\n",
" 'Fordwich',\n",
" 'Fowey',\n",
" 'Framlingham',\n",
" 'Frinton and Walton',\n",
" 'Frodsham',\n",
" 'Frome',\n",
" 'Gainsborough',\n",
" 'Garstang',\n",
" 'Gateshead',\n",
" 'Gillingham',\n",
" 'Glastonbury',\n",
" 'Glossop',\n",
" 'Godalming',\n",
" 'Godmanchester',\n",
" 'Goole',\n",
" 'Gorleston',\n",
" 'Gosport',\n",
" 'Grange-over-Sands',\n",
" 'Grantham',\n",
" 'Gravesend',\n",
" 'Grays',\n",
" 'Great Dunmow',\n",
" 'Great Torrington',\n",
" 'Great Yarmouth',\n",
" 'Greater Willington',\n",
" 'Grimsby',\n",
" 'Guildford',\n",
" 'Guisborough',\n",
" 'Hadleigh',\n",
" 'Hailsham',\n",
" 'Halesowen',\n",
" 'Halesworth',\n",
" 'Halifax',\n",
" 'Halstead',\n",
" 'Haltwhistle',\n",
" 'Harlow',\n",
" 'Harpenden',\n",
" 'Harrogate',\n",
" 'Harrow',\n",
" 'Hartland',\n",
" 'Hartlepool',\n",
" 'Harwich',\n",
" 'Harworth and Bircotes',\n",
" 'Haslemere',\n",
" 'Haslingden',\n",
" 'Hastings',\n",
" 'Hatfield',\n",
" 'Hatherleigh',\n",
" 'Havant',\n",
" 'Haverhill',\n",
" 'Haxby',\n",
" 'Hayle',\n",
" 'Haywards Heath',\n",
" 'Heanor and Loscoe',\n",
" 'Heathfield',\n",
" 'Hebden Royd',\n",
" 'Hedge End',\n",
" 'Hednesford',\n",
" 'Hedon',\n",
" 'Helmsley',\n",
" 'Helston',\n",
" 'Hemel Hempstead',\n",
" 'Hemsworth',\n",
" 'Hendon',\n",
" 'Henley-in-Arden',\n",
" 'Henley-on-Thames',\n",
" 'Hertford',\n",
" 'Hessle',\n",
" 'Hetton',\n",
" 'Hexham',\n",
" 'Heywood',\n",
" 'High Wycombe',\n",
" 'Higham Ferrers',\n",
" 'Highworth',\n",
" 'Hinckley',\n",
" 'Hingham',\n",
" 'Hitchin',\n",
" 'Hoddesdon',\n",
" 'Holbeach',\n",
" 'Holsworthy',\n",
" 'Holt',\n",
" 'Honiton',\n",
" 'Horley',\n",
" 'Horncastle',\n",
" 'Hornsea',\n",
" 'Hornsey',\n",
" 'Horsforth',\n",
" 'Horsham',\n",
" 'Horwich',\n",
" 'Houghton Regis',\n",
" 'Howden',\n",
" 'Huddersfield',\n",
" 'Hungerford',\n",
" 'Hunstanton',\n",
" 'Huntingdon',\n",
" 'Hyde',\n",
" 'Hythe',\n",
" 'Ilford',\n",
" 'Ilfracombe',\n",
" 'Ilkeston',\n",
" 'Ilkley',\n",
" 'Ilminster',\n",
" 'Immingham',\n",
" 'Ingleby Barwick',\n",
" 'Ipswich',\n",
" 'Irthlingborough',\n",
" 'Ivybridge',\n",
" 'Jarrow',\n",
" 'Keighley',\n",
" 'Kempston',\n",
" 'Kendal',\n",
" 'Kenilworth',\n",
" 'Kesgrave',\n",
" 'Keswick',\n",
" 'Kettering',\n",
" 'Keynsham',\n",
" 'Kidderminster',\n",
" 'Kidsgrove',\n",
" 'Kimberley',\n",
" \"King's Lynn\",\n",
" 'Kingsbridge',\n",
" 'Kingston-upon-Thames',\n",
" 'Kington',\n",
" 'Kirkby Lonsdale',\n",
" 'Kirkby Stephen',\n",
" 'Kirkby-in-Ashfield',\n",
" 'Kirkbymoorside',\n",
" 'Kirkham',\n",
" 'Kirton-in-Lindsey',\n",
" 'Knaresborough',\n",
" 'Knutsford',\n",
" 'Langport',\n",
" 'Launceston',\n",
" 'Leatherhead',\n",
" 'Lechlade',\n",
" 'Ledbury',\n",
" 'Leek',\n",
" 'Leigh-on-Sea',\n",
" 'Leigh',\n",
" 'Leighton-Linslade',\n",
" 'Leiston',\n",
" 'Leominster',\n",
" 'Letchworth Garden City',\n",
" 'Lewes',\n",
" 'Leyburn',\n",
" 'Leyton',\n",
" 'Liskeard',\n",
" 'Littlehampton',\n",
" 'Loddon',\n",
" 'Loftus',\n",
" 'Long Sutton',\n",
" 'Longridge',\n",
" 'Longtown',\n",
" 'Looe',\n",
" 'Lostwithiel',\n",
" 'Loughborough',\n",
" 'Loughton',\n",
" 'Louth',\n",
" 'Lowestoft',\n",
" 'Ludgershall',\n",
" 'Ludlow',\n",
" 'Luton',\n",
" 'Lutterworth',\n",
" 'Lydd',\n",
" 'Lydney',\n",
" 'Lyme Regis',\n",
" 'Lynton and Lynmouth',\n",
" 'Lytham St Annes',\n",
" 'Mablethorpe and Sutton',\n",
" 'Macclesfield',\n",
" 'Madeley',\n",
" 'Maghull',\n",
" 'Maidenhead',\n",
" 'Maidstone',\n",
" 'Maldon',\n",
" 'Malmesbury',\n",
" 'Maltby',\n",
" 'Malton',\n",
" 'Malvern',\n",
" 'Manningtree',\n",
" 'Mansfield',\n",
" 'Marazion',\n",
" 'March',\n",
" 'Margate',\n",
" 'Market Bosworth',\n",
" 'Market Deeping',\n",
" 'Market Drayton',\n",
" 'Market Harborough',\n",
" 'Market Rasen',\n",
" 'Market Weighton',\n",
" 'Marlborough',\n",
" 'Marlow',\n",
" 'Maryport',\n",
" 'Masham',\n",
" 'Matlock',\n",
" 'Medlar with Wesham',\n",
" 'Melksham',\n",
" 'Meltham',\n",
" 'Melton Mowbray',\n",
" 'Mere',\n",
" 'Mexborough',\n",
" 'Middleham',\n",
" 'Middlesbrough',\n",
" 'Middleton',\n",
" 'Middlewich',\n",
" 'Midhurst',\n",
" 'Midsomer Norton',\n",
" 'Mildenhall',\n",
" 'Millom',\n",
" 'Milton Keynes',\n",
" 'Minchinhampton',\n",
" 'Minehead',\n",
" 'Minster',\n",
" 'Mirfield',\n",
" 'Mitcham',\n",
" 'Mitcheldean',\n",
" 'Morecambe',\n",
" 'Moreton-in-Marsh',\n",
" 'Moretonhampstead',\n",
" 'Morley',\n",
" 'Morpeth',\n",
" 'Mossley',\n",
" 'Much Wenlock',\n",
" 'Nailsea',\n",
" 'Nailsworth',\n",
" 'Nantwich',\n",
" 'Needham Market',\n",
" 'Nelson',\n",
" 'Neston',\n",
" 'New Alresford',\n",
" 'New Mills',\n",
" 'New Milton',\n",
" 'New Romney',\n",
" 'Newark-on-Trent',\n",
" 'Newbiggin-by-the-Sea',\n",
" 'Newbury',\n",
" 'Newcastle-under-Lyme',\n",
" 'Newent',\n",
" 'Newhaven',\n",
" 'Newlyn',\n",
" 'Newmarket',\n",
" 'Newport Pagnell',\n",
" 'Newport',\n",
" 'Newquay',\n",
" 'Newton Abbot',\n",
" 'Newton-le-Willows',\n",
" 'Normanton',\n",
" 'North Hykeham',\n",
" 'North Petherton',\n",
" 'North Tawton',\n",
" 'North Walsham',\n",
" 'Northallerton',\n",
" 'Northam',\n",
" 'Northampton',\n",
" 'Northfleet',\n",
" 'Northleach with Eastington',\n",
" 'Northwich',\n",
" 'Norton-on-Derwent',\n",
" 'Nuneaton',\n",
" 'Oakengates',\n",
" 'Oakham',\n",
" 'Okehampton',\n",
" 'Oldbury',\n",
" 'Oldham',\n",
" 'Ollerton and Boughton',\n",
" 'Olney',\n",
" 'Ongar',\n",
" 'Orford',\n",
" 'Ormskirk',\n",
" 'Ossett',\n",
" 'Oswestry',\n",
" 'Otley',\n",
" 'Ottery St Mary',\n",
" 'Oundle',\n",
" 'Paddock Wood',\n",
" 'Padiham',\n",
" 'Padstow',\n",
" 'Paignton',\n",
" 'Painswick',\n",
" 'Partington',\n",
" 'Patchway',\n",
" 'Pateley Bridge',\n",
" 'Peacehaven',\n",
" 'Penistone',\n",
" 'Penkridge',\n",
" 'Penrith',\n",
" 'Penryn',\n",
" 'Penwortham',\n",
" 'Penzance',\n",
" 'Pershore',\n",
" 'Peterlee',\n",
" 'Petersfield',\n",
" 'Petworth',\n",
" 'Pickering',\n",
" 'Pocklington',\n",
" 'Polegate',\n",
" 'Pontefract',\n",
" 'Ponteland',\n",
" 'Poole',\n",
" 'Porthleven',\n",
" 'Portishead and North Weston',\n",
" 'Portland',\n",
" 'Potton',\n",
" 'Poynton-with-Worth',\n",
" 'Preesall',\n",
" 'Prescot',\n",
" 'Princes Risborough',\n",
" 'Prudhoe',\n",
" 'Pudsey',\n",
" 'Queenborough-in-Sheppey',\n",
" 'Radstock',\n",
" 'Ramsey',\n",
" 'Ramsgate',\n",
" 'Raunds',\n",
" 'Rawtenstall',\n",
" 'Rayleigh',\n",
" 'Reading',\n",
" 'Redcar',\n",
" 'Redditch',\n",
" 'Redenhall with Harleston',\n",
" 'Redruth',\n",
" 'Reepham',\n",
" 'Reigate',\n",
" 'Richmond',\n",
" 'Ringwood',\n",
" 'Ripley',\n",
" 'Ripon',\n",
" 'Rochdale',\n",
" 'Rochester',\n",
" 'Rochford',\n",
" 'Romford',\n",
" 'Romsey',\n",
" 'Ross-on-Wye',\n",
" 'Rothbury',\n",
" 'Rotherham',\n",
" 'Rothwell',\n",
" 'Rowley Regis',\n",
" 'Royal Leamington Spa',\n",
" 'Royal Tunbridge Wells',\n",
" 'Royal Wootton Bassett',\n",
" 'Royston',\n",
" 'Rugby',\n",
" 'Rugeley',\n",
" 'Rushden',\n",
" 'Ryde',\n",
" 'Rye',\n",
" 'Saffron Walden',\n",
" 'Salcombe',\n",
" 'Sale',\n",
" 'Saltash',\n",
" 'Sandbach',\n",
" 'Sandhurst',\n",
" 'Sandiacre',\n",
" 'Sandown',\n",
" 'Sandwich',\n",
" 'Sandy',\n",
" 'Sawbridgeworth',\n",
" 'Saxmundham',\n",
" 'Scarborough',\n",
" 'Scunthorpe',\n",
" 'Seaford',\n",
" 'Seaham',\n",
" 'Seaton',\n",
" 'Sedbergh',\n",
" 'Selby',\n",
" 'Selsey',\n",
" 'Settle',\n",
" 'Sevenoaks',\n",
" 'Shaftesbury',\n",
" 'Shanklin',\n",
" 'Shefford',\n",
" 'Shepshed',\n",
" 'Shepton Mallet',\n",
" 'Sherborne',\n",
" 'Sheringham',\n",
" 'Shifnal',\n",
" 'Shildon',\n",
" 'Shipston-on-Stour',\n",
" 'Shirebrook',\n",
" 'Shoreham-by-Sea',\n",
" 'Shrewsbury',\n",
" 'Sidmouth',\n",
" 'Silloth',\n",
" 'Silsden',\n",
" 'Sittingbourne',\n",
" 'Skegness',\n",
" 'Skelmersdale',\n",
" 'Skelton-in-Cleveland',\n",
" 'Skipton',\n",
" 'Sleaford',\n",
" 'Slough',\n",
" 'Smethwick',\n",
" 'Snaith and Cowick',\n",
" 'Snodland',\n",
" 'Soham',\n",
" 'Solihull',\n",
" 'Somerton',\n",
" 'South Cave',\n",
" 'South Elmsall',\n",
" 'South Kirkby and Moorthorpe',\n",
" 'South Molton',\n",
" 'South Petherton',\n",
" 'South Shields',\n",
" 'South Woodham Ferrers',\n",
" 'Southall',\n",
" 'Southam',\n",
" 'Southborough',\n",
" 'Southend-on-Sea',\n",
" 'Southgate',\n",
" 'Southminster',\n",
" 'Southport',\n",
" 'Southsea',\n",
" 'Southwell',\n",
" 'Southwick',\n",
" 'Southwold',\n",
" 'Spalding',\n",
" 'Spennymoor',\n",
" 'Spilsby',\n",
" 'St Austell',\n",
" 'St Blaise',\n",
" 'St Columb Major',\n",
" 'St Helens',\n",
" 'St Ives',\n",
" 'St Just-in-Penwith',\n",
" 'St Mary Cray',\n",
" 'St Mawes',\n",
" 'St Neots',\n",
" 'Stafford',\n",
" 'Staines-upon-Thames',\n",
" 'Stainforth',\n",
" 'Stalbridge',\n",
" 'Stalham',\n",
" 'Stalybridge',\n",
" 'Stamford',\n",
" 'Stanhope',\n",
" 'Stanley',\n",
" 'Stapleford',\n",
" 'Staveley',\n",
" 'Stevenage',\n",
" 'Steyning',\n",
" 'Stockport',\n",
" 'Stocksbridge',\n",
" 'Stockton-on-Tees',\n",
" 'Stone',\n",
" 'Stonehouse',\n",
" 'Stony Stratford',\n",
" 'Stotfold',\n",
" 'Stourbridge',\n",
" 'Stourport-on-Severn',\n",
" 'Stow-on-the-Wold',\n",
" 'Stowmarket',\n",
" 'Stratford-upon-Avon',\n",
" 'Stretford',\n",
" 'Strood',\n",
" 'Stroud',\n",
" 'Sturminster Newton',\n",
" 'Sudbury',\n",
" 'Surbiton',\n",
" 'Sutton Coldfield',\n",
" 'Sutton',\n",
" 'Swaffham',\n",
" 'Swanage',\n",
" 'Swanley',\n",
" 'Swanscombe and Greenhithe',\n",
" 'Swindon',\n",
" 'Syston',\n",
" 'Tadcaster',\n",
" 'Tadley',\n",
" 'Tamworth',\n",
" 'Taunton',\n",
" 'Tavistock',\n",
" 'Teignmouth',\n",
" 'Telford',\n",
" 'Telscombe',\n",
" 'Tenbury Wells',\n",
" 'Tenterden',\n",
" 'Tetbury',\n",
" 'Tewkesbury',\n",
" 'Thame',\n",
" 'Thatcham',\n",
" 'Thaxted',\n",
" 'Thetford',\n",
" 'Thirsk',\n",
" 'Thornaby-on-Tees',\n",
" 'Thornbury',\n",
" 'Thorne',\n",
" 'Thorpe St Andrew',\n",
" 'Thrapston',\n",
" 'Tickhill',\n",
" 'Tidworth',\n",
" 'Tipton',\n",
" 'Tisbury',\n",
" 'Tiverton',\n",
" 'Todmorden',\n",
" 'Tonbridge',\n",
" 'Topsham',\n",
" 'Torpoint',\n",
" 'Torquay',\n",
" 'Totnes',\n",
" 'Tottenham',\n",
" 'Totton and Eling',\n",
" 'Tow Law',\n",
" 'Towcester',\n",
" 'Tring',\n",
" 'Trowbridge',\n",
" 'Twickenham',\n",
" 'Tynemouth',\n",
" 'Uckfield',\n",
" 'Ulverston',\n",
" 'Uppingham',\n",
" 'Upton-upon-Severn',\n",
" 'Uttoxeter',\n",
" 'Uxbridge',\n",
" 'Ventnor',\n",
" 'Verwood',\n",
" 'Wadebridge',\n",
" 'Wadhurst',\n",
" 'Wainfleet All Saints',\n",
" 'Wallasey',\n",
" 'Wallingford',\n",
" 'Wallsend',\n",
" 'Walsall',\n",
" 'Waltham Abbey',\n",
" 'Waltham Cross',\n",
" 'Walthamstow',\n",
" 'Walton-on-Thames',\n",
" 'Wantage',\n",
" 'Ware',\n",
" 'Wareham',\n",
" 'Warminster',\n",
" 'Warrington',\n",
" 'Warwick',\n",
" 'Washington',\n",
" 'Watchet',\n",
" 'Watford',\n",
" 'Wath-upon-Dearne',\n",
" 'Watlington',\n",
" 'Watton',\n",
" 'Wellingborough',\n",
" 'Wellington',\n",
" 'Wells-next-the-Sea',\n",
" 'Welwyn Garden City',\n",
" 'Wem',\n",
" 'Wembley',\n",
" 'Wendover',\n",
" 'West Bedlington',\n",
" 'West Bromwich',\n",
" 'West Ham',\n",
" 'West Malling',\n",
" 'West Mersea',\n",
" 'West Tilbury',\n",
" 'Westbury',\n",
" 'Westerham',\n",
" 'Westhoughton',\n",
" 'Weston-super-Mare',\n",
" 'Wetherby',\n",
" 'Weybridge',\n",
" 'Weymouth',\n",
" 'Whaley Bridge',\n",
" 'Whitby',\n",
" 'Whitchurch',\n",
" 'Whitehaven',\n",
" 'Whitehill',\n",
" 'Whitnash',\n",
" 'Whittlesey',\n",
" 'Whitworth',\n",
" 'Wickham',\n",
" 'Wickwar',\n",
" 'Widnes',\n",
" 'Wigan',\n",
" 'Wigton',\n",
" 'Willenhall',\n",
" 'Willesden',\n",
" 'Wilton',\n",
" 'Wimbledon',\n",
" 'Wimborne Minster',\n",
" 'Wincanton',\n",
" 'Winchcombe',\n",
" 'Winchelsea',\n",
" 'Windermere',\n",
" 'Windsor',\n",
" 'Winsford',\n",
" 'Winslow',\n",
" 'Winterton',\n",
" 'Wirksworth',\n",
" 'Wisbech',\n",
" 'Witham',\n",
" 'Withernsea',\n",
" 'Witney',\n",
" 'Wiveliscombe',\n",
" 'Wivenhoe',\n",
" 'Woburn Sands',\n",
" 'Woburn',\n",
" 'Woking',\n",
" 'Wokingham',\n",
" 'Wolsingham',\n",
" 'Wolverton and Greenleys',\n",
" 'Wood Green',\n",
" 'Woodbridge',\n",
" 'Woodley',\n",
" 'Woodstock',\n",
" 'Wooler',\n",
" 'Workington',\n",
" 'Worksop',\n",
" 'Worthing',\n",
" 'Wotton-under-Edge',\n",
" 'Wragby',\n",
" 'Wymondham',\n",
" 'Yarm',\n",
" 'Yarmouth',\n",
" 'Yate',\n",
" 'Yateley',\n",
" 'Yeovil']}"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"towns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"OK, cool, there is a field called 'cities' that has all the cities. So we can just use the same grammar as before but plug in that list of cities."
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"new_trip_rules = {\n",
" \"place\": towns['cities'],\n",
" \"date\": all_dates,\n",
" \"price\": all_dollar_amounts,\n",
" \"origin\": [\n",
" \"Remember that day #date# when you spent #price# to go to #place#\"\n",
" ]\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And we can test out this new set of rules."
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remember that day 15/07/2016 when you spent -$1.66 to go to Ripon\n",
"Remember that day 31/07/2016 when you spent -$1.66 to go to Liverpool\n",
"Remember that day 10/07/2017 when you spent -$1.66 to go to Cantebury\n",
"Remember that day 12/08/2016 when you spent -$1.66 to go to St Albans\n",
"Remember that day 17/07/2016 when you spent -$4.08 to go to Sunderland\n"
]
}
],
"source": [
"grammar = tracery.Grammar(new_trip_rules) # create a grammar object from the rules\n",
"grammar.add_modifiers(base_english) # add pre-programmed modifiers\n",
"\n",
"for i in range(5):\n",
" print(grammar.flatten(\"#origin#\")) # and flatten, starting with origin rule"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment