-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperFunctions.py
101 lines (79 loc) · 2.11 KB
/
helperFunctions.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
import constants
import sys
import os
import porter
#To get stop words from the file
def populateStopWords():
with open(constants.stopWordsFile,encoding=constants.encoding) as stopwords_file:
for line in stopwords_file:
constants.stopwords_l.append(line.replace("\n",""))
#To remove stop words from list
#input list
#return list
def remStopWords(query_l):
words_l = []
for word_s in query_l:
if not word_s in constants.stopwords_l:
words_l.append(word_s)
return words_l
#Dict to string conversion
#input dict{String:String}
#return String
def getStr(dictP):
sRet = ""
for key, value in dictP.items():
sRet = sRet +" "+ str(key)+ " "+str(value)
return sRet
#Make a String of fixed length number by adding zeroes before it
#input number,number
#return String
def makeFixedLengthStr(length,n):
sLen = str(length)
zeros=""
while (n-len(sLen)) > 0:
zeros=zeros+"0"
n=n-1
zeros = zeros+sLen
return zeros
#Make a String of fixed length by adding spaces after it
#input number,number
#return String
def makeFixedLengthSpace(length,n):
sLen = str(length)
zeros=""
while (n-len(sLen)) > 0:
zeros=zeros+" "
n=n-1
zeros = sLen+zeros
return zeros
def normList(L, normalizeTo=1.0):
'''normalize values of a list to make its max = normalizeTo'''
vMax = max(L)
return [ float(x)/(float(vMax)*1.0)*normalizeTo for x in L]
def normalizeTElement(listT):
list0 = []
list1 = []
listR = []
dict0 = {}
for t in listT:
list0.append(t[0])
list1.append(t[1])
list1 = normList(list1)
i=0
for l in list0:
listR.append((l,list1[i]))
i+=1
return listR
def normalizeDictEle(dictP):
dictR = {}
for k,v in dictP.items():
dictR[k] = dict(normalizeTElement(v))
return dictR
#print(normalizeTElement([(1,3),(2,5.0),(3,6.2),(4,6.7),(5,11),(6,33.78),(7,56.22)]))
'''
dictc = {}
dictc[1] = [(1,3),(2,5.0),(3,6.2),(4,6.7)]
dictc[2] = [(5,11),(6,33.78),(7,56.22)]
dictc = normalizeDictEle(dictc)
print(dictc)
'''