-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing2.py
114 lines (82 loc) · 11.6 KB
/
testing2.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 24 20:26:18 2017
@author: Firdauz_Fanani
"""
import csv,numpy,nltk,pandas
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
from nltk import sent_tokenize, word_tokenize, pos_tag
import time
#%%
start_time = time.time()
#Baca data CSV dengan pandas
testing = pandas.read_csv('C:\Users\user\Google Drive\Skripsi\data_test.csv',
names=["Content","Label"])
konten= testing['Content']
label_string_test = testing['Label']
print konten
#%%
#Mengganti Slang Word data tes
def replace_semua(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
slang_word = {'dpt':'dapat','krn':'karena','utk':'untuk','tp':'tapi','sy':'saya',
'sj':'saja','bln':'bulan','bgt':'banget','knp':'kenapa','tlg ' : 'tolong '
,'dwpan' : 'depan','dwngan' : 'dengan','byk' : 'banyak','smoga' : 'semoga',
'kpn' : 'kapan','sdh' : 'sudah','kelwbihan' : 'kelebihan','smua':'semua',
'brani' : 'berani','jd':'jadi',' tlng ' : ' tolong ','jgn2' : 'jangan-jangan',
'bagusn' : 'bagusan','lmynlh' : 'lumayanlah',' ngk ' : ' enggak ',' bner ' : ' bener ',
' blom ' : ' belum ',' pk ' : ' pakai ','maff' : 'maaf','splikasi' :'aplikasi','dnk' : 'dong',
'knp' : 'kenapa','gk' : 'gak','klu' : 'kalau','bli' : 'beli','bncmark' : 'benchmark','bsa' : 'bisa',
'awal2' : 'awal-awal','ttg' : 'tentang','udh' : 'udah','blm' : 'belum','msuk' : 'masuk',
'abg' : 'abang','klo' : 'kalau','brp' : 'berapa','kalo' : 'kalau','reales' : 'rilis','blon' : 'belum',
'gmn' : 'gimana','gmna' : 'gimana','jwb' : 'jawab','yng' : 'yang','yg' : 'yang','syng' : 'sayang','bgs' : 'bagus',
'idup' : 'hidup','dgn' : 'dengan','dpn' : 'depan','dpnx' : 'depannya','blkng' : 'belakang','ngak' : 'enggak','dripada' : 'daripada','bgt' : 'banget','bda' : 'beda','pke' : 'pake','kpke' : 'kepakai','bw' : 'bawa','bs' : 'bisa','knapa' : 'kenapa','hpx' : 'hapenya','bgus2' : 'bagus-bagus','bls' : 'balas','lg' : 'lagi','ttp' : 'tetap','km' : 'kamu','dr' : 'dari','tmn2' : 'teman-teman','blakang' : 'belakang','dri' : 'dari','skrg' : 'sekarang','jg' : 'juga','gpp' : 'gapapa','wktu' : 'waktu','tmpt' : 'tempat','dibles' : 'dibales','karna' : 'karena','stelan' : 'setelan','kwalitas' : 'kualitas','tdk' : 'tidak','jga' : 'juga','mnta' : 'minta','lbih' : 'lebih','riwue' : 'review','skligus':'sekaligus','Ccok':'cocok','drpd':'daripada','tdk':'tidak','trs':'terus','pdhl':'padahal','tlp':'telepon','telefon':'telepon','udh':'udah','dmn':'dimana','smg':'semoga','sblm':'sebelum','lbh':'lebih','tmpt':'tempat','byr':'bayar','thn':'tahun','tsb':'tersebut','spt':'seperti','spy':'supaya'}
slang=[]
for conten in testing['Content']:
txt = replace_semua(conten.lower(), slang_word)
slang.append(txt)
testing['slang'] = slang
#print slang
#%%
#Stop word data tes
stopword = ['ada','adalah','adanya','adapun','agak','agaknya','agar','akan','akankah',
'akhir','akhiri','akhirnya','aku','akulah','amat','amatlah','anda','andalah',
'antar','antara','antaranya','apa','apaan','apabila','apakah','apalagi','apatah',
'artinya','asal','asalkan','atas','atau','ataukah','ataupun','awal','awalnya','bagai',
'bagaikan','bagaimana','bagaimanakah','bagaimanapun','bagi','bagian','bahkan','bahwa',
'bahwasanya','baik','bakal','bakalan','balik','banyak','bapak','baru','bawah','beberapa',
'begini','beginian','beginikah','beginilah','begitu','begitukah','begitulah','begitupun',
'bekerja','belakang','belakangan','belum','belumlah','benar','benarkah','benarlah','berada',
'berakhir','berakhirlah','berakhirnya','berapa','berapakah','berapalah','berapapun','berarti',
'berawal','berbagai','berdatangan','beri','berikan','berikut','berikutnya','berjumlah','berkali-kali',
'berkata','berkehendak','berkeinginan','berkenaan','berlainan','berlalu','berlangsung','berlebihan','bermacam',
'bermacam-macam','bermaksud','bermula','bersama','bersama-sama','bersiap','bersiap-siap','bertanya','bertanya-tanya','berturut','berturut-turut','bertutur','berujar','berupa','besar','betul','betulkah','biasa','biasanya','bila','bilakah','bisa','bisakah','boleh','bolehkah','bolehlah','buat','bukan','bukankah','bukanlah','bukannya','bulan','bung','cara','caranya','cukup','cukupkah','cukuplah','cuma','dahulu','dalam','dan','dapat','dari','daripada','datang','dekat','demi','demikian','demikianlah','dengan','depan','di','dia','diakhiri','diakhirinya','dialah','diantara','diantaranya','diberi','diberikan','diberikannya','dibuat','dibuatnya','didapat','didatangkan','digunakan','diibaratkan','diibaratkannya','diingat','diingatkan','diinginkan','dijawab','dijelaskan','dijelaskannya','dikarenakan','dikatakan','dikatakannya','dikerjakan','diketahui','diketahuinya','dikira','dilakukan','dilalui','dilihat','dimaksud','dimaksudkan','dimaksudkannya','dimaksudnya','diminta','dimintai','dimisalkan','dimulai','dimulailah','dimulainya','dimungkinkan','dini','dipastikan','diperbuat','diperbuatnya','dipergunakan','diperkirakan','diperlihatkan','diperlukan','diperlukannya','dipersoalkan','dipertanyakan','dipunyai','diri','dirinya','disampaikan','disebut','disebutkan','disebutkannya','disini','disinilah','ditambahkan','ditandaskan','ditanya','ditanyai','ditanyakan','ditegaskan','ditujukan','ditunjuk','ditunjuki','ditunjukkan','ditunjukkannya','ditunjuknya','dituturkan','dituturkannya','diucapkan','diucapkannya','diungkapkan','dong','dua','dulu','empat','enggak','enggaknya','entah','entahlah','guna','gunakan','hal','hampir','hanya','hanyalah','hari','harus','haruslah','harusnya','hendak','hendaklah','hendaknya','hingga','ia','ialah','ibarat','ibaratkan','ibaratnya','ibu','ikut','ingat','ingat-ingat','ingin','inginkah','inginkan','ini','inikah','inilah','itu','itukah','itulah','jadi','jadilah','jadinya','jangan','jangankan','janganlah','jauh','jawab','jawaban','jawabnya','jelas','jelaskan','jelaslah','jelasnya','jika','jikalau','juga','jumlah','jumlahnya','justru','kala','kalau','kalaulah','kalaupun','kalian','kami','kamilah','kamu','kamulah','kan','kapan','kapankah','kapanpun','karena','karenanya','kasus','kata','katakan','katakanlah','katanya','ke','keadaan','kebetulan','kecil','kedua','keduanya','keinginan','kelamaan','kelihatan','kelihatannya','kelima','keluar','kembali','kemudian','kemungkinan','kemungkinannya','kenapa','kepada','kepadanya','kesampaian','keseluruhan','keseluruhannya','keterlaluan','ketika','khususnya','kini','kinilah','kira','kira-kira','kiranya','kita','kitalah','kok','kurang','lagi','lagian','lah','lain','lainnya','lalu','lama','lamanya','lanjut','lanjutnya','lebih','lewat','lima','luar','macam','maka','makanya','makin','malah','malahan','mampu','mampukah','mana','manakala','manalagi','masa','masalah','masalahnya','masih','masihkah','masing','masing-masing','mau','maupun','melainkan','melakukan','melalui','melihat','melihatnya','memang','memastikan','memberi','memberikan','membuat','memerlukan','memihak','meminta','memintakan','memisalkan','memperbuat','mempergunakan','memperkirakan','memperlihatkan','mempersiapkan','mempersoalkan','mempertanyakan','mempunyai','memulai','memungkinkan','menaiki','menambahkan','menandaskan','menanti','menanti-nanti','menantikan','menanya','menanyai','menanyakan','mendapat','mendapatkan','mendatang','mendatangi','mendatangkan','menegaskan','mengakhiri','mengapa','mengatakan','mengatakannya','mengenai','mengerjakan','mengetahui','menggunakan','menghendaki','mengibaratkan','mengibaratkannya','mengingat','mengingatkan','menginginkan','mengira','mengucapkan','mengucapkannya','mengungkapkan','menjadi','menjawab','menjelaskan','menuju','menunjuk','menunjuki','menunjukkan','menunjuknya','menurut','menuturkan','menyampaikan','menyangkut','menyatakan','menyebutkan','menyeluruh','menyiapkan','merasa','mereka','merekalah','merupakan','meski','meskipun','meyakini','meyakinkan','minta','mirip','misal','misalkan','misalnya','mula','mulai','mulailah','mulanya','mungkin','mungkinkah','nah','naik','namun','nanti','nantinya','nyaris','nyatanya','oleh','olehnya','pada','padahal','padanya','pak','paling','panjang','pantas','para','pasti','pastilah','penting','pentingnya','per','percuma','perlu','perlukah','perlunya','pernah','persoalan','pertama','pertama-tama','pertanyaan','pertanyakan','pihak','pihaknya','pukul','pula','pun','punya','rasa','rasanya','rata','rupanya','saat','saatnya','saja','sajalah','saling','sama','sama-sama','sambil','sampai','sampai-sampai','sampaikan','sana','sangat','sangatlah','satu','saya','sayalah','se','sebab','sebabnya','sebagai','sebagaimana','sebagainya','sebagian','sebaik','sebaik-baiknya','sebaiknya','sebaliknya','sebanyak','sebegini','sebegitu','sebelum','sebelumnya','sebenarnya','seberapa','sebesar','sebetulnya','sebisanya','sebuah','sebut','sebutlah','sebutnya','secara','secukupnya','sedang','sedangkan','sedemikian','sedikit','sedikitnya','seenaknya','segala','segalanya','segera','seharusnya','sehingga','seingat','sejak','sejauh','sejenak','sejumlah','sekadar','sekadarnya','sekali','sekali-kali','sekalian','sekaligus','sekalipun','sekarang','sekarang','sekecil','seketika','sekiranya','sekitar','sekitarnya','sekurang-kurangnya','sekurangnya','sela','selain','selaku','selalu','selama','selama-lamanya','selamanya','selanjutnya','seluruh','seluruhnya','semacam','semakin','semampu','semampunya','semasa','semasih','semata','semata-mata','semaunya','sementara','semisal','semisalnya','sempat','semua','semuanya','semula','sendiri','sendirian','sendirinya','seolah','seolah-olah','seorang','sepanjang','sepantasnya','sepantasnyalah','seperlunya','seperti','sepertinya','sepihak','sering','seringnya','serta','serupa','sesaat','sesama','sesampai','sesegera','sesekali','seseorang','sesuatu','sesuatunya','sesudah','sesudahnya','setelah','setempat','setengah','seterusnya','setiap','setiba','setibanya','setidak-tidaknya','setidaknya','setinggi','seusai','sewaktu','siap','siapa','siapakah','siapapun','sini','sinilah','soal','soalnya','suatu','sudah','sudahkah','sudahlah','supaya','tadi','tadinya','tahu','tahun','tak','tambah','tambahnya','tampak','tampaknya','tandas','tandasnya','tanpa','tanya','tanyakan','tanyanya','tapi','tegas','tegasnya','telah','tempat','tengah','tentang','tentu','tentulah','tentunya','tepat','terakhir','terasa','terbanyak','terdahulu','terdapat','terdiri','terhadap','terhadapnya','teringat','teringat-ingat','terjadi','terjadilah','terjadinya','terkira','terlalu','terlebih','terlihat','termasuk','ternyata','tersampaikan','tersebut','tersebutlah','tertentu','tertuju','terus','terutama','tetap','tetapi','tiap','tiba','tiba-tiba','tidak','tidakkah','tidaklah','tiga','tinggi','toh','tunjuk','turut','tutur','tuturnya','ucap','ucapnya','ujar','ujarnya','umum','umumnya','ungkap','ungkapnya','untuk','usah','usai','waduh','wah','wahai','waktu','waktunya','walau','walaupun','wong','yaitu','yakin','yakni','yang']
sw=[]
for conten in slang:
kata= filter(lambda x: x not in stopword, conten)
sw.append(kata)
testing['stopword'] = sw
#%%
# buat stemmer data tes
factory = StemmerFactory()
stemmer = factory.create_stemmer()
# stem
st=[]
for conten in sw:
output = stemmer.stem(conten)
st.append(output)
testing['stemmed'] = st
#print st
#%%
#Pisahkan kata/tokenize data tes
PK= [nltk.word_tokenize(PisahKata) for PisahKata in st]
#print PK
#print sw
#panjang = len(kata)
#print panjang
#%%
#Ubah list ke str
string_test = map(' '.join, PK)
print string_test