Document similarity

更新时间:
复制 MD 格式

The Document Similarity component in Machine Learning Designer calculates how alike two text columns are, treating each space-separated word as the unit of comparison. It runs on MaxCompute and supports five algorithm families — covering edit-based, kernel-based, and fingerprint-based approaches.

Document similarity uses words as its unit of comparison (space-separated tokens). To compare character-by-character, use the String Similarity component instead.

Choose a similarity method

Each method outputs either a distance (higher = more different) or a similarity (higher = more alike). For distance methods, similarity is derived as Similarity = 1 − Distance.

Method Output type Output parameter Best for
levenshtein Distance levenshtein Counting word-level edits between two documents
levenshtein_sim (default) Similarity levenshtein_sim General-purpose word-edit similarity
lcs Distance lcs Finding the longest shared word sequence (distance view)
lcs_sim Similarity lcs_sim Finding the longest shared word sequence (similarity view)
ssk Similarity ssk Subsequence-based matching with configurable word-pair weight
cosine Similarity cosine Bag-of-words angle similarity; well-suited for longer documents
simhash_hamming Distance simhash_hamming Near-duplicate detection via 64-bit fingerprints (distance view)
simhash_hamming_sim Similarity simhash_hamming_sim Near-duplicate detection via 64-bit fingerprints (similarity view)

Distance-to-similarity formulas:

  • Levenshtein and LCS: Similarity = 1 − Distance

  • Simhash_Hamming: Similarity = 1 − Distance / 64.0

How Simhash_Hamming works: SimHash maps each document to a 64-bit binary fingerprint. The Hamming distance counts the number of characters of binary fingerprints on the same position that differ. See Similarity Estimation Techniques from Rounding Algorithms and Hamming distance (Wikipedia) for background.

Limitations

The Document Similarity component requires MaxCompute computing resources.

Configure the component

Method 1: PAI console

On the pipeline page in Machine Learning Designer, configure the following parameters.

Fields setting tab

Parameter Description
First column for similarity calculation The first input column. Defaults to the first string column in the table.
Second column for similarity calculation The second input column. Defaults to the second string column in the table.
Columns appended to output table Additional columns from the input table to include in the output.
Similarity column in output table Name of the output similarity column. Default: output. The name can be up to 128 characters and must start with a letter; only letters, digits, and underscores (_) are allowed.

Parameters setting tab

Parameter Description
Similarity calculation method The algorithm to use. Valid values: levenshtein, levenshtein_sim (default), lcs, lcs_sim, ssk, cosine, simhash_hamming, simhash_hamming_sim.
Substring length (available in SSK and Cosine) The value of k — the number of words combined into each unit for comparison. Active when the method is levenshtein, ssk, or cosine. Valid values: (0, 100]. Default: 2.
Matching word pair weight (available in SSK) The lambda weight applied to matched word pairs in SSK. Active when the method is ssk. Valid values: (0, 1). Default: 0.5.
If k exceeds the number of words in either input string, the cosine and ssk methods return a similarity of 0. Set k to a value no greater than the word count of the shorter input.

Tuning tab

Parameter Description
Computing cores Number of cores for the computation. Defaults to a system-determined value.
Memory size per core (unit: MB) Memory per core in MB. Defaults to a system-determined value.

Method 2: PAI commands

Run PAI commands using an SQL script component or an ODPS SQL node.

PAI -name doc_similarity
    -project algo_public
    -DinputTableName="pai_test_doc_similarity"
    -DoutputTableName="pai_test_doc_similarity_output"
    -DinputSelectedColName1="col0"
    -DinputSelectedColName2="col1"
Parameter Required Description Default
inputTableName Yes Name of the input table.
outputTableName Yes Name of the output table.
inputSelectedColName1 No First column used for similarity calculation. First string column in the table
inputSelectedColName2 No Second column used for similarity calculation. Second string column in the table
inputAppendColNames No Columns from the input table to append to the output. None
inputTablePartitions No Partitions to read from the input table. Full table
outputColName No Name of the similarity output column. Max 128 characters; must start with a letter; only letters, digits, and underscores allowed. output
method No Similarity calculation method. Valid values: levenshtein, levenshtein_sim, lcs, lcs_sim, ssk, cosine, simhash_hamming, simhash_hamming_sim. levenshtein_sim
lambda No Weight of a matched word pair for SSK. Valid values: (0, 1). 0.5
k No Substring length for SSK and Cosine. Valid values: (0, 100]. If k exceeds the word count of either input string, similarity outputs 0. 2
lifecycle No Lifecycle of the output table (days).
coreNum No Number of cores for the computation. Automatically allocated
memSizePerCore No Memory per core in MB. Automatically allocated

Example

This example calculates Levenshtein similarity between two columns of city-name strings.

Step 1: Create the input table

Use an ODPS SQL node to create the input table. For details, see Develop an ODPS SQL task.

drop table if exists pai_doc_similarity_input;
create table pai_doc_similarity_input as
select * from
(
  select 0 as id, "Beijing Shanghai" as col0, "Beijing Shanghai" as col1 from dual
  union all
  select 1 as id, "Beijing Shanghai" as col0, "Beijing Shanghai Shenzhen" as col1 from dual
) tmp

Step 2: Run the similarity calculation

drop table if exists pai_doc_similarity_output;
PAI -name doc_similarity
    -project algo_public
    -DinputTableName=pai_doc_similarity_input
    -DoutputTableName=pai_doc_similarity_output
    -DinputSelectedColName1=col0
    -DinputSelectedColName2=col1
    -Dmethod=levenshtein_sim
    -DinputAppendColNames=id,col0,col1;

Output table: pai_doc_similarity_output

id col0 col1 output
0 Beijing Shanghai Beijing Shanghai 1.0
1 Beijing Shanghai Beijing Shanghai Shenzhen 0.6666666666666667

Row 0 scores 1.0 because both strings are identical. Row 1 scores approximately 0.667 because "Beijing Shanghai" shares 2 of 3 words with "Beijing Shanghai Shenzhen" after Levenshtein normalization.

FAQ

My cosine or ssk similarity output is always 0. What's wrong?

When k is greater than the number of words in either input string, both methods treat the two strings as identical in their internal representation and return a similarity of 0. Set k to a value no greater than the minimum word count across your input pairs.

What is the difference between distance and similarity outputs?

Methods named without _sim (for example, levenshtein, lcs, simhash_hamming) output a distance — a higher value means the two documents are more different. Methods ending in _sim output a similarity — a higher value means the two documents are more alike. The conversion is Distance = 1.0 − Similarity (or Distance = 64.0 × (1 − Similarity) for Simhash_Hamming).

Should I use document similarity or string similarity?

Document similarity treats space-separated tokens as comparison units, making it appropriate for multi-word text. If your input is a single string where character-level comparison matters (for example, product codes or short IDs), use the String Similarity component instead.

What's next