5 分類與標(biāo)注詞匯

importos, re,nltk

fromnltk.corpusimportwords, state_union,brown,treebank

fromcollectionsimportdefaultdict

列表與元組

# words = ['I', 'turned', 'off', 'the', 'spectroroute','the']

# words2=('I', 'turned', 'off', 'the', 'spectroroute','the','I')

# print (set(words))

# #print(reversed(words))

# print(sorted(words))

# print (set(words2))

# print(reversed(words2))

# print(sorted(words2))


#NOUN 名詞

# brown_news_tagged=brown.tagged_words(categories='news',tagset='universal')

# word_tag_pairs=nltk.bigrams(brown_news_tagged)

# noun_proceders = [a[1]for(a,b)in word_tag_pairs if b[1]=='NOUN']

# fdist=nltk.FreqDist(noun_proceders)

# common_proceders=[tag for (tag,value) in fdist.most_common()]

# print(common_proceders) 獲取名詞前置的高頻詞類




#Verb 動(dòng)詞

獲得過去分詞以及過去式詞形相同的動(dòng)詞

# wsj=treebank.tagged_words()

# cfd1=nltk.ConditionalFreqDist(wsj)

# vl=[w for w in cfd1.conditions()if 'VBN' in cfd1[w] and 'VBD' in cfd1[w]]

# print(vl)


獲取某過去分詞詞以及其tag的位置

# cfd2=nltk.ConditionalFreqDist((tag,word)for (word,tag)in wsj)

# vbn_list=list(cfd2['VBN'])

# idx1=wsj.index(('kicked','VBN'))

# print(idx1)


獲取其前置詞

# for v in vbn_list:

#? ? idx=wsj.index((v, 'VBN'))

#? ? print (wsj[idx-1:idx])

等同于:

#print([wsj[wsj.index((v, 'VBN'))-1:wsj.index((v, 'VBN'))] for v in vbn_list])



#Ajectives and Adverbs 形容詞和副詞

詞典反置是常用方法

# def findtags(tag_prefix, tagges_text):

#? ? cfd=nltk.ConditionalFreqDist((tag,word) for (word,tag) in tagges_text

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if tag.startswith(tag_prefix))

#? ? return dict((tag, cfd[tag].most_common(5) for tag in cfd.conditions()))



#exploring tagged? corpora 探索標(biāo)注的數(shù)據(jù)庫

# brwon_learnd_tagged=brown.tagged_words(categories='learned', tagset='universal')

# tags=[b[1]for(a,b)in nltk.bigrams(brwon_learnd_tagged)if a[0]=='often']

# #print(tags)

# fd=nltk.FreqDist(tags)

# print(fd.tabulate())


# brwon_learnd_tagged=brown.tagged_words(categories='news', tagset='universal')

# cfd=nltk.ConditionalFreqDist((word.lower(),tag)

#? ? ? ? ? ? ? ? ? ? ? ? ? ? for (word,tag) in brwon_learnd_tagged)

# for word in sorted(cfd.conditions()):

#? ? if len(cfd[word])>3:

#? ? ? ? tags=[tag for (tag, _) in cfd[word].most_common()]

#? ? ? ? #print(cfd[word])

#? ? ? ? print(word, tags)


#dictionary 詞典:默認(rèn)詞典

# news_words = brown.words(categories='news')

# fd=nltk.FreqDist(news_words)

# v1000=[word for (word, _) in fd.most_common(1000)]

# mapping=defaultdict(lambda: 'UNK')

# for word in v1000:

#? ? mapping[word]=word

# new_word=[mapping[word] for word in news_words]

# print(new_word[:20])



# incrementally updating a Dictionary 詞典內(nèi)容遞增

# words = words.words('en')

# last_letters=defaultdict(list)

# for word in words:

#? ? key=word[-2:] 發(fā)現(xiàn)有該類鍵寒亥,就將其名稱以及值添加到字典中

#? ? last_letters[key].append(word)

# print(last_letters['zy'][:10])

#

# anagrams=defaultdict(list) 找出有特定字母組成的所有的詞

# for word in words:

#? ? key=''.join(sorted(word))

#? ? anagrams[key].append(word)

Nltk提供的簡(jiǎn)單方法

# anagrams=nltk.Index((''.join(sorted(w)),w)for w in words)

# print(anagrams['abc'])


#invert a dictionary 反置詞典 便于查找

# pos={'cats':'N','name':'N','old':'ADJ','young':'ADJ','run':'V', 'sing':'V'}

# #pos2=dict((value,key)for (key,value)in pos.items())

# pos2=nltk.Index((value,key)for (key,value)in pos.items())

# print(pos2['N'])


#Automatic Tagging 自動(dòng)標(biāo)注: 用100個(gè)高頻詞匯的高頻tag做tagger

#The Lookup Tagger 查找tagger

# brown_tagged_sents=brown.tagged_sents(categories='news')

# fd=nltk.FreqDist(brown.words(categories='news'))

# cfd=nltk.ConditionalFreqDist(brown.tagged_words(categories='news'))

# most_freq_words=fd.most_common(100)

# likely_tags=dict((word, cfd[word].max())for (word,_)in most_freq_words)

# baseline_tagger=nltk.UnigramTagger(model=likely_tags)

# print(cfd['news'].max())

# print(cfd['news'].tabulate())

# print(baseline_tagger.evaluate(brown_tagged_sents))



#N-Gram Tagging 多級(jí)標(biāo)注

brown_tagged_sents=brown.tagged_sents(categories='news')

brown_sents=brown.sents(categories='news')

size=int(len(brown_tagged_sents)*0.9)

train_sents=brown_tagged_sents[:size]? 將數(shù)據(jù)拆分

#print(train_sents[3])

test_sents=brown_tagged_sents[size:]

#

unigram_tagger=nltk.UnigramTagger(train_sents)

print(unigram_tagger.size())

#print(unigram_tagger.tag(brown_sents[3]))

#

# print(bigram_tagger.evaluate(test_sents))

#combination

# t0=nltk.DefaultTagger('NN')

# t1=nltk.UnigramTagger(train_sents, backoff=t0)

# t2=nltk.BigramTagger(train_sents, cutoff=2, backoff=t1)

#print(t2.evaluate(test_sents))

# test_tags = [tag for sent in brown.sents(categories='editorial')

#? ? ? ? ? ? ? ? ? for (word, tag) in t2.tag(sent)]

# gold_tags = [tag for (word, tag) in brown.tagged_words(categories='editorial')]

# print(nltk.ConfusionMatrix(gold_tags, test_tags))

# cfd=nltk.ConditionalFreqDist(

#? ? ? ? ? ? ? ? ? ? ? ? ? ? ((x[1],y[0]),y[1])

#? ? ? ? ? ? ? ? ? ? ? ? ? ? for sent in brown_tagged_sents

#? ? ? ? ? ? ? ? ? ? ? ? ? ? for x,y in nltk.bigrams(sent))

#

# ambigous_context=[c for c in cfd.conditions() if len(cfd[c])>1]

# print(sum(cfd[c].N()for c in ambigous_context)/cfd.N())

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末念颈,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子抬吟,更是在濱河造成了極大的恐慌莺奸,老刑警劉巖丑孩,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異灭贷,居然都是意外死亡温学,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門甚疟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來仗岖,“玉大人,你說我怎么就攤上這事览妖≡簦” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵讽膏,是天一觀的道長(zhǎng)檩电。 經(jīng)常有香客問我,道長(zhǎng)府树,這世上最難降的妖魔是什么俐末? 我笑而不...
    開封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮挺尾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘站绪。我一直安慰自己遭铺,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著魂挂,像睡著了一般甫题。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上涂召,一...
    開封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天坠非,我揣著相機(jī)與錄音,去河邊找鬼果正。 笑死炎码,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的秋泳。 我是一名探鬼主播潦闲,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼迫皱!你這毒婦竟也來了歉闰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤卓起,失蹤者是張志新(化名)和其女友劉穎和敬,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體戏阅,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡昼弟,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了饲握。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片私杜。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖救欧,靈堂內(nèi)的尸體忽然破棺而出衰粹,到底是詐尸還是另有隱情,我是刑警寧澤笆怠,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布铝耻,位于F島的核電站,受9級(jí)特大地震影響蹬刷,放射性物質(zhì)發(fā)生泄漏瓢捉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一办成、第九天 我趴在偏房一處隱蔽的房頂上張望泡态。 院中可真熱鬧,春花似錦迂卢、人聲如沸某弦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽靶壮。三九已至怔毛,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間腾降,已是汗流浹背拣度。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留螃壤,地道東北人抗果。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像映穗,于是被迫代替她去往敵國和親窖张。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,476評(píng)論 0 23
  • 娃娃快兩歲了蚁滋,從發(fā)現(xiàn)這個(gè)小生命開始到現(xiàn)在看過的育兒書大約五六本宿接,每本書或多或少都讓我從中吸取了些養(yǎng)分,獨(dú)獨(dú)這本給我...
    秋or秋閱讀 310評(píng)論 0 0
  • 和大家分享一個(gè)老故事辕录,關(guān)于選擇的問題睦霎,在生活中我們時(shí)時(shí)刻刻都面臨選擇,選擇對(duì)了走诞,最好副女。有時(shí)候并不如人意,可能選擇了...
    呦釋原點(diǎn)閱讀 208評(píng)論 0 1
  • 我喜歡安靜蚣旱,出奇的喜歡碑幅。但是我卻害怕孤獨(dú),因?yàn)楣陋?dú)是真的會(huì)讓人上癮塞绿。 寫簡(jiǎn)書也有段時(shí)間了沟涨,雖然自己現(xiàn)在依然停留在菜...
    小撒Samuel閱讀 421評(píng)論 7 5
  • 圣誕節(jié)這天,我早上六點(diǎn)半起床异吻,梳妝打扮裹赴,陪著男票去參加朋友的婚禮,就是圖上這位高高帥帥的哥們诀浪,他的身材氣質(zhì)在所有朋...
    櫻花麻神閱讀 183評(píng)論 1 1