pythontr.com
#!/usr/bin/python #-*- coding:utf-8 -*- ############################################################################### # Bu betik keyworld density oranını bulmak icin yazılmıştır # 25.11.2012 # Huseyin OZDEMI # husonet # pythontr.com ############################################################################### import re import locale from collections import defaultdict # html clean RE_HTML_CLEAN_DESEN = "<(script|style).*?>.*?</(script|style)>" RE_HTML_CLEAN_DESEN = re.compile(RE_HTML_CLEAN_DESEN, re.DOTALL) TEXT = """ <p>Diyarbakır Valiliği tarafından yapılan açıkla ile, operasyonda ölen kişinin <b>PKK/KCK'nın Dibek grubu lideri Rojad kod adlı terörist olduğunu</b> duyuruldu.<br /><br />Valilik, <b>ölen PKK'lının 2 Kasım'da Narlı Askeri Üs Bölgesi'ne yapılan saldırının planlayıcılarından olduğunu</b> açıkladı.<br /><br /></b>İKİ TERÖRİST ÖLDÜRÜLDÜ</b><br /><br /><a href="[url]http://www.habermonitor.com/tr/haber/trend/Osmaniye[/url]">Osmaniye</a> Valiliği, <b>Hasanbeyli ilçesinde teröristlerle güvenlik güçleri arasında çıkan çatışmada iki bölücü terör örgütü mensubunun etkisiz hale getirildiğini</b> duyurdu.<br /><br />Valilik ayrıca <b>5 adet M16 piyade tüfeği, 3 adet kaleşnikof piyade tüfeği, 1 adet roketatar ve bu silahlara ait mühimmat ile örgütsel doküman ele geçirildiğini ve operasyonların devam ettiğini</b> belirtti.<br /></p> """ #------------------------------------------------------------------------------ # html temizler def clean_full_html(html, remove_entities=False): cleaned = re.sub(RE_HTML_CLEAN_DESEN, "", html)# remove inline js/css cleaned = re.sub("<(.| )*?>", "", cleaned) # remove remaining html tags cleaned = re.sub(" ", "", cleaned) # [ ]+ if remove_entities: cleaned = re.sub("&[^;]*; ?", "", cleaned) return cleaned #------------------------------------------------------------------------------ # Diziyi asc ve desc olarak sıralama yapar def sort(sVal, sort_type=False): return sorted(sVal.items(), key=lambda x: x[1], reverse=sort_type) #------------------------------------------------------------------------------ # str word count u olusturur format 1 verilirse dizi olarak gonderir def str_word_count( string , format = 0 , charlist = '' ): if isinstance( string , str ): words = re.sub( '[^\w '+ charlist +']' , '' , string ) #Remove everything except alphanumeric, spaces and chars from charlist words = words.replace( ' ' , ' ' ).split( ' ' ) #Replacing double spaces with single space and creating list if format == 0: return len( words ) elif format == 1: return words elif format == 2: result = {} for word in words: result[ string.find( word ) ] = word return result return False #------------------------------------------------------------------------------ # Kucuk harfe cevirir def kucuk_harf_cevir(sStr): str = sStr aranan = '' HARFDIZI = [ ('İ','i'), ('Ğ','ğ'),('Ü','ü'), ('Ş','ş'), ('Ö','ö'),('Ç','ç'), ('I','ı') ] for aranan, harf in HARFDIZI: str = str.replace(aranan, harf) str = str.lower() return str #------------------------------------------------------------------------------ # Bu fonksiyon aynı degerleri sayar def array_count_values(sInput): result = defaultdict(int) for x in sInput: result[x] += 1 return result #------------------------------------------------------------------------------ # Bu fonksiyon numara formatlar def number_format(num, places=0): return locale.format("%.*f", (places, num), True) #------------------------------------------------------------------------------ # Keyworld densityler def keyword_density(sVal): words =str_word_count(kucuk_harf_cevir(clean_full_html(sVal)), 1, 'üğşçöı') num_words = len(words) word_count = array_count_values(words) for key in sort(word_count, True): oran = number_format((float(key[1])/num_words) * 100, 2) print key[0] +'='+ str(key[1]) + ' Density ' + oran if __name__ == '__main__': locale.setlocale(locale.LC_NUMERIC, '') keyword_density(TEXT)
Yorumlar