👨‍💻 dev railbot the weighted beefup

development

owly

闇の伝説
Staff member
戦闘 コーダー
project progress at 100%
 
0% 100%


A) digivolve limuniqueQ->WeightedResponder (remove redundant attributes to free up garbage collection)
B) requip ECV2 with digivoluved ver.

PLs:
+Jv
+KT
+py
+vb.net
+C#
+swift
+vb dll
+c#dll


@fukurou
 
Last edited by a moderator:

owly

闇の伝説
Staff member
戦闘 コーダー
Code:
Public Function GetAResponse() As String
    Dim size As Integer = _responses.Count
    If size = 0 Then Return String.Empty

    ' Assign weights: newer items get higher weight
    Dim weights(size - 1) As Integer
    Dim totalWeight As Integer = 0
    For i As Integer = 0 To size - 1
        weights(i) = i + 1
        totalWeight += weights(i)
    Next

    ' Weighted random selection
    Dim rnd As New Random()
    Dim pick As Integer = rnd.Next(totalWeight)
    Dim cumulative As Integer = 0
    For i As Integer = 0 To size - 1
        cumulative += weights(i)
        If pick < cumulative Then
            Return _responses(i)
        End If
    Next

    Return _responses(size - 1)
End Function
 

fukurou

the supreme coder
ADMIN
Code:
Public Class WeightedResponder
    Private responses As List(Of String)
    Private ReadOnly lim As Integer

    Public Sub New(lim As Integer)
        responses = New List(Of String)()
        Me.lim = lim
    End Sub

    Public Function GetAResponse() As String
        Dim size As Integer = responses.Count
        If size = 0 Then Return ""

        Dim totalWeight As Integer = 0
        Dim weights(size - 1) As Integer
        For i As Integer = 0 To size - 1
            weights(i) = i + 1
            totalWeight += weights(i)
        Next

        Dim rnd As New Random()
        Dim pick As Integer = rnd.Next(totalWeight)

        Dim cumulative As Integer = 0
        For i As Integer = 0 To size - 1
            cumulative += weights(i)
            If pick < cumulative Then
                Return responses(i)
            End If
        Next

        Return responses(size - 1)
    End Function

    Public Function ResponsesContainsStr(item As String) As Boolean
        Return responses.Contains(item)
    End Function

    Public Function StrContainsResponse(item As String) As Boolean
        For Each response As String In responses
            If String.IsNullOrEmpty(response) Then Continue For
            If item.Contains(response) Then Return True
        Next
        Return False
    End Function

    Public Sub AddResponse(s1 As String)
        If responses.Contains(s1) Then
            responses.Remove(s1)
            responses.Add(s1)
            Return
        End If
        If responses.Count > lim - 1 Then
            responses.RemoveAt(0)
        End If
        responses.Add(s1)
    End Sub

    Public Sub AddResponses(ParamArray replies() As String)
        For Each value As String In replies
            AddResponse(value)
        Next
    End Sub

    Public Function GetSavableStr() As String
        Return String.Join("_", responses)
    End Function

    Public Function GetLastItem() As String
        If responses.Count = 0 Then Return ""
        Return responses(responses.Count - 1)
    End Function

    Public Function CloneObj() As WeightedResponder
        Dim clonedResponder As New WeightedResponder(Me.lim)
        clonedResponder.responses = New List(Of String)(Me.responses)
        Return clonedResponder
    End Function
End Class
 
Last edited:

fukurou

the supreme coder
ADMIN
Code:
Public Class EventChatV2
    Private _dictionary As New Dictionary(Of String, WeightedResponder)()
    Private _modifiedKeys As New HashSet(Of String)()
    Private ReadOnly _lim As Integer

    Public Sub New(lim As Integer)
        _lim = lim
    End Sub

    Public Function GetModifiedKeys() As HashSet(Of String)
        Return _modifiedKeys
    End Function

    Public Function KeyExists(key As String) As Boolean
        ' if the key was active true is returned
        Return _modifiedKeys.Contains(key)
    End Function

    ' Add items
    Public Sub AddItems(ur As WeightedResponder, ParamArray args As String())
        For Each arg In args
            _dictionary(arg) = ur.CloneObj()
        Next
    End Sub

    Public Sub AddFromDB(key As String, value As String)
        If String.IsNullOrEmpty(value) OrElse value = "null" Then
            Return
        End If
        Dim values As String() = value.Split("_"c)
        If Not _dictionary.ContainsKey(key) Then
            _dictionary(key) = New WeightedResponder(_lim)
        End If
        For Each item In values
            _dictionary(key).AddResponse(item)
        Next
    End Sub

    ' Add key-value pair
    Public Sub AddKeyValue(key As String, value As String)
        _modifiedKeys.Add(key)
        If _dictionary.ContainsKey(key) Then
            _dictionary(key).AddResponse(value)
        Else
            Dim newResponder = New WeightedResponder(_lim)
            newResponder.AddResponse(value)
            _dictionary(key) = newResponder
        End If
    End Sub

    Public Sub AddKeyValues(elizaResults As List(Of AXKeyValuePair))
        For Each pair In elizaResults
            ' Access the key and value of each AXKeyValuePair object
            AddKeyValue(pair.GetKey(), pair.GetValue())
        Next
    End Sub

    ' Get response
    Public Function Response(in1 As String) As String
        If _dictionary.ContainsKey(in1) Then
            Return _dictionary(in1).GetAResponse()
        End If
        Return String.Empty
    End Function

    Public Function ResponseLatest(in1 As String) As String
        If _dictionary.ContainsKey(in1) Then
            Return _dictionary(in1).GetLastItem()
        End If
        Return String.Empty
    End Function

    Public Function GetSaveStr(key As String) As String
        If _dictionary.ContainsKey(key) Then
            Return _dictionary(key).GetSavableStr()
        End If
        Return String.Empty
    End Function
End Class
 

fukurou

the supreme coder
ADMIN
Swift:
class WeightedResponder {
    private var responses: [String]
    private let lim: Int

    init(lim: Int) {
        self.responses = []
        self.lim = lim
    }

    func getAResponse() -> String {
        let size = responses.count
        if size == 0 { return "" }

        var weights = [Int](repeating: 0, count: size)
        var totalWeight = 0
        for i in 0..<size {
            weights[i] = i + 1
            totalWeight += weights[i]
        }

        let pick = Int.random(in: 0..<totalWeight)
        var cumulative = 0
        for i in 0..<size {
            cumulative += weights[i]
            if pick < cumulative {
                return responses[i]
            }
        }

        return responses[size - 1]
    }

    func responsesContainsStr(_ item: String) -> Bool {
        return responses.contains(item)
    }

    func strContainsResponse(_ item: String) -> Bool {
        for response in responses {
            if response.isEmpty { continue }
            if item.contains(response) { return true }
        }
        return false
    }

    func addResponse(_ s1: String) {
        if let index = responses.firstIndex(of: s1) {
            responses.remove(at: index)
            responses.append(s1)
            return
        }
        if responses.count > lim - 1 {
            responses.removeFirst()
        }
        responses.append(s1)
    }

    func addResponses(_ replies: String...) {
        for value in replies {
            addResponse(value)
        }
    }

    func getSavableStr() -> String {
        return responses.joined(separator: "_")
    }

    func getLastItem() -> String {
        return responses.last ?? ""
    }

    func cloneObj() -> WeightedResponder {
        let cloned = WeightedResponder(lim: self.lim)
        cloned.responses = self.responses
        return cloned
    }
}
 
Top