-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
40 lines (32 loc) · 1.33 KB
/
Copy pathutils.py
File metadata and controls
40 lines (32 loc) · 1.33 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
from difflib import SequenceMatcher
def find_longest_match_in_name(names: list) -> str:
"""
https://stackoverflow.com/questions/58585052/find-most-common-substring-in-a-list-of-strings
https://www.adamsmith.haus/python/docs/difflib.SequenceMatcher.get_matching_blocks
Parameters
----------
names : list
A list of names, each one a string.
Returns
-------
max_occurring_substring : str
The piece of string that accurs most commonly in the received list of names.
"""
substring_counts = {}
names_list = list(names)
for i in range(0, len(names)):
for j in range(i+1, len(names)):
string1 = str(names_list[i])
string2 = str(names_list[j])
match = SequenceMatcher(None, string1, string2).find_longest_match(
0, len(string1), 0, len(string2))
matching_substring = string1[match.a:match.a+match.size]
if (matching_substring not in substring_counts):
substring_counts[matching_substring] = 1
else:
substring_counts[matching_substring] += 1
# max() looks at the output of get method
max_occurring_key = max(substring_counts, key=substring_counts.get)
for char in " - - ":
max_occurring_key = max_occurring_key.strip(char)
return max_occurring_key