-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandomSample.py
46 lines (34 loc) · 1.13 KB
/
RandomSample.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
##
import os, sys
import SonicScrewdriver as utils
import random
rowindices, columns, metadata = utils.readtsv("/Users/tunder/Dropbox/PythonScripts/hathimeta/ExtractedMetadata.tsv")
initialsample = random.sample(rowindices, 2000)
directorylist = os.listdir("/Users/tunder/Dropbox/pagedata/mixedtraining/pagefeatures")
existingfiles = list()
for filename in directorylist:
if filename.startswith(".") or filename.startswith("_"):
continue
htid = utils.pairtreelabel(filename[0:-7])
existingfiles.append(htid)
counter = 0
toremove = list()
for htid in initialsample:
if htid in existingfiles:
counter +=1
toremove.append(htid)
print("Found " + str(counter) + " duplicates.")
for htid in toremove:
initialsample.remove(htid)
genresrepresented = set()
for htid in initialsample:
genrestring = metadata["genres"][htid]
genreinfo = genrestring.split(";")
for genre in genreinfo:
genresrepresented.add(genre)
print(genresrepresented)
user = input("Use this sample? ")
if user == "y":
with open("/Users/tunder/Dropbox/pagedata/cotraining.txt", mode="w", encoding = "utf-8") as f:
for htid in initialsample:
f.write(htid + '\n')