python利用jieba進行中文分詞去停用詞,pythonjieba

來源:互聯網
上載者:User

python利用jieba進行中文分詞去停用詞,pythonjieba

中文分詞(Chinese Word Segmentation) 指的是將一個漢字序列切分成一個一個單獨的詞。

分詞模組jieba,它是python比較好用的分詞模組。待分詞的字串可以是 unicode 或 UTF-8 字串、GBK 字串。注意:不建議直接輸入 GBK 字串,可能無法預料地錯誤解碼成 UTF-8

支援三種分詞模式

1 精確模式,試圖將句子最精確地切開,適合文本分析;

2 全模式,把句子中所有的可以成詞的詞語都掃描出來, 速度非常快,但是不能解決歧義;

3 搜尋引擎模式,在精確模式的基礎上,對長詞再次切分,提高召回率,適合用於搜尋引擎分詞。

 

# 精確模式seg_list = jieba.cut("我去過清華大學和北京大學。")

# 全模式seg_list = jieba.cut("我去過清華大學和北京大學。", cut_all=True)

# 搜尋引擎模式seg_list = jieba.cut_for_search("我去過清華大學和北京大學。")

 

#精確模式: 我/ 去過/ 清華大學/ 和/ 北京大學/ 。

#全模式: 我/ 去過/ 清華/ 清華大學/ 華大/ 大學/ 和/ 北京/ 北京大學/ 大學/ /

#搜尋引擎模式: 我/ 去過/ 清華/ 華大/ 大學/ 清華大學/ 和/ 北京/ 大學/ 北京大學/

#coding=utf-8
import jieba. analyse
stopwords=[]
for word in open('stopwords.txt','r'):
stopwords.append(word.strip())
article=open('1.txt','r').read()
words=jieba.cut(article,cut_all=False)
stayed_line=""
for word in words:
if word.encode("utf-8")not in stopwords:
stayed_line+=word+" "
print stayed_line

w=open('2.txt','w')
w.write(stayed_line.encode('utf-8'))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.