SequenceMatcher in Python and Calling Python Script in PHP

|
| By Webner

Introduction to SequenceMatcher in Python and Calling Python Script in PHP

SequenceMatcher is a class in Python which compares pairs of sequences of any type.SequenceMatcher is a class which comes under the difflib module. It provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce difference information in various formats, including HTML and context and unified diffs.

difflib.SequenceMatcher
Python Script in PHP

Example of SequenceMatcher in Python

To use SequenceMatcher and integrate some of its modules in the Python code we need:
difflib : to use the SequenceMatcher class
Sys : (System-specific parameters and functions) It is to access some variables used or maintained by the interpreter.

Python code is as follows:

import difflib
import sys

a=sys.argv[1]
b=sys.argv[2]
seq=difflib.SequenceMatcher(None, a,b)
d=seq.ratio()*100
print d

Sys.argv :sys.argv is a list in Python, which contains the command-line arguments passed to the script.

Then save this script in a file like sequencematcher.py

Integrate this script with PHP:

Once Python code is written then call that function in PHP code. For this use shell_exec method which executes command via shell and returns the complete output as a string.

$my_url = 'company';
$my_refer = 'com_';
$output = shell_exec("Python sequencematcher.py .$my_refer .$my_url");
print_r($output);

Here we are passing two variables to the Python script which will be compared by Python code then the printed result in Python code will be the return value of shell_exec() command received in $output variable.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *