Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· OpenAIAi2 WildChat-4.8M (AI2)

covert to python package main import ( “encoding/json” “errors” “fmt” “math/rand”…

  1. User

    covert to python

    package main

    import ( “encoding/json” “errors” “fmt” “math/rand” “strings” “sync” “time” “unicode” )

    type PostsUnit struct { Board, Thread string }

    var PostsCache = map[PostsUnit][]string{} var PostsMu sync.Mutex

    type Chain struct { First []string Next map[string][]string }

    func NewChain(texts []string) *Chain { firstWords := make([]string, 0) nextWords := make(map[string][]string)

    for _, text := range texts { f := strings.Split(text, " “) f = Filter(f, func(s string) bool { return Any([]rune(s), func(r rune) bool { return !unicode.IsSpace® }) }) if len(f) == 0 { continue } firstWords = append(firstWords, f[0]) for i, word := range f { if i+1 != len(f) { nextWords[word] = append(nextWords[word], f[i+1]) } } } return &Chain{ First: firstWords, Next: nextWords, } }

    func (chain *Chain) BuildText(maxlen int) string { if len(chain.First) == 0 { return “” }

    cur := chain.First[rand.Intn(len(chain.First))] result := []string{cur}

    for i := 0; i < maxlen && len(chain.Next[cur]) != 0; i++ { cur = chain.Next[cur][rand.Intn(len(chain.Next[cur]))] result = append(result, cur) }

    return strings.Join(result, " “) }

    func RemoveTags(text string) string { replacer := strings.NewReplacer( “””, “””, " (OP)", “”, “”, “\n”, “>”, “>”, “/”, “/”, )

    text = replacer.Replace(text)

    runes := []rune(text) tag := false

    result := make([]rune, 0)

    for _, r := range runes { if r == ‘>’ && tag { tag = false continue } if r == ‘<’ && !tag { tag = true } if tag { continue } result = append(result, r) }

    return string(result) }

    func GetPosts(board string, thread string) ([]string, error) { PostsMu.Lock() defer PostsMu.Unlock()

    unit := PostsUnit{board, thread} if PostsCache[unit] != nil { return PostsCache[unit], nil }

    url := fmt.Sprintf( “https://2ch.hk/%s/res/%s.json”, board, thread, )

    req := GetRequest{ RequestInternal: RequestInternal{ Url: url, Timeout: time.Second * 30, }, }

    resp, err := req.Perform() if err != nil { return nil, err }

    var posts struct { Threads []struct { Posts []struct { Comment string } } }

    json.Unmarshal(resp, &posts)

    if len(posts.Threads) == 0 { return nil, errors.New(“указанный тред не найден!”) } if len(posts.Threads[0].Posts) == 0 { return nil, errors.New(“не найдено ни одного поста!”) } for _, com := range posts.Threads[0].Posts { PostsCache[unit] = append( PostsCache[unit], RemoveTags(com.Comment), ) }

    return PostsCache[unit], nil }

  2. Assistant
    import json
    import errors
    import fmt
    import rand
    import strings
    import sync
    import time
    import unicode
    
    
    class PostsUnit:
        def __init__(self, board, thread):
            self.board = board
            self.thread = thread
    
    
    PostsCache = {}
    PostsMu = sync.Mutex()
    
    
    class Chain:
        def __init__(self, first, next):
            self.first = first
            self.next = next
    
    
    def NewChain(texts):
        firstWords = []
        nextWords = {}
    
        for text in texts:
            f = text.split(" ")
            f = [s for s in f if any([not unicode.isspace(r) for r in s])]
            if len(f) == 0:
                continue
            firstWords.append(f[0])
            for i, word in enumerate(f):
                if i + 1 != len(f):
                    nextWords[word] = nextWords.get(word, []) + [f[i + 1]]
        
        return Chain(firstWords, nextWords)
    
    
    def BuildText(chain, maxlen):
        if len(chain.first) == 0:
            return ""
    
        cur = chain.first[rand.randint(0, len(chain.first) - 1)]
        result = [cur]
    
        for i in range(maxlen):
            if len(chain.next.get(cur, [])) == 0:
                break
            cur = chain.next[cur][rand.randint(0, len(chain.next[cur]) - 1)]
            result.append(cur)
    
        return " ".join(result)
    
    
    def RemoveTags(text):
        replacer = {"\"": "\"",
                    " (OP)": "",
                    "<br>": "\n",
                    ">": ">",
                    "/": "/"
                    }
    
        text = text
        runes = list(text)
        tag = False
        result = []
    
        for r in runes:
            if r == '>' and tag:
                tag = False
                continue
            if r == '<' and not tag:
                tag = True
            if tag:
                continue
            result.append(r)
    
        return ''.join(result)
    
    
    def GetPosts(board, thread):
        with PostsMu:
            unit = PostsUnit(board, thread)
            if unit in PostsCache:
                return PostsCache[unit]
    
            url = f"https://2ch.hk/{board}/res/{thread}.json"
    
            req = GetRequest(RequestInternal(url=url, timeout=time.seconds(30)))
    
            resp, err = req.Perform()
            if err:
                return None
    
            posts = json.loads(resp)
    
            if len(posts.get('Threads', [])) == 0:
                raise errors.New("Указанный тред не найден!")
            if len(posts['Threads'][0].get('Posts', [])) == 0:
                raise errors.New("Не найдено ни одного поста!")
            
            PostsCache[unit] = [RemoveTags(com['Comment']) for com in posts['Threads'][0]['Posts']]
            
            return PostsCache[unit]
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.