python 使用字典中随机选取字符的最简单的字符级语言模型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用字典中随机选取字符的最简单的字符级语言模型相关的知识,希望对你有一定的参考价值。

import random 
import string
from string import punctuation

# Get all the alphabets 
alphabetsList = list(string.ascii_letters) 

# Get all the punctuations
punctuationList = list(punctuation)

# Create a dictionary with all these characters and space
dictionary = alphabetsList + punctuationList + [" "]


def randomCharPicker(aList):
    """
    Returns a randomly selected a character from aList
    Probability of selection is 1 / length of dictionary
    """
    return random.choice(aList)


if __name__ == "__main__":
    tempSentence = ""
    # Creating a sentence of 100 characters
    for _ in range(100):
        # Pick a random charater from dictionary
        randomChar = randomCharPicker(dictionary)
        # Append it to the output sentence
        tempSentence += randomChar
        # Display the sentence
        print("Current sentence: {}".format(tempSentence))

以上是关于python 使用字典中随机选取字符的最简单的字符级语言模型的主要内容,如果未能解决你的问题,请参考以下文章

将字符串转换为字典的简单方法

C#中如何产生随机字符串

什么是字典中少量模式的最简单快速的字符串匹配算法,以找到一个小字符串

python 生成随机字符串

java怎么实现自动生成一个由英文字母(区分大小写)、数字、符号组成,6-12个字符 的密码

python生成随机数随机字符串