if num <= 40:
            a = (t[1], t[0])
            new_list.append(a)
    new_list.sort()
    return new_list


def ls_convert(mylist):
    new_list = []
    for num, t in enumerate(mylist, 1):
        if num <= 40:
            a = str("%s:%s" % (t[0], t[1]))
            new_list.append(a)
    return new_list


print("------------------------")

for fn in ['Obama', 'Romney']:
    if fn == 'Obama':
        pairs = ls_wc(d_Obama)
    else:
        pairs = ls_wc(d_Romney)
    high_count = 20
    low_count = 2
    body = ''
    for word, cnt in pairs:
        body = body + " " + make_HTML_word(word, cnt, high_count, low_count)
    box = make_HTML_box(body)  # creates HTML in a box
    print_HTML_file(box, fn)  # writes HTML to file name 'testFile.html'
            clean_txt_inp_split.remove(another_word) # menghapus kata yang telah dibersihkan sebelumnya jika kata tersebut merupakan stop word
sorted_word_list = sorted(clean_txt_inp_split, reverse = True) # men-sortir kata-kata yang telah dibersihkan dari stop words dari yang terbesar
counter_word = Counter(sorted_word_list).most_common(50) # menghitung jumlah kemunculan kata-kata yang telah diurutkan, dan mengambil top 50

#----mencetak output----#
for i in range((len(counter_word))): 
    print("{:3d} : {:<14s}".format(counter_word[i][1], counter_word[i][0]), end='') # mencetak dengan format value 3 spasi dan key 14 spasi
    if (i+1)%4==0:
        print() # pindah baris jika sudah membuat 4 kolom
print('\n')
holder = input("Please type Enter to continue...") 
file_inp.close() # menutup file_inp
file_inp_stop.close() # menutup file_inp_stop

#----membuat file HTML----#
high_count= counter_word[0][1] # nilai terbesar untuk kemunculan
low_count = counter_word[-1][1] # nilai terkecil untuk kemunculan
counter_word.sort() # mengurutkan berdasarkan abjad
body = '' # membuat string kosong untuk menyimpan body di loop
for word,cnt in counter_word:
    body = body + " " + make_HTML_word(word,cnt,high_count,low_count) #menentukan body untuk box berdasarkan word cloud
box = make_HTML_box(body)  #membuat box di HTML berdasarkan body
file_title = "A Word Cloud of " + file_name
print_HTML_file(box, file_title) # membuat file HTML


 
    


def ls_convert(mylist):
    new_list = []
    for num,t in enumerate(mylist,1):
        if num <=40:
            a = str("%s:%s" % (t[0],t[1]))
            new_list.append(a)
    return new_list    

print("------------------------")

for fn in ['Obama', 'Romney']:
    if fn == 'Obama':
        pairs = ls_wc(d_Obama)
    else:
        pairs = ls_wc(d_Romney)   
    high_count=20
    low_count=2
    body=''
    for word,cnt in pairs:
        body = body + " " + make_HTML_word(word,cnt,high_count,low_count)
    box = make_HTML_box(body)  # creates HTML in a box
    print_HTML_file(box,fn)  # writes HTML to file name 'testFile.html'








Exemple #4
0
    print("{:3d}:{:15s}".format(word[i][0], str(word[i][1])), end="")
    samping += 1
    if samping == 4:
        print("")
        samping = 0
print("\n")
input("Please type Enter to continue ")

#=====================================
boi = ''

#karena yang masuk hanya 50 teratas, maka delete saja selain yang 50 pertama di list
#kalau kurang dari 50, for ini ga jalan kok
for i in range(50, len(word)):
    del word[50]

#mensort sesuatu dengan menggunakan key lambda
#lambda itu function gitu seperti ini yang dipake :
"""
def lambda (i) :
    return i[1]
"""
word = sorted(word, key=lambda i: i[1])

#bagian ini untuk membuat html yang diminta
for i in range(panjang):
    boi += " " + htmlFunctions.make_HTML_word(word[i][1], word[i][0], maxc,
                                              minc)
box = htmlFunctions.make_HTML_box(boi)
htmlFunctions.print_HTML_file(box, 'a word cloud of ' + ayy)
Exemple #5
0
    for i in range(len(list_akhir)):
        print("{:>3d} : {:<14s}".format(list_akhir[i][0], list_akhir[i][1]),
              end='')
        batasan += 1
        if batasan > 3:
            print()
            batasan = 0
        list_akhir[i] = [list_akhir[i][1], list_akhir[i][0]]

#Mengurutkan list berdasarkan alfabet
    list_akhir.sort()

    #Meminta input enter dari user untuk melanjutkan membuat file html
    input('\n\nPlease type Enter to continue ...')

    #Membuat file html
    body = ''
    for word, cnt in list_akhir:
        body = body + " " + htmlFunctions.make_HTML_word(
            word, cnt, high_count, low_count)
    box = htmlFunctions.make_HTML_box(body)
    htmlFunctions.print_HTML_file(box, nama_file + ' Word Cloud')

#Jika terjadi error salah input atau ada interupsi dari user
except (FileNotFoundError):
    print("File doesn't exist, please try again!")
except (IOError):
    print('Input or output error, please try again!')
except (KeyboardInterrupt):
    print('Keyboard interruption, please try again!')
for i in range(0, 38, 4):
    print(romneyList[i][0], ":", romneyList[i][1], "         ",
          romneyList[i + 1][0], ":", romneyList[i + 1][1], "         ",
          romneyList[i + 2][0], ":", romneyList[i + 2][1], "         ",
          romneyList[i + 3][0], ":", romneyList[i + 3][1])

high_count = 200
low_count = 20
body = ""
i = 0

for cnt, word in obamaList:
    body = body + " " + make_HTML_word(word, cnt, high_count, low_count)
    i += 1
    if i == 40:
        break
box = make_HTML_box(body)
print_HTML_file(box, 'Obama')

body = ""
i = 0

for cnt, word in romneyList:
    body = body + " " + make_HTML_word(word, cnt, high_count, low_count)
    i += 1
    if i == 40:
        break

box = make_HTML_box(body)
print_HTML_file(box, 'Romney')