-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifference.py
More file actions
59 lines (39 loc) · 1.17 KB
/
difference.py
File metadata and controls
59 lines (39 loc) · 1.17 KB
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
# -*- coding: utf-8 -*-
"""
Name: difference.py
Description: Calculates percentage difference between 2 strings
Author: Bryan Tutt
Created: 26Nov2018
"""
# from difflib import SequenceMatcher
import difflib
import string
# gets percentage difference between 2 strings
def similar(a, b):
return str(difflib.SequenceMatcher(None, a, b).ratio()*100) + '%'
# def thediff(a, b):
# return difflib.unified_diff(a, b)
# removes punctuation
def remove_punctuation(sentence):
nopunct = sentence.translate(None, string.punctuation)
return nopunct.upper()
# sorts sentence
def sorted_sentence(sentence):
words = sentence.split(' ')
words.sort()
newSentence = ' '.join(words)
print('Sorted: ', newSentence.strip())
return newSentence.strip()
def run(field_a, field_b):
cleaned_a = remove_punctuation(field_a)
cleaned_b = remove_punctuation(field_b)
sorted_A = sorted_sentence(cleaned_a)
sorted_B = sorted_sentence(cleaned_b)
percentdifference = similar(sorted_A, sorted_B)
return percentdifference
a = 'Ziggy Stardust David Bowie'
b = 'Bowie, David Stardust yggiz '
# a = "bob obrien"
# b = "bob o'brien"
bob = run(a, b)
print(bob)