test_str_distance_util

python 5 days, 21 hours ago
import unittest from unittest.mock import MagicMock import numpy as np from str_distance_util import StrDistanceUtil class TestStrDistanceUtil(unittest.TestCase): def setUp(self): # Mock config and gcp_ai_service self.config = {} self.gcp_ai_service = MagicMock() self.embedding_util_mock = MagicMock() # Initialize StrDistanceUtil with a mock EmbeddingsUtil self.util = StrDistanceUtil(self.config, self.gcp_ai_service) self.util.embedding_util = self.embedding_util_mock def test_distance(self): list1 = [1, 2, 3] list2 = [4, 5, 6] expected = np.linalg.norm(np.array(list1) - np.array(list2)) result = self.util.distance(list1, list2) self.assertAlmostEqual(result, expected, places=5) def test_cosine_distance(self): list1 = [1, 0, 0] list2 = [0, 1, 0] expected = 1 - np.dot(list1, list2) / (np.linalg.norm(list1) * np.linalg.norm(list2)) result = self.util.cosine_distance(list1, list2) self.assertAlmostEqual(result, expected, places=5) def test_get_str_distance(self): call_id = "1234" google_output = ["hello", "world"] azure_output = ["hi", "planet"] # Mock embeddings google_embeddings = [[0.1, 0.2], [0.4, 0.5]] azure_embeddings = [[0.1, 0.1], [0.3, 0.5]] self.embedding_util_mock.get_embeddings.side_effect = [google_embeddings, azure_embeddings] result = self.util.get_str_distance(call_id, google_output, azure_output) # Expected result expected = [ {"CALL_ID": "1234", "GOOGLE_NAMING": "hello", "AZURE_NAMING": "hi", "DISTANCE": 0.1}, {"CALL_ID": "1234", "GOOGLE_NAMING": "world", "AZURE_NAMING": "planet", "DISTANCE": 0.1}, ] self.assertEqual(len(result), len(expected)) for r, e in zip(result, expected): self.assertEqual(r["CALL_ID"], e["CALL_ID"]) self.assertEqual(r["GOOGLE_NAMING"], e["GOOGLE_NAMING"]) self.assertEqual(r["AZURE_NAMING"], e["AZURE_NAMING"]) self.assertAlmostEqual(r["DISTANCE"], e["DISTANCE"], places=5) def test_get_cosine_str_distance(self): call_id = "5678" google_output = ["alpha", "beta"] azure_output = ["gamma", "delta"] # Mock embeddings google_embeddings = [[0.5, 0.5], [0.1, 0.9]] azure_embeddings = [[0.4, 0.4], [0.0, 1.0]] self.embedding_util_mock.get_embeddings.side_effect = [google_embeddings, azure_embeddings] result = self.util.get_cosine_str_distance(call_id, google_output, azure_output) # Expected result expected = [ {"CALL_ID": "5678", "GOOGLE_NAMING": "alpha", "AZURE_NAMING": "gamma", "DISTANCE": 0.0204}, {"CALL_ID": "5678", "GOOGLE_NAMING": "beta", "AZURE_NAMING": "delta", "DISTANCE": 0.0}, ] self.assertEqual(len(result), len(expected)) for r, e in zip(result, expected): self.assertEqual(r["CALL_ID"], e["CALL_ID"]) self.assertEqual(r["GOOGLE_NAMING"], e["GOOGLE_NAMING"]) self.assertEqual(r["AZURE_NAMING"], e["AZURE_NAMING"]) self.assertAlmostEqual(r["DISTANCE"], e["DISTANCE"], places=4) if __name__ == "__main__": unittest.main()
26
Posted By
Python Script to create AWS beanstalk
#!/usr/bin/python
  
import boto
python aws beanstalk
sandeep sandeep
List all files and folders using python os mo
import os

def list_files_folders(path):
python python-os
kishore_kumar
Get current environment variables in python
import os
env = os.environ

python python-os
kishore_kumar
Get os details using python os
import os
print os.uname()
# Don't use os.system('uname -a'), its j
python python-os
kishore_kumar
Get stats ( lines, words, char count ) of fil
def file_stats(path):
    f = open(path, 'r')
    lines = f.readlines()
python
kishore_kumar
Use map function in python
def get_double(num):
    return num * 2

python
kishore_kumar
Python sample codes for beginners
print "Welcome to python"
python
gaya38 gaya38
Python program for even number checking
a=input("Enter a value:")
if (a%2==0):
    print "The given number is even numb
python
gaya38 gaya38
Python program for prime number check
a=input("Enter a value:")
k=0
b=(a/2)+1
python
gaya38 gaya38
Pass command line arguments in python
import sys
x=len(sys.argv)
a=[]
python
gaya38 gaya38
Python program for the largest number in an a
a = [1,43,98,5]#Dummy data
for l in range(len(a)-1):
        if (a[l]>a[l+1]):
python
gaya38 gaya38
print list of even numbers within a range
n=100
a=[10,20,30,40,50]
b=[60,70,80,90]
python
gaya38 gaya38
generate fibonacci series in python
n=input("Enter the constraint to print n
m=input("Enter the maximum value to prin
a=0
python
gaya38 gaya38
Generate Random number within the range in py
import random
print random.uniform(10,500)
python
gaya38 gaya38
Shuffle list elements in python
import random;
z = [1,90,4,2]
z = random.shuffle(z)
python
gaya38 gaya38
use python requests to get contents of url (
import requests

req = requests.get("https://httpbin.org/
python python-requests
kishore_kumar
how to iterate or get values in python dictio
sample_dict = { "number": 1, "fruits": [

for key in sample_dict:
python
kishore_kumar
create matrix and multiply using numpy in pyt
import numpy as np

matrix = [[1,2,3], [4,5,6], [7,8,9]]
python numpy
kishore_kumar
generate random numbers matrix with numpy pyt
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar
Find min , max and mean for numpy arrays
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar