1

好的,这是交易的人。我有two text files。每个都包含500行(句子)。

我已将它们加载到memory它们自己的数组中(数据类型:字符串)。让我们命名array A & B

接下来我得到first sentencein ,将array A其拆分为另一个用作分隔符,以便获取单词。array CSPACE

然后对于 中的每个句子array B,我再次将其拆分为array Dusing SPACEas delimiter 以获取单词,并将每个单词array C与每个单词进行比较以array D计算两个句子之间的百分比匹配。

我计算第一个句子array A与 中所有句子的平均匹配百分比array B

然后我将它存储到一个Array E包含所有句子array A及其平均匹配百分比的文件中。

我对first sentence数组 A 中的每个标题都做了上面的操作。

问题是处理数组 A 中的每个标题大约需要 15 秒。无论如何我可以优化这个时间以更快吗?

硬件: AMD Phenom I 32 位四核

代码:

Imports System.IO
Imports System.Object
Imports System.Xml
Imports System.Text.RegularExpressions

Module Module1

    Sub Main()
        'Important File Paths
        Dim titlesFilePath As String = Environment.CurrentDirectory & "\titles.txt"
        Dim xmlTitlesFilePath As String = Environment.CurrentDirectory & "\extractedTitles.txt"
        Dim stopWordsFilePath As String = Environment.CurrentDirectory & "\stopWords.txt"

        'Import Important Data From Files -> Memory
        Dim titles As Array = FileToArray(titlesFilePath)
        Dim stopWords As Array = FileToArray(stopWordsFilePath)
        Dim xmlDataUnprocessed As Array = FileToArray(xmlTitlesFilePath)

        'Delimters To Filter Titles For
        Dim userDefinedDelimeters(4, 1)

        userDefinedDelimeters(0, 0) = "-"
        userDefinedDelimeters(0, 1) = " "

        userDefinedDelimeters(1, 0) = ","
        userDefinedDelimeters(1, 1) = " "

        userDefinedDelimeters(2, 0) = "—"
        userDefinedDelimeters(2, 1) = " "

        userDefinedDelimeters(3, 0) = "'s"
        userDefinedDelimeters(3, 1) = ""

        userDefinedDelimeters(4, 0) = "'"
        userDefinedDelimeters(4, 1) = " "

        'Declare Important Variables
        Dim xmlData(xmlDataUnprocessed.Length / 2, 1)
        Dim xmlTurn = 0
        Dim xmlDataCount = 0

        'Create Feed Title/URL Array
        For i = 0 To (xmlDataUnprocessed.Length - 1)
            If xmlTurn = 0 Then
                xmlData(xmlDataCount, 0) = xmlDataUnprocessed(i)
                xmlTurn = 1
            Else
                xmlData(xmlDataCount, 1) = xmlDataUnprocessed(i)
                xmlTurn = 0

                xmlDataCount += 1
            End If
        Next


        'CPU-Intensive Stuff Occurs
        Dim xmlTitle As String
        Dim xmlTitleWords As Array
        Dim savedTitleWords As Array
        Dim titleResults(xmlData.GetUpperBound(0) - 1, 1)
        Dim titlePercentageMatch As Integer
        Dim numberOfTitlesMatched As Integer


        For i = 0 To xmlData.GetUpperBound(0) - 1
            Console.WriteLine("Working On Title No. " & i & " Out Of " & xmlData.GetUpperBound(0) - 1)
            titlePercentageMatch = 0
            numberOfTitlesMatched = 0

            xmlTitle = xmlData(i, 0)
            xmlTitle = processTitle(stopWords, userDefinedDelimeters, xmlTitle)
            xmlTitleWords = xmlTitle.Split(" ")

            For Each title In titles
                title = processTitle(stopWords, userDefinedDelimeters, title)
                savedTitleWords = title.split(" ")
                Dim compareResult = compareTitle(xmlTitleWords, savedTitleWords)
                If compareResult > 0 Then
                    titlePercentageMatch += compareResult
                    numberOfTitlesMatched += 1
                End If
            Next

            titleResults(i, 0) = xmlData(i, 0)
            titleResults(i, 1) = (titlePercentageMatch / numberOfTitlesMatched)
        Next

        For i = 0 To titleResults.GetUpperBound(0) - 1
            Console.WriteLine(titleResults(i, 0) & " ---> " & titleResults(i, 1) & vbCrLf)
        Next

        Console.Read()
    End Sub

    Function compareTitle(ByRef xmlTitleWords As Array, ByRef savedTitleWords As Array)
        Dim NumberOfMatches = 0

        For Each xmlWord In xmlTitleWords
            For Each savedWord In savedTitleWords
                If (xmlWord.ToString.ToLower = savedWord.ToString.ToLower) Then
                    NumberOfMatches += 1
                End If
            Next
        Next

        Return ((NumberOfMatches / xmlTitleWords.Length) * 100)
    End Function

    Function processTitle(ByRef stopWordArray As Array, ByRef delimArray As Array, ByVal title As String)
        title = removeStopWords(stopWordArray, title)
        title = removeDelims(delimArray, title)

        Return title
    End Function

    Function removeStopWords(ByRef stopWordsArray As Array, ByVal sentence As String)
        For i = 0 To stopWordsArray.Length - 1
            If sentence.ToLower.Contains(" " & stopWordsArray(i).ToString.ToLower & " ") = True Then
                sentence = Microsoft.VisualBasic.Strings.Replace(sentence, " " & stopWordsArray(i) & " ", " ", 1, -1, Constants.vbTextCompare)
                'ElseIf sentence.ToLower.Contains(stopWordsArray(i).ToString.ToLower & " ") = True Then
                'sentence = Microsoft.VisualBasic.Strings.Replace(sentence, stopWordsArray(i) & " ", "", 1, -1, Constants.vbTextCompare)
            End If

            sentence = Regex.Replace(sentence, "\s+", " ")

            Dim Words = sentence.ToLower.Split(" ")

            If Words(0).ToString.ToLower & " " = stopWordsArray(i).ToString.ToLower & " " Then
                sentence = sentence.Remove(0, stopWordsArray(i).ToString.ToLower.Length + 1)
            End If

            Words = sentence.ToLower.Split(" ")
            Dim LastWord = Words(Words.Length - 1)
            'Console.WriteLine(LastWord & "++")

            If " " & LastWord.ToString.ToLower = " " & stopWordsArray(i).ToString.ToLower Then
                sentence = sentence.Remove(sentence.Length - 1 - LastWord.Length, stopWordsArray(i).ToString.ToLower.Length + 1)
            End If

        Next

        sentence = Regex.Replace(sentence, "\s+", " ")

        Return sentence
    End Function

    Function removeDelims(ByRef delimArray As Array, ByVal sentence As String)
        For i = 0 To delimArray.GetUpperBound(0) - 1
            sentence = sentence.Replace(delimArray(i, 0), delimArray(i, 1))
        Next
        sentence = Regex.Replace(sentence, "\s+", " ")
        Return sentence
    End Function

    Function FileToArray(ByVal filePath As String) As String()
        Dim content As String
        Dim lines As New ArrayList
        Dim sr As System.IO.StreamReader

        ' read the file's lines into an ArrayList
        Try
            sr = New System.IO.StreamReader(filePath)
            Do While sr.Peek() >= 0
                lines.Add(sr.ReadLine())
            Loop
        Finally
            If Not sr Is Nothing Then sr.Close()
        End Try

        ' convert from ArrayList to a String array
        Return CType(lines.ToArray(GetType(String)), String())
    End Function

End Module

编辑:我希望它不会太混乱。对于那个很抱歉! 编辑2:提供酱汁:P

4

3 回答 3

2

您的基本算法是N*M*A 2其中

  • N = 第一个文件中的标题数
  • M = 第二个文件中的标题数
  • A = 每个标题的平均字数

如果您有 500*500*5 2,则您将推送 6,250,000 个不区分大小写的字符串比较。但这就是你所做的一切。您的内部循环要求processTitle每个title外部循环的长度。它不需要这样做。

单线程

您可以做的是有一个预处理步骤,将每个单词替换为代表该单词的整数(符号)。为此,您将使用字典来查找符号,如果没有,则分配一个新的唯一符号(例如,保留一个整数计数器并使用下一个值)。

然后,您的主处理循环将与以前类似,但您正在执行整数比较(快得多)。实际上,您希望此处理步骤进行比较和收集统计信息。其他所有东西都应该移出。

多线程

保留您的预处理步骤。

并行化您的处理步骤。一种方法是将Parallel.For()用于最外层循环:Parallel.For(0, xmlData.GetUpperBound(0) - 1, Sub(i) ... End Sub)其中动作是您的循环体。TPL 可能会很好地平衡负载(均匀地使用 4 个内核)。

另一种方法是使用任务并行库来启动对 1/4 数据进行操作的任务。然后开始使用结果的延续。

于 2012-11-14T19:56:31.290 回答
1

一次只读取array B内存,然后逐行读取array A

在适用的情况下使用 StringBuilder 而不是字符串。

另外,看看你是否可以使用并行处理,即:任务。

至于距离,匹配算法,你没有提到它是什么以及它是如何做的。很难说什么。

于 2012-11-14T18:59:58.463 回答
1

你在一些事情上放慢了自己的速度......

  1. If (xmlWord.ToString.ToLower = savedWord.ToString.ToLower) Then

    为什么不将所有内容都更改为小写,然后再将其放入数组中?

  2. title = processTitle(stopWords, userDefinedDelimeters, title)

    为什么不在将它加载到数组中之前这样做呢?

提前完成所有处理,然后单独进行比较。

于 2012-11-14T19:18:29.113 回答