Randomly generate a compound word of two Chinese characters

Source: Internet
Author: User

# -*- coding: utf-8 -*-import randomclass gennickname:     def __init__ (self,num=30):         #print   "__init__"          self.list=[]         self.wordnum=0        self.num=num         self.nicknames=[]    def getwordsfromtxt (self):         file=open ("Source.txt", "R")         for l  in file.readlines ():             #print   "l: "  +l            line=l.decode ( "GBK"). Split ()              #print  len (line)             for e in line:                  #print  e                 self.list.append (e)           #print  len (self.list)          #print   "L:  %s " %self.list        file.close ()      Def gennickname (self):         self.getwordsfromtxt ()          self.wordnum=len (self.list) -1         for k in range (Self.num):              i=random.randint (0, self.wordnum)              j=random.Randint (0, self.wordnum)              #print  len (self.list)              #print   "one  nick name :  "+self.list[i]+self.list[j]             self.nicknames.append (Self.list[i]+self.list[j])     def  ShowNames (self):        print  "All nicks names:"          print  "*" *50        i=0         for name in self.nickNames:             print name,             i=i+1             if (i%10==0):   &Nbsp;             print  " "                 print  "*" *50             if __name__== "__main__":     g = gennickname ()     g.gennickname ()      G.shownames ()

The code is as shown above.

Implementation is relatively simple, the main difficulty is to read from the TXT file characters after the segmentation. Normally, you can use the. Split function, but the text uses Chinese characters, and the encoding of Chinese characters is GBK, so we need to decode the GBK before splitting.

Each time you run, you can generate 50 randomly combined words, as follows:

All Nicks names:************************************************** ah from the Furnace 痼 column of the pigtail to the inflammation of the Qi-chi-Yang gambling class outside through the Feng and Jiao Xiang diving Laos dozen Qiao to kill pick up preach Yue collapse I dispute Careful to lure graves while the morning Song Nailin itch au Australia liao grain purple flood Guosa animal Relief Hammer Draft of the production injustice Ying mast wax le ling ken mold ketone shu egg accompany fart qian wet cyanide stem fry all 邹捍佃 sound collision leather color vegetable hunting auspicious word Twist report bi *********************** ***************************


Randomly generate a compound word of two Chinese characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.