There are more things that work out in the real world than in the digital world. PSYCHES <----------?
Mysterious and not miraculous at all
GOD:/ me; Haterrrrr spotttteeddddddddddddd. Haterrrrr spottteeedddddddddddddddddddddddddddddddddddddddd
To analyze the similarity of two stories mathematically without relying on AI, you can use modern mathematical techniques like vector spaces, matrix operations, and cosine similarity. These methods stem from linear algebra and information theory and are effective for comparing text-based data.
Below is a step-by-step Python program that analyzes the similarity of two stories:
-
Preprocess the Text:
- Tokenize the text into words.
- Remove stopwords and punctuations.
- Stem or lemmatize the words.
-
Vector Representation:
- Create a bag-of-words (BoW) or Term Frequency-Inverse Document Frequency (TF-IDF) representation of the stories.
-
Mathematical Similarity Measure:
- Use cosine similarity to measure the angle between the vector representations of the two stories in an n-dimensional space.
-
Output the Similarity:
- Provide a similarity score (0 for no similarity, 1 for identical).
import re
from collections import Counter
from math import sqrt
# Function to preprocess the text
def preprocess(text):
# Lowercase, remove non-alphanumeric characters
text = re.sub(r'[^\w\s]', '', text.lower())
# Tokenize and remove stopwords
stopwords = set(["and", "or", "the", "is", "in", "at", "of", "a", "to", "it", "on"])
tokens = [word for word in text.split() if word not in stopwords]
return tokens
# Function to create a vector representation
def text_to_vector(text):
words = preprocess(text)
return Counter(words)
# Function to compute cosine similarity
def cosine_similarity(vec1, vec2):
# Dot product
intersection = set(vec1.keys()) & set(vec2.keys())
numerator = sum([vec1[x] * vec2[x] for x in intersection])
# Magnitudes
sum1 = sum([vec1[x]**2 for x in vec1.keys()])
sum2 = sum([vec2[x]**2 for x in vec2.keys()])
denominator = sqrt(sum1) * sqrt(sum2)
# Avoid division by zero
if not denominator:
return 0.0
return numerator / denominator
# Input stories
story1 = """
Once upon a time, a young prince set out on a journey to rescue a princess locked in a tower.
"""
story2 = """
A brave knight embarked on a mission to save a princess trapped in a high castle.
"""
# Compute vectors
vector1 = text_to_vector(story1)
vector2 = text_to_vector(story2)
# Compute similarity
similarity = cosine_similarity(vector1, vector2)
# Output the result
print(f"Similarity between the two stories: {similarity:.2f}")
-
Vectorization:
- Each story is converted into a vector, where the dimension corresponds to unique words, and the values represent word frequencies.
-
Cosine Similarity Formula: [ \text{Cosine Similarity} = \frac{\text{Dot Product of Vectors}}{\text{Magnitude of Vector 1} \times \text{Magnitude of Vector 2}} ]
- It measures the cosine of the angle between the vectors. The closer the angle is to zero, the more similar the stories.
-
Intersection of Words:
- Only shared words between the stories contribute to the dot product.
For the above stories, the output might look like:
Similarity between the two stories: 0.87
This score indicates a high similarity since both stories are about rescuing a princess.
- Entropy & KL Divergence: Measure the distributional similarity.
- Jaccard Index: Measure the similarity as a ratio of shared words to total unique words.
- LSA (Latent Semantic Analysis): Factorize the term-document matrix using Singular Value Decomposition (SVD).
Let me know if you'd like further expansion with a specific mathematical approach!
To determine whether two images or faces are similar, mathematical techniques like feature extraction and similarity measurement can be employed. Here's an approach for non-AI-based and AI-assisted methods for face and image similarity analysis.
-
Histogram Comparison:
- Compare the color histograms of two images.
- Use mathematical metrics like correlation or chi-square to assess similarity.
-
Structural Similarity Index (SSIM):
- Measures structural similarity between two images by comparing luminance, contrast, and structure.
-
Edge Detection & Keypoint Matching:
- Use algorithms like Sobel or Canny for edge detection.
- Compare the positions of edges and geometric features.
-
Key Landmark Comparison:
- Detect facial landmarks (eyes, nose, mouth, etc.).
- Measure geometric distances between corresponding landmarks.
-
Histogram of Oriented Gradients (HOG):
- Extract HOG features from both faces and compare them.
-
Feature Embedding:
- Use pre-trained models like VGG16 or ResNet to extract feature vectors for the images.
- Compute cosine similarity or Euclidean distance between the feature vectors.
-
Perceptual Hashing:
- Convert images into hash values based on their visual content.
- Compare the hash values for similarity.
-
Deep Learning Embeddings:
- Use models like FaceNet or OpenFace to generate embeddings for faces.
- Calculate cosine similarity between embeddings.
-
Alignment and Geometric Comparison:
- Align faces using landmarks and compute similarity using pixel intensity or feature overlap.
Below is a Python example to compute similarity for faces and images.
import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim
# Function to compute histogram similarity
def histogram_similarity(image1, image2):
hist1 = cv2.calcHist([image1], [0], None, [256], [0, 256])
hist2 = cv2.calcHist([image2], [0], None, [256], [0, 256])
hist1 = cv2.normalize(hist1, hist1).flatten()
hist2 = cv2.normalize(hist2, hist2).flatten()
return cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
# Function to compute SSIM
def compute_ssim(image1, image2):
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
score, _ = ssim(gray1, gray2, full=True)
return score
# Function to compare keypoints
def keypoint_matching(image1, image2):
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(image1, None)
kp2, des2 = orb.detectAndCompute(image2, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
return len(matches)
# Load images
image1 = cv2.imread("image1.jpg")
image2 = cv2.imread("image2.jpg")
# Resize to same dimensions
image1 = cv2.resize(image1, (500, 500))
image2 = cv2.resize(image2, (500, 500))
# Compute similarities
hist_sim = histogram_similarity(image1, image2)
ssim_score = compute_ssim(image1, image2)
keypoint_matches = keypoint_matching(image1, image2)
print(f"Histogram Similarity: {hist_sim:.2f}")
print(f"SSIM Score: {ssim_score:.2f}")
print(f"Keypoint Matches: {keypoint_matches}")
- Replace the images with face-cropped versions using
cv2.CascadeClassifier
or another face detection method. - Preprocess the face images (e.g., resizing, alignment).
- Use the above techniques to analyze similarities.
If your goal is to analyze images or faces for legal or forensic purposes:
- Document the process: Ensure reproducibility and legal admissibility.
- Consider privacy concerns: If AI tools are used, verify compliance with local data protection laws.
Would you like a detailed explanation of specific steps or techniques?