Tuesday, September 28, 2010

Django: 404 page

settings.py:
set DEBUG=False

create 404.html page in your templates directory.

That's it!

Google docs online viewer

Very good viewer for your pdfs from Google. Just copy&paste the code:
<iframe src="http://docs.google.com/gview?url=http://infolab.stanford.edu/pub/papers/google.pdf&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>

replace part written in bold font with your document name

Sunday, September 26, 2010

Postgresql

Create/Alter new user:

su – postgres
psql
alter user postgres with password ‘newpassword’; OR ALTER USER username ENCRYPTED password ‘newpassword’;


Create database:
create database databaseName;
create database databaseName with encoding ‘SQL_ASCII’; OR create database databaseName with encoding ‘utf8′;
grant all privileges on database databaseName to username;

copy files to remote server via ssh

rsync -e ssh -rvzl ttt user@host:/home/ttt

Friday, September 24, 2010

Installation on Ubuntu Lucid

The problem with locales:
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory

set locale:
/usr/share/locales/install-language-pack

Repositories list: /etc/apt/sources.list

get installed pkgs: dpkg --get-selections
dpkg --get-selections|grep python

gcc installation:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential
gcc -v
make -v

gmake:
sudo ln -s /usr/bin/make /usr/bin/gmake

svn:
sudo apt-get install subversion

django:
sudo apt-get install apache2 libapache2-mod-python
svn co http://code.djangoproject.com/svn/django/trunk/ django_src
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
ln -s `pwd`/django_src/django YOUR-DIR/django
sudo cp ~/django_src/django/bin/django-admin.py /usr/local/bin

for posgresql:
apt-get install libreadline6
apt-get install libreadline6-devel
apt-get install libz-dev

psycopg2:
sudo apt-get install python-psycopg2

chardet:
wget http://chardet.feedparser.org/download/python2-chardet-2.0.1.tgz
tar -xzvf python2-chardet-2.0.1.tgz
cd python2-chardet-2.0.1
python setup.py install


matplotlib:
sudo apt-get install python-matplotlib
chmod 777 /dir-with-.mathplotlib or export HOME=any-dir-with-writing-access

PIL:
sudo apt-get install python-imaging

for R:
sudo gedit /etc/apt/sources.list
deb http://rh-mirror.linux.iastate.edu/CRAN/bin/linux/ubuntu lucid/
sudo apt-get update
sudo apt-get install r-base

after that I got problem trying to execute R:
/usr/lib/R/bin/exec/R: symbol lookup error: /usr/local/lib/libreadline.so.6: undefined symbol: PC

the solution:
sudo rm /usr/local/lib/libreadline.*
sudo ldconfig

postgresql:
wget http://wwwmaster.postgresql.org/redir/180/f/source/v9.0.0/postgresql-9.0.0.tar.bz2
bunzip2 postgresql-9.0.0.tar.bz2
tar xzvf postgresql-9.0.0.tar
cd postgresql-9.0.0
./configure

Tuesday, September 21, 2010

MySQL->PostgreSQL

Since I had lots of problems with cyrillic encodings in MySQL, I've decided to move to PostgreSQL.
I've changed my SQL scheme a bit: auto increment PK was converted to serial in PostgreSQL.
Also, I've decided to use Django, so first of all I described my DB model:

from django.db import models

class Cues(models.Model):
    data = models.CharField(max_length=50)
   
    def __unicode__(self):
            return self.data

class Reacts(models.Model):
    data = models.CharField(max_length=100)  

    def __unicode__(self):
            return self.data

class Weights(models.Model):
    cue=models.ForeignKey(Cues)   
    react = models.ForeignKey(Reacts)
    weight=models.FloatField()    

After that I executed scripts:
python manage.py validate
python manage.py sqlall avs
python manage.py syncdb

and it automatically created schemas:
CREATE TABLE avs_cues
(
  id serial NOT NULL,
  data character varying(50) NOT NULL,
  CONSTRAINT avs_cues_pkey PRIMARY KEY (id)
)


CREATE TABLE avs_reacts
(
  id serial NOT NULL,
  data character varying(100) NOT NULL,
  CONSTRAINT avs_reacts_pkey PRIMARY KEY (id)
)



CREATE TABLE avs_weights
(
  id serial NOT NULL,
  cue_id integer NOT NULL,
  react_id integer NOT NULL,
  weight double precision NOT NULL,
  CONSTRAINT avs_weights_pkey PRIMARY KEY (id),
  CONSTRAINT avs_weights_cue_id_fkey FOREIGN KEY (cue_id)
      REFERENCES avs_cues (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
  CONSTRAINT avs_weights_react_id_fkey FOREIGN KEY (react_id)
      REFERENCES avs_reacts (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
WITH (
  OIDS=FALSE
);
ALTER TABLE avs_weights OWNER TO postgres;

-- Index: avs_weights_cue_id

-- DROP INDEX avs_weights_cue_id;

CREATE INDEX avs_weights_cue_id
  ON avs_weights
  USING btree
  (cue_id);

-- Index: avs_weights_react_id

-- DROP INDEX avs_weights_react_id;

CREATE INDEX avs_weights_react_id
  ON avs_weights
  USING btree
  (react_id);
! The problem is that framework doesn't allow to create composite foreign key



Example of query (returns cue and number od reaction for this cue):
select avs_cues.data, count(react_id) from avs_weights,avs_cues where avs_cues.id=avs_weights.cue_id group by avs_cues.data;

Thursday, September 16, 2010

MySQL Database scheme

I've decided to work with utf-8 encoding instead of cp1251.
First, set it:

SET names 'utf8';
DROP TABLE IF EXISTS weights;

DROP TABLE IF EXISTS cues;
DROP TABLE IF EXISTS reacts;

 CREATE TABLE cues
       (
         id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         data VARCHAR(50) collate  utf8_general_ci
       )

CREATE TABLE reacts
       (
         id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         data VARCHAR(100) collate utf8_general_ci
       )

CREATE TABLE weights
       (
         cue_id INT NOT NULL,
         react_id INT NOT NULL,
     weight DOUBLE NOT NULL,
     FOREIGN KEY (cue_id) REFERENCES cues(id),
     FOREIGN KEY (react_id) REFERENCES reacts(id),
     PRIMARY KEY(cue_id, react_id)
       )


Python:
# -*- coding: utf-8 -*- 
import MySQLdb


##connection:
conn = MySQLdb.connect (host = "localhost",
                        user = "user1",
                         passwd = "pass1",
                         db = "db_name")

cursor = conn.cursor ()
cursor.execute ("SET names 'utf8'")
....
cursor.execute ("SELECT id FROM cues WHERE data="+"\""+cue+"\"")   
    cue_id=cursor.fetchone ()[0]

..
cursor.close()
conn.close()

Tuesday, September 14, 2010

Python: Working with unicode, cp1251 encodings

It's impossible to capitalize or change symbol's case(lowercase or uppercase)  if you're using e.g. cp1251 or non-unicode format.

To fix it:
>>> print unicode("ПРИВЕТ",'utf-8').lower()
привет

Monday, September 13, 2010

MySQL + Python

I've decided to work with datatables instead of text files.
Guide for ubuntu (9.10, 10.4):
If you have mysql & python installed, you need:
1. Download MySQLdb files from SourceForge
2. tar -xzvf MySQL-python-1.2.3.tar.gz
3. cd MySQL-python-1.2.3
4. python setup.py build
Here there might be some errors:
 File "setup.py", line 5, in
    from setuptools import setup, Extension
ImportError: No module named setuptools

To fix:  sudo apt-get install python-setuptools python-dev libmysqlclient15-dev
or EnvironmentError: mysql_config not found
To fix: export PATH=$PATH:/usr/local/mysql/bin
5. Again: python setup.py build
6. sudo python setup.py install
7. Check: python
>>> import MySQLdb

Connect to MySQL using bash:
mysql -h localhost -u root -p

Create database: create database my_db;


Script example:
import MySQLdb

conn = MySQLdb.connect (host = "localhost",
                         user = "root",
                         passwd = "my_ps",
                         db = "my_db")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()

Manual: http://www.kitebird.com/articles/pydbapi.html 

Thursday, September 2, 2010

ANN Models for language acquisition

Source: http://www.lsi.upc.edu/~jpoveda/publications/ideal06.pdf
Jordi Poveda,  Alfredo Velidodo from Technical University of Catalonia (UPC), Barcelona, Spain
Nativists: Chomsky, Fodor and Pinker
Non-nativists: Mac Whinney, Bates and Snow (among them)

Probabilistic interpretation of neural networks:

1. MacKay, D. J. C.: Bayesian Methods for Back-propagation Networks. In Domany,   E., van Hemmen, J. L., and Schulten, K. (eds.), Models of Neural Networks III, Ch.   6 (1994). Springer, New York.

2. Neal, R.: Bayesian Learning for Neural Networks, PhD thesis (1994). University of  Toronto, Canada

3. Bishop, C.: Neural Networks for Pattern Recognition (1995). Oxford University
   Press, New York

4. Friston, K.: A Theory of Cortical Responses. Philosophical Transactions of the Royal Society, B, 360(2005)

Artificial Neural Network Architectures for Language Acquisition:
1. Kohonen Self-Organizing Maps
Results:  Anderson, B.: Kohonen Neural Networks and Language. Brain and Language,  (1999) 86-94
2. Elman's SRN for Building Word Meaning Representations
Description:  Distributed representations (i.e. as a vector of weights) for the meaning of a word. The word meaning representations are built
from contextual features, by putting the word in relation to its context, as it
occurs in a stream of input sentences. This is indeed what Li and Farkas do in
the WCD (Word Co-ocurrence Detector) subsystem of their DevLex and
SOMBIP models
Results:  Elman, J. L.: Finding Structure in Time. Cognitive Science, 14 (1990) 179-211



 Models:
1.  Rumelhart, D., McClelland, J.: On the Learning of the Past Tenses of English Verbs. Parallel distributed processing: Explorations in the microstructure of cognition (1986)
2. TRACE: McClelland, J. L., Elman, J. L.: Interactive Processes in Speech Perception: The  TRACE Model. Parallel distributed processing (1986)
3. SOMBIP: A Model of Bilingual Lexicon Acquisition
Li, P., Farkas, I.: A Self-Organizing Connectionist Model of Bilingual Processing.
Bilingual Sentence Processing (2002)
4. DevLex: A Model of Early Lexical Acquisition
Li, P., Farkas, I., MacWhinney, B.: Early Lexical Development in a Self-Organizing Neural Network. Neural Networks (2004)