Turn Text Into HERE Maps with Python NLTK

Solution

Server Setup

Configuration

#!/bin/bash 
export APP_ID_HERE=your-app-id-here
export APP_CODE_HERE=your-app-code-here

Structure

├── app   
| ├── __init__.py
│ └── api_1_0
│ ├── __init__.py
│ ├── demo.py
│ ├── health.py
├── HERE.sh
├── manage.py
├── config.py
└── requirements.txt
Flask
Flask-Script
gunicorn
nltk
requests

App

import os
import app from flask_script
import Manager, Server
app = app.create_app('default')
manager = Manager(app)
if __name__ == '__main__':
port = os.environ('PORT', 8000)
manager.add_command('runserver', Server(port=port))
manager.run()
import osclass Config(object):
SECRET_KEY = os.environ.get('FLASK_SECRET_KEY')
APP_ID_HERE = os.environ.get('APP_ID_HERE')
APP_CODE_HERE = os.environ.get('APP_CODE_HERE')
@staticmethod
def init_app(app):
pass
config = {'default': Config}
from config import config
from flask import Flask
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
from .api_1_0 import api as api_1_0_blueprint
app.register_blueprint(api_1_0_blueprint, url_prefix='/api/1.0')
return app
from flask import Blueprint
api = Blueprint('api', __name__)
from . import health
from . import demo

Healthcheck

from flask import jsonify
from flask import current_app as app
from . import api
@api.route('/health', methods=['GET'])
def handle_health():
return jsonify({
'hello': 'world',
'app_id_here': app.config['APP_ID_HERE'],
'app_code_here': app.config['APP_CODE_HERE']
})

Text

Venice
Mdina
Aswan
Soro
Gryfino

Extract

@api.route('/tokens', methods=['GET'])
def handle_tokenize():
# Take URL as input and fetch the body
url = request.args.get('url')
response = session.get('url')

# Parse HTML from the given URL
body = BeautifulSoup(response.content, 'html.parser')

# Remove JavaScript and CSS from our life
for script in body(['script', 'style']):
script.decompose()

text = body.get_text()

# Ignore punctuation
tokenizer = RegexpTokenizer(r'\w+')

# Ignore duplicates
tokens = set(tokenizer.tokenize(text))

# Remove any stop words
stop_words_set = set(stopwords.words())
tokens = [w for w in tokens if not w in stop_words_set]

# Now just get proper nouns
tagged = pos_tag(tokens)
tokens = [w for w,pos in tagged if pos in ['NNP', 'NNPS']]

return jsonify(list(tokens))
$ python
...
>>> import nltk
>>> nltk.download('stopwords')
>>> nltk.download('averaged_perceptron_tagger')
#!/bin/bash
curl http://localhost:8000/api/1.0/tokens?url=$1
["Retreat","Industry","Boushnak","Frise","National","Mesa","Chicago","Washington","Forest","Angeles","Canyons","Colorado",...]

Geocode

@api.route('/geocode', methods=['GET'])
def handle_geocode():
uri = 'https://geocoder.api.here.com/6.2/geocode.json'
headers = {}
params = {
'app_id': app.config['APP_ID_HERE'],
'app_code': app.config['APP_CODE_HERE'],
'searchtext': request.args.get('searchtext')
}

response = session.get(uri, headers=headers, params=params)
return jsonify(response.json())
#!/bin/bash
curl http://localhost:8000/api/1.0/geocode?searchtext=$1
"DisplayPosition": { "Latitude": 53.25676, "Longitude": 14.48947 } 

Map

@api.route('/mapview', methods=['GET'])
def handle_mapview():
uri = 'https://image.maps.api.here.com/mia/1.6/mapview'
headers = {}
params = {
'app_id': app.config['APP_ID_HERE'],
'app_code': app.config['APP_CODE_HERE'],
'poi': request.args.get('poi')
}

response = session.get(uri, headers=headers, params=params)
image_path = tempfile.mktemp()
open(image_path, 'wb').write(response.content)

return image_path

Summary

--

--

--

Manage Developer Relations @Dolby; Maker at Robot Garden; Previously HERE, GE, Rackspace, DreamWorks Animation, MathWorks, Carnegie Mellon

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create one use-case blog or case-study on how industries are using MongoDB.

Using animated sprites to make damage VFX in Unity

What they publish: This women’s site is primarily, buts 12301

Linked List using Python

Fully Automated NextJS Builds/Deployments (GitHub, AWS ECR, Fargate Service, ECS)- Part 1/4

B2B series: Create shorter XPath

Two Ways Convert Load to Gcash (2022 Guide)

Two Ways Convert Load to Gcash (2022 Guide)

Reducing P99 Latency to 150 μs and Hardware Cost by 75% with a Scale-Out DBMS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jayson DeLancey

Jayson DeLancey

Manage Developer Relations @Dolby; Maker at Robot Garden; Previously HERE, GE, Rackspace, DreamWorks Animation, MathWorks, Carnegie Mellon

More from Medium

Scrapy 101: Architecture and Lifecycle

Dynamic web scraping using selenium : scrape protected websites

<a href=”http://www.freepik.com">Designed by upklyak / Freepik</a>

Flask Introduction for API Development with Python and Jinja Templates

How to Create a Repl.it Template