【问题标题】:Potential Duplicates Detection, with 3 Severity Level潜在重复检测,具有 3 个严重级别
【发布时间】:2014-02-09 01:55:03
【问题描述】:

我想制作一个程序来检测具有 3 个严重级别的潜在重复项。 让我们考虑一下我的数据只有两列,但有数千行。 第二列中的数据仅用逗号分隔。数据示例:

Number | Material
1      | helmet,valros,42
2      | helmet,iron,knight
3      | valros,helmet,42
4      | knight,helmet
5      | valros,helmet,42
6      | plain,helmet
7      | helmet, leather

我的 3 个级别是:

非常高: A,B,C vs A,B,C

高: A,B,C vs B,C,A

一般: A,B,C vs A,B

目前我只能做第一关,我不知道如何做第二和第三关。

我尝试过的。

Sub duplicates_separation()

    Dim duplicate(), i As Long
    Dim delrange As Range, cell As Long
    Dim shtIn As Worksheet, shtOut As Worksheet



     Set shtIn = ThisWorkbook.Sheets("input")
    Set shtOut = ThisWorkbook.Sheets("output")

    x = 2
    y = 1

    Set delrange = shtIn.Range("b1:b10000")  'set your range here

   ReDim duplicate(0)
'search duplicates in 2nd column
    For cell = 1 To delrange.Cells.Count
        If Application.CountIf(delrange, delrange(cell)) > 1 Then
            ReDim Preserve duplicate(i)
            duplicate(i) = delrange(cell).Address
            i = i + 1
        End If
    Next


        'print duplicates
    For i = UBound(duplicate) To LBound(duplicate) Step -1
    shtOut.Cells(x, 1).EntireRow.Value = shtIn.Range(duplicate(i)).EntireRow.Value


End Sub

程序检测到的重复项:

3      | valros,helmet,42
 5      | valros,helmet,42

我的预期:

Number | Material
1      | helmet,valros,42
3      | valros,helmet,42
5      | valros,helmet,42
4      | knight,helmet
2      | helmet,iron,knight        

我有一个检测重复 lv 2 的想法,但我认为它会很复杂,并且会使程序变慢。

  1. 使用“文本到列”命令将第 2 列转换为列
  2. 从 A 到 Z 对列进行排序(按字母顺序)
  3. 连接列
  4. 在检测重复 lv 1 时像做一样计数

有没有办法检测第二和第三级重复?


更新

昨天我去朋友家咨询这个问题,但是他的解决方案是JAVA语言..>我不明白

public class ali {

    static void sPrint(String[] Printed) {
        for (int iC = 0; iC < Printed.length; iC++) {
            System.out.print(String.valueOf(Printed[iC]) + " | ");
        }
        System.out.println();
    }

    public static void main(String Args[]) {
        int defaultLength = 10;
        int indexID = 0;
        int indexDesc = 1;
        String[] DETECTORP1 = new String[defaultLength];
        String[] DETECTORP2 = new String[defaultLength];
        String[] DETECTORP3 = new String[defaultLength];
        String[] DETECTORP4 = new String[defaultLength];
        String[][] theString = new String[5][2];
        theString[0] = new String[]{"1", "A, B, C, D"};
        theString[1] = new String[]{"2", "A, B, C, D"};
        theString[2] = new String[]{"3", "A, B, C, D, E"};
        theString[3] = new String[]{"4", "A, B, D, C, E"};
        theString[4] = new String[]{"5", "A, B, D, C, E, F"};
        int P1 = 0;
        int P2 = 0;
        int P3 = 0;
        int P4 = 0;
        for (int iC = 0; iC < theString.length; iC++) {
            System.out.println(theString[iC][indexID] + " -> " + theString[iC][indexDesc]);
        }
        for (int iC = 0; iC < theString.length; iC++) {
            int LEX;
            String theReference[] = theString[iC][indexDesc].replace(",", ";;").split(";;");
            for (int iD = 0; iD < theString.length; iD++) {
                if (iC != iD) {
                    String theCompare[] = theString[iD][1].replace(",", ";;").split(";;");
                    if (theReference.length == theCompare.length) {
                        LEX=0;
                        int theLength = theReference.length;
                        for (int iE = 0; iE < theLength; iE++) {
                            if (theReference[iE].equals(theCompare[iE])) {
                                LEX += 1;
                            }
                        }
                        if (LEX == theLength) {
                            DETECTORP1[P1] = theString[iC][indexID] + " WITH " + theString[iD][indexID];
                            P1 += 1;
                        } else {
                            LEX = 0;
                            for (int iF = 0; iF < theReference.length; iF++) {
                                for (int iG = 0; iG < theCompare.length; iG++) {
                                    if (theReference[iF].equals(theCompare[iG])) {
                                        LEX += 1;
                                        break;
                                    }
                                }
                            }
                            if (LEX == theReference.length) {
                                DETECTORP2[P2] = theString[iC][indexID] + " WITH " + theString[iD][indexID];
                                P2 += 1;
                            }

                        }

                    } else {
                        LEX = 0;
                        if (theReference.length > theCompare.length) {
                            for (int iF = 0; iF < theReference.length; iF++) {
                                for (int iG = 0; iG < theCompare.length; iG++) {
                                    if (iG == iF) {
                                        if (theReference[iF].equals(theCompare[iF])) {
                                            LEX += 1;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (LEX <= theReference.length && LEX >= theCompare.length) {
                                DETECTORP3[P3] = theString[iC][indexID] + " WITH " + theString[iD][indexID];
                                P3 += 1;
                            }
                        } else {
                            LEX =0;
                            for (int iF = 0; iF < theCompare.length; iF++) {
                                for (int iG = 0; iG < theReference.length; iG++) {
                                    if (iG == iF) {
                                        if (theCompare[iF].equals(theReference[iF])) {
                                            LEX += 1;
                                        //    System.out.println(theReference[iG] + "==" + theCompare[iG]);
                                            break;
                                        }
                                    }
                                }
                            }
                            if (LEX <= theCompare.length && LEX >= theReference.length) {
                                DETECTORP3[P3] = theString[iC][indexID] + " WITH " + theString[iD][indexID];
                                P3 += 1;
                            }
                        }

                    }
                }

            }

        }
        sPrint(DETECTORP1);
        sPrint(DETECTORP2);
        sPrint(DETECTORP3);
    }
}

如何在 VBA 中做到这一点?

【问题讨论】:

  • 这可能会启发一种前进的方式:Levenshtein distance
  • 这很鼓舞人心,你知道如何在 VBA 中做到吗?
  • 嗯,你看过维基百科的文章吗?那里有两个示例实现。只需翻译成 VBA。
  • 我只知道VBA,呵呵。但我会试试的。

标签: vba excel duplicates


【解决方案1】:

真的,这取决于您要如何定义“严重性级别”。这是一种方法,不一定是最好的:使用 Levensthein 距离。

用一个字符的属性符号来表示您的每件商品,例如

H    helmet
K    knight
I    iron
$    Leather
^    Valros
╔    Plain
¢    Whatever
etc.

然后将您的材质列表转换为包含代表这些属性的字符序列的字符串:

HIK = helmet,iron,knight
¢H  = plain,helmet

然后计算这两个字符串之间的 Levenshtein 距离。那将是您的“严重程度”。

Debug.Print LevenshteinDistance("HIK","¢H")
'returns 3

Levenshtein 距离的两种实现在Wikipedia 中显示。事实上,你很幸运:StackOverflow 上的某个人ported this to VBA

在下面的 cmets 部分中,您说您不喜欢用一个字符的符号来表示每个可能的属性。很公平;我同意这有点傻。解决方法:事实上,可以调整 Levenshtein 距离算法,使其不查看字符串中的每个字符,而是查看数组的每个元素,并在此基础上进行比较。我将展示如何在my answer 中对您的后续问题进行此更改。

【讨论】:

  • 如果我的数据中有数千个属性,那么我必须单独定义代表我的数据的符号吗?
  • 顺便说一句,你的第一步怎么做?我真的很陌生,我想学习。
  • 1) 是的。 2)尝试一些东西;如果您遇到任何具体问题,请返回并发布问题。
  • 那么,levensthein 距离不是我的问题的解决方案。我已经更新了问题
  • 考虑到这一点,我相信您可以调整 Levensthein 距离代码,以便不需要用一个字符来表示每个属性。如果我处于你的位置,这就是我会尝试做的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
  • 2020-04-08
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多