我有一个对象被保存在内存中。对象是“Trigram()”
这是一小部分字符串;
tmodels("en") = New String() {" th", "the"}
然后像这样使用
For Each key As String In tmodels.Keys
Dim t As String() = DirectCast(tmodels(key), String())
Models(key) = New TrigramModel(t)
Next
'release temp data structure
tmodels = Nothing
冒着发布过多整个 TrigramModel 类的风险。
Public Class TrigramModel
Private m_trigrams As Trigram()
Public ReadOnly Property Trigrams() As Trigram()
Get
Return m_trigrams
End Get
End Property
Public Sub New(ByVal trigramsAndCounts As Hashtable)
Dim keys2 As New List(Of String)()
Dim scores2 As New List(Of Integer)()
'convert hashtable to arrays
For Each key As String In trigramsAndCounts.Keys
keys2.Add(key)
scores2.Add(CInt(trigramsAndCounts(key)))
Next
Dim keys As String() = keys2.ToArray()
Dim scores As Integer() = scores2.ToArray()
' sort array results
Array.Sort(scores, keys)
Array.Reverse(keys)
Array.Reverse(scores)
'build final array
Dim result As New List(Of Trigram)()
For x As Integer = 0 To keys.Length - 1
result.Add(New Trigram(keys(x), scores(x)))
Next
m_trigrams = result.ToArray()
End Sub
Public Sub New(ByVal tgrams As String())
Dim keys2 As New List(Of String)()
Dim scores2 As New List(Of Integer)()
'convert hashtable to arrays
Dim score As Integer = 0
For Each key As String In tgrams
keys2.Add(key)
scores2.Add(System.Math.Max(System.Threading.Interlocked.Increment(score), score - 1))
Next
Dim keys As String() = keys2.ToArray()
Dim scores As Integer() = scores2.ToArray()
' sort array results
Array.Sort(scores, keys)
Array.Reverse(keys)
Array.Reverse(scores)
'build final array
Dim result As New List(Of Trigram)()
For x As Integer = 0 To keys.Length - 1
result.Add(New Trigram(keys(x), scores(x)))
Next
m_trigrams = result.ToArray()
End Sub
Public Function HasTrigram(ByVal trigram As String) As Boolean
For Each t As Trigram In m_trigrams
If t.t = trigram Then
Return True
End If
Next
Return False
End Function
Public Function GetScore(ByVal trigram As String) As Integer
For Each t As Trigram In m_trigrams
If t.t = trigram Then
Return t.score
End If
Next
Throw New Exception((Convert.ToString("No score found for '") & trigram) + "'")
End Function
End Class
Public Class Trigram
Public t As String = Nothing
Public score As Integer = 0
Public Sub New(ByVal t As String, ByVal s As Integer)
Me.t = t
score = s
End Sub
End Class
我该如何处理 Trigramm() 或您在本课程中看到的任何其他问题?