Artificial Intelligence

source: http://alledesign.deviantart.com/art/Artificial-intelligence-252772866

Agenda

  • Scope of AI
  • Google App Engine
  • Make simple chatbot app skeleton

Scope of Artificial Intelligence

panorama

Scope of AI

Extension of human intelligence

  • Think : knowledge representation
  • Percept : vision, speech recognition, language
  • Act : robots
  • Cooperate : multi agent

Google App Engine

detour

Google App Engine

Free 10 apps *

appengine

*)Daily Quotas/apps :

  • Bandwidth : 1GB max 56MB/min
  • Code + static : 1GB
  • Datastore : 1GB, 200 index, 50K ops (r,w,s)
  • Mail : 100, 32/min, 2K attachments 100MB/day
  • URL Fetch : 657K calls, 3K calls/min
  • Task Queue : 100K
  • XMPP : 46M calls
source: http://screenshots.en.sftcdn.net/en/scrn/70000/70210/google-app-engine-1.jpg

API

Python

  • Python 2.7
  • High-Replication Datastore
  • Memcache
  • URL Fetch
  • Mail
  • Image Processing
  • other native API/libraries
https://developers.google.com/appengine/docs/python/overview

Getting started

GAE Python

  • Python 2.7.x (http://www.python.org)
  • Google App Engine SDK (1.7.2)
  • required native libraries

Launcher

create & manage app dev

launcher

Sample app.yaml file

Application metadata

application: nama_app
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.1"

Sample main.py file

Application main

import webapp2

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

Simple NLP Application

Question Answering/Chatbot

Requirement

tools/libs *

Bottlepy

from bottle import route, run, template

@route('/hello/:name')
def index(name='World'):
    return template('Hello {{name}}!', name=name)

run(host='localhost', port=8080)
bottlepy

Using Bottlepy

modified app.yaml

application: kebalikan
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app
        

Using Bottlepy

modified main.py

from bottle import *
import parse

@route('/')
@view('index')
def index():
    return {'q':'', 'a':'', 'd':''}
    
@post('/tanya')
@view('index')
def jawab():
    masukan = request.forms.get('q', '')
    stat, tanggapan,debug = parse.respon(masukan)
    return {'q': masukan,'status':stat, 'a':tanggapan, 'd':debug}

app = default_app()
        
source:

Using Bottlepy

template

<!DOCTYPE html>
<html>
    <head>
        <title>test app</title>
    </head>
    <body>
        <form method="POST" id="newcat" action="/tanya">
            <input type="text" name="q" id="q" value="{{q}}"/>
            <input type="submit" name="ask" value="tanya"/>
        </form>
        <div id="result" style="width:500px;background-color:#ccc;float:left;padding:8px;">{{a}}<br/>
            {{d}}
        </div>
    </body>
</html>
        
source:

Using NLTK

hacking nltk for app engine

bottlepy
  • NOT explicitly supported for GAE *hack it!*
  • create nltk directory in project folder
  • create empty __init__.py file
  • import what you need
  • expect for error, solve by copying required file
  • test run?
  • modify some files (nltk.data)
  • try again until success or stress

Using NLTK

Parsing subset of Indonesian sentences

KALIMAT -> PERTANYAAN | PERNYATAAN | PERINTAH | LARANGAN | SAPAAN
...
PERNYATAAN -> SUBJEK PREDIKAT | KATA_TUNJUK KATA_BENDA KATA_BENDA
...
KATA_BENDA -> KATA_BENDA KATA_HUBUNG KATA_BENDA | KATA_BENDA KATA_BENDA | KATA_BENDA KATA_SIFAT | KATA_PERAN KATA_BENDA
...
KATA_GANTI_ORANG -> 'saya' | 'kamu' | 'dia' | 'ia' | 'beliau' | 'aku' | 'kalian' | 'kami' | 'kita' | 'ibu' | 'bapak' | 'anak' | 'anda'
...
      

Using pebahasa

Indonesian NLP libs

  • morphological operation, tokenization
  • term extraction
  • POS Tagging
  • summarization
  • html cleaning
  • classification? not yet. check pylibsvm
  • demo : http://nlp.pebbie.net

The Code

for initialization

import nltk
import nltk.data
import pickle
import random
from tokenization import tokenisasi_kalimat
from nltk.parse.earleychart import EarleyChartParser

gr = nltk.data.load('file:id.cfg')
parser = EarleyChartParser(gr)
        

The Code

for bots

def respon(inputkal):
    sent = inputkal.strip()
    if len(sent)==0: return False,'',[]
    sent = tokenisasi_kalimat(sent)
    try:
        trees = parser.nbest_parse(sent)
        for n in trees[0]:
            if isinstance(n, nltk.tree.Tree):
                if n.node == 'SAPAAN':
                    if n[0].node == 'SAPAAN_SINGKAT':
                        return False, n[0][0]+' juga',n
                    else:
                        return False,nn[1][0],n
                elif n.node == 'PERNYATAAN':
                    if random.randint(0,10)>4:
                        return False, 'Oke',n
                    else:
                        return False, 'Oya?',n
                elif n.node == 'PERTANYAAN':
                    return False, 'Saya tidak tahu',n
        return False,repr(e)
    except:
        if sent[0] == 'quit': return True, 'OK',sent
        return False,'saya tidak mengerti',sent
        
The question of whether a computer can think is no more interesting than the question of whether a submarine can swim.
E.W. Dijkstra

<Thank You!>

further discussion