<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PyGaze</title>
	<atom:link href="http://www.pygaze.org/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pygaze.org</link>
	<description>Open source eye-tracking software and more.</description>
	<lastBuildDate>Sun, 21 Feb 2021 22:21:08 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Tutorial: creating a Twitterbot</title>
		<link>http://www.pygaze.org/2016/03/how-to-code-twitter-bot/</link>
		<comments>http://www.pygaze.org/2016/03/how-to-code-twitter-bot/#comments</comments>
		<pubDate>Fri, 04 Mar 2016 11:15:28 +0000</pubDate>
		<dc:creator><![CDATA[Edwin Dalmaijer]]></dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[Markov chain]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.pygaze.org/?p=405</guid>
		<description><![CDATA[Although it sounds like a lot of effort, creating a Twitter bot is actually really easy! This tutorial, along with some simple tools, can help you create Twitter bots that respond when they see certain phrases, or that periodically post a tweet. These bots work with Markov chains, which can generate text that looks superficially good, but is actually quite nonsensical. You can make the bots read your favourite texts, and they will produce new random text in the same style!<p> <a class="continue-reading-link" href="http://www.pygaze.org/2016/03/how-to-code-twitter-bot/">Continue reading<i class="icon-right-dir"></i></a></p>]]></description>
				<content:encoded><![CDATA[<h3>TL;DR</h3>
<p>Although it sounds like a lot of effort, creating a Twitter bot is actually really easy! This tutorial, along with some simple tools, can help you create Twitter bots that respond when they see certain phrases, or that periodically post a tweet. These bots work with Markov chains, which can generate text that looks superficially good, but is actually quite nonsensical. You can make the bots read your favourite texts, and they will produce new random text in the same style!
</p>
<h3>All code is available on GitHub</h3>
<p>The examples on this page use a custom Python library, written by <a href="http://www.pygaze.org/esdalmaijer">Edwin Dalmaijer</a> (that&#8217;s me). This library is open source, and available on GitHub for free. You are very welcome to download and use it, but I would like to kindly ask you to not use it for doing evil stuff. So don&#8217;t start spamming or harassing people!
</p>
<ul>
<li><a href="https://github.com/esdalmaijer/markovbot">GitHub page</a></li>
<li><a href="https://github.com/esdalmaijer/markovbot/archive/master.zip">Direct download (.zip)</a></li>
</ul>
<h3>Step 1: Create a Twitter account</h3>
<p>This is an easy step, no code here yet. Simply follow the instructions to create a new Twitter account.
</p>
<ul>
<li>Go to <a href="https://twitter.com/signup">Twitter&#8217;s sign-up page</a>.</li>
<li>Fill out all details, and <b>make sure to include your phone number</b>. This is a requirement for remote access, and you will need that to make the Twitter bot work.</li>
</ul>
<h3>Step 2: Create a Twitter app</h3>
<p>Not all Twitter accounts are created equal. You will need developer access to yours, if you want to use it via a Python script.
</p>
<ul>
<li>Go to <a href="https://apps.twitter.com/">apps.twitter.com</a></li>
<li>Click on the &#8216;Create New App&#8217; button.</li>
<li>Fill out the details on the form. You have to give your app a name, description, and website (this can be a simple place holder, like <i>http://www.example.com</i>)</li>
<li>Read the Developer Agreement, and check the box at the bottom if you agree. Then click on the &#8216;Create your Twitter application&#8217; button.</li>
</ul>
<h3>Step 3: Keys and access tokens</h3>
<p>This is an important step, as you will need the keys and access tokens for you app. They allow you to sign in to your account via a Python script.
</p>
<ul>
<li>After creating your new app, you were redirected to its own page. If you weren&#8217;t, go to <a href="https://apps.twitter.com/">apps.twitter.com</a> and click on your apps name.</li>
<li>On the app&#8217;s page, click on the &#8216;Keys and Access Tokens&#8217; page.</li>
<li>At the bottom of this page, click on the &#8216;Create my access token&#8217; button.</li>
<li>Make sure you make note of the following four keys, as you will need these later. (Just leave the tab open in your browser, or copy them to a text file or something. Make sure nobody else can access them, though!)</li>
</ul>
<table>
<tr>
<th>Consumer Key (API Key)</th>
<td>[ <i>copy this value from Consumer Settings</i> ]</td>
<tr>
<tr>
<th>Consumer Secret (API Secret)</th>
<td>[ <i>copy this value from Consumer Settings</i> ]</td>
<tr>
<tr>
<th>Access Token</th>
<td>[ <i>copy this value from Your Access Token</i> ]</td>
<tr>
<tr>
<th>Access Token Secret</th>
<td>[ <i>copy this value from Your Access Token</i> ]</td>
<tr>
</table>
<h3>Getting a Python Twitter library</h3>
<p>Before being able to log into Twitter via Python, you need a Python wrapper for the Twitter API. There are several options out there, and I haven&#8217;t excessively researched all of them. This guide will go with Mike Verdone&#8217;s (<a href="https://twitter.com/sixohsix">@sixohsix</a> on Twitter) <a href="http://mike.verdone.ca/twitter/">Python Twitter Tools</a> library, which is nice and elegant. You can also find its source code on <a href="https://github.com/sixohsix/twitter">GitHub</a>.
</p>
<ul>
<li>If you don&#8217;t have it already, you need to install <a href="https://pypi.python.org/pypi/setuptools">setuptools</a>. Download the <a href="https://bootstrap.pypa.io/ez_setup.py">ez_setup.py script</a>, and run it with the Python installation you want to have it installed in.</li>
<li>BONUS STEP: If you don&#8217;t know how to run a Python script, read this step. On Linux and OS X, open a terminal. Then type <b><i>cd DIR</i></b> and hit Enter, but replace DIR by the path to the folder that contains the ez_setup.py script you just downloaded. Next, type <b><i>python ez_setup.py</i></b> and hit Enter. This will run the script that installs setuptools. On Windows, the easiest thing to do is to make a new batch file. To do this, open a text editor (Notepad is fine). Now write <b><i>&#8220;C:\Python27\python.exe&#8221; &#8220;ez_setup.py&#8221;</i></b> on the first line, and <b><i>pause</i></b> on the second line. (Replace &#8220;C:\Python27&#8243; with the path to your Python installation!) Save the file as &#8220;run_ez_setup<b>.bat</b>&#8220;, and make sure to save it in the same folder as the ez_setup.py script. The .bat extension is very important, as it will make your file into a batch file. Double-click the batch file to run it. This will open a Command Prompt in which the ez_setup.py script will be run.</li>
<li>After installing setuptools, you can use it to easily install the Twitter library. On Linux and OS X, open a terminal and type <b><i>easy_install twitter</i></b>. On Windows, create another batch file, write <b><i>&#8220;C:\Python27\Scripts\easy_install.exe&#8221; twitter</i></b> on the first line, and <b><i>pause</i></b> on the second line. (Replace &#8220;C:\Python27&#8243; with the path to your Python installation!)</li>
<li>If you did everything correctly, the Twitter library should now be installed. Test it by opening a Python console, and typing <b><i>import twitter</i></b>. If you don&#8217;t see any errors, that means it works.</li>
</ul>
<h3>Setting up</h3>
<p>Time to get things ready for your Twitter bot script. You need to things: a dedicated folder to store things in, and the markovbot Python library. The <a href="https://github.com/esdalmaijer/markovbot">markovbot</a> library is written by me, and you can easily grab it off GitHub. You&#8217;re also very welcome to post any questions, issues, or additions to the code on GitHub.
</p>
<ul>
<li>Create a new folder, and give it a name. In this example, we will use the name &#8216;TweetBot&#8217; for this folder.</li>
<li>Go to the <a href="https://github.com/esdalmaijer/markovbot">markovbot GitHub page</a>.</li>
<li>Click on the &#8216;Download ZIP&#8217; button, or use this <a href="https://github.com/esdalmaijer/markovbot/archive/master.zip">direct download link</a></li>
<li>Unzip the &#8216;markovbot-master.zip&#8217; file you just downloaded.</li>
<li>Copy the &#8216;markovbot&#8217; folder into the &#8216;TweetBot&#8217; folder.</li>
</ul>
<h3>Getting data</h3>
<p>To establish a Markov chain, you need data. And lots of it. You also need the data to be in machine-readable form, e.g. in a plain text file. Fortunately, <a href="https://www.gutenberg.org/">Project Gutenberg</a> offers an excellent online library, with free books that also come in the form of text files. I&#8217;m talking about . (Please do note that Project Gutenberg is intended for humans to read, and not for bots to crawl through. If you want to use their books, make sure you download and read them yourself. Also, make sure that you have the right to download a book before you do it. Not all countries have the same copyright laws as the United States, where Gutenberg is based.)
</p>
<ul>
<li>Download a book of your choice, for example <a href="https://www.gutenberg.org/ebooks/15489">Sigmund Freud&#8217;s Dream Psychology</a>. Make sure to download the utf-8 encoded text file! (At the bottom of the list.)</li>
<li>Copy (or move) the text file you just downloaded to the &#8216;TweetBot&#8217; folder, and name it &#8216;Freud_Dream_Psychology.txt&#8217;.</li>
</ul>
<h3>Writing your script</h3>
<p>You should have everything now: A Twitter account with developer&#8217;s access, a Twitter library for Python, the custom markovbot library, and some data to read. Best of all: You&#8217;ve organised all of this in a folder, in precisely the way it is described above. Now, it&#8217;s finally time to start coding the actual Twitterbot!
</p>
<ul>
<li>Start your favourite code editor, and open a new Python script.</li>
<li>Save the script in the &#8216;TwitterBot&#8217; folder, for example as &#8216;my_first_bot.py&#8217;.</li>
<li>Start by importing the <b><i>MarkovBot</i></b> class from the <b><i>markovbot</i></b> module you need:</li>
</ul>
<p></p><pre class="crayon-plain-tag">import os
from markovbot import MarkovBot</pre><p> </p>
<ul>
<li>I&#8217;m assuming you are familiar enough with Python that you know what importing a library means. If you are not, maybe it&#8217;d be good to <a href="https://en.wikibooks.org/wiki/A_Beginner's_Python_Tutorial/Importing_Modules">read</a> <a href="http://www.learnpython.org/en/Modules_and_Packages">about</a> <a href="https://docs.python.org/2/tutorial/modules.html">them</a>.</li>
<li>The next step is to initialise your bot. The MarkovBot class requires no input arguments, so creating an instance is as simple as this:</li>
</ul>
<p></p><pre class="crayon-plain-tag"># Initialise a MarkovBot instance
tweetbot = MarkovBot()</pre><p> </p>
<ul>
<li>The next step is important! Before he can generate any text, the MarkovBot needs to read something. You can make it read your Freud example book.</li>
<li>The bot expects you to give him a full path to the file, so you need to construct that first:</li>
</ul>
<p></p><pre class="crayon-plain-tag"># Get the current directory's path
dirname = os.path.dirname(os.path.abspath(__file__))
# Construct the path to the book
book = os.path.join(dirname, 'Freud_Dream_Psychology.txt')
# Make your bot read the book!
tweetbot.read(book)</pre><p> </p>
<ul>
<li>At this point, your bot is clever enough to generate some text. You can try it out, by using its <b><i>generate_text</i></b> method. This takes one argument, and three (optional) keyword arguments (but only one of those is interesting).</li>
<li><b><i>generate_text</i></b>&#8216;s argument is the number of words you want in your text. Let&#8217;s try 25 for now.</li>
<li><b><i>generate_text</i></b>&#8216;s interesting keyword argument is <b><i>seedword</i></b>. You can use it to define one or more keywords that you would like the bot to try and start its sentence with:</li>
</ul>
<p></p><pre class="crayon-plain-tag">my_first_text = tweetbot.generate_text(25, seedword=['dream', 'psychoanalysis'])
print("tweetbot says:")
print(my_first_text)</pre><p> </p>
<ul>
<li>That&#8217;s cool, but what about the Twitter part? Remember you generated those keys and access tokens? You&#8217;ll need them now:</li>
</ul>
<p></p><pre class="crayon-plain-tag"># ALL YOUR SECRET STUFF!
# Consumer Key (API Key)
cons_key = ''
# Consumer Secret (API Secret)
cons_secret = ''
# Access Token
access_token = ''
# Access Token Secret
access_token_secret = ''</pre><p> </p>
<ul>
<li>Replace each set of empty quotes (&#8221;) with your own keys and tokens (these should also between quotes).</li>
<li>First note on the codes here: It&#8217;s actually not very advisable to stick your crucial and secret information in a script. Your script can be read by humans and machines, so this is a highly unsafe procedure! It&#8217;s beyond the scope of this tutorial, but do try to find something better if you have the time.</li>
<li>Second note: Now that you are pasting your secret stuff into a plain script, make sure you paste it correctly! There shouldn&#8217;t be any spaces in the codes, and it&#8217;s really easy to miss a character while copying. If you run into any login error, make sure that your keys have been copied in correctly!</li>
<li>Time for the bot to log in to Twitter:</li>
</ul>
<p></p><pre class="crayon-plain-tag"># Log in to Twitter
tweetbot.twitter_login(cons_key, cons_secret, access_token, access_token_secret)</pre><p> </p>
<ul>
<li>Only one more thing to do before you can start up the bot: You need to decide what your bot should do.</li>
<li>There are two different things the MarkovBot class can do. The first is to periodically post something. This is explained further down.</li>
<li>The second thing the MarkovBot class can do, is to monitor Twitter for specific things. You can specify a target string, which the bot will then use to track what happens on Twitter. For more information on the search string, see the <a href="https://dev.twitter.com/streaming/overview/request-parameters#track">Twitter API website</a>.</li>
<li>The target string determines what tweets your bot will reply to, but it doesn&#8217;t determine what the bot says. For that, you need to specify keywords. These can go in a big list, which the bot will use whenever he sees a new tweet that matches the target string. Your bot will try to find any of your keywords in the tweets he reads, and will then attempt to use the keywords he found to start a reply with. For example, if your target string is <b><i>&#8216;#MarryMeFreud&#8217;</i></b>, and your keywords are <b><i>[&#8216;marriage&#8217;, &#8216;ring&#8217;, &#8216;flowers&#8217;, &#8216;children&#8217;]</i></b>, then your bot could find a tweet that reads <i>I want your flowers and your children! #MarryMeFreud</i>. In this case, the bot would read the tweet, find &#8216;flowers&#8217; and &#8216;children&#8217;, and it will attempt to use those to start his reply. (Note: He won&#8217;t use both words, this is a very simple hierarchical thing, where the bot will try &#8216;flowers&#8217; first, and &#8216;children&#8217; if &#8216;flowers&#8217; doesn&#8217;t work.)</li>
<li>In addition to the above, you can also have the MarkovBot add prefixes and suffixes to your tweets. This allows you to, for example, always start your tweet with a mention of someone (e.g. &#8216;@example&#8217;), or to always end with a hashtag you like (e.g. &#8216;#askFreud&#8217;).</li>
<li>Finally, the MarkovBot allows you to impose some boundaries on its behaviour. Specifically, it allows you to specify the maximal conversational depth at which it is still allowed to reply. If you are going to use your bot to reply to people, this is something you really should do. For example, if your bot always replies to people who mention &#8216;@example&#8217;, they are likely to wish to talk to Edward Xample. It&#8217;s funny to get one or two random responses, but as the conversation between people and Edward Xample continuous, you really don&#8217;t want your bot to keep talking to them. For this purpose, you can set the <b><i>maxconvdepth</i></b> to 2. This will allow your bot to reply only in conversations with no more than two replies.</li>
</ul>
<p></p><pre class="crayon-plain-tag"># Set some parameters for your bot
targetstring = 'MarryMeFreud'
keywords = ['marriage', 'ring', 'flowers', 'children', 'religion']
prefix = None
suffix = '#FreudSaysIDo'
maxconvdepth = None</pre><p> </p>
<ul>
<li>The MarkovBot&#8217;s <b><i>twitter_autoreply_start</i></b> method can start a Thread that will track tweets with your target string, and automatically reply to them using your chosen parameters.</li>
<li>If you want to stop your MarkovBot from automatically replying, you can call its <b><i>twitter_autoreply_start</i></b> method.</li>
<li>How quick your bot replies to tweets is highly dependent on how many tweets are posted that match your target string.</li>
</ul>
<p></p><pre class="crayon-plain-tag"># Start auto-responding to tweets
tweetbot.twitter_autoreply_start(targetstring, keywords=keywords, prefix=prefix, suffix=suffix, maxconvdepth=maxconvdepth)

# Use the following to stop auto-responding
# (Don't do this directly after starting it, or your bot will do nothing!)
tweetbot.twitter_autoreply_stop()</pre><p> </p>
<ul>
<li>Another thing the MarkovBot can do, is to periodically post a new tweet. You can start this with the <b><i>twitter_tweeting_start</i></b> method.</li>
<li>The <b><i>keywords</i></b>, <b><i>prefix</i></b>, and <b><i>suffix</i></b> keywords are available for this function too. The <b><i>keywords</i></b> work a bit different though: For every tweet, one of them is randomly selected. You can also pass <b><i>None</i></b> instead of a list of keywords, in which case your bot will just freestyle it.</li>
<li>One very important thing, is the timing. The <b><i>twitter_tweeting_start</i></b> method provides three keywords to control its timing: <b><i>days</i></b>, <b><i>hours</i></b>, and <b><i>minutes</i></b>. The time you specify here, is the time the bot waits between tweets. You can use all three keywords, or just a single one. If you don&#8217;t specify anything, the bot will use its default of one day.</li>
<li>If you want your bot to stop, you can use the <b><i>twitter_tweeting_stop</i></b> method.</li>
</ul>
<p></p><pre class="crayon-plain-tag"># Start periodically tweeting
tweetbot.twitter_tweeting_start(days=0, hours=19, minutes=30, keywords=None, prefix=None, suffix='#PyGaze')

# Use the following to stop periodically tweeting
# (Don't do this directly after starting it, or your bot will do nothing!)
tweetbot.twitter_tweeting_stop()</pre><p> </p>
<h3>Spamming and trolling</h3>
<p>Although Twitter bots can easily be used to spam and troll people, I kindly ask you not to do it. You gain absolutely nothing by doing it, and Twitter&#8217;s API is built in such a way that it automatically blocks accounts that do too much, so you will be shut down for spamming. Nobody likes spammers and trolls, so don&#8217;t be one.
</p>
<h3>Conclusion</h3>
<p>Creating a Twitter bot is easy! If you want to use my software to create your own, please do go ahead. It&#8217;s free and open, and this page lists the instructions on how to download it. I would love to hear about your projects, so please feel free to leave a comment with your story.
</p>
<h3>UPDATE: Join competitions with your bot!</h3>
<p><a href="https://twitter.com/fourtonfish">Stefan Bohacek</a> was kind enough to point out the existence of a monthly bot competition that he organises. Have a look at his <a href="https://botwiki.org/monthly-bot-challenge/">Botwiki website</a> to learn about this month&#8217;s theme, and how to compete.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pygaze.org/2016/03/how-to-code-twitter-bot/feed/</wfw:commentRss>
		<slash:comments>120</slash:comments>
		</item>
		<item>
		<title>Sigmund Freud Twitter Bot</title>
		<link>http://www.pygaze.org/2016/03/sigmund-freud-twitter-bot/</link>
		<comments>http://www.pygaze.org/2016/03/sigmund-freud-twitter-bot/#comments</comments>
		<pubDate>Wed, 02 Mar 2016 17:09:32 +0000</pubDate>
		<dc:creator><![CDATA[Edwin Dalmaijer]]></dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[Markov chain]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Sigmund Freud]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.pygaze.org/?p=391</guid>
		<description><![CDATA[Sigmund Freud is back! He returned in the form of a <a href="https://twitter.com/SigbotFreud">Twitter bot</a> that replies when someone uses the hashtag <a href="https://twitter.com/hashtag/askFreud">#askFreud</a> in their tweets. Not unlike the real Freud, Sigbot produces nonsensical, but real-looking text that is produced using a Markov chain. The bot can recognise and respond to specific keywords, and it can speak both German and English.<p> <a class="continue-reading-link" href="http://www.pygaze.org/2016/03/sigmund-freud-twitter-bot/">Continue reading<i class="icon-right-dir"></i></a></p>]]></description>
				<content:encoded><![CDATA[<h3>TL;DR</h3>
<p>Sigmund Freud is back! He returned in the form of a <a href="https://twitter.com/SigbotFreud">Twitter bot</a> that replies when someone uses the hashtag <a href="https://twitter.com/hashtag/askFreud">#askFreud</a> in their tweets. Not unlike the real Freud, Sigbot produces nonsensical, but real-looking text that is produced using a Markov chain. The bot can recognise and respond to specific keywords, and it can speak both German and English.
</p>
<h3>What does it say?</h3>
<p><a href="https://twitter.com/SigbotFreud">Sigbot Freud</a> replies to every tweet that has the <a href="https://twitter.com/hashtag/askFreud">#askFreud</a> hashtag. The bot can pick up on keywords that relate to psychoanalysis, and it will reply in Freud&#8217;s writing style. You can try for yourself by posting a tweet with the <a href="https://twitter.com/hashtag/askFreud">#askFreud</a> hashtag, or see Sigbot&#8217;s timeline below:
</p>
<p>As you might well have heard, <a href="https://en.wikipedia.org/wiki/Sigmund_Freud">Sigmund Freud</a> (1856 – 1939) was a famous (and infamous) psychologist. He is the founder of <a href="https://en.wikipedia.org/wiki/Psychoanalysis">psychoanalysis</a>, which a lot of people consider to be at the basis of our current psychological therapy. Nowadays, you would be hard pressed to find a psychologist who actually believes in Freud&#8217;s theories, but that does not mean his legacy is worthless. Freud introduced or popularised several key psychological concepts, such as talk-based therapy, the unconsciousness, and childhood trauma (or regular development). In addition, he was an important voice against (theistic) religion, and overly prude and restrictive societies. In sum, although Freud&#8217;s ideas are highly controversial, he did have an undeniable impact on philosophy and psychology.
</p>
<p><a class="twitter-timeline" data-dnt="true" href="https://twitter.com/SigbotFreud" data-widget-id="705050587772166144">Tweets by @SigbotFreud</a><br />
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></p>
<h3>How does it work?</h3>
<p>Thanks to the efforts of <a href="http://www.gutenberg.org/wiki/Main_Page">Project Gutenberg</a>, a lot of <a href="http://www.gutenberg.org/ebooks/author/391">Sigmund Freud&#8217;s books</a> are available online, for free (copyrights on his works have expired). Sigbot has read a lot of these books to learn about how Freud would phrase things. The bot was specifically interested in the superficial statics of Freud&#8217;s texts, and asked the question: How often do words occur in each other&#8217;s vicinity?
</p>
<p>After learning about the statistics of Freud&#8217;s writing, Sigbot uses a <a href="https://en.wikipedia.org/wiki/Markov_chain">Markov chain</a> to generate random text that is based on the statistics of Freud&#8217;s writing. The principle is as follows: If you feed Sigbot two words, it will check what other words in Freud&#8217;s writing are likely to follow your two words. The bot will then randomly choose a likely match (with more probable matches being selected more often). You now have three words: the two original ones, and the one generated by the bot. The bot will use the last two words (one original and one bot-generated one) to generate a fourth word. It will then go on to generate a fifth word, again based on the last two words. The cool thing is that the bot will continue to generate words until you are satisfied with the length of the produced text. (If you&#8217;re confused at this point, don&#8217;t worry: There is an example in the next paragraphs.)
</p>
<div id="attachment_392" style="width: 436px" class="wp-caption alignnone"><a href="http://www.pygaze.org/wp-content/uploads/2016/03/Sigmund_Freud_LIFE.jpg"><img src="http://www.pygaze.org/wp-content/uploads/2016/03/Sigmund_Freud_LIFE.jpg" alt="SIGMUND FREUD" width="426" height="599" class="size-full wp-image-392" /></a><p class="wp-caption-text">Sigmund Freud and his cigar. Photo from <a href="https://en.wikipedia.org/wiki/File:Sigmund_Freud_LIFE.jpg">WikiMedia</a>.</p></div>
<p>The above theory might be a bit confusing, so here is an example. In the sentences &#8220;My mother works on Mondays&#8221; and &#8220;My mother cycles around town&#8221;, the words &#8220;My mother&#8221; are followed by &#8220;works&#8221; and &#8220;cycles&#8221;. A Markov-based bot that is learning about these sentences will notice and remember the co-occurrence of &#8220;My mother&#8221; and &#8220;cycles&#8221; / &#8220;works&#8221;. In other words, the bot learned about the statistics of the two sentences.
</p>
<p>After the bot has learned about the sentences, you can feed it the words &#8220;My mother&#8221;. The bot will produce either &#8220;cycles&#8221; or &#8220;works&#8221;, because these were the words that co-occurred with &#8220;My mother&#8221; in the two training sentences. Let&#8217;s say the bot chooses &#8220;cycles&#8221;. It can then go on by itself, using the words &#8220;mother cycles&#8221; to generate another word. In the two sentence that the bot learned about, the only word that could follow &#8220;mother cycles&#8221; was &#8220;around&#8221;, which means that the bot can only generate that word.</p>
<p>The current sentence is &#8220;My mother cycles around&#8221;, and the bot can again use the last two words to generate a word that is likely to follow &#8220;cycles around&#8221;. In this case that word would be &#8220;around&#8221;, as this is the only word in the two learned sentences that co-occurs with &#8220;cycles around&#8221;.
</p>
<p>The example bot has only learned about two sentences, so it will only be able to produce a very limited amount of text. The Sigbot, on the other hand, had 6 English and 21(!) German books to learn from. This means it can produce an incredibly large number of different texts!
</p>
<p>To make Sigbot more interactive, I calculated word frequencies in Freud&#8217;s books. The result is a very long list of words that occur very often. Some of these are obvious, such as &#8216;and&#8217;, &#8216;or&#8217;, and &#8216;the&#8217;, but I have filtered those out (that was the only manual labour; if you know of a way to automate it, I would love to hear about it!). What remains after filtering the boring words, is a large selection of hundreds of keywords that relate to Freud&#8217;s work. If you use these keywords in tweets with <a href="https://twitter.com/hashtag/askFreud">#askFreud</a>, Sigbot will recognise them, and it will use them to generate its response to your tweets.
</p>
<h3>Is Sigbot Intelligent?</h3>
<p>After hearing about Sigbot&#8217;s efforts, you might think that it is quite a clever bot. After all, it managed to read books, it can find and remember patterns in language, and it can use its knowledge to talk. It can even reply to the specific things that people ask. However, this does <b>not</b> mean Sigbot is intelligent! The bot has no idea about what it does, it does not understand language, and it does not understand your questions. Everything Sigbot does, is purely probabilistic.
</p>
<div id="attachment_397" style="width: 750px" class="wp-caption alignnone"><a href="http://www.pygaze.org/wp-content/uploads/2016/03/twitter_bot.png"><img src="http://www.pygaze.org/wp-content/uploads/2016/03/twitter_bot.png" alt="XKCD 1646 TWITTERBOT" width="740" height="263" class="size-full wp-image-397" /></a><p class="wp-caption-text">An <a href="http://xkcd.com/1646/">xkcd</a> comic on Twitter bots.</p></div>
<p>In Freud&#8217;s writing, sometimes words occurred more frequently in combination with other words. Sigbot simply uses those frequencies to produce text that matches the word-combination frequencies in Freud&#8217;s work. That&#8217;s it. Unlike the bot in the <a href="http://xkcd.com/1646/">relevant xkcd comic</a> above, Sigbot won&#8217;t become sentient, and it won&#8217;t try to harm you.
</p>
<p><b>UPDATE</b> (3 March 2016, 00:36): Having said all that stuff about Sigbot not becoming any cleverer, I was a bit surprised to see the tweet below. Sigbot seems to be trying to sell someone an eBook. Maybe it <i>is</i> becoming sentient!
</p>
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr"><a href="https://twitter.com/van_Vulpen">@van_Vulpen</a> Dream about the Mission of Project Gutenberg&#39;s Reflections on War and Death, by Sigmund Freud This eBook is for rest.</p>
<p>&mdash; Sigmund Freud (@SigbotFreud) <a href="https://twitter.com/SigbotFreud/status/705180612567670786">March 2, 2016</a></p></blockquote>
<p><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<h3>Bilingualism</h3>
<p>Freud wrote in German, and his books have been translated into English by others. Sigbot is completely agnostic to language, and has learned about the books of each language separately. This means he can produce both German and English responses to your questions. Twitter can automatically detect the language of your tweets, and Sigbot will use this information to select the right language.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pygaze.org/2016/03/sigmund-freud-twitter-bot/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
