数碎片

问题描述 投票:0回答:1

我编写了一系列宏来提取小说中的句子和单词。对每个单词的数量进行计数,并且对于每个单词,提供包含该单词的例句。句子多样性被优化,这意味着每当找到一个单词时,如果与该单词相关的前一个句子的频率(语料库内的总频率)为 n,则如果该单词在语料库中的频率为 n,则将选择为该单词选择的新句子。小于 n。

我遇到的问题是片段(不是正确句子的东西,例如标题)。

我想要做的是计算片段中的单词(只要单词不是全部大写——这些单词就被简单地删除),并将片段与语料库的其余部分合并。

片段是人为分配的频率,从 10001(而不是 1)开始,以便尽快将它们从语料库中取出。

我已经兜圈子好几天了。我可以正确计算非碎片。但是当我尝试添加片段时,我发现其中一些片段没有被标记为片段(频率 > 10000),或者它们的频率没有正确计算。

如果此描述令人困惑,我很抱歉。连我都感到困惑。如果需要,请要求澄清。

这是代码。我非常感谢您能提供的任何帮助。我知道我的代码没有达到应有的简化程度。这是我可以(几乎)工作的唯一版本。

此代码查看标题为“Raw_Data”的工作表。 A 列是单词。如果单词全部大写,则为空。 B 栏是句子。

B 列中总是有内容,因此当宏在 B 列中发现空白时结束。

如果B列内容是片段,则C列为“片段”,否则为空白。

任何帮助将不胜感激。非常感谢你们。新手编码器。抱歉。

Sub Macro_05_FindFrequencies()
Dim FrequencyDict As New Dictionary
Dim SentenceDict As New Dictionary

Dim SentenceFrequencyDict As New Dictionary
Dim Row As Long
Dim Word, Sentence, OldSentence As String
Dim SentenceLength As Integer
Dim TargetWord As Variant
Dim Flag As Boolean


Row = 1

Do Until Sheets("Raw_Data").Range("B" & Row).Value = ""

Do Until Sheets("Raw_Data").Range("A" & Row).Value <> ""
Row = Row + 1
Loop

Word = Sheets("Raw_Data").Range("A" & Row).Value
Sentence = Sheets("Raw_Data").Range("B" & Row).Value



If FrequencyDict.Exists(Word) = True Then
FrequencyDict(Word) = FrequencyDict(Word) + 1
Flag = False
OldSentence = SentenceDict(Word)
    
    If SentenceFrequencyDict.Exists(Sentence) = True And SentenceFrequencyDict(OldSentence) >= SentenceFrequencyDict(Sentence) Then
    SentenceFrequencyDict(Sentence) = SentenceFrequencyDict(Sentence) + 1
    SentenceFrequencyDict(OldSentence) = SentenceFrequencyDict(OldSentence) - 1
    SentenceDict(Word) = Sentence
    Flag = True
    End If
    
    If Flag = False And SentenceFrequencyDict.Exists(Sentence) = True And SentenceFrequencyDict(OldSentence) < SentenceFrequencyDict(Sentence) Then
    SentenceDict(Word) = OldSentence
    Flag = True
    End If
        
    If Flag = False And SentenceFrequencyDict.Exists(Sentence) = False Then
    SentenceDict(Word) = Sentence
    SentenceFrequencyDict(Sentence) = 1
        If Sheets("Raw_Data").Range("C" & Row).Value = "Fragment" Then
         SentenceFrequencyDict(Sentence) = 10001
         End If
    End If
         

End If
 
 
If FrequencyDict.Exists(Word) = False Then
    FrequencyDict(Word) = 1
    SentenceDict(Word) = Sentence
    Flag = False

    If SentenceFrequencyDict.Exists(Sentence) = False Then
            SentenceFrequencyDict(Sentence) = 1
            Flag = True
            If Sheets("Raw_Data").Range("C" & Row).Value = "Fragment" Then
            SentenceFrequencyDict(Sentence) = 10001
            End If
    End If
    
   
   If Flag = False And SentenceFrequencyDict.Exists(Sentence) = True Then
            SentenceFrequencyDict(Sentence) = SentenceFrequencyDict(Sentence) + 1
   End If

   
End If

Row = Row + 1
Loop

Row = 1
For Each TargetWord In FrequencyDict.Keys
Sheets("Frequencies").Range("A" & Row).Value = TargetWord
Sheets("Frequencies").Range("B" & Row).Value = FrequencyDict(TargetWord)
Sheets("Frequencies").Range("C" & Row).Value = SentenceDict(TargetWord)
Sentence = Sheets("Frequencies").Range("C" & Row).Value
Sheets("Frequencies").Range("D" & Row).Value = Len(Sentence)
Sheets("Frequencies").Range("E" & Row).Value = SentenceFrequencyDict(Sentence)

Row = Row + 1
Next TargetWord


End Sub
excel vba
1个回答
0
投票

在评估 if-then 语句中是否存在 VBA 字典条目时,您只有一次机会。 当您有多个 if-then 语句评估相同的字典条目是否存在时,第一个语句之后的语句将毫无用处,因为评估过程会导致不存在的条目存在。因此,避免在 if-then 语句之前使用类似的内容直接评估字典条目的存在性 -

enter code here
存在标志=假
enter code here
如果 Dict.Exists(Entry) = True 那么
enter code here
存在标志=真
enter code here
结束如果 然后在 if-thens 中使用 ExistenceFlag。

© www.soinside.com 2019 - 2024. All rights reserved.