相关疑难解决方法(0)

使用Java中的Levenshtein距离改善搜索结果

我有以下工作Java代码,用于搜索单词列表中的单词,并且它完美地工作并且符合预期:

public class Levenshtein {
    private int[][] wordMartix;

    public Set similarExists(String searchWord) {

        int maxDistance = searchWord.length();
        int curDistance;
        int sumCurMax;
        String checkWord;

        // preventing double words on returning list
        Set<String> fuzzyWordList = new HashSet<>();

        for (Object wordList : Searcher.wordList) {
            checkWord = String.valueOf(wordList);
            curDistance = calculateDistance(searchWord, checkWord);
            sumCurMax = maxDistance + curDistance;
            if (sumCurMax == checkWord.length()) {
                fuzzyWordList.add(checkWord);
            }
        }
        return fuzzyWordList;
    }

    public int calculateDistance(String inputWord, String checkWord) {
        wordMartix = new int[inputWord.length() + 1][checkWord.length() + 1];

        for …
Run Code Online (Sandbox Code Playgroud)

java algorithm search search-engine levenshtein-distance

31
推荐指数
2
解决办法
2243
查看次数

余弦相似度与汉明距离

为了计算两个文档之间的相似性,我创建了一个包含术语频率的特征向量.但是,对于下一步,我不能在" 余弦相似度 "和" 汉明距离 " 之间做出决定.

我的问题:您对这些算法有经验吗?哪一个给你更好的结果?

除此之外:你能告诉我如何在PHP中编码余弦相似度吗?对于汉明距离,我已经得到了代码:

function check ($terms1, $terms2) {
    $counts1 = array_count_values($terms1);
    $totalScore = 0;
    foreach ($terms2 as $term) {
        if (isset($counts1[$term])) $totalScore += $counts1[$term];
    }
    return $totalScore * 500 / (count($terms1) * count($terms2));
}
Run Code Online (Sandbox Code Playgroud)

我不想使用任何其他算法.我只想帮助两者之间做出决定.

也许有人可以说一些如何改进算法.如果你过滤掉停用词或常用词,你会得到更好的结果吗?

我希望你能帮助我.提前致谢!

php similarity relationship

18
推荐指数
3
解决办法
1万
查看次数

PHP中的字符串相似性:levenshtein类似于长字符串的函数

levenshteinPHP中的函数适用于最大长度为255的字符串.在PHP中计算句子相似度得分的好方法是什么.

基本上我有一个句子数据库,我想找到近似重复. similar_text功能不给我预期的结果.对我来说,检测类似句子的最简单方法是:

$ss="Jack is a very nice boy, isn't he?";
$pp="jack is a very nice boy is he";

$ss=strtolower($ss);  // convert to lower case as we dont care about case
$pp=strtolower($pp);

$score=similar_text($ss, $pp);
echo "$score %\n";  // Outputs just 29 %

$score=levenshtein ( $ss, $pp );
echo "$score\n";  // Outputs '5', which indicates they are very similar. But, it does not work for more than 255 chars :(
Run Code Online (Sandbox Code Playgroud)

php similarity string-matching

8
推荐指数
1
解决办法
8959
查看次数