首页 > 编程知识 正文

python写古诗,python 词向量

时间:2023-05-05 12:51:17 阅读:190124 作者:1935

基于python单词向量的古诗生成器fromgensim.modelsimportword2vec #语向量fromrandomimportchoicefromos.pathimportexistsimportwarnings.from 警告class CONF: path='古诗. txt' window=16 #滑动窗口大小min_count=60 #过滤低频字size=125 #字向量维topn=14 # 诗词开放度model _ path=' word2vec ' class model : def _ _ init _ (self,window,topn, model ) : self.window=window self.topn=topn self.model=model #词矢量模型self.chr _ dict=model.wv.index2wox config ) : ifexists (config.model_path )模型读取model=word2vec.load ) config.model _ path ) else: # 数据读取web encoding=' utf-8 ' (ASF : ls _ of _ ls _ of _ c=[ list (line.strip (] forlineinf ) #模型培训和保存) window=config.window, min _ count=config.min _ count (model.save ) config.model _ path (model.model _ path ) return cls (config.save ) mode ) Mel _ math ) retid form ) : filter=lambda lst : (t [0] fortinlstift [0] ) # 标题完成iflen(title ) 4: if not title : title=choice (self.chr _ dict ) for _ in range (4- len ) title ) ) ) le[-1], self.topn//2(similar_chr=filter ) similar _ chr ) char=choice ([ cforcinsimilar _ chrifcnotintitle ] ) title fle : predict_chr=self.model.predict _ output _ word (len (poem )1) ) predict_chr=filter ) predict _ chr (length=form[0]* ) form[1]1) return ' 《%s》 ' % '.join (poem [ :-length ] ) ) () ) n''.join(poem(-Length: ) (defmain ) config=conf ) : form={ (五言绚句) : (4,5 )七言绚句) 3360 ) 9 ) m=model.initialize(config ) while True: title=input ('输入标题:').strip ) try : poem=m.poem _ Geng redpoem=m.poem_generator(title,form[ (七言绚句) ) (033m ) (print ) ) ) ) ) 033 [ 0m ' % poem ] # YEM # yes

PS:源码、word2vec和古诗词.txt文件将在Github上提供下载。

Github地址:https://github.com/zhangziqi1999/python-poems-writer

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。