Nobodies for Bots

From Hackers & Designers
Revision as of 15:13, 17 January 2020 by Karl (talk | contribs)
Nobodies for Bots
Name Nobodies for Bots
Location Parnassos Cultuurcentrum, Utrecht
Date 2017/09/27
Time 10:00-20:00
PeopleOrganisations Hackers & Designers
Type Meetup
Web Yes
Print No

How would you chat to a bot if it wasn’t optimised for human natural language interaction?

Racter.jpg

nobodies.for-bots is a full day masterclass part of Nederlands Film Festival Interactive: Storyspace (27-28 September 2017, Utrecht). Participants experiment in chatting with bots shaped in a language based upon initial human user input, which are then swapped between groups, so you don’t talk with just some version of yourself. The intent is to ‘make oneself understood’ by bots, rather than the other way around.

It’s like turning inside out human-centric user friendliness and fully embracing pan-usership!

#!/usr/bin/env python
# -*- coding: UTF-8 =-*-

#first run 'pip install nltk'
#built on hardmath123's article hardmath123.github.io/socket-science-2.html
import socket
import select
import random
import ssl
import sys, os
import time
import re
from nltk.corpus import wordnet

if len(sys.argv) != 5:
  print "Usage: python %s <host> <channel> (no need for '#')> [--ssl|--plain] <nick>"
  exit(0)

HOST = sys.argv[1]
CHANNEL = '##'+sys.argv[2]
SSL = sys.argv[3].lower() == '--ssl'
PORT = 6697 if SSL else 6667
NICK = sys.argv[4]

plain = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl.wrap_socket(plain) if SSL else plain

print "Connecting ☺︎"

s.connect((HOST, PORT))
def read_loop(callback):
  data = ''
  CRLF = '\r\n'
  while True:
    time.sleep(0.2) #prevent CPU hogging
    try:
      readables, writables, exceptionals = select.select([s], [s], [s])
      if len(readables) == 1:
        data += s.recv(512);
        while CRLF in data:
          msg = data[:data.index(CRLF)]
          data = data[data.index(CRLF)+2:]
          callback(msg)
    except KeyboardInterrupt:
      print 'Leaving!'
      s.sendall('PART %s :Bye\r\n'%(CHANNEL))
      s.close()
      exit(0)

print 'Registering...'

s.sendall('NICK %s\r\n'%(NICK))
s.sendall("USER %s * * :aff-ect's companion species\r\n"%(NICK))

connected = False

def send(x):
    s.send(x+"\r\n")

def display(x):
    send("PRIVMSG %s :%s"%(CHANNEL, x))

def replace_keep_case(word, replacement, text):
    def func(match):
        g = match.group()
        if g.islower(): return replacement.lower()
        if g.istitle(): return replacement.title()
        if g.isupper(): return replacement.upper()
        return replacement
    return re.sub(word, func, text, flags=re.I)

def got_msg(msg):
  print msg
  global connected
  words = msg.split(' ')
  if 'PING' in msg:
    s.sendall('PONG\r\n')
  if words[1] == '001' and not connected:
    # as per section 5.1 of the RFC, `001` is the numeric response for
    # successful connection && welcome message.
    connected = True
    s.sendall('JOIN %s\r\n'%(CHANNEL))
    print 'Joining ☺︎'
  elif words[1] == 'PRIVMSG' and words[2] == CHANNEL and connected:
      words[3]=words[3].replace(':','')
      words = words[3:]
      synonymlist=[]
      for part in words:
        #   newsentence=synonymlist.append(wordnet.synset("'"+part+".n.01'"))
          syn = wordnet.synsets(part)
          newword=syn[0].lemmas()[0].name()
          synonymlist.append(newword)
          display(synonymlist)
        #   display(syn.lemma_names[1])
    #   display(" ".join(newsentence))


read_loop(got_msg)

To apply → check this page and send an email to siuli@filmfestival.nl (first come, first served).