In this guide, I’ll show you how to integrate Python’s powerful Natural Language Processing (NLP) capabilities with your Laravel application. We’ll create a simple system where Laravel sends user input to a Python script for analysis and receives the processed data.
This is especially useful for features like sentiment analysis, text classification, or keyword extraction. Let’s dive in step by step, ensuring each step is easy to follow and implement.
Steps to Integrate Python NLP with Laravel
First, create or use an existing Laravel project. If you don’t have one, start by creating a new Laravel project:
composer create-project --prefer-dist laravel/laravel laravel-nlp
Ensure Python is installed on your system. Install the necessary Python libraries using pip. For this example, we’ll use the TextBlob library for sentiment analysis.
pip install textblob flask
Create a Python script (nlp_service.py) to handle the NLP processing.
from flask import Flask, request, jsonify
from textblob import TextBlob
app = Flask(__name__)
@app.route('/analyze', methods=['POST'])
def analyze():
data = request.get_json()
text = data.get('text', '')
# Perform sentiment analysis
analysis = TextBlob(text)
response = {
'polarity': analysis.sentiment.polarity,
'subjectivity': analysis.sentiment.subjectivity
}
return jsonify(response)
if __name__ == '__main__':
app.run(port=5000)
Run the Python script:
python nlp_service.py
This starts a Flask server on port 5000, which will act as the NLP API.
In Laravel, create a controller to handle user input and communicate with the Python API.
php artisan make:controller NLPController
Open app/Http/Controllers/NLPController.php and update it.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class NLPController extends Controller
{
public function analyze(Request $request)
{
$text = $request->input('text');
// Call the Python NLP API
$response = Http::post('http://127.0.0.1:5000/analyze', [
'text' => $text,
]);
return response()->json($response->json());
}
}
Define a route in routes/web.php to access the NLP feature.
use App\Http\Controllers\NLPController;
Route::post('/analyze', [NLPController::class, 'analyze']);
Create a simple form in a Blade template (resources/views/nlp.blade.php) to send text input for analysis.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NLP Integration</title>
</head>
<body>
<h1>NLP Sentiment Analysis</h1>
<form method="POST" action="/analyze">
@csrf
<textarea name="text" placeholder="Enter text here" rows="5" cols="30"></textarea><br><br>
<button type="submit">Analyze</button>
</form>
</body>
</html>
Run the Laravel application:
php artisan serve
You might also like:
In this article, we will see laravel 8 create a custom helper function example. As we all know laravel provides man...
Oct-12-2020
Hello, web developers! In this article, I will show you how to install and use Trix Editor in a Laravel 11 application,...
Aug-28-2024
In this article, we will see laravel 9 multiple authentications using middleware. Using middleware we authenticate the u...
Apr-13-2022
Hey everyone! Ever found yourself in need of a straightforward example for integrating search and pagination in a Larave...
Jan-01-2024