A Python application for comparing pairs of strings using three metrics:
- Hamming Distance
- Jaccard Similarity
- T Similarity (Tanimoto with character frequencies)
The project applies Object-Oriented Programming (OOP) concepts such as abstraction, inheritance, polymorphism, and separation of responsibilities.
string-comparison-tool/
├── data/
│ ├── raw/
│ │ └── input.csv
│ └── processed/
│ └── output.csv
├── src/
│ ├── base.py
│ ├── control_flow.py
│ ├── hamming.py
│ ├── input_output.py
│ ├── jaccard.py
│ └── tanimoto.py
├── main.py
├── requirements.txt
└── README.md
- Python 3
- pandas
Go to the project directory:
cd string-comparison-toolCreate a virtual environment:
python3 -m venv .venvActivate it:
source .venv/bin/activateInstall the dependencies:
pip install -r requirements.txtThe program reads:
data/raw/input.csv
The CSV file must contain:
input_one,input_two
Each row contains two strings to compare.
Activate the virtual environment:
source .venv/bin/activateCreate the output directory if necessary:
mkdir -p data/processedRun the program from the project root:
python main.pyThe generated output is saved to:
data/processed/output.csv
input.csv
|
v
extract_data()
|
v
control_flow.run()
|
+----> Hamming
|
+----> Jaccard
|
+----> T Similarity
|
v
results
|
v
write_output()
|
v
output.csv
The output CSV contains:
input_one,input_two,H,J,T
Where:
H= Hamming DistanceJ= Jaccard SimilarityT= T Similarity
Example:
input_one,input_two,H,J,T
hello,hello,0.0,1.0,1.0
hello,hullo,1.0,0.6,0.6666666666666666
For Hamming Distance, strings with different lengths return an empty value because the algorithm requires strings of equal length.
+------------------------+
| SimilarityMetric |
| <<ABC>> |
+------------------------+
| + calculate(s1, s2) |
+-----------+------------+
|
+-----------------+-----------------+
| | |
v v v
+----------------+ +----------------+ +----------------+
| HammingDistance| |JaccardSimilarity| | TSimilarity |
+----------------+ +----------------+ +----------------+
| + calculate() | | + calculate() | | + calculate() |
+----------------+ +----------------+ +----------------+
+----------------+
| control_flow |
+----------------+
| + run() |
+-------+--------+
|
+--------------+--------------+
| | |
v v v
Hamming Jaccard TSimilarity
|
v
+----------------+
| input_output |
+----------------+
| + extract_data()|
| + write_output()|
+----------------+
Defines the common interface for all similarity algorithms through the abstract calculate() method.
Counts the positions that differ between two strings. Both strings must have the same length.
Compares the sets of characters in two strings using intersection and union.
Compares character frequencies using the sum of minimum frequencies divided by the sum of maximum frequencies.
Reads the input CSV with Pandas and writes the calculated results to the output CSV.
Coordinates the complete process: extraction, metric calculation, and output generation.
The entry point of the application. It starts the process by calling run().
- Python
- Pandas
- Object-Oriented Programming
- Abstract Base Classes
- CSV processing