Skip to main content

A set of similarity metricts to compare html files.

Project description

This package provides a set of functions to measure the similarity between web pages.

Install

The quick way:

pip install html-similarity

How it works?

Structural Similarity

Uses sequence comparison of the html tags to compute the similarity, by default.

We not implement the similarity based on tree edit distance because it is slower than sequence comparison.

structural_similarity accepts an algorithm keyword to pick the comparison strategy:

  • indel (default): flat tag-sequence comparison using rapidfuzz’s bit-parallel Indel/LCS implementation. Fastest option, but blind to nesting (e.g. moving an element to a different parent without changing the overall tag order won’t affect the score).

  • pq_gram: tree-structure aware. Compares pq-gram profiles, which approximate Tree Edit Distance in roughly linear time while still capturing parent/child relationships. Slower than indel but catches structural changes that a flat sequence misses.

  • difflib: legacy flat tag-sequence comparison (the original implementation), kept mainly for benchmarking against indel.

See notebooks/structural_similarity_benchmark.ipynb for a notebook that compares the speed and the structural sensitivity of all three.

Style Similarity

Extracts css classes of each html document and calculates the jaccard similarity of the sets of classes.

Joint Similarity (Structural Similarity and Style Similarity)

The joint similarity metric is calculated as:

k * structural_similarity(document_1, document_2) + (1 - k) * style_similarity(document_1, document_2)

All the similarity metrics takes values between 0 and 1.

Recommendations for joint similarity

Using k=0.3 give use better results. The style similarity gives more information about the similarity rather than the structural similarity.

Examples

Here is a example:

In [1]: html_1 = '''
<h1 class="title">First Document</h1>
<ul class="menu">
    <li class="active">Documents</li>
    <li>Extra</li>
</ul>
'''

In [2]: html_2 = '''
<h1 class="title">Second document Document</h1>
<ul class="menu">
    <li class="active">Extra Documents</li>
</ul>
'''

In [3] from html_similarity import style_similarity, structural_similarity, similarity

In [4]: style_similarity(html_1, html_2)
Out[4]: 1.0

In [7]: structural_similarity(html_1, html_2)
Out[7]: 0.9090909090909091

In [8]: similarity(html_1, html_2)
Out[8]: 0.9545454545454546

References

Thanks

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

html_similarity-0.4.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

html_similarity-0.4.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file html_similarity-0.4.1.tar.gz.

File metadata

  • Download URL: html_similarity-0.4.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.1

File hashes

Hashes for html_similarity-0.4.1.tar.gz
Algorithm Hash digest
SHA256 267472a07bd49a38361043c5411fd0a5673bbfea8f941ab65feaf563a2f0e4fe
MD5 08f4dc1da0c27b444e04116e67c9ffc3
BLAKE2b-256 e9bd8896f7e1a792489bd6a2262cebd8b0d2d6116fb7e0d56feb3a5156b074b7

See more details on using hashes here.

File details

Details for the file html_similarity-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for html_similarity-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0ab3b08629b8a133f58975c886f38f7848b5f9e84930b7d3e3905247fec4e810
MD5 11fa6fdbf0062f6de841a224444234b6
BLAKE2b-256 2935d804482ea27f19f43a4a278d9279c79e4e2b0aeb74fbc38d92592381dd64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page