Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

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

  1. User

    convert 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

    Here's the Go code translated to Python:

    import json
    import random
    import re
    import requests
    from threading import Lock
    from typing import List, Dict, Union
    
    posts_cache: Dict[str, List[str]] = {}
    posts_lock = Lock()
    
    
    class Chain:
        def __init__(self, texts: List[str]):
            self.first = []
            self.next = {}
    
            for text in texts:
                words = text.split()
                words = list(filter(lambda s: any(not c.isspace() for c in s), words))
                if not words:
                    continue
                self.first.append(words[0])
                for i, word in enumerate(words[:-1]):
                    if word in self.next:
                        self.next[word].append(words[i + 1])
                    else:
                        self.next[word] = [words[i + 1]]
    
        def build_text(self, maxlen: int) -> str:
            if not self.first:
                return ""
            current_word = random.choice(self.first)
            result = [current_word]
    
            for _ in range(maxlen):
                current_word = random.choice(self.next.get(current_word, [""]))
                if not current_word:
                    break
                result.append(current_word)
    
            return " ".join(result)
    
    
    def remove_tags(text: str) -> str:
        text = re.sub(r"<[^>]+>", "", text)
        return text
    
    
    def get_posts(board: str, thread: str) -> Union[List[str], None]:
        global posts_cache
        with posts_lock:
            key = f"{board}-{thread}"
            if key in posts_cache:
                return posts_cache[key]
    
            url = f"https://2ch.hk/{board}/res/{thread}.json"
            response = requests.get(url, timeout=30)
            if response.status_code != 200:
                return None
    
            posts_data = response.json()
            if not posts_data.get("threads"):
                return None
    
            posts = []
            for post in posts_data["threads"][0]["posts"]:
                cleaned_comment = remove_tags(post["comment"])
                posts.append(cleaned_comment)
            posts_cache[key] = posts
    
            return posts
    
    # Example usage
    board = "test_board"
    thread = "test_thread"
    texts = get_posts(board, thread)
    if texts:
        chain = Chain(texts)
        text = chain.build_text(100)
        print(text)
    

    Please note the following considerations for this Python translation:

    1. The requests library is used to make HTTP requests.
    2. Regular expressions are used with the re library to remove tags from the text.
    3. A global dictionary with a lock is used to cache posts (similar to what's done in the Go code).
    4. Python's list comprehension and filter functions are used to imitate the behavior of Go's Filter and Any functions.
    5. The chains are generated using a dictionary and a list, as per the Chain class translation.
    6. Error handling is not exactly the same since Go and Python handle errors differently.

    Be aware that the input parameters for get_posts and the Chain usage should be adapted to the actual representation of boards and threads you will use, as the given board and thread variables in the example are placeholders.

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