一:python基礎,自然語言概念
from nltk.book import *
1,text1.concordance("monstrous") 用語索引
2,text1.similar("best")
3,text2.common_contexts(["monstrous", "very"])
4,text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])
5,text3.generate()
6,sorted(set(text3))
7,text3.count("smote")
8,100 * text4.count('a') / len(text4)
ex1 = ['Monty', 'Python', 'and', 'the', 'Holy', 'Grail']。鏈表list
sorted(ex1),len(set(ex1)), ex1.count('the')。
['Monty', 'Python'] + ['and', 'the', 'Holy', 'Grail']
sent1.append("Some")
text4[173],text4.index('awaken'),text5[16715:16735],index從0開始,不包含右邊的index
FreqDist(text1) 頻率分布
高頻詞和低頻詞,停用詞 hapaxes() 低頻詞
long_words = [w for w in V if len(w) > 15]
fdist5 = FreqDist(text5)
sorted([w for w in set(text5) if len(w) > 7 and fdist5[w] > 7])
bigrams
>>> bigrams(['more', 'is', 'said', 'than', 'done'])
[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]
text4.collocations()
詞長,詞頻
用途:
1,詞意消歧
2,指代消解
3,機器翻譯
4,人機對話系統
5,文本的含義
一個標識符token是表示一個我們想要放在一組對待的字符序列——如:hairy、his 或者:)——的術語
一個詞類型是指一個詞在一個文本中獨一無二的出現形式或拼寫
將文本當做詞鏈表,文本不外乎是詞和標點符號的序列
1,變量
2,字符串 name * 2
3,鏈表 list :saying = ['After', 'all', 'is', 'said', 'and', 'done'];saying[-2:]?saying[-2:0]
4,條件:[w for w in text if condition] and or
5,嵌套代碼塊,控制結構 冒號表示當前語句與后面的縮進塊有關聯
if len(word) >= 5:
print 'word length is greater than or equal to 5'
for word in ['Call', 'me', 'Ishmael', '.']:
print word
6,函數 :def mult(x, y),局部變量,全局變量global
7,模塊module:textproc.py; from textproc import plural;plural('wish')
8,包package
函數含義
s.startswith(t) 測試s 是否以t 開頭
s.endswith(t) 測試s 是否以t 結尾
t in s 測試s 是否包含t
s.islower() 測試s 中所有字符是否都是小寫字母
s.isupper() 測試s 中所有字符是否都是大寫字母
s.isalpha() 測試s 中所有字符是否都是字母
s.isalnum() 測試s 中所有字符是否都是字母或數字
s.isdigit() 測試s 中所有字符是否都是數字
s.istitle() 測試s 是否首字母大寫(s 中所有的詞都首字母大寫)
二:語料庫
1,古騰堡語料庫
古騰堡項目,gutenberg
文本特征:平均詞長、平均句子長度,詞頻
2,網絡和聊天文本
3,布朗語料庫
from nltk.corpus import brown
brown.categories()
4,路透社語料庫
5,就職演說語料庫
6,標注文本語料庫
文本語料庫的結構:

載入你自己的語料庫
條件頻率分布:

條件和事件:
pairs = [('news', 'The'), ('news', 'Fulton'), ('news', 'County'), ...]
繪制分布圖和分布表
詞匯工具:Toolbox和 Shoebox
WordNet
WordNet 是一個面向語義的英語詞典,由同義詞的集合—或稱為同義詞集(synsets)—
組成,並且組織成一個網絡
意義與同義詞:wn.synsets('motorcar');wn.synset('car.n.01').lemma_names;
['car', 'auto', 'automobile', 'machine', 'motorcar']
WordNet的層次結構

WordNet 概念層次片段:每個節點對應一個同義詞集;邊表示上位詞/下位詞關系,即
上級概念與從屬概念的關系;
詞匯關系:上/下位,整體/部分,蘊涵,反義詞
語義相似度:
path_similarityassigns是基於上位詞層次結構中相互連接的概念之間的最短路徑在0-1 范圍的打分(兩者之間沒有路徑就返回-1)。同義詞集與自身比較將返回1;Path方法是兩個概念之間最短路徑長度的倒數
is-a關系是縱向的,has-part關系是橫向
齊夫定律:f(w)是一個自由文本中的詞w 的頻率。假設一個文本中的所有詞都按照它
們的頻率排名,頻率最高的在最前面。齊夫定律指出一個詞類型的頻率與它的排名成反
比(即f×r=k,k 是某個常數)。例如:最常見的第50 個詞類型出現的頻率應該是最常
見的第150 個詞型出現頻率的3 倍
三:加工原料文本
分詞和詞干提取
1,分詞
tokens = nltk.word_tokenize(raw)
2,處理HTML
raw = nltk.clean_html(html)
3,讀取本地文件
f = open('document.txt'); raw = f.read()
4,NLP 的流程

5,字符串:最底層的文本處理
字符串運算:+,* 【b = [' ' * 2 * (7 - i) + 'very' * i for i in a]】
輸出字符串:print monty
訪問單個字符:monty[0]
訪問子字符串:monty[6:10];monty[-12:-7]

更多的字符串操作:

鏈表與字符串的差異
query = 'Who knows?'
beatles = ['John', 'Paul', 'George', 'Ringo']
字符串是不可變的,鏈表是可變的
6,Unicode編碼,解碼
在 Python中使用本地編碼
#!/bin/env python
# -*- coding: UTF-8 -*-
#Filename:build_SmartNavigation.py
7,正則表達式re
[w for w in wordlist if re.search('ed$', w)]
[w for w in wordlist if re.search('^..j..t..$', w)] [^aeiouAEIOU]
sum(1 for w in text if re.search('^e-? mail$', w))
[w for w in wordlist if re.search('^[ghi][mno][jlk][def]$', w)]
[w for w in chat_words if re.search('^m+i+n+e+$', w)]
[w for w in chat_words if re.search('^[ha]+$', w)] +*
【轉義】,{}【出現次數】,()【范圍】和|【取或】
[w for w in wsj if re.search('^[0-9]+-[a-z]{3,5}$', w)]
[w for w in wsj if re.search('(ed|ing)$', w)]

re的用處:查找詞干;搜索已分詞文本;
8,規范化文本【 詞干提取器 :詞形歸並】
lower();
詞干提取:
porter = nltk.PorterStemmer();
[porter.stem(t) for t in tokens];
詞形歸並:
詞形歸並是一個過程,將一個詞的各種形式(如:appeared,appears)映射到這個詞標
准的或引用的形式,也稱為詞位或詞元(如:appear)
wnl = nltk.WordNetLemmatizer()
[wnl.lemmatize(t) for t in tokens]
9,用正則表達式為文本分詞
re.split(r' ', raw)
re.split(r'[ tn]+', raw)
re.split(r'W+', raw)
10,NLTK 的正則表達式分詞器
nltk.regexp_tokenize()
11,斷句,分詞:分詞是將文本分割成基本單位或標記,例如詞和標點符號
現在分詞的任務變成了一個搜索問題:找到將文本字符串正確分割成詞匯的字位串

text = "doyouseethekittyseethedoggydoyoulikethekittylikethedoggy"
>>> seg1 = "0000000000000001000000000010000000000000000100000000000"
>>> seg2 = "0100100100100001001001000010100100010010000100010010000"
>>> seg3 = "0000100100000011001000000110000100010000001100010000001"
>>> evaluate(text, seg3)
46
>>> evaluate(text, seg2)
47
>>> evaluate(text, seg1)
63
利用模擬退火算法
12,從鏈表到字符串
silly = ['We', 'called', 'him', 'Tortoise', 'because', 'he', 'taught', 'us', '.']
' '.join(silly)
'We called him Tortoise because he taught us .'
"%s wants a %s %s" % ("Lee", "sandwich", "for lunch")