2015-11-11

python regular expression 參數小技巧


測試小結果
1.  python regular expression 的參數中 re.Locale 跟 re.Unicode 有很大的不同,unicode 會讓 英文字跟中文字視為文字: \w ; 而 locale 會讓地區字,如中文,跟英文視為不同字, 此時的 \w 只單指英文字
   
re.L (locale)會讓 regular 在遇到中文時才會判斷 \b(即\w\W交換邊界)
re.U (unicode)會讓 regular 在遇到空白' '時才會判斷 \b(即\w\W交換邊界), 中英字都算 \w

請看例子
>>> re.findall(r'\b\d+\.?\d*\ {0,2}[份吋寸度瓶]{1,2}\b', " 2 瓶  w", re.U)
['2 瓶']
發現了嗎?空白' ' 跟中文字被視為邊界
>>> re.findall(r'\b\d+\.?\d*\ {0,2}[份吋寸度瓶]{1,2}\b', " 2 瓶  w", re.L)
[]
發現了嗎?空白' ' 跟中文字非邊界字

>>> re.findall(r'\b\d+\.?\d*\ {0,2}[份吋寸度瓶]{1,2}\b', "2 瓶w", re.U)
[]
在unicode 時英文=中文
>>> re.findall(r'\b\d+\.?\d*\ {0,2}[份吋寸度瓶]{1,2}\b', "2 瓶w", re.L)
['2 瓶']
在locale 時英文 跟 中文 又變成不同國了

掌握這種小技巧,你就可以無往不利了
要寫出一個超強的 re 解析器也就不是什麼難事了

def get_product_regex():
    regex = []
    special_words = ['.*(?:多拉|哆啦)[Aa]夢',
                     '漢堡QQ糖', "M&M's", "\d{2}姨"
                    ]
   
    special_words = "|".join(special_words)
    regex += [special_words]
    # << 先用中文分開所有字串 >>
    #regex += [r'\b[a-zA-Z]{1,2}\ *號[^\w\b']
    regex += [r'[\d\.]+[合][\d\.]+']
    regex += [r'[\d\.]+[倍元入種切枚包組箱粒支號盒公分升斤束片張重瓦尺年份吋寸度瓶]{1,2}']
    regex += [r'[LMXSlmxs]{1,2}\ *[號]{1,2}']
    regex += [r'[\u4e00-\ufaff /]+']
    # 數字+單位
    regex += [r'\d+\.?\d*(?:[mM][lL]|[kK][gG]|[cC][mM]|[oO][zZ]|gm|g|[cC][cC]|%)']
   
    #regex += ['[a-zA-Z\d\'\"\.\*\-&~]+']
   
    # << 英文部分處理 >>
    # 數字加一個 " 或 ' 結束 (avex note: 使用 re.Local 時 ' " 無法配合\b判斷, 故要獨立)
    regex += [r'\d+\.?\d*[\"\'%/]']
    # ex, 123 x 456
    regex += [r'\d+\ *[\*xX]\ *\d+']
    # 數字加一個以英文字母結束;
    regex += [r'\d+\.?\d+\b']
    regex += [r'\d+\.?\d*[A-Za-z]\b']
    regex += [r'\d+\.?\d*[A-Za-z]+']
   
    # 英文
    regex += [r'[a-zA-Z]+\.[a-zA-Z]+\b']
#    regex += [r'\b[a-zA-Z,\.\~]+\b']
#     regex += [r'\b[a-zA-Z]+\b']
    # 大寫英文
    #regex += [r'(?<=[\d\.])[A-Z]+\b|[A-Z]+\b']
   
    # 數字
    regex += [r'\d+\"?\'?']
   
    # 英文含數字
    regex += [r'[a-zA-Z\d\-\+\'\ \"\.~/]+']

    s_reg = "|".join(regex)
    #print(s_reg)
    #r = re.compile(s_reg, re.IGNORECASE)
    r = re.compile(s_reg, re.L)
   
    return r


測試字串:
>>>s = '0.5L佳珍密封罐 000060 4DC 12V車用電風扇5" 插式sp1627df uE-35、L號 R.F 12升貝納頌100%拿鐵3合1裝2.3kg咖啡2oz(22g*10)6入'

>>> r.findall(s)

['0.5L', '佳珍密封罐 ', '000060', ' ', '4DC', ' ', '12V', '車用電風扇', '5"', ' 插式', 'sp1627df uE-35', 'L號', ' ', 'R.F', ' ', '12升', '貝納頌', '100%', '拿鐵', '3合1', '裝', '2.3kg', '咖啡', '2oz', '22g', '10', '6入']



看吧,這麼複雜的字串都可以幫你分類的好好的,大數據就是要從基礎建設開始



python 支援的正規表示式(regular expression) function 比較

match: 依規則搜尋單一結果,搜尋一定是從頭(^)開始比對,即使用 re.MULTILINE
search: 依規則搜尋單一結果,搜尋不需要是從頭(^)開始比對,若要限制一定從頭比對可以加入 ^,例如 "^要找的規則"
findall: 用 search 去搜尋,並且給出多個結果

finditer: 同 findall, 但回傳資料同 match 有position資料,而非 findall 的 string




2015-10-28

安裝 opencc for python

溫馨提示:本篇為 opencc 0.4.3 安裝教學,若你要安裝新版的 opencc 1.0.4請到這裡  

如果你只要安裝 opencc 指令工具那你只要
MAC
# brew install opencc
CENTOS
# yum install opencc

但若你 是要 opencc 給 python 執行,那你只能:

2015-10-20

如何在 rabbitmq-server 上用指令設定帳號


請依以下指令設定 帳號 成 administrator, 你可以用此帳號遠端登入 management web ui
#rabbitmqctl add_user 帳號   密碼
#rabbitmqctl set_user_tags 帳號 administrator
#rabbitmqctl set_permissions -p / 帳號 ".*" ".*" ".*"


 如果你想用原來的 guest/guest 帳號遠端登入 management web ui

 主要就是在 config 檔 /etc/rabbitmq/rabbitmq.config 輸入底下 loop back 設定, 然後重開 rabbitmq 即可
[{rabbit, [{loopback_users, []}]}].
開啟 web 管理介面的指令 rabbitmq-plugins enable rabbitmq_management

2015-10-06

MySQL 5.6 varchar 超過最大長度 max-lenght/max-size 還被儲存問題

如果你發現你的 MySQL 好像容忍度大增,例如
* 明明是 unsigned int 的欄位你卻可以儲存負數(-1)
* 明明限制 varchar 長度為 10 但卻可以輸入 11 長字串儲存時自動截斷字串(auto truncate)
 "123456789012345" => "1234567890"  (auto truncated & saved)

這是因為
MySQL 有二個設定

2015-08-11

「可疑 IP 列表」
 可疑 IP 有可能是電腦被綁架,大多非主動攻擊者,但都有攻擊現象,故建議短期內還是加入 「IP 阻攔列表裡」


192.187.110.98
182.118.53.125
218.246.34.99
185.78.112.26
104.236.212.155
137.226.113.7




2015-07-31

使用 python + django 做一個大數據網路平台


你是不是想知道使用 python + django 做一個大數據網路平台可以做到什麼樣的程度?
我發現 這裡 比異鳥 (http://www.biyibird.info) 有個網站,它使用了 python + django + search engine + jquery
很輕鬆的從龐大數據庫內抓取必要的資料,並減少資料庫的負擔。
想寫好一篇作文最佳的捷徑就是多看些文章
想學 python 的人最好的方式就是多參考一些網站,這個網站大家可以多多瀏覽、瞭解、參考看看



2015-07-29

Using .htaccess to redirect from one domain to another

If you want to redirect your www.from.com  to  www.to.com

[.htaccess]
in your root directory(or sub-directory, according to ur needs), create an .htaccess file, and input content below.


RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.from.com$ [NC]
RewriteRule ^(.*)$ http://www.to.com/$1 [R=301,L]


[例子]
RewriteEngine On
# 從  from.com  轉到 www.to.info
RewriteCond %{HTTP_HOST} ^from\.com$ [NC]
RewriteRule ^(.*)$ http://www.to.info/$1 [L,R=301]
# 從  www.from.com  轉到 www.to.info
RewriteCond %{HTTP_HOST} ^www\.from\.com$ [NC]
RewriteRule ^(.*)$ http://www.to.info/$1 [R=301,L]



[try]

go to browser URL input http://www.from.com/account/login and then you will be redirected to http://www.to.com/account/login

URI is corrected be mapped.