Trying RNN based character models on Persian poetry

Afshin Khashei
3 min readFeb 26, 2020

After reading the famous and amazing blog post “ Unreasonable effectiveness of recurrent neural networks “ I decided to try to build a character model on Persian poems.

For this trial, I chose “The Book of Kings” (Shahnameh), which is a long epic poem written by the Persian poet Ferdowsi around 1000 CE. Shahnameh is the national epic of Greater Iran. Consisting of some 50,000 “distichs” or couplets (two-line verses), Shahnameh is also the world’s longest epic poem written by a single poet.

Verses in Shahnameh have two important properties:

  • Every verse has two rhyming lines
  • All lines have the same rhythm. In more technical terms Persian poetry is quantitative, and the metrical patterns are made of long, short and overlong syllables. Shahnameh particularly uses a pattern in the form “u — — u — — u — — u -” where u stands for short and dash stands for long patterns. For instance, each line, from rhythm perspective will sound like this: “you don’t care, I don’t care, we don’t care, at all” (Ta tan tan, Ta tan tan, Ta tan tan, Ta tan)

I trained a three-layer LSTM models in Tensorflow with 400 cells on each layer and generated new text based on this model., The result was very interesting and as unreasonable as it should be. The model learned to generate verses (two lines) with correct words, rhyme, and rhythm with a decent grammar and almost no mistake like this:

روان را به دانش به کار آوری
به توران به خواب اندر آرد سری

If you can read Persian feel free to check some of the results which I posted on darbare.com. Also included one example here:

ادامه داستان

سواران گردنکشان دسته دید
خردمند را او بدان خسته دید

سکندر نگه کرد پس پهلوان
به بدخواه شد شاد و روشن روان

سپاه اندر آمد به پیش سوار
خردمند و شایسته‌ی کارزار

بفرمود تا بنده آگاه دید
چنین تا بر شاه ایران کشید

نهادند چیزی که پوشیده بود
جهان را درم داد و دینار بود

سران افسر از گوهر شاهوار
نخست آفرین کرد بر کردگار

چو بهرام بشنید گریان شدند
وزان جایگه شاد و خندان شدند

بفرمود تا ناسزا بنگرید
یکی باد سرد از جگر برکشید

شب تیره چون رستم او را بدید
سپهدار پیران به هامون کشید

دمان تا فراز آمد اندر کمان
برو بر نکرد ایچ پیدا زبان

بران تیغ زهر آب داده به دست
به هاماوران داد جویان به دست

سیاوش چو بشنید شاه جهان
که آمد میان کهان و مهان

عنان برگرفتند یکسر به من
بخواند آگهی بر سر انجمن

چنین داد پاسخ که از مهتران
خداوند شمشیر و گرز گران

روان را به دانش به کار آوری
به توران به خواب اندر آرد سری

به گرد اندر آرد بهنگام کار
درختی چنان خسته در کارزار

Originally published at https://www.linkedin.com. on January 16, 2018

--

--