String similarity - Top N

Updated at:

For each row in an input table, this component calculates string similarity against all rows in a mapping table and returns the top N closest matches.

Choose a similarity method

Five similarity methods are available. The default, levenshtein_sim, works well for most use cases.

MethodFull name
levenshtein_sim (default)Levenshtein similarity
lcs_simLongest Common Subsequence similarity
sskString Subsequence Kernel
cosineCosine similarity
simhash_hamming_simSimHash Hamming similarity
The ssk, cosine, and simhash_hamming_sim methods require the k (substring length) parameter. The ssk and simhash_hamming_sim methods also require the lambda (matching string weight) parameter.

Configure the component

Configure the component on the Designer workflow page or using a PAI command.

Configure via the Designer UI

On the Designer workflow page, select the component and configure the following parameters.

Field settings tab

ParameterDescription
Columns to append from input tableColumns from the input table to include in the output
Columns to append from mapping tableColumns from the mapping table to include in the output
Left table column for similarity calculationColumn from the input table to use for similarity calculation
Mapping table column for similarity calculationColumn from the mapping table to use for similarity calculation. The component calculates similarity between each row in the input table and all strings in the mapping table, then returns the top N results.
Similarity column name in output tableName for the similarity score column in the output table. Must start with a letter and contain only letters (a–z, A–Z), digits, and underscores (_). Maximum length: 128 bytes. Default: output

Parameter settings tab

ParameterDescription
Number of top similarity valuesNumber of top matches to return per input row. Must be a positive integer. Default: 10
Similarity calculation methodSimilarity method to use. Default: levenshtein_sim. See Choose a similarity method
Substring length (k)Required when the method is ssk, cosine, or simhash_hamming_sim. Value range: (0, 100). Default: 2
Matching string weight (lambda)Required when the method is ssk or simhash_hamming_sim. Value range: (0, 1). Default: 0.5

Execution tuning tab

ParameterDescription
Number of coresNumber of CPU cores to allocate. Allocated automatically by default
Memory per core (MB)Memory per CPU core. Allocated automatically by default

Configure via PAI command

Run the component using the SQL Script component. For more information, see SQL Script.

PAI -name string_similarity_topn
    -project algo_public
    -DinputTableName="pai_test_string_similarity_topn"
    -DoutputTableName="pai_test_string_similarity_topn_output"
    -DmapTableName="pai_test_string_similarity_map_topn"
    -DinputSelectedColName="col0"
    -DmapSelectedColName="col1"

Parameters

ParameterRequiredDescriptionDefault
inputTableNameYesName of the input table—
mapTableNameYesName of the mapping table—
outputTableNameYesName of the output table—
inputSelectedColName1NoColumn from the input table to use for similarity calculationFirst STRING column in the table
inputSelectedColName2NoColumn from the mapping table to use for similarity calculationFirst STRING column in the table
mapSelectedColNameYesColumn from the mapping table to use for similarity calculation—
inputAppendColNamesNoColumns from the input table to include in the output table—
inputAppendRenameColNamesNoAliases for appended columns from the input table—
mapAppendColNamesNoColumns from the mapping table to include in the output table—
mapAppendRenameColNamesNoAliases for appended columns from the mapping table—
inputTablePartitionsNoPartitions of the input table to processAll partitions
mapTablePartitionsNoPartitions of the mapping table to processAll partitions
outputColNameNoName of the similarity score column in the output table. Same naming constraints as the UI parameteroutput
methodNoSimilarity method. Valid values: levenshtein_sim, lcs_sim, ssk, cosine, simhash_hamming_simlevenshtein_sim
kNoSubstring length. Required when method is ssk, cosine, or simhash_hamming_sim. Value range: (0, 100)2
lambdaNoMatching string weight. Required when method is ssk or simhash_hamming_sim. Value range: (0, 1)0.5
lifecycleNoNumber of days to retain the output table. Must be a positive integer—
coreNumNoNumber of CPU cores to allocateSystem-assigned
memSizePerCoreNoMemory per CPU core (MB)Automatically assigned

Example

  • Input data

    Use an ODPS SQL node to create the pai_ft_string_similarity_topn_input table:

    CREATE TABLE pai_ft_string_similarity_topn_input AS
    SELECT * FROM (
        SELECT 0 AS id, 'Beijing' AS col0
        UNION ALL
        SELECT 1 AS id, 'Beijing Shanghai' AS col0
        UNION ALL
        SELECT 2 AS id, 'Beijing Shanghai Shenzhen' AS col0
    ) tmp;

    Result:

    id

    col0

    0

    Beijing

    1

    Beijing Shanghai

    2

    Beijing Shanghai Shenzhen

  • Run the PAI command

    Run the following PAI command using an SQL Script component or ODPS SQL node:

    PAI -name string_similarity_topn
        -project algo_public
        -DinputTableName=pai_ft_string_similarity_topn_input
        -DmapTableName=pai_ft_string_similarity_topn_input
        -DoutputTableName=pai_ft_string_similarity_topn_output
        -DinputSelectedColName=col0
        -DmapSelectedColName=col0
        -DinputAppendColNames=col0
        -DinputAppendRenameColNames=input_col0
        -DmapAppendColNames=col0
        -DmapAppendRenameColNames=map_col0
        -Dmethod=simhash_hamming_sim
  • Output

    input_col0

    map_col0

    output

    Beijing

    Beijing

    1.0

    Beijing

    Beijing Shanghai

    0.90625

    Beijing

    Beijing Shanghai Shenzhen

    0.796875

    Beijing Shanghai

    Beijing Shanghai

    1.0

    Beijing Shanghai

    Beijing

    0.90625

    Beijing Shanghai

    Beijing Shanghai Shenzhen

    0.828125

    Beijing Shanghai Shenzhen

    Beijing Shanghai Shenzhen

    1.0

    Beijing Shanghai Shenzhen

    Beijing Shanghai

    0.828125

    Beijing Shanghai Shenzhen

    Beijing

    0.796875

Resource usage

This component uses M × N computational complexity: to find the closest matches for N input rows within a mapping table of M rows, the algorithm computes M × N pairwise similarity scores.

ResourceFormulaLimit
Worker count(M × N) / (1,024 × 1,024 × 32)Max 1,000 workers
Memory per workerN / 8 MB4 GB – 64 GB
Maximum computing units (CUs)1,000 × 64 / 416,000 CUs

One computing unit (CU) provides 4 GB of memory. For billing details, see Billing example for Machine Learning Designer.

What's next