用于从标记数据集中提取正则表达式的技术

时间:2012-05-24 15:40:16

标签: regex algorithm nlp machine-learning

假设我有一个数十万个字符串的数据集(恰好是自然语言句子,如果重要的话),每个字符串都标有一个“标签”。每个句子都标记有一个标签,并且大约有10个标签,每个标签大约有10%的数据集属于它们。标签内的句子结构有很高的相似性。

我知道上面的声音听起来像机器学习问题的经典例子,但我想问一个稍微不同的问题。 是否有任何已知的技术以编程方式为每个标签生成一组正则表达式,这些技术可以成功地对训练数据进行分类,同时仍然可以推广到未来的测试数据?

我对参考文献非常满意;我意识到这不是一个简单的算法:)

PS:我知道分类的常用方法是使用机器学习技术,如SVM等。但是,我明确地想要一种生成正则表达式的方法。 (我会很高兴使用机器学习技术来生成正则表达式,而不是使用机器学习技术来进行分类本身!)

3 个答案:

答案 0 :(得分:3)

这个问题通常被设计为如何从字符串集生成有限自动机,而不是正则表达式,尽管显然可以从FAs生成RE,因为它们是equivalent

如果你四处寻找自动归纳,你应该能够找到很多关于这个主题的文献,包括GA方法。

答案 1 :(得分:1)

据我所知,这是当前进化计算研究的主题。

以下是一些例子:

的幻灯片40-44

https://cs.byu.edu/sites/default/files/Ken_De_Jong_slides.pdf (截至发布此答案时存在幻灯片。)

另外,请参阅

http://www.citeulike.org/user/bartolialberto/article/10710768

对GECCO 2012上提出的系统进行更详细的审查。

答案 2 :(得分:0)

注意: 这可能会有所帮助。以下函数为给定值ab生成RegEx模式。其中ab都是字母字符串。该函数将生成一个公平的RegEx模式,以匹配ab之间的范围。该函数只需要前三个字符来生成模式,并生成一个result,它可能类似starts-with()函数,在某种语言中具有一般RegEx优势。

一个简单的VB.NET示例

Public Function GetRangePattern(ByVal f_surname As String, ByVal l_surname As String) As String
        Dim f_sn, l_sn As String
        Dim mnLength% = 0, mxLength% = 0, pdLength% = 0, charPos% = 0
        Dim fsn_slice$ = "", lsn_slice$ = ""
        Dim rPattern$ = "^"
        Dim alphas As New Collection
        Dim tmpStr1$ = "", tmpStr2$ = "", tmpStr3$ = ""

        '///init local variables
        f_sn = f_surname.ToUpper.Trim
        l_sn = l_surname.ToUpper.Trim

        '///do null check
        If f_sn.Length = 0 Or l_sn.Length = 0 Then
            Return "-!ERROR!-"
        End If

        '///return if both equal
        If StrComp(f_sn, l_sn, CompareMethod.Text) = 0 Then
            Return "^" & f_sn & "$"
        End If

        '///return if 1st_name present in 2nd_name
        If InStr(1, l_sn, f_sn, CompareMethod.Text) > 0 Then
            tmpStr1 = f_sn
            tmpStr2 = l_sn.Replace(f_sn, vbNullString)
            If Len(tmpStr2) > 1 Then
                tmpStr3 = "[A-" & tmpStr2.Substring(1) & "]*"
            Else
                tmpStr3 = tmpStr2 & "*"
            End If
            tmpStr1 = "^" & tmpStr1 & tmpStr3 & ".*$"
            tmpStr1 = tmpStr1.ToUpper
            Return tmpStr1
        End If

        '///initialize alphabets
        alphas.Add("A", CStr(Asc("A")))
        alphas.Add("B", CStr(Asc("B")))
        alphas.Add("C", CStr(Asc("C")))
        alphas.Add("D", CStr(Asc("D")))
        alphas.Add("E", CStr(Asc("E")))
        alphas.Add("F", CStr(Asc("F")))
        alphas.Add("G", CStr(Asc("G")))
        alphas.Add("H", CStr(Asc("H")))
        alphas.Add("I", CStr(Asc("I")))
        alphas.Add("J", CStr(Asc("J")))
        alphas.Add("K", CStr(Asc("K")))
        alphas.Add("L", CStr(Asc("L")))
        alphas.Add("M", CStr(Asc("M")))
        alphas.Add("N", CStr(Asc("N")))
        alphas.Add("O", CStr(Asc("O")))
        alphas.Add("P", CStr(Asc("P")))
        alphas.Add("Q", CStr(Asc("Q")))
        alphas.Add("R", CStr(Asc("R")))
        alphas.Add("S", CStr(Asc("S")))
        alphas.Add("T", CStr(Asc("T")))
        alphas.Add("U", CStr(Asc("U")))
        alphas.Add("V", CStr(Asc("V")))
        alphas.Add("W", CStr(Asc("W")))
        alphas.Add("X", CStr(Asc("X")))
        alphas.Add("Y", CStr(Asc("Y")))
        alphas.Add("Z", CStr(Asc("Z")))

        '///populate max-min length values
        mxLength = f_sn.Length
        If l_sn.Length > mxLength Then
            mnLength = mxLength
            mxLength = l_sn.Length
        Else
            mnLength = l_sn.Length
        End If
        '///padding values
        pdLength = mxLength - mnLength
        f_sn = f_sn.PadRight(mxLength, "A")
        'f_sn = f_sn.PadRight(mxLength, "~")
        l_sn = l_sn.PadRight(mxLength, "Z")
        'l_sn = l_sn.PadRight(mxLength, "~")

        '///get a range like A??-B??
        If f_sn.Substring(0, 1).ToUpper <> l_sn.Substring(0, 1).ToUpper Then
            fsn_slice = f_sn.Substring(0, 3).ToUpper
            lsn_slice = l_sn.Substring(0, 3).ToUpper
            tmpStr1 = fsn_slice.Substring(0, 1) & fsn_slice.Substring(1, 1) & "[" & fsn_slice.Substring(2, 1) & "-Z]"
            tmpStr2 = lsn_slice.Substring(0, 1) & lsn_slice.Substring(1, 1) & "[A-" & lsn_slice.Substring(2, 1) & "]"
            tmpStr3 = "^(" & tmpStr1 & "|" & tmpStr2 & ").*$"
            Return tmpStr3
        End If

        '///looping charwise
        For charPos = 0 To mxLength
            fsn_slice = f_sn.Substring(charPos, 1)
            lsn_slice = l_sn.Substring(charPos, 1)
            If StrComp(fsn_slice, lsn_slice, CompareMethod.Text) = 0 Then
                rPattern = rPattern & fsn_slice
            Else
                'rPattern = rPattern & "("
                If charPos < mxLength Then
                    Try
                        If Asc(fsn_slice) < Asc(lsn_slice) Then
                            tmpStr1 = fsn_slice & "[" & f_sn.Substring(charPos + 1, 1) & "-Z" & "]|"
                            If CStr(alphas.Item(Key:=CStr(Asc(fsn_slice) + 1))) < CStr(alphas.Item(Key:=CStr(Asc(lsn_slice) - 1))) Then
                                tmpStr2 = "[" & CStr(alphas.Item(Key:=CStr(Asc(fsn_slice) + 1))) & "-" & CStr(alphas.Item(Key:=CStr(Asc(lsn_slice) - 1))) & "]|"
                            Else
                                tmpStr2 = vbNullString
                            End If
                            tmpStr3 = lsn_slice & "[A-" & l_sn.Substring(charPos + 1, 1) & "]"
                            rPattern = rPattern & "(" & tmpStr1 & tmpStr2 & tmpStr3 & ").*$"
                            'MsgBox("f_sn:= " & f_sn & " -- l_sn:= " & l_sn & vbCr & rPattern)
                            Exit For
                        Else
                            Return "-#ERROR#-"
                        End If
                    Catch ex As Exception
                        Return "-|ERROR|-" & ex.Message
                    End Try
                End If
            End If
        Next charPos
        Return rPattern
    End Function

它被称为

?GetRangePattern("ABC","DEF")

生成此

"^(AB[C-Z]|DE[A-F]).*$"