Natural language flight search parser — extract structured flight data from freeform text
Project description
flights-nlp ✈️
A robust, Django-friendly natural language flight search query parser for English and Japanese. It translates freeform text queries into structured parameters ready for flight search APIs (like Amadeus, Sabre, or Skyscanner).
Features
- 🧠 Hybrid Route Extraction: Combines spaCy NLP models (
en_core_web_sm/ja_core_news_sm) with geographic database lookups to reliably extract origin and destination cities/airports, resolving sequence ambiguities. - 🇯🇵 Spaceless Japanese Support: Fully parses spaceless Japanese query layouts (e.g.,
東京から札幌明日片道). - 📍 Multi-Airport Cities (MAC): Resolves city and airport names (e.g.,
羽田,成田) to their IATA codes and ties them back to parent metropolitan codes (e.g.,TYO). - 🔍 Airport Disambiguation: Extracts parenthesized hints like
大阪(伊丹)to target specific airports (e.g.,ITM) instead of whole metropolitan areas. - 📅 Relative Dates & Durations: Parses relative terms (
today,tomorrow,next week,来週の月曜日), relative offsets (3日後,2週間後), and trip durations (for a week,1週間,3日間). - 👥 Passenger Parsing: Normalizes numbers and counts for adults, children, and infants (e.g.,
大人二人,2 adults 1 infant). - 🛡️ Django Ready: Fully optimized for web frameworks with startup model pre-loading (
preload_models), custom exception classes, and standard Python logging hooks.
Installation
Install the package via pip or uv:
pip install flights-nlp
Note: SpaCy language pipelines (en-core-web-sm and ja-core-news-sm) will be resolved and loaded during parsing or preloading.
Quick Start (Python API)
Basic Parsing
from datetime import date
from flights_nlp import parse
# Parse a query relative to a reference date
result = parse("Tokyo to Sapporo next week 3 adults", reference_date=date(2026, 7, 12))
print(result.origin) # "Tokyo"
print(result.origin_iata) # ["NRT", "HND"]
print(result.origin_city_code) # "TYO"
print(result.destination) # "Sapporo"
print(result.destination_iata) # ["CTS", "OKD"]
print(result.departure_date) # 2026-07-20
print(result.adults) # 3
print(result.trip_type) # "round_trip"
Serializing to JSON
print(result.to_json(indent=2))
Django Startup Warmup (Model Pre-loading)
To prevent the initial web request from experiencing a ~2 second model loading latency, call preload_models inside your Django app's startup hook:
# myapp/apps.py
from django.apps import AppConfig
from flights_nlp import preload_models
class MyAppConfig(AppConfig):
name = "myapp"
def ready(self):
# Preload English and Japanese models
preload_models(["en", "ja"])
Custom Exception Handling
The library raises structured subclasses of FlightsNLPError (which inherits from ValueError):
RouteExtractionError: Raised when origin/destination cannot be resolved.DateResolutionError: Raised when date formats are invalid.PassengerParsingError: Raised when passenger parameters are faulty.
Command Line Interface (CLI)
The package installs a CLI tool flights-nlp to parse queries directly from the terminal.
JSON Output (Default)
flights-nlp "Tokyo to Sapporo next week 3 adults"
Table Output
flights-nlp "東京から札幌明日片道" --format table
Output:
┌────────────────┬─────────────────────────┐
│ Field │ Value │
├────────────────┼─────────────────────────┤
│ Origin │ Tokyo [TYO] (NRT,HND) │
│ Destination │ Sapporo [SPK] (CTS,OKD) │
│ Departure Date │ 2026-07-13 │
│ Return Date │ — │
│ Trip Type │ one_way │
│ Adults │ 1 │
│ Children │ 0 │
│ Infants │ 0 │
└────────────────┴─────────────────────────┘
Interactive Playground (Streamlit)
For developer testing, run the Streamlit visual debugger locally:
uv run streamlit run app.py
This starts a dashboard where you can type queries, inspect spaCy tokenization / entity tags, check JSON representations, and test various reference dates reactively.
Running Tests
To run the full unit test suite:
pytest tests/
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flights_nlp-0.1.1.tar.gz.
File metadata
- Download URL: flights_nlp-0.1.1.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2d42cc151122ef419c65788967f4cb762fd74387fc6bb5a2738507f7210ca3a
|
|
| MD5 |
5adbd385c27ce5b0124fdbe4a22ef5d2
|
|
| BLAKE2b-256 |
d3095b2600fb3897cc391d80e9280b20058b573511cf99e84666987c0463eb1d
|
File details
Details for the file flights_nlp-0.1.1-py3-none-any.whl.
File metadata
- Download URL: flights_nlp-0.1.1-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
beab4e2b90556c6c79035e1c205ff0f170cac37ff0847f0cabb48e44ef9076bf
|
|
| MD5 |
99e1fabb9a651f42a6c5bd4b12bbb888
|
|
| BLAKE2b-256 |
83403c7448609d75cd12b67059cceb702cf5e83cf3952e72b6c2242adef0495d
|