Saturday, January 19, 2013

grep + find example 範例

find ./ -name \*.py -exec grep -wl nltk {} \;

Find all the files which contain 'nltk' in their content in current directory.

找出當前目錄下所有內容含有'nltk'這個字的python檔案


mysql-python:EnvironmentError: mysql_config not found


1. 首先安装mysql
sudo apt-get install mysql-server
 
2. 安装mysql-python
下载MySQL-python-1.2.3.tar.gz(见附件),解压到指定目录。
到解压后的MySQL-python-1.2.3目录下编译配置MySQL-python:
python setup.py build
此时系统报错:EnvironmentError: mysql_config not found
很明显没有mysql_config这个文件
执行find / -name mysql_config,没有任何数据,表明系统中没有mysql_config这个文件
 
 
网上有人解释说使用apt-get安装的MySQL是没有mysql_config这个文件的
解决办法:sudo apt-get install libmysqld-dev
 
此时执行  find / -name mysql_config 在/usr/bin/下发现了这个文件
然后修改MySQL-python-1.2.3目录下的site.cfg文件
去掉mysql_config=XXX这行的注释,并改成mysql_config=/usr/bin/mysql_config(以mysql_config文件所在机器上的目录为准)
执行下面命令,此时可以成功编译安装了:
python setup.py build
python setup.py install

Python strip lstrip rstrip usage

theString = 'saaaay yes no yaaaass' 
print theString.strip('say') 
print theString.strip('say ') # there is a space after "say"
print theString.lstrip('say') 
print theString.rstrip('say') 

Result: 
yes no 
es no 
yes no yaaaass 
saaaay yes no

Python ImportError:No module named MySQLdb


solution:
Use one of this commands, depends what os and software do u have and use
  1. easy_install mysql-python (mix os)
  2. pip install mysql-python (mix os)
  3. apt-get install python-mysqldb (Linux Ubuntu, ...)
  4. cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
  5. yum install mysql-python (Linux Fedora, ...)



Reference :  http://stackoverflow.com/questions/454854/no-module-named-mysqldb


ImportError: No module named _tkinter


I'm using ubuntu11.10 and python2.7.2 .
A weird error:

File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in 
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter


But Tk was already installed in my computer...
$ sudo apt-get install python-tkReading package lists...Done
Building dependency tree       Reading state information...Done
python-tk is already the newest version.
0 upgraded,0 newly installed,0 to remove and0not upgraded.



It may be the problem of having two different versions of python installed in the computer.

Solution:
sudo ln -f /usr/bin/python2.7 /usr/local/bin/python


error when installing nltk ImportError: no module named yaml


1.Download PyYAML
 http://pyyaml.org/download/pyyaml/PyYAML-3.10.tar.gz
 
2.Unpack the archive and install the package by executing
  $python setup.py install
 
3.The source distribution includes a comprehensive test suite. To run the tests, type  
  $python setup.py test
 

Reference: http://pyyaml.org/wiki/PyYAML



ImportError: No module named xxxxx


用python時常遇到的一個錯誤: ImportError: No module named xxxxx   (xxxxx為module名)
例:  ImportError: No module named nltk

根據小弟經驗,發生這種錯誤通常有兩種可能
1. 沒有安裝xxxxx這個module,那就去網路找一下安裝就行了
2. 已安裝這個module,但python找不到這個moudle安裝在哪(有可能是同時安裝了兩個版本的python所導致)。
   這時有多種解法,最簡單的就是先找到這個moudle安裝的路徑,並把該路徑append到程式碼中,例: http://vocaloidmanager.blogspot.tw/2013/01/importerror-no-module-named-nltk.html

ImportError: No module named nltk


Solution:
In your code, simply append a path for nltk before you import nltk.
For example:

sys.path.append("/usr/local/lib/python2.7/site-packages/nltk-2.0b8-py2.7.egg/")
import nltk
 

The path /usr/local/lib/python2.7/site-packages/nltk-2.0b8-py2.7.egg/ is where the folder "nltk" located.


vim delete mutiple lines (刪除多行)

delete everything from line a to line b 
:a, b de

ex: 
:1,10 de


vim delete empty line (刪除空白行)


delete those lines which have no content :
:g/^$/d

delete those lines which are only composed of "space":
:g/^\s*$/d

delete those lines which are composed of "space or tab":
:g/^[ |\t]*$/d

刪除沒有內容的空行
:g/^$/d
刪除包含有空格組成的空行
:g/^\s*$/d
除以空格或tab開頭到結尾的空行
:g/^[ |\t]*$/d

From http://blog.cmchen.net/2006/12/25/325/

remove weird symbols in vim (刪除奇怪字元)



to remove character such as ^M, ^K,...

In vi, do a :%s/^M//g
To get the ^M hold the control key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurances and replace them with nothing.


如何紀錄linux終端的操作日誌


linux終端下,為方便檢查操作中可能出現的錯誤,以及避免螢幕滾屏的限制,我們可以把操作日誌記錄下來。常用的工具有 screen,script,以及tee等。
  1. screen — screen manager with VT100/ANSI terminal emulatio
  > screen -L
  這裡是我們的操作
  > exit
  結束後會生成一個類似這樣的檔案名: screenlog.0
  > more screenlog.0
  這樣可以查看你剛才進行的操作,發現可能存在的問題
  2. script  — make typescript of terminal session
  > script
  我們的操作
  > exit
  生成一個這樣的檔:typescript
  > more typescript
  這裡查看我們進行的操作
  3. tee –  read from standard input and write to standard output and files
  這個命令可以讀取終端輸入輸出到終端或者檔中,有時候可以用來記錄make等命令可能產生的輸出到終端的大量內容輸出到檔中去。這樣可以方便記 錄這些命令的日誌。
  > make 2>&1 | tee make.log
  當然,我們也可以直接重定向到一個檔中
  > make > make.log
  PS: 2>&1是為了記錄錯誤日誌
  if you want to filter the control symbols, try to use the "col" command like this:
  $ cat screenlog.0 | col -b > screenlog
  or
  $ cat typescript | col -b > scriptlog
  還有一個比較好用的命令tail -f
  比如把輸出定向到text
  ./run.sh>text
  tail -f  text 這個命令會follow輸出定向檔的內容


ImportError: cannot import name urandom


I use Ubuntu11.10 and python2.7.2+ .
File "/usr/lib/python2.7/random.py", line 47, in <module>
    from os import urandom as _urandom
ImportError: cannot import name urandom

After I checked os.py , I found that there is no module named "urandom" in it:
$ python
Python 2.7.2+ (default, Aug 16 2011, 07:24:41)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.urandom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'urandom'

So I reinstall python2.7.2 :
$ ./configure
$ make
$ sudo make install

then it fixed the error!


vim 縮排


在 VIM 中撰寫程式,可以利用「gg」指令將游標移到視窗最上方、利用「v」切換到選取模式、再用「G」將游標移到檔案尾端 (即達到全選的功能),最後按「=」,VIM 便會幫你的程式做自動對齊

另外也可在選取後按
Shift+< : 將選取的程式碼往左縮排一次
Shift+> : 將選取的程式碼往右縮排一次
或是按一個數字後再按Shift+< , 就可做指定次數的縮排

Unable to find a medium containing a live file system


When I was trying to install linux(ubuntu 11.10) from live CD, it showed "Unable to find a medium containing a live file system"
 
solution:
1.Pull out the ATA which linked to CD-ROM and hard-disks, then exchange the ATA head which was connected to your CD-ROM with the ATA head which was connected to hard-disks. In other words, use different heads to connect them to CD-ROM and hard-disks.

2.Restart the computer, done!




用linux(ubuntu)光碟開機想重灌時出現 Unable to find a medium containing a live file system
 
我的解法:
1.把連接光碟機和硬碟的ATA拔掉, 把ATA原本插光碟機的接頭(長的那端)插在硬碟上, 原本插光碟機的ATA接頭(較短那端)插在硬碟上, 總之把ATA接頭在光碟機和硬碟間多換幾次試試
2.重新開機, 就解決了!

修改Ubuntu 11.10 ssh登入畫面


在一般linux(例:CentOS)要修改ssh登入畫面,只要修改 /etc/motd 這個檔的內容就行。
但Ubuntu(11.10)較不同,
修改/etc/motd後下次登入還是會顯示同樣畫面。(因/etc/motd只是指向/var/run/motd的symbolic link)

最簡單修改Ubuntu11.10登入畫面的方法是:
1.在/etc/新增一個檔案motd.static
   sudo vi /etc/motd.static
   接著自己增添motd.static的內容,增添的內容就是新的登入畫面(但無法加入\d等顯示系統資訊的指令)

2.讓/etc/motd改指向/etc/motd.static
 sudo ln -f -s /etc/motd.static /etc/motd
  
3.重新登入,登入顯示訊息就會變成 /etc/motd.static 的內容 




若想修改顯示的系統訊息(如kernel版本, 時間),
則可修改/etc/update-motd.d/下的檔案,
例如在沒有更動/etc/motd的情況下,把/etc/update-motd.d/00-header的執行權限取消,
則登入後就不會顯示"Welcome to Ubuntu 11.10 (GNU/Linux 3.x.x-xx-generic-pae...)"

Ubuntu linux cat 指令合併檔案


用法:

cat file.01 file.02 file.03 > file.merge
(將 file.01  file.02  file.03 合併到 file.merge)

另外,如果要合併的檔名有空白字元的話,那就要檔名前後加上 " "

cat "arthur today.001"  "arthur today.001"  "arthur today.004" > arthurtoday.avi



Reference:
http://www.arthurtoday.com/2010/10/ubuntu-linux.html

Ubuntu上的Apache設定檔 httpd.conf


我的Ubuntu11.10是用Apache2,
但找不到httpd.conf這個檔,後來發現Ubuntu的Apache2設定檔不是httpd.conf。
Apache2的設定檔是/etc/apache2/apache2.conf
但若要調整網站設定(ServerName, DocumentRoot等) ,則應修改/etc/apache2/sites-available/default

Reference : http://www.arthurtoday.com/2009/11/ubuntu-httpdcon.html

C語言 fopen 用法


需include stdio.h


FILE *fopen ( const char *filename, const char *mode ); 

開啟檔案,filename 為檔案名稱含路徑,mode 字串為檔案的模式;

開啟成功傳回一個 FILE指標 ,失敗傳回 NULL。



  • mode字串種類如下: 
  • "r"       唯讀文字檔 
  • "w"     唯寫文字檔;檔案若存在,刪除內容重新寫入;檔案不存在則建立新檔 
  • "a"      附加文字檔,從檔案尾端寫入,檔案不存在時則建立新檔 
  • "rb"     唯讀二進位檔 
  • "wb"   唯寫二進位檔;檔案若存在,刪除內容重新寫入;檔案不存則在建立新檔 
  • "ab"    附加二進位檔,從檔案尾端寫入,檔案不存在時建立新檔 
  • "r+"     可讀寫文字檔;檔案若存在,從檔案開頭寫入;檔案不存在則建立新檔 
  • "w+"   可讀寫文字檔;檔案若存在,覆寫資料;檔案不存在則建立新檔 
  • "a+"    可 附加、讀取 ,從檔案尾端寫入,檔案不存在時則建立新檔 
  • "r+b"   二進位檔可讀寫 
  • "w+b" 二進位檔可讀寫 
  • "a+b"  二進位檔附加、讀取



轉錄自
https://sites.google.com/site/teachyin/c-c/12-dang-an/1-dang-an-han-shi

Stanford Parser tutorial example guide ( Stanford Parser 教學 範例 )

A simple tutorial and example for Stanford Parser version 2.0.3 in Linux


1.Stanford parser download:
 http://nlp.stanford.edu/software/lex-parser.shtml


2.Decompress
tar -xzvf stanford-parser-2012-07-09.tgz
cd stanford-parser-2012-07-09


3.
(1)Parsing English file:
./lexparser.sh Inputfile
(Inputfile is the file you want to parse)
you can modify lexparser.sh to change options,
for example,  change outputformat from "penn,typedDependencies" to "oneline"


(2)Parsing Chinese file:

(i) First, modify lexparser-lang.sh:
vi lexparser-lang.sh

     Change memory option:
Change mem=10g to mem=2g  (Since my computer is 32bit and not enough memory)

     Modify line 48 to:
java -server -Xmx"$mem" -Xms"$mem" -cp "$scriptdir/*:" edu.stanford.nlp.parser.lexparser.LexicalizedParser -v -maxLength "$len" \

     Modify line 50 to:
-outputFilesExtension "$out_file"."$len".stp -outputFormat "oneline" \
   

(ii) Decompress the Chinese grammar file:
jar -xvf stanford-parser-2012-07-09-models.jar

(iii) Now we can parse :
./lexparser-lang.sh Chinese 100 edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz hahaha Inputfile

the output file will be "Inputfile.hahaha.100.stp"

(Inputfile is the file you want to parse, 100 is maximum length of the sentences to parse, edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz is serialized grammar file,hahaha is prefix for the output filename)

you can modify lexparser-lang.sh to change options,
for example,  change outputformat from "penn,typedDependencies" to "oneline"


Reference: http://linglit194.linglit.tu-darmstadt.de/linguisticsweb/bin/view/LinguisticsWeb/StanfordParser


後記 : 2012-07-09這個版本的lexparser-lang.sh有一些bug...搞了好久才弄清楚怎麼用