I’ve implemented the following functions, but I still cannot pass…
Question Answered step-by-step I’ve implemented the following functions, but I still cannot pass… I’ve implemented the following functions, but I still cannot pass the test cases. I need help figuring out what changes need to be made to pass the test cases. I have provided the code and the test cases.import mathimport refrom collections import defaultdict class VecDense: def tokenizeDoc(self, oneDoc: str): “””This method tokenizes a a string. :param oneDoc: a string. :return: a tokenized sting. “”” sanitizedStr = re.sub(r'[^a-zA-Z0-9 ]’, ”, oneDoc) tokens = sanitizedStr.lower().split(” “) return tokens def getVecLength(self, vecIn: list): “””This method computes the length of a vector. :param vecIn: a list representing a vector, one element per dimension. :return: the length of the vector. “”” return len(vecIn) def normalizeVec(self, vecIn:list): “””This method normalizes a vector to unit length. :param vecIn: a list representing a vector, one element per dimension. :return: a list representing a vector, that has been normalized to unit length. “”” vmag = math.sqrt(sum(vecIn[i]*vecIn[i] for i in range(len(vecIn)))) return [ vecIn[i]/vmag for i in range(len(vecIn)) ] def dotProductVec(self, vecInA:list, vecInB:list): “””This method takes the dot product of two vectors. :param vecInA, vecInB: two lists representing vectors, one element per dimension. :return: the dot product. “”” dot_product = 0 for vec1, vec2 in zip(vecInA, vecInB): dot_product += vec1 * vec2 return dot_product def cosine(self, vecInA: list, vecInB: list): “””This method obtains the cosine between two vectors (which is nominally the dot product of two vectors of unit length). :param vecInA, vecInB: two lists representing vectors, one element per dimension. :return: the cosine. “”” dot_product = self.dotProductVec(vecInA, vecInB) mod_vec1 = 0 mod_vec2 = 0 for vec in vecInA: mod_vec1 += vec ** 2 mod_vec1 = mod_vec1 ** (0.5) for vec in vecInB: mod_vec2 += vec ** 2 mod_vec2 = mod_vec2 ** (0.5) cosine_val = dot_product / (mod_vec1 * mod_vec2) return cosine_val def computeCentroidVector(self, tokensIn:list, vecDict:dict): “””This method calculates the centroid vector from a list of tokens. The centroid vector is the “average” vector of a list of tokens. #NOTE: Special considerations: – all tokens should be converted to lower case. – if a vector isn’t in the dictionary, it shouldn’t be a part of the average. :param tokensIn: a list of tokens. :param vecDict: the vector library is a dictionary, ‘vecDict’, whose keys are tokens, and values are lists representing vectors. :return: the centroid vector, represented as a list. “”” tokensIn = [token.lower() for token in tokensIn] tokens_we_care_abt = [token for token in tokensIn if token in vecDict] vectors = [vecDict[token] for token in tokens_we_care_abt] centroid_vector = [] for index in range(len(vectors[0])): sum = 0 for vector in vectors: sum += vector[index] centroid_vector.append(sum/len(vectors)) return centroid_vector TEST CASES:import pytestfrom collections import defaultdictfrom main import VecDense, VecSparseTFIDF, loadVectors, d..>@pytest.fixture(autouse=True) # Check the length calculation for dense vectors is correctdef test_vecDenseLength(): vecDense = VecDense() testVec1 = [0.5, 0.4, 0.3, 0.2] length = vecDense.getVecLength(testVec1) #print(length) assert (length == pytest.approx(0.734, abs=0.01))# Check that the vector normalization is workingdef test_vecDenseNormalize(): vecDense = VecDense() testVec1 = [0.5, 0.4, 0.3, 0.2] normVec = vecDense.normalizeVec(testVec1) length = vecDense.getVecLength(normVec) assert (length == pytest.approx(1.0, abs=0.01))def test_vecDenseDotProduct(): vecDense = VecDense() testVec1 = [0.5, 0.4, 0.3] testVec2 = [0.3, 0.2, 0.1] dot = vecDense.dotProductVec(testVec1, testVec2) assert(dot == pytest.approx(0.26, abs=0.01))def test_vecDenseCosine(): vecDense = VecDense() vecDict = { ‘cat’: [0.5, 0.4, 0.3, 0.1], ‘dog’: [0.6, 0.4, 0.2, 0.9], ‘apple’: [0.3, 0.3, 0.3, 0.5], ‘banana’: [0.5, 0.5, 0.3, 0.5], } cosineCatDog = vecDense.cosine(vecDict[‘cat’], vecDict[‘dog’]) cosineAppleBanana = vecDense.cosine(vecDict[‘apple’], vecDict[‘banana’]) cosineCatApple = vecDense.cosine(vecDict[‘cat’], vecDict[‘apple’]) cosineDogDog = vecDense.cosine(vecDict[‘dog’], vecDict[‘dog’]) print(cosineCatDog) print(cosineAppleBanana) print(cosineCatApple) print(cosineDogDog) assert(cosineCatDog == pytest.approx(0.73, abs=0.01)) assert(cosineAppleBanana == pytest.approx(0.97, abs=0.01)) assert(cosineCatApple == pytest.approx(0.79, abs=0.01)) assert(cosineDogDog == pytest.approx(1.00, abs=0.01))def test_vecDenseCentroid(): vecDense = VecDense() vecDict = { ‘cat’: [0.5, 0.4, 0.3, 0.1], ‘dog’: [0.6, 0.4, 0.2, 0.9], ‘apple’: [0.3, 0.3, 0.3, 0.5], ‘banana’: [0.5, 0.5, 0.3, 0.5], } sent1 = “the cat saw the dog with the apple” centroidVec = vecDense.computeCentroidVector(vecDense.tokenizeDoc(sent1), vecDict) print( centroidVec ) assert( centroidVec[0] == pytest.approx(0.466, abs=0.01)) assert( centroidVec[1] == pytest.approx(0.366, abs=0.01)) assert( centroidVec[2] == pytest.approx(0.266, abs=0.01)) assert( centroidVec[3] == pytest.approx(0.500, abs=0.01)) python – please help asap! Computer Science Engineering & Technology Python Programming CSC 439 Share QuestionEmailCopy link Comments (0)


