-
Notifications
You must be signed in to change notification settings - Fork 0
/
Word_Segmentation_Jieba.py
53 lines (41 loc) · 1.22 KB
/
Word_Segmentation_Jieba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# coding: utf-8
# In[1]:
import sys
import time
import jieba
import jieba.analyse as jalz
import jieba.posseg as japseg
from pymongo import MongoClient
#jieba.enable_parallel(4)
#t1 = time.time()
jieba.load_userdict("/home/cylin/Code/Jieba/user_dict") #自訂詞庫檔
jieba.analyse.set_stop_words("/home/cylin/Code/Jieba/stopwords")
#sentence = open("/home/cylin/Code/qqq.txt",'rb').read()
def Extract_Tags(sentence):
words = jalz.extract_tags(sentence,20)
print ",".join(words)
"""
for word in words:
print word.encode('utf-8')
"""
def CutWords(sentence):
words = japseg.cut(sentence)
for word in words:
if word.flag =='n':
print("%s , %s"%(word.word,word.flag))
def Tokenize_Word(sentence):
"""斷字斷詞"""
words = jieba.tokenize(sentence)
for word in words:
print("%s start %d end %d"%(word[0],word[1],word[2]))
def main():
client = MongoClient('localhost',27017)
db = client.SocialMedia
News = db.AppleNews
for data in News.find()[0:10]:
sentence = data['Title']+','+data['Content']
print data['Title']
print data['NewsLink']
CutWords(sentence)
if __name__=='__main__':
main()