Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Ngram language model in Python. (Nグラム言語モデル)

NotificationsYou must be signed in to change notification settings

KentoW/Ngram-language-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

概要

N-gram言語モデルをPythonで実装
加算スムージングとKneser-nayスムージングを実装

ngram.pyの使い方

# Sample code.fromngramimportNgramngram=Ngram(input_file='corpus.txt',N=3,smoothing='kneser-nay')ngram.train()ngram.ppl(input_file=args.corpus)#ATTENTION!! very slowgenerated_list=ngram.generate(window=5,mode='sample',temprature=1.1,max_word=30,delta=0.5)print("生成結果")forwords,log_probingenerated_list:print(log_prob,"".join(words[3::]))

メソッド

train() 言語モデルの学習
ppl() パープレキシティの計算
generate() 文の生成

パラメータ

input_file : 入力データ
N : N-gramのN
smoothing : スムージングの種類(vanilla, add, kneser-nay)
window : ビーム探索の探索窓幅
mode : ビーム探索の方法(greedy, sample)
temprature : ビーム探索の方法がsampleのとき、確率分布から単語をsampleする温度
max_word : 生成する単語数
delta: 加算スムージング及びKneser-nayスムージングのパラメータ

入力データ

1単語をスペースで分割した1行1文先頭に#(シャープ)記号を入れてコメントアウトを記述可能

# 文1単語1 単語2 単語3 ...# 文2単語10 単語11 単語10 ......

例としてWiki.pyを使用して収集した アニメのあらすじ文章をcorpus.txtに保存

About

Ngram language model in Python. (Nグラム言語モデル)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp