Various scripts for playing around with natural language processing/generation

edX Lightning Talk.ipynb 22KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {
  6. "slideshow": {
  7. "slide_type": "slide"
  8. }
  9. },
  10. "source": [
  11. "# Generating random poems with Python #\n",
  12. "\n",
  13. "\n",
  14. "<div style=\"text-align:center;margin-top:40px\">(I never said they would be good poems)</div>"
  15. ]
  16. },
  17. {
  18. "cell_type": "markdown",
  19. "metadata": {
  20. "slideshow": {
  21. "slide_type": "slide"
  22. }
  23. },
  24. "source": [
  25. "## Phone autocomplete ##\n",
  26. "\n",
  27. "You can generate random text that sounds like you with your smartphone keyboard:\n",
  28. "\n",
  29. "<img align=\"left\" style=\"width:50%\" src=\"images/phone_keyboard.png\">\n",
  30. "<img align=\"right\" style=\"width:50%\" src=\"images/phone_autocomplete.gif\">"
  31. ]
  32. },
  33. {
  34. "cell_type": "markdown",
  35. "metadata": {
  36. "slideshow": {
  37. "slide_type": "slide"
  38. }
  39. },
  40. "source": [
  41. "## So, how does it work? ##\n",
  42. "\n",
  43. "First, we need a **corpus**, or the text our generator will recombine into new sentences:"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": 1,
  49. "metadata": {
  50. "collapsed": true,
  51. "slideshow": {
  52. "slide_type": "fragment"
  53. }
  54. },
  55. "outputs": [],
  56. "source": [
  57. "corpus = 'The quick brown fox jumps over the lazy dog'"
  58. ]
  59. },
  60. {
  61. "cell_type": "markdown",
  62. "metadata": {
  63. "slideshow": {
  64. "slide_type": "slide"
  65. }
  66. },
  67. "source": [
  68. "Simplest word **tokenization** is to split on spaces:"
  69. ]
  70. },
  71. {
  72. "cell_type": "code",
  73. "execution_count": 2,
  74. "metadata": {
  75. "slideshow": {
  76. "slide_type": "fragment"
  77. }
  78. },
  79. "outputs": [
  80. {
  81. "data": {
  82. "text/plain": [
  83. "['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']"
  84. ]
  85. },
  86. "execution_count": 2,
  87. "metadata": {},
  88. "output_type": "execute_result"
  89. }
  90. ],
  91. "source": [
  92. "words = corpus.split(' ')\n",
  93. "words"
  94. ]
  95. },
  96. {
  97. "cell_type": "markdown",
  98. "metadata": {
  99. "slideshow": {
  100. "slide_type": "slide"
  101. }
  102. },
  103. "source": [
  104. "To create **bigrams**, iterate through the list of words with two indices, one of which is offset by one:"
  105. ]
  106. },
  107. {
  108. "cell_type": "code",
  109. "execution_count": 3,
  110. "metadata": {
  111. "slideshow": {
  112. "slide_type": "fragment"
  113. }
  114. },
  115. "outputs": [
  116. {
  117. "data": {
  118. "text/plain": [
  119. "[('The', 'quick'),\n",
  120. " ('quick', 'brown'),\n",
  121. " ('brown', 'fox'),\n",
  122. " ('fox', 'jumps'),\n",
  123. " ('jumps', 'over'),\n",
  124. " ('over', 'the'),\n",
  125. " ('the', 'lazy'),\n",
  126. " ('lazy', 'dog')]"
  127. ]
  128. },
  129. "execution_count": 3,
  130. "metadata": {},
  131. "output_type": "execute_result"
  132. }
  133. ],
  134. "source": [
  135. "bigrams = [b for b in zip(words[:-1], words[1:])]\n",
  136. "bigrams"
  137. ]
  138. },
  139. {
  140. "cell_type": "markdown",
  141. "metadata": {
  142. "slideshow": {
  143. "slide_type": "slide"
  144. }
  145. },
  146. "source": [
  147. "How do we use the bigrams to predict the next word given the first word?"
  148. ]
  149. },
  150. {
  151. "cell_type": "markdown",
  152. "metadata": {
  153. "slideshow": {
  154. "slide_type": "fragment"
  155. }
  156. },
  157. "source": [
  158. " Return every second element where the first element matches the **condition**:"
  159. ]
  160. },
  161. {
  162. "cell_type": "code",
  163. "execution_count": 4,
  164. "metadata": {
  165. "slideshow": {
  166. "slide_type": "fragment"
  167. }
  168. },
  169. "outputs": [
  170. {
  171. "data": {
  172. "text/plain": [
  173. "['quick', 'lazy']"
  174. ]
  175. },
  176. "execution_count": 4,
  177. "metadata": {},
  178. "output_type": "execute_result"
  179. }
  180. ],
  181. "source": [
  182. "condition = 'the'\n",
  183. "next_words = [bigram[1] for bigram in bigrams\n",
  184. " if bigram[0].lower() == condition]\n",
  185. "next_words"
  186. ]
  187. },
  188. {
  189. "cell_type": "markdown",
  190. "metadata": {
  191. "collapsed": true,
  192. "slideshow": {
  193. "slide_type": "fragment"
  194. }
  195. },
  196. "source": [
  197. "(<span style=\"color:blue\">The</span> <span style=\"color:red\">quick</span>) (quick brown) ... (<span style=\"color:blue\">the</span> <span style=\"color:red\">lazy</span>) (lazy dog)\n",
  198. "\n",
  199. "Either “<span style=\"color:red\">quick</span>” or “<span style=\"color:red\">lazy</span>” could be the next word."
  200. ]
  201. },
  202. {
  203. "cell_type": "markdown",
  204. "metadata": {
  205. "collapsed": true,
  206. "slideshow": {
  207. "slide_type": "slide"
  208. }
  209. },
  210. "source": [
  211. "## Trigrams and Ngrams ##\n",
  212. "\n",
  213. "We can partition by threes too:\n",
  214. "\n",
  215. "(<span style=\"color:blue\">The</span> <span style=\"color:red\">quick brown</span>) (quick brown fox) ... (<span style=\"color:blue\">the</span> <span style=\"color:red\">lazy dog</span>)\n",
  216. "\n",
  217. "Or, the condition can be two words (`condition = 'the lazy'`):\n",
  218. "\n",
  219. "(The quick brown) (quick brown fox) ... (<span style=\"color:blue\">the lazy</span> <span style=\"color:red\">dog</span>)\n",
  220. "\n",
  221. "These are **trigrams**.\n",
  222. "\n",
  223. "We can partition any **N** number of words together as **ngrams**."
  224. ]
  225. },
  226. {
  227. "cell_type": "markdown",
  228. "metadata": {
  229. "slideshow": {
  230. "slide_type": "slide"
  231. }
  232. },
  233. "source": [
  234. "So earlier we got:"
  235. ]
  236. },
  237. {
  238. "cell_type": "code",
  239. "execution_count": 5,
  240. "metadata": {
  241. "slideshow": {
  242. "slide_type": "fragment"
  243. }
  244. },
  245. "outputs": [
  246. {
  247. "data": {
  248. "text/plain": [
  249. "['quick', 'lazy']"
  250. ]
  251. },
  252. "execution_count": 5,
  253. "metadata": {},
  254. "output_type": "execute_result"
  255. }
  256. ],
  257. "source": [
  258. "next_words"
  259. ]
  260. },
  261. {
  262. "cell_type": "markdown",
  263. "metadata": {
  264. "slideshow": {
  265. "slide_type": "fragment"
  266. }
  267. },
  268. "source": [
  269. "How do we know which one to pick as the next word?\n",
  270. "\n",
  271. "Why not the word that occurred the most often after the condition in the corpus?"
  272. ]
  273. },
  274. {
  275. "cell_type": "markdown",
  276. "metadata": {
  277. "slideshow": {
  278. "slide_type": "fragment"
  279. }
  280. },
  281. "source": [
  282. "We can use a **Conditional Frequency Distribution (CFD)** to figure that out!\n",
  283. "\n",
  284. "A **CFD** can tell us: given a **condition**, what is **likely** to follow?"
  285. ]
  286. },
  287. {
  288. "cell_type": "markdown",
  289. "metadata": {
  290. "slideshow": {
  291. "slide_type": "slide"
  292. }
  293. },
  294. "source": [
  295. "## Conditional Frequency Distributions (CFDs) ##"
  296. ]
  297. },
  298. {
  299. "cell_type": "code",
  300. "execution_count": 6,
  301. "metadata": {
  302. "slideshow": {
  303. "slide_type": "fragment"
  304. }
  305. },
  306. "outputs": [
  307. {
  308. "name": "stdout",
  309. "output_type": "stream",
  310. "text": [
  311. "['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog', 'and', 'the', 'quick', 'cat']\n"
  312. ]
  313. }
  314. ],
  315. "source": [
  316. "words = ('The quick brown fox jumped over the '\n",
  317. " 'lazy dog and the quick cat').split(' ')\n",
  318. "print(words)"
  319. ]
  320. },
  321. {
  322. "cell_type": "code",
  323. "execution_count": 7,
  324. "metadata": {
  325. "collapsed": true,
  326. "slideshow": {
  327. "slide_type": "fragment"
  328. }
  329. },
  330. "outputs": [],
  331. "source": [
  332. "from collections import defaultdict\n",
  333. "\n",
  334. "cfd = defaultdict(lambda: defaultdict(lambda: 0))"
  335. ]
  336. },
  337. {
  338. "cell_type": "markdown",
  339. "metadata": {
  340. "slideshow": {
  341. "slide_type": "slide"
  342. }
  343. },
  344. "source": [
  345. "## Conditional Frequency Distributions (CFDs) ##"
  346. ]
  347. },
  348. {
  349. "cell_type": "code",
  350. "execution_count": 8,
  351. "metadata": {
  352. "slideshow": {
  353. "slide_type": "fragment"
  354. }
  355. },
  356. "outputs": [
  357. {
  358. "data": {
  359. "text/plain": [
  360. "{'and': {'the': 1},\n",
  361. " 'brown': {'fox': 1},\n",
  362. " 'dog': {'and': 1},\n",
  363. " 'fox': {'jumped': 1},\n",
  364. " 'jumped': {'over': 1},\n",
  365. " 'lazy': {'dog': 1},\n",
  366. " 'over': {'the': 1},\n",
  367. " 'quick': {'brown': 1},\n",
  368. " 'the': {'lazy': 1, 'quick': 2}}"
  369. ]
  370. },
  371. "execution_count": 8,
  372. "metadata": {},
  373. "output_type": "execute_result"
  374. }
  375. ],
  376. "source": [
  377. "for i in range(len(words) - 2): # loop to the next-to-last word\n",
  378. " cfd[words[i].lower()][words[i+1].lower()] += 1\n",
  379. "\n",
  380. "# pretty print the defaultdict\n",
  381. "{k: dict(v) for k, v in dict(cfd).items()}"
  382. ]
  383. },
  384. {
  385. "cell_type": "markdown",
  386. "metadata": {
  387. "slideshow": {
  388. "slide_type": "slide"
  389. }
  390. },
  391. "source": [
  392. "## Conditional Frequency Distributions (CFDs) ##"
  393. ]
  394. },
  395. {
  396. "cell_type": "markdown",
  397. "metadata": {
  398. "slideshow": {
  399. "slide_type": "fragment"
  400. }
  401. },
  402. "source": [
  403. "So, what's the most likely word to follow `'the'`?"
  404. ]
  405. },
  406. {
  407. "cell_type": "code",
  408. "execution_count": 9,
  409. "metadata": {
  410. "slideshow": {
  411. "slide_type": "fragment"
  412. }
  413. },
  414. "outputs": [
  415. {
  416. "data": {
  417. "text/plain": [
  418. "'quick'"
  419. ]
  420. },
  421. "execution_count": 9,
  422. "metadata": {},
  423. "output_type": "execute_result"
  424. }
  425. ],
  426. "source": [
  427. "max(cfd['the'])"
  428. ]
  429. },
  430. {
  431. "cell_type": "markdown",
  432. "metadata": {
  433. "slideshow": {
  434. "slide_type": "slide"
  435. }
  436. },
  437. "source": [
  438. "## Whole sentences can be the conditions and values too ##\n",
  439. "\n",
  440. "Which is basically the way cleverbot works ([http://www.cleverbot.com/](http://www.cleverbot.com/)):\n",
  441. "\n",
  442. "![Cleverbot](images/cleverbot.png)"
  443. ]
  444. },
  445. {
  446. "cell_type": "markdown",
  447. "metadata": {
  448. "slideshow": {
  449. "slide_type": "slide"
  450. }
  451. },
  452. "source": [
  453. "## Random text! ##"
  454. ]
  455. },
  456. {
  457. "cell_type": "code",
  458. "execution_count": 10,
  459. "metadata": {
  460. "slideshow": {
  461. "slide_type": "fragment"
  462. }
  463. },
  464. "outputs": [
  465. {
  466. "name": "stdout",
  467. "output_type": "stream",
  468. "text": [
  469. "her reserve and concealment towards some feelings in moving slowly together . You will shew\n"
  470. ]
  471. }
  472. ],
  473. "source": [
  474. "import nltk\n",
  475. "import random\n",
  476. "\n",
  477. "TEXT = nltk.corpus.gutenberg.words('austen-emma.txt')\n",
  478. "\n",
  479. "# NLTK shortcuts :)\n",
  480. "bigrams = nltk.bigrams(TEXT)\n",
  481. "cfd = nltk.ConditionalFreqDist(bigrams)\n",
  482. "\n",
  483. "# pick a random word from the corpus to start with\n",
  484. "word = random.choice(TEXT)\n",
  485. "# generate 15 more words\n",
  486. "for i in range(15):\n",
  487. " print(word + ' ', end='')\n",
  488. " if word in cfd:\n",
  489. " word = random.choice(list(cfd[word].keys()))\n",
  490. " else:\n",
  491. " break"
  492. ]
  493. },
  494. {
  495. "cell_type": "markdown",
  496. "metadata": {
  497. "slideshow": {
  498. "slide_type": "slide"
  499. }
  500. },
  501. "source": [
  502. "## Random poems ##\n",
  503. "\n",
  504. "Generating random poems is accomplished by limiting the choice of the next word by some constraint:\n",
  505. "\n",
  506. "* words that rhyme with the previous line\n",
  507. "* words that match a certain syllable count\n",
  508. "* words that alliterate with words on the same line\n",
  509. "* etc."
  510. ]
  511. },
  512. {
  513. "cell_type": "markdown",
  514. "metadata": {
  515. "slideshow": {
  516. "slide_type": "slide"
  517. }
  518. },
  519. "source": [
  520. "# Rhyming\n",
  521. "\n",
  522. "**Written English != Spoken English**\n",
  523. "\n",
  524. "English has a highly **nonphonemic orthography**, meaning that the letters often have no correspondence to the pronunciation. E.g.:\n",
  525. "\n",
  526. "\n",
  527. "\"meet\" vs. \"meat\"\n",
  528. "\n",
  529. "The vowels are spelled differently, yet they rhyme."
  530. ]
  531. },
  532. {
  533. "cell_type": "markdown",
  534. "metadata": {
  535. "slideshow": {
  536. "slide_type": "fragment"
  537. }
  538. },
  539. "source": [
  540. "Fun fact: They used to be pronounced differently in Middle English during the invention of the printing press and standardized spelling."
  541. ]
  542. },
  543. {
  544. "cell_type": "markdown",
  545. "metadata": {
  546. "slideshow": {
  547. "slide_type": "slide"
  548. }
  549. },
  550. "source": [
  551. "# International Phonetic Alphabet (IPA)\n",
  552. "\n",
  553. "An alphabet that can represent all varieties of human pronunciation.\n",
  554. "\n",
  555. "* meet: /mit/\n",
  556. "* meat: /mit/"
  557. ]
  558. },
  559. {
  560. "cell_type": "markdown",
  561. "metadata": {
  562. "slideshow": {
  563. "slide_type": "fragment"
  564. }
  565. },
  566. "source": [
  567. "Note: this is only the IPA transcription for only one **accent** of English."
  568. ]
  569. },
  570. {
  571. "cell_type": "markdown",
  572. "metadata": {
  573. "slideshow": {
  574. "slide_type": "slide"
  575. }
  576. },
  577. "source": [
  578. "# Syllables\n",
  579. "\n",
  580. "* \"poet\" = 2 syllables\n",
  581. "* \"does\" = 1 syllable\n",
  582. "\n",
  583. "Can the IPA tell us the number of syllables in a word too?"
  584. ]
  585. },
  586. {
  587. "cell_type": "markdown",
  588. "metadata": {
  589. "slideshow": {
  590. "slide_type": "slide"
  591. }
  592. },
  593. "source": [
  594. "# Syllables\n",
  595. "\n",
  596. "* poet: /ˈpoʊət/\n",
  597. "* does: /ˈdʌz/\n",
  598. "\n",
  599. "Not really... We cannot easily identify three syllables from that transcription.\n",
  600. "\n",
  601. "Sometimes the transcriber denotes syllable breaks (with a `.` or a `'`), but sometimes they don't."
  602. ]
  603. },
  604. {
  605. "cell_type": "markdown",
  606. "metadata": {
  607. "slideshow": {
  608. "slide_type": "slide"
  609. }
  610. },
  611. "source": [
  612. "# Arpabet\n",
  613. "\n",
  614. "A phonetic alphabet developed by ARPA in the 70s that:\n",
  615. "\n",
  616. "* Encodes phonemes specific to American English.\n",
  617. "* Meant to be a machine readable code. It is ASCII only.\n",
  618. "* Denotes how stressed every vowel is from 0-2."
  619. ]
  620. },
  621. {
  622. "cell_type": "markdown",
  623. "metadata": {
  624. "slideshow": {
  625. "slide_type": "fragment"
  626. }
  627. },
  628. "source": [
  629. "This is perfect! Word's syllable count equals the number of digits in the Arpabet encoding."
  630. ]
  631. },
  632. {
  633. "cell_type": "markdown",
  634. "metadata": {
  635. "slideshow": {
  636. "slide_type": "slide"
  637. }
  638. },
  639. "source": [
  640. "# CMU Pronouncing Dictionary (CMUdict)\n",
  641. "\n",
  642. "A large open source dictionary of English words to North American pronunciations in Arpanet encoding."
  643. ]
  644. },
  645. {
  646. "cell_type": "markdown",
  647. "metadata": {
  648. "slideshow": {
  649. "slide_type": "fragment"
  650. }
  651. },
  652. "source": [
  653. "Conveniently, it is also in NLTK..."
  654. ]
  655. },
  656. {
  657. "cell_type": "markdown",
  658. "metadata": {
  659. "slideshow": {
  660. "slide_type": "slide"
  661. }
  662. },
  663. "source": [
  664. "# Counting Syllables"
  665. ]
  666. },
  667. {
  668. "cell_type": "code",
  669. "execution_count": 11,
  670. "metadata": {
  671. "collapsed": true,
  672. "slideshow": {
  673. "slide_type": "fragment"
  674. }
  675. },
  676. "outputs": [],
  677. "source": [
  678. "import string\n",
  679. "from nltk.corpus import cmudict\n",
  680. "cmu = cmudict.dict()\n",
  681. "\n",
  682. "def count_syllables(word):\n",
  683. " lower_word = word.lower()\n",
  684. " if lower_word in cmu:\n",
  685. " return max([len([y for y in x if y[-1] in string.digits])\n",
  686. " for x in cmu[lower_word]])"
  687. ]
  688. },
  689. {
  690. "cell_type": "code",
  691. "execution_count": 12,
  692. "metadata": {
  693. "slideshow": {
  694. "slide_type": "fragment"
  695. }
  696. },
  697. "outputs": [
  698. {
  699. "name": "stdout",
  700. "output_type": "stream",
  701. "text": [
  702. "poet: 2\n",
  703. "does: 1\n"
  704. ]
  705. }
  706. ],
  707. "source": [
  708. "print(\"poet: {}\\ndoes: {}\".format(count_syllables(\"poet\"),\n",
  709. " count_syllables(\"does\")))"
  710. ]
  711. },
  712. {
  713. "cell_type": "markdown",
  714. "metadata": {
  715. "slideshow": {
  716. "slide_type": "slide"
  717. }
  718. },
  719. "source": [
  720. "![Buzzfeed Haiku Generator](images/buzzfeed.png)\n",
  721. "\n",
  722. "[http://mule.hallada.net/nlp/buzzfeed-haiku-generator/](http://mule.hallada.net/nlp/buzzfeed-haiku-generator/)"
  723. ]
  724. },
  725. {
  726. "cell_type": "markdown",
  727. "metadata": {
  728. "collapsed": true,
  729. "slideshow": {
  730. "slide_type": "slide"
  731. }
  732. },
  733. "source": [
  734. "## Remember these? ##\n",
  735. "\n",
  736. "![madlibs](images/madlibs.png)"
  737. ]
  738. },
  739. {
  740. "cell_type": "markdown",
  741. "metadata": {
  742. "slideshow": {
  743. "slide_type": "slide"
  744. }
  745. },
  746. "source": [
  747. "## Mad Libs ##\n",
  748. "\n",
  749. "These worked so well because they forced the random words (chosen by you) to fit into the syntactical structure and parts-of-speech of an existing sentence.\n",
  750. "\n",
  751. "You end up with **syntactically** correct sentences that are **semantically** random.\n",
  752. "\n",
  753. "We can do the same thing!"
  754. ]
  755. },
  756. {
  757. "cell_type": "markdown",
  758. "metadata": {
  759. "slideshow": {
  760. "slide_type": "slide"
  761. }
  762. },
  763. "source": [
  764. "## NLTK Syntax Trees! ##"
  765. ]
  766. },
  767. {
  768. "cell_type": "code",
  769. "execution_count": 13,
  770. "metadata": {
  771. "slideshow": {
  772. "slide_type": "fragment"
  773. }
  774. },
  775. "outputs": [
  776. {
  777. "name": "stdout",
  778. "output_type": "stream",
  779. "text": [
  780. "(S\n",
  781. " (NP (DT the) (NN quick))\n",
  782. " (VP\n",
  783. " (VB brown)\n",
  784. " (NP\n",
  785. " (NP (JJ fox) (NN jumps))\n",
  786. " (PP (IN over) (NP (DT the) (JJ lazy) (NN dog)))))\n",
  787. " (. .))\n"
  788. ]
  789. }
  790. ],
  791. "source": [
  792. "from stat_parser import Parser\n",
  793. "parsed = Parser().parse('The quick brown fox jumps over the lazy dog.')\n",
  794. "print(parsed)"
  795. ]
  796. },
  797. {
  798. "cell_type": "markdown",
  799. "metadata": {
  800. "slideshow": {
  801. "slide_type": "slide"
  802. }
  803. },
  804. "source": [
  805. "## NLTK Syntax Trees! ##"
  806. ]
  807. },
  808. {
  809. "cell_type": "code",
  810. "execution_count": 14,
  811. "metadata": {
  812. "slideshow": {
  813. "slide_type": "fragment"
  814. }
  815. },
  816. "outputs": [
  817. {
  818. "name": "stdout",
  819. "output_type": "stream",
  820. "text": [
  821. " S \n",
  822. " ________________________|__________________________ \n",
  823. " | VP | \n",
  824. " | ____|_____________ | \n",
  825. " | | NP | \n",
  826. " | | _________|________ | \n",
  827. " | | | PP | \n",
  828. " | | | ________|___ | \n",
  829. " NP | NP | NP | \n",
  830. " ___|____ | ___|____ | _______|____ | \n",
  831. " DT NN VB JJ NN IN DT JJ NN . \n",
  832. " | | | | | | | | | | \n",
  833. "the quick brown fox jumps over the lazy dog . \n",
  834. "\n"
  835. ]
  836. }
  837. ],
  838. "source": [
  839. "parsed.pretty_print()"
  840. ]
  841. },
  842. {
  843. "cell_type": "markdown",
  844. "metadata": {
  845. "slideshow": {
  846. "slide_type": "slide"
  847. }
  848. },
  849. "source": [
  850. "## Swapping matching syntax subtrees between two corpora ##"
  851. ]
  852. },
  853. {
  854. "cell_type": "code",
  855. "execution_count": 15,
  856. "metadata": {
  857. "slideshow": {
  858. "slide_type": "fragment"
  859. }
  860. },
  861. "outputs": [
  862. {
  863. "name": "stdout",
  864. "output_type": "stream",
  865. "text": [
  866. "(SBARQ\n",
  867. " (SQ\n",
  868. " (NP (PRP I))\n",
  869. " (VP (VBP do) (RB not) (VB advise) (NP (DT the) (NN custard))))\n",
  870. " (. .))\n",
  871. "I do not advise the custard .\n",
  872. "==============================\n",
  873. "I do n't want the drone !\n",
  874. "(SBARQ\n",
  875. " (SQ\n",
  876. " (NP (PRP I))\n",
  877. " (VP (VBP do) (RB n't) (VB want) (NP (DT the) (NN drone))))\n",
  878. " (. !))\n"
  879. ]
  880. }
  881. ],
  882. "source": [
  883. "from syntax_aware_generate import generate\n",
  884. "\n",
  885. "# inserts matching syntax subtrees from trump.txt into\n",
  886. "# trees from austen-emma.txt\n",
  887. "generate('trump.txt', word_limit=10)"
  888. ]
  889. },
  890. {
  891. "cell_type": "markdown",
  892. "metadata": {
  893. "slideshow": {
  894. "slide_type": "slide"
  895. }
  896. },
  897. "source": [
  898. "## spaCy ##\n",
  899. "\n",
  900. "![spaCy speed comparison](images/spacy_speed.png)\n",
  901. "\n",
  902. "[https://spacy.io/docs/api/#speed-comparison](https://spacy.io/docs/api/#speed-comparison)"
  903. ]
  904. },
  905. {
  906. "cell_type": "markdown",
  907. "metadata": {
  908. "slideshow": {
  909. "slide_type": "slide"
  910. }
  911. },
  912. "source": [
  913. "![Screenshot of displaCy, a dependency visualizer for spaCy](images/displacy.png)\n",
  914. "[https://demos.explosion.ai/displacy/](https://demos.explosion.ai/displacy/)"
  915. ]
  916. },
  917. {
  918. "cell_type": "markdown",
  919. "metadata": {
  920. "slideshow": {
  921. "slide_type": "slide"
  922. }
  923. },
  924. "source": [
  925. "## Character-based Recurrent Neural Networks ##\n",
  926. "\n",
  927. "![RNN Paper](images/rnn_paper.png)\n",
  928. "\n",
  929. "[http://www.cs.utoronto.ca/~ilya/pubs/2011/LANG-RNN.pdf](http://www.cs.utoronto.ca/~ilya/pubs/2011/LANG-RNN.pdf)"
  930. ]
  931. },
  932. {
  933. "cell_type": "markdown",
  934. "metadata": {
  935. "slideshow": {
  936. "slide_type": "slide"
  937. }
  938. },
  939. "source": [
  940. "## Implementation: char-rnn ##\n",
  941. "\n",
  942. "![char-rnn](images/char-rnn.png)\n",
  943. "\n",
  944. "[https://github.com/karpathy/char-rnn](https://github.com/karpathy/char-rnn)"
  945. ]
  946. },
  947. {
  948. "cell_type": "markdown",
  949. "metadata": {
  950. "slideshow": {
  951. "slide_type": "slide"
  952. }
  953. },
  954. "source": [
  955. "## Generating Shakespeare with char-rnn ##\n",
  956. "\n",
  957. "![Shakespeare](images/shakespeare.png)\n",
  958. "\n",
  959. "[http://karpathy.github.io/2015/05/21/rnn-effectiveness/](http://karpathy.github.io/2015/05/21/rnn-effectiveness/)"
  960. ]
  961. },
  962. {
  963. "cell_type": "markdown",
  964. "metadata": {
  965. "slideshow": {
  966. "slide_type": "slide"
  967. }
  968. },
  969. "source": [
  970. "![Screenshot of word-rnn readme on Github](images/word-rnn.png)\n",
  971. "[word-rnn](https://github.com/larspars/word-rnn)\n",
  972. "\n",
  973. "[word-rnn-tensorflow](https://github.com/hunkim/word-rnn-tensorflow)"
  974. ]
  975. },
  976. {
  977. "cell_type": "markdown",
  978. "metadata": {
  979. "collapsed": true,
  980. "slideshow": {
  981. "slide_type": "slide"
  982. }
  983. },
  984. "source": [
  985. "# The end #\n",
  986. "\n",
  987. "Questions?\n",
  988. "\n",
  989. "Full write up at: [hallada.net/blog](http://www.hallada.net/2017/07/11/generating-random-poems-with-python.html)"
  990. ]
  991. }
  992. ],
  993. "metadata": {
  994. "celltoolbar": "Slideshow",
  995. "kernelspec": {
  996. "display_name": "Python 3",
  997. "language": "python",
  998. "name": "python3"
  999. },
  1000. "language_info": {
  1001. "codemirror_mode": {
  1002. "name": "ipython",
  1003. "version": 3
  1004. },
  1005. "file_extension": ".py",
  1006. "mimetype": "text/x-python",
  1007. "name": "python",
  1008. "nbconvert_exporter": "python",
  1009. "pygments_lexer": "ipython3",
  1010. "version": "3.5.2"
  1011. },
  1012. "livereveal": {
  1013. "scroll": true,
  1014. "theme": "simple",
  1015. "transition": "linear"
  1016. }
  1017. },
  1018. "nbformat": 4,
  1019. "nbformat_minor": 2
  1020. }