def task5(): # Task 5 tcan = fileMK.readFileToArray(outputPath + "task4-can.txt") dictcan = fileMK.sortedDictFromWords(tcan) s = "" for i in range(10): s += str(dictcan[i]).replace("can ","") + "\n" path = outputPath + "task5-can.txt" fileMK.writeTextToFile(s, path) tgeneral = fileMK.readFileToArray(outputPath + "task4-general.txt") dictgeneral = fileMK.sortedDictFromWords(tgeneral) s = "" for i in range(10): s += str(dictgeneral[i]).replace("general ","") + "\n" path = outputPath + "task5-general.txt" fileMK.writeTextToFile(s, path)
def task6(): # Task 6 hamilton = fileMK.readFile(corpusPath + "Federalist Hamilton.txt") madison = fileMK.readFile(corpusPath + "Federalist Madison.txt") sorted_hamilton = fileMK.sortedDictFromWords(hamilton.lower().split(" ")) sorted_madison = fileMK.sortedDictFromWords(madison.lower().split(" ")) sHamilton = "" sMadison = "" for i in range(15): sHamilton += str(sorted_hamilton[i]) + "\n" sMadison += str(sorted_madison[i]) + "\n" sHamilton = sHamilton.replace("(", "").replace(")", "").replace("'","") sMadison = sMadison.replace("(", "").replace(")", "").replace("'","") h = "Hamilton:\n" + str(sHamilton) m = "Madison:\n" + str(sMadison) print(h) print(m) path = outputPath + "task6.txt" fileMK.writeTextToFile(h + "\n" + m, path)
def task1(): # Task 1 words = [] for i in range(len(t)): words += (t[i].split(" ")) sorted_dict = fileMK.sortedDictFromWords(words) s = "" for i in range(50): s += str(sorted_dict[i]) + "\n" s = s.replace("(", "").replace(")", "").replace("'","").replace(",",":") path = input(" Enter Path & Filename (like \"D:\\terms.txt\")\n") if path == "": path = outputPath + "task1.txt" fileMK.writeTextToFile(s, path)
def task5(): # Task 5 hamilton = fileMK.readFile(corpusPath + "Federalist Hamilton.txt") sorted_hamilton = fileMK.sortedDictFromWords(hamilton.lower().split(" ")) x = [] y = [] for i in range(len(sorted_hamilton)): x.append(i) y.append(sorted_hamilton[i][1]) fileMK.plotZipf(x, y, "Zipf's Law - Plot") x = [] y = [] for i in range(len(sorted_hamilton)): x.append(math.log(i+1)) y.append(math.log(sorted_hamilton[i][1])) fileMK.plotZipf(x, y, "Zipf's Law - Log-Log-Plot")