c++字符串求相似度


#pragma once

#include <cstddef>
#include <cstdint>
#include <algorithm>
#include <string>

template <typename T = double>
inline std::enable_if_t<std::is_floating_point_v<T>, T>
jaroSimilarity(const std::string source,
    const std::string target)
{
    if (source == target)
        return 1;
    if (source.empty() || target.empty())
        return 0;

    const auto sl = source.length();
    const auto tl = target.length();

    const auto match_distance = std::max(sl, tl) < 2
        ? 0
        : std::max(sl, tl) / 2 - 1;

    auto source_matches = std::make_unique<bool[]>(sl);
    auto target_matches = std::make_unique<bool[]>(tl);
    std::size_t matches = 0;
    for (std::size_t i = 0; i < sl; ++i) {
        const auto end = std::min(i + match_distance + 1, tl);
        const auto start = i > match_distance ? (i - match_distance) : 0u;
        for (auto k = start; k < end; ++k) {
            if (!target_matches[k] && source[i] == target[k]) {
                target_matches[k] = source_matches[i] = true;
                ++matches;
                break;
            }
        }
    }
    if (matches == 0) {
        return 0;
    }

    std::size_t t = 0;
    for (std::size_t i = 0, k = 0; i < sl; ++i) {
        if (source_matches[i]) {
            while (!target_matches[k]) ++k;
            if (source[i] != target[k]) ++t;
            ++k;
        }
    }

    const T m = static_cast<T>(matches);
    return (m / sl + m / tl + 1 - t / m / 2) / 3.0;
}

bool jaroWinklerSimilarity(const std::string source,
    const std::string target,
    const std::size_t prefix = 2,
    const double boost_treshold = 0.7,
    const double scaling_factor = 0.1)
{
    const auto similarity = jaroSimilarity<double>(source, target);

    if (similarity > boost_treshold) {
        const auto l = std::min({ source.length(), target.length(), prefix });
        std::size_t common_prefix = 0;
        for (; common_prefix < l; ++common_prefix) {
            if (source[common_prefix] != target[common_prefix]) break;
        }
        return (similarity
            + scaling_factor * common_prefix * (1 - similarity))>boost_treshold;
    }

    return false;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹平华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值