MARK CHANG'S BLOG

若發生公式跑掉或無法正常顯示的情形,請在公式上按右鍵設定:math setting-> math render->SVG
  • About Me
  • Archive
  • feeds

Posts match “ Python ” tag:

about 9 years ago

Python -- List Comprehension

List comprehension(列表示推導)是一種可以讓程式碼更簡潔,增加可讀性與執行效率的方法。
可以將很多行的for迴圈縮在短短一行之內,很方便!

1.

假如我們現在有一個list--l1

l1=[2,5,3,8,4,9]

若我們想要建立一個新表單l2,是把l1中的每一個元素加上二,最先想到的寫法是:

l2=[]
for l in l1: 
   l2.append(l+2) 

知道怎麼用List comprehension的話,我們可以用列表示:

l2=[l+2 for l in l1] 

這樣看起來就簡潔多了!

Read on →
  • Python
  • list_comprehension
  • February 16, 2014 12:13
  • Permalink
  • Comments
 
about 9 years ago

Python nltk -- Sinica Treebank

今天我們來談談如何用python nltk做中文的的自然語言處理
nltk有內建的中文treebank,是中研院的sinica treebank
是免費的中文treebank
至於要如何使用呢?
首先 先載入模組

>>> from nltk.corpus import sinica_treebank
>>> import nltk

接下來我們來看看treebank裡面的東西:

Read on →
  • Python
  • natural_language_processing
  • nltk
  • March 09, 2014 15:41
  • Permalink
  • Comments
 
about 9 years ago

Python Standard Library -- urlparse

今天來談到URL parse(URL剖析)

在製作web crawler(爬網頁程式)的時候
常常會需要用到URL剖析

1.URL的組成架構

URL的組成架構如下:
[scheme]:/[net_loc]/[path];[params]?[query]#[fragment]

舉個例子
http://www.espn.com:80/basketball/nba/index.htmllang=engl?team=dallas#Roster;

則此URL的架構為:
[scheme] http
[net_loc] www.espn.com:80
[path] /basketball/nba/index.html
[params] lang=engl
[query] team=dallas
[fragment] Roster

Read on →
  • Python
  • web_crawler
  • python_standard_library
  • March 11, 2014 10:56
  • Permalink
  • Comments
 
about 9 years ago

Python nltk -- Tree

今天我們來看看nltk.Tree要怎麼用

先載入模組

>>> from nltk import Tree

1.build syntax tree

舉個例子
Gary plays baseball
此句子的剖析樹(syntax tree)是這樣:
nltk1.png

用nltk.Tree來建構tree:

>>> tree=Tree('S',[Tree('NP',['Gary']),
...           Tree('VP',[Tree('VT',['plays']),
...                     Tree('NP',['baseball'])])])
>>> tree.draw()

即可將剖析樹畫出來
若沒安裝 python-tk,你也可以這樣把tree印出

>>> tree.pprint()
'(S (NP Gary) (VP (VT plays) (NP baseball)))'
Read on →
  • Python
  • nltk
  • natural_language_processing
  • grammar
  • March 01, 2014 00:36
  • Permalink
  • Comments
 
about 9 years ago

Python nltk -- Logic 2 : Lambda Calculus

本篇介紹如何用python nltk 的應用,邏輯語意與lambda calculus

1.introduction

邏輯語意學在語意推導方面,通常會用到
使用就可以把一個句子的語意,從個別單字中推導出來
至於 是什麼呢?
簡而言之,lambda calculus是一種數學運算,由以下三種元素組成

是
是 ,就是把function中的variable拿到前面,加個
是將另一個 放到 後面
然後可以進行一種運算,叫做 ,如下

還有另一種運算叫做 ,其實就是更改變數名稱而已

Read on →
  • Python
  • nltk
  • semantics
  • logic
  • March 13, 2014 22:09
  • Permalink
  • Comments
 
about 9 years ago

Python -- Functional Programming Style 2

用Functional Programming style來寫程式的時候
可以大幅減少行數和變數的數量
本文接續上一篇:http://cpmarkchang.logdown.com/posts/178995-python-functional-programming-style-1
繼續探討這種Functional Programming style在python中的應用

1. operator

首先,載入 operator 這個模組

>>> import operator

然後,來介紹到底要怎麼使用

Read on →
  • functional_programming
  • Python
  • operator
  • functools
  • March 20, 2014 21:35
  • Permalink
  • Comments
 
almost 9 years ago

Python nltk -- Logic 4 : Model and Satisfiability

1.Model and Satisfiability

可滿足性(Satisfiability)是在探討, 邏輯式子所建立出的模型(Model),
可不可以找到一組解, 使得這個 Model 算出來的值可以是
例如:

則當 時,
則 Model 是 Satisfiable
另一例子:

這種情形,不論 或 的值, 永遠都是
則 Model 是 Unsatisfiable

Read on →
  • Python
  • nltk
  • semantics
  • logic
  • March 29, 2014 23:39
  • Permalink
  • Comments
 
almost 9 years ago

Python nltk -- Logic 3 : Discourse Representation Theory

1. Introduction

Discourse 的意思是對話
在對話中,常常會用到 代名詞 ,像是 he, she 或 it.
我們把這種代名詞叫做 anaphoric pronouns
因為要從前面的句子去判斷,這些代名詞代表什麼
比如有個句子 A woman walks. She smokes.
在下一句的 She 是指前一句提到的 A woman

那要怎麼讓電腦去判斷, 代名詞 到底代表前面提到的什麼?
這就要用到 Discourse Representation Theory (DRT) 來處理了
例如 A woman walks 這句話,用 DRT 可以表示成這樣:

Read on →
  • Python
  • nltk
  • semantics
  • logic
  • March 21, 2014 11:58
  • Permalink
  • Comments
 
almost 9 years ago

Python Built-in Functions -- eval and execute

1.Introduction

如果要把 string 的內容, 當成程式碼來執行, 可以用到 eval 或 exec

例如有個 string , 為 s1="3+5" 我們想要算它執行的結果, 可用

>>> s1="3+5"
>>> eval(s1)
8

來看一下怎麼用 eval 或 exec

2. eval

eval 是當我們要計算某一個字串中的運算, 並且 會回傳計算結果 ,如下

>>> eval('3+1')
4
Read on →
  • Python
  • eval
  • exec
  • April 23, 2014 09:59
  • Permalink
  • Comments
 

Copyright © 2013 cpmarkchang . Powered by Logdown.
Based on work at subtlepatterns.com.