2009-08-05 08:59:46 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/01/29
|
2015-01-03 21:30:57 +00:00
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2014-10-05 20:31:16 +00:00
|
|
|
#
|
2015-01-03 21:33:16 +00:00
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
2014-10-05 20:31:16 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2009-08-05 08:59:46 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
import difflib
|
2009-10-18 08:46:00 +00:00
|
|
|
import itertools
|
2009-06-01 09:55:11 +00:00
|
|
|
import logging
|
|
|
|
import string
|
|
|
|
from collections import defaultdict, namedtuple
|
|
|
|
from unicodedata import normalize
|
|
|
|
|
2011-01-11 12:36:05 +00:00
|
|
|
from hscommon.util import flatten, multi_replace
|
2011-01-18 16:33:33 +00:00
|
|
|
from hscommon.trans import tr
|
2014-10-05 20:31:16 +00:00
|
|
|
from hscommon.jobprogress import job
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
(WEIGHT_WORDS, MATCH_SIMILAR_WORDS, NO_FIELD_ORDER,) = range(3)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
JOB_REFRESH_RATE = 100
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def getwords(s):
|
2011-01-22 15:12:18 +00:00
|
|
|
# We decompose the string so that ascii letters with accents can be part of the word.
|
2020-01-01 02:16:27 +00:00
|
|
|
s = normalize("NFD", s)
|
|
|
|
s = multi_replace(s, "-_&+():;\\[]{}.,<>/?~!@#$*", " ").lower()
|
2021-04-29 03:08:43 +00:00
|
|
|
# logging.debug(f"DEBUG chars for: {s}\n"
|
|
|
|
# f"{[c for c in s if ord(c) != 32]}\n"
|
|
|
|
# f"{[ord(c) for c in s if ord(c) != 32]}")
|
|
|
|
# HACK We shouldn't ignore non-ascii characters altogether. Any Unicode char
|
|
|
|
# above common european characters that cannot be "sanitized" (ie. stripped
|
|
|
|
# of their accents, etc.) are preserved as is. The arbitrary limit is
|
|
|
|
# obtained from this one: ord("\u037e") GREEK QUESTION MARK
|
2020-01-01 02:16:27 +00:00
|
|
|
s = "".join(
|
2021-04-29 03:08:43 +00:00
|
|
|
c for c in s
|
2021-04-29 03:29:35 +00:00
|
|
|
if (ord(c) <= 894
|
2021-04-29 03:08:43 +00:00
|
|
|
and c in string.ascii_letters + string.digits + string.whitespace
|
|
|
|
)
|
|
|
|
or ord(c) > 894
|
2020-01-01 02:16:27 +00:00
|
|
|
)
|
|
|
|
return [_f for _f in s.split(" ") if _f] # remove empty elements
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def getfields(s):
|
2020-01-01 02:16:27 +00:00
|
|
|
fields = [getwords(field) for field in s.split(" - ")]
|
2010-08-11 14:39:06 +00:00
|
|
|
return [_f for _f in fields if _f]
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def unpack_fields(fields):
|
|
|
|
result = []
|
|
|
|
for field in fields:
|
|
|
|
if isinstance(field, list):
|
|
|
|
result += field
|
|
|
|
else:
|
|
|
|
result.append(field)
|
|
|
|
return result
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def compare(first, second, flags=()):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Returns the % of words that match between ``first`` and ``second``
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
The result is a ``int`` in the range 0..100.
|
|
|
|
``first`` and ``second`` can be either a string or a list (of words).
|
2009-06-01 09:55:11 +00:00
|
|
|
"""
|
|
|
|
if not (first and second):
|
|
|
|
return 0
|
|
|
|
if any(isinstance(element, list) for element in first):
|
|
|
|
return compare_fields(first, second, flags)
|
2020-01-01 02:16:27 +00:00
|
|
|
second = second[:] # We must use a copy of second because we remove items from it
|
2009-06-01 09:55:11 +00:00
|
|
|
match_similar = MATCH_SIMILAR_WORDS in flags
|
|
|
|
weight_words = WEIGHT_WORDS in flags
|
|
|
|
joined = first + second
|
2020-01-01 02:16:27 +00:00
|
|
|
total_count = sum(len(word) for word in joined) if weight_words else len(joined)
|
2009-06-01 09:55:11 +00:00
|
|
|
match_count = 0
|
|
|
|
in_order = True
|
|
|
|
for word in first:
|
|
|
|
if match_similar and (word not in second):
|
|
|
|
similar = difflib.get_close_matches(word, second, 1, 0.8)
|
|
|
|
if similar:
|
|
|
|
word = similar[0]
|
|
|
|
if word in second:
|
|
|
|
if second[0] != word:
|
|
|
|
in_order = False
|
|
|
|
second.remove(word)
|
2020-01-01 02:16:27 +00:00
|
|
|
match_count += len(word) if weight_words else 1
|
2009-06-01 09:55:11 +00:00
|
|
|
result = round(((match_count * 2) / total_count) * 100)
|
|
|
|
if (result == 100) and (not in_order):
|
2020-01-01 02:16:27 +00:00
|
|
|
result = 99 # We cannot consider a match exact unless the ordering is the same
|
2009-06-01 09:55:11 +00:00
|
|
|
return result
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def compare_fields(first, second, flags=()):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Returns the score for the lowest matching :ref:`fields`.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
``first`` and ``second`` must be lists of lists of string. Each sub-list is then compared with
|
2014-10-05 20:31:16 +00:00
|
|
|
:func:`compare`.
|
2009-06-01 09:55:11 +00:00
|
|
|
"""
|
|
|
|
if len(first) != len(second):
|
|
|
|
return 0
|
|
|
|
if NO_FIELD_ORDER in flags:
|
|
|
|
results = []
|
2020-01-01 02:16:27 +00:00
|
|
|
# We don't want to remove field directly in the list. We must work on a copy.
|
2009-06-01 09:55:11 +00:00
|
|
|
second = second[:]
|
|
|
|
for field1 in first:
|
|
|
|
max = 0
|
|
|
|
matched_field = None
|
|
|
|
for field2 in second:
|
|
|
|
r = compare(field1, field2, flags)
|
|
|
|
if r > max:
|
|
|
|
max = r
|
|
|
|
matched_field = field2
|
|
|
|
results.append(max)
|
|
|
|
if matched_field:
|
|
|
|
second.remove(matched_field)
|
|
|
|
else:
|
2020-01-01 02:16:27 +00:00
|
|
|
results = [
|
|
|
|
compare(field1, field2, flags) for field1, field2 in zip(first, second)
|
|
|
|
]
|
2009-06-01 09:55:11 +00:00
|
|
|
return min(results) if results else 0
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def build_word_dict(objects, j=job.nulljob):
|
|
|
|
"""Returns a dict of objects mapped by their words.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
objects must have a ``words`` attribute being a list of strings or a list of lists of strings
|
|
|
|
(:ref:`fields`).
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
The result will be a dict with words as keys, lists of objects as values.
|
|
|
|
"""
|
|
|
|
result = defaultdict(set)
|
2020-01-01 02:16:27 +00:00
|
|
|
for object in j.iter_with_progress(
|
|
|
|
objects, "Prepared %d/%d files", JOB_REFRESH_RATE
|
|
|
|
):
|
2009-06-01 09:55:11 +00:00
|
|
|
for word in unpack_fields(object.words):
|
|
|
|
result[word].add(object)
|
|
|
|
return result
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def merge_similar_words(word_dict):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Take all keys in ``word_dict`` that are similar, and merge them together.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
``word_dict`` has been built with :func:`build_word_dict`. Similarity is computed with Python's
|
|
|
|
``difflib.get_close_matches()``, which computes the number of edits that are necessary to make
|
|
|
|
a word equal to the other.
|
2009-06-01 09:55:11 +00:00
|
|
|
"""
|
2010-08-11 14:39:06 +00:00
|
|
|
keys = list(word_dict.keys())
|
2020-01-01 02:16:27 +00:00
|
|
|
keys.sort(key=len) # we want the shortest word to stay
|
2009-06-01 09:55:11 +00:00
|
|
|
while keys:
|
|
|
|
key = keys.pop(0)
|
|
|
|
similars = difflib.get_close_matches(key, keys, 100, 0.8)
|
|
|
|
if not similars:
|
|
|
|
continue
|
|
|
|
objects = word_dict[key]
|
|
|
|
for similar in similars:
|
|
|
|
objects |= word_dict[similar]
|
|
|
|
del word_dict[similar]
|
|
|
|
keys.remove(similar)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def reduce_common_words(word_dict, threshold):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Remove all objects from ``word_dict`` values where the object count >= ``threshold``
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
``word_dict`` has been built with :func:`build_word_dict`.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
The exception to this removal are the objects where all the words of the object are common.
|
|
|
|
Because if we remove them, we will miss some duplicates!
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
uncommon_words = set(
|
|
|
|
word for word, objects in word_dict.items() if len(objects) < threshold
|
|
|
|
)
|
2010-08-11 14:39:06 +00:00
|
|
|
for word, objects in list(word_dict.items()):
|
2009-06-01 09:55:11 +00:00
|
|
|
if len(objects) < threshold:
|
|
|
|
continue
|
|
|
|
reduced = set()
|
|
|
|
for o in objects:
|
|
|
|
if not any(w in uncommon_words for w in unpack_fields(o.words)):
|
|
|
|
reduced.add(o)
|
|
|
|
if reduced:
|
|
|
|
word_dict[word] = reduced
|
|
|
|
else:
|
|
|
|
del word_dict[word]
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
# Writing docstrings in a namedtuple is tricky. From Python 3.3, it's possible to set __doc__, but
|
|
|
|
# some research allowed me to find a more elegant solution, which is what is done here. See
|
|
|
|
# http://stackoverflow.com/questions/1606436/adding-docstrings-to-namedtuples-in-python
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
class Match(namedtuple("Match", "first second percentage")):
|
2013-10-20 17:33:27 +00:00
|
|
|
"""Represents a match between two :class:`~core.fs.File`.
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
Regarless of the matching method, when two files are determined to match, a Match pair is created,
|
|
|
|
which holds, of course, the two matched files, but also their match "level".
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
.. attribute:: first
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
first file of the pair.
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
.. attribute:: second
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
second file of the pair.
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
.. attribute:: percentage
|
2013-08-21 02:52:43 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
their match level according to the scan method which found the match. int from 1 to 100. For
|
|
|
|
exact scan methods, such as Contents scans, this will always be 100.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2013-10-20 17:33:27 +00:00
|
|
|
__slots__ = ()
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def get_match(first, second, flags=()):
|
2020-01-01 02:16:27 +00:00
|
|
|
# it is assumed here that first and second both have a "words" attribute
|
2009-06-01 09:55:11 +00:00
|
|
|
percentage = compare(first.words, second.words, flags)
|
|
|
|
return Match(first, second, percentage)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
def getmatches(
|
2020-01-01 02:16:27 +00:00
|
|
|
objects,
|
|
|
|
min_match_percentage=0,
|
|
|
|
match_similar_words=False,
|
|
|
|
weight_words=False,
|
|
|
|
no_field_order=False,
|
|
|
|
j=job.nulljob,
|
|
|
|
):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Returns a list of :class:`Match` within ``objects`` after fuzzily matching their words.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
:param objects: List of :class:`~core.fs.File` to match.
|
|
|
|
:param int min_match_percentage: minimum % of words that have to match.
|
|
|
|
:param bool match_similar_words: make similar words (see :func:`merge_similar_words`) match.
|
|
|
|
:param bool weight_words: longer words are worth more in match % computations.
|
|
|
|
:param bool no_field_order: match :ref:`fields` regardless of their order.
|
|
|
|
:param j: A :ref:`job progress instance <jobs>`.
|
|
|
|
"""
|
2009-10-18 08:46:00 +00:00
|
|
|
COMMON_WORD_THRESHOLD = 50
|
|
|
|
LIMIT = 5000000
|
|
|
|
j = j.start_subjob(2)
|
|
|
|
sj = j.start_subjob(2)
|
|
|
|
for o in objects:
|
2020-01-01 02:16:27 +00:00
|
|
|
if not hasattr(o, "words"):
|
2009-10-18 08:46:00 +00:00
|
|
|
o.words = getwords(o.name)
|
|
|
|
word_dict = build_word_dict(objects, sj)
|
|
|
|
reduce_common_words(word_dict, COMMON_WORD_THRESHOLD)
|
|
|
|
if match_similar_words:
|
|
|
|
merge_similar_words(word_dict)
|
|
|
|
match_flags = []
|
|
|
|
if weight_words:
|
|
|
|
match_flags.append(WEIGHT_WORDS)
|
|
|
|
if match_similar_words:
|
|
|
|
match_flags.append(MATCH_SIMILAR_WORDS)
|
|
|
|
if no_field_order:
|
|
|
|
match_flags.append(NO_FIELD_ORDER)
|
2011-01-18 16:33:33 +00:00
|
|
|
j.start_job(len(word_dict), tr("0 matches found"))
|
2009-10-18 08:46:00 +00:00
|
|
|
compared = defaultdict(set)
|
|
|
|
result = []
|
|
|
|
try:
|
|
|
|
# This whole 'popping' thing is there to avoid taking too much memory at the same time.
|
|
|
|
while word_dict:
|
|
|
|
items = word_dict.popitem()[1]
|
|
|
|
while items:
|
|
|
|
ref = items.pop()
|
|
|
|
compared_already = compared[ref]
|
|
|
|
to_compare = items - compared_already
|
|
|
|
compared_already |= to_compare
|
|
|
|
for other in to_compare:
|
|
|
|
m = get_match(ref, other, match_flags)
|
|
|
|
if m.percentage >= min_match_percentage:
|
|
|
|
result.append(m)
|
|
|
|
if len(result) >= LIMIT:
|
|
|
|
return result
|
2011-01-18 16:33:33 +00:00
|
|
|
j.add_progress(desc=tr("%d matches found") % len(result))
|
2009-10-18 08:46:00 +00:00
|
|
|
except MemoryError:
|
|
|
|
# This is the place where the memory usage is at its peak during the scan.
|
|
|
|
# Just continue the process with an incomplete list of matches.
|
2020-01-01 02:16:27 +00:00
|
|
|
del compared # This should give us enough room to call logging.
|
|
|
|
logging.warning(
|
|
|
|
"Memory Overflow. Matches: %d. Word dict: %d"
|
|
|
|
% (len(result), len(word_dict))
|
|
|
|
)
|
2009-06-01 09:55:11 +00:00
|
|
|
return result
|
2009-10-18 08:46:00 +00:00
|
|
|
return result
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2016-06-08 16:06:08 +00:00
|
|
|
def getmatches_by_contents(files, j=job.nulljob):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Returns a list of :class:`Match` within ``files`` if their contents is the same.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
:param j: A :ref:`job progress instance <jobs>`.
|
|
|
|
"""
|
2009-10-18 08:46:00 +00:00
|
|
|
size2files = defaultdict(set)
|
2016-06-08 16:06:08 +00:00
|
|
|
for f in files:
|
|
|
|
if f.size:
|
|
|
|
size2files[f.size].add(f)
|
2014-10-13 19:08:59 +00:00
|
|
|
del files
|
2009-10-18 08:46:00 +00:00
|
|
|
possible_matches = [files for files in size2files.values() if len(files) > 1]
|
|
|
|
del size2files
|
|
|
|
result = []
|
2011-01-18 16:33:33 +00:00
|
|
|
j.start_job(len(possible_matches), tr("0 matches found"))
|
2009-10-18 08:46:00 +00:00
|
|
|
for group in possible_matches:
|
|
|
|
for first, second in itertools.combinations(group, 2):
|
2010-01-13 09:04:53 +00:00
|
|
|
if first.is_ref and second.is_ref:
|
2020-01-01 02:16:27 +00:00
|
|
|
continue # Don't spend time comparing two ref pics together.
|
2009-10-18 08:46:00 +00:00
|
|
|
if first.md5partial == second.md5partial:
|
2016-06-08 16:06:08 +00:00
|
|
|
if first.md5 == second.md5:
|
2009-10-18 08:46:00 +00:00
|
|
|
result.append(Match(first, second, 100))
|
2011-01-18 16:33:33 +00:00
|
|
|
j.add_progress(desc=tr("%d matches found") % len(result))
|
2009-10-18 08:46:00 +00:00
|
|
|
return result
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2011-09-07 19:46:41 +00:00
|
|
|
class Group:
|
2013-08-18 22:36:09 +00:00
|
|
|
"""A group of :class:`~core.fs.File` that match together.
|
|
|
|
|
|
|
|
This manages match pairs into groups and ensures that all files in the group match to each
|
|
|
|
other.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
.. attribute:: ref
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
The "reference" file, which is the file among the group that isn't going to be deleted.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
.. attribute:: ordered
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
Ordered list of duplicates in the group (including the :attr:`ref`).
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
.. attribute:: unordered
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
Set duplicates in the group (including the :attr:`ref`).
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
.. attribute:: dupes
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
An ordered list of the group's duplicate, without :attr:`ref`. Equivalent to
|
|
|
|
``ordered[1:]``
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
.. attribute:: percentage
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
Average match percentage of match pairs containing :attr:`ref`.
|
2013-08-18 22:36:09 +00:00
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
|
|
|
# ---Override
|
2009-06-01 09:55:11 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._clear()
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.unordered
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.ordered.__getitem__(key)
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.ordered)
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.ordered)
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
# ---Private
|
2009-06-01 09:55:11 +00:00
|
|
|
def _clear(self):
|
|
|
|
self._percentage = None
|
|
|
|
self._matches_for_ref = None
|
|
|
|
self.matches = set()
|
|
|
|
self.candidates = defaultdict(set)
|
|
|
|
self.ordered = []
|
|
|
|
self.unordered = set()
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def _get_matches_for_ref(self):
|
|
|
|
if self._matches_for_ref is None:
|
|
|
|
ref = self.ref
|
|
|
|
self._matches_for_ref = [match for match in self.matches if ref in match]
|
|
|
|
return self._matches_for_ref
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
# ---Public
|
2009-06-01 09:55:11 +00:00
|
|
|
def add_match(self, match):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Adds ``match`` to internal match list and possibly add duplicates to the group.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
A duplicate can only be considered as such if it matches all other duplicates in the group.
|
|
|
|
This method registers that pair (A, B) represented in ``match`` as possible candidates and,
|
|
|
|
if A and/or B end up matching every other duplicates in the group, add these duplicates to
|
|
|
|
the group.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
:param tuple match: pair of :class:`~core.fs.File` to add
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def add_candidate(item, match):
|
|
|
|
matches = self.candidates[item]
|
|
|
|
matches.add(match)
|
|
|
|
if self.unordered <= matches:
|
|
|
|
self.ordered.append(item)
|
|
|
|
self.unordered.add(item)
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
if match in self.matches:
|
|
|
|
return
|
|
|
|
self.matches.add(match)
|
|
|
|
first, second, _ = match
|
|
|
|
if first not in self.unordered:
|
|
|
|
add_candidate(first, second)
|
|
|
|
if second not in self.unordered:
|
|
|
|
add_candidate(second, first)
|
|
|
|
self._percentage = None
|
|
|
|
self._matches_for_ref = None
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-09-05 14:58:35 +00:00
|
|
|
def discard_matches(self):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Remove all recorded matches that didn't result in a duplicate being added to the group.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
You can call this after the duplicate scanning process to free a bit of memory.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
discarded = set(
|
|
|
|
m
|
|
|
|
for m in self.matches
|
|
|
|
if not all(obj in self.unordered for obj in [m.first, m.second])
|
|
|
|
)
|
2009-09-05 14:58:35 +00:00
|
|
|
self.matches -= discarded
|
2009-06-01 09:55:11 +00:00
|
|
|
self.candidates = defaultdict(set)
|
2009-09-05 14:58:35 +00:00
|
|
|
return discarded
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def get_match_of(self, item):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Returns the match pair between ``item`` and :attr:`ref`.
|
|
|
|
"""
|
2009-06-01 09:55:11 +00:00
|
|
|
if item is self.ref:
|
|
|
|
return
|
|
|
|
for m in self._get_matches_for_ref():
|
|
|
|
if item in m:
|
|
|
|
return m
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def prioritize(self, key_func, tie_breaker=None):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Reorders :attr:`ordered` according to ``key_func``.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
:param key_func: Key (f(x)) to be used for sorting
|
|
|
|
:param tie_breaker: function to be used to select the reference position in case the top
|
|
|
|
duplicates have the same key_func() result.
|
|
|
|
"""
|
2009-06-01 09:55:11 +00:00
|
|
|
# tie_breaker(ref, dupe) --> True if dupe should be ref
|
2012-07-31 15:37:51 +00:00
|
|
|
# Returns True if anything changed during prioritization.
|
2011-09-23 17:14:57 +00:00
|
|
|
master_key_func = lambda x: (-x.is_ref, key_func(x))
|
2012-07-31 15:37:51 +00:00
|
|
|
new_order = sorted(self.ordered, key=master_key_func)
|
|
|
|
changed = new_order != self.ordered
|
|
|
|
self.ordered = new_order
|
2009-06-01 09:55:11 +00:00
|
|
|
if tie_breaker is None:
|
2012-07-31 15:37:51 +00:00
|
|
|
return changed
|
2009-06-01 09:55:11 +00:00
|
|
|
ref = self.ref
|
|
|
|
key_value = key_func(ref)
|
|
|
|
for dupe in self.dupes:
|
|
|
|
if key_func(dupe) != key_value:
|
|
|
|
break
|
|
|
|
if tie_breaker(ref, dupe):
|
|
|
|
ref = dupe
|
|
|
|
if ref is not self.ref:
|
|
|
|
self.switch_ref(ref)
|
2012-07-31 15:37:51 +00:00
|
|
|
return True
|
|
|
|
return changed
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-09-05 14:58:35 +00:00
|
|
|
def remove_dupe(self, item, discard_matches=True):
|
2009-06-01 09:55:11 +00:00
|
|
|
try:
|
|
|
|
self.ordered.remove(item)
|
|
|
|
self.unordered.remove(item)
|
|
|
|
self._percentage = None
|
|
|
|
self._matches_for_ref = None
|
2020-01-01 02:16:27 +00:00
|
|
|
if (len(self) > 1) and any(
|
|
|
|
not getattr(item, "is_ref", False) for item in self
|
|
|
|
):
|
2009-09-05 14:58:35 +00:00
|
|
|
if discard_matches:
|
2009-06-01 09:55:11 +00:00
|
|
|
self.matches = set(m for m in self.matches if item not in m)
|
|
|
|
else:
|
|
|
|
self._clear()
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def switch_ref(self, with_dupe):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Make the :attr:`ref` dupe of the group switch position with ``with_dupe``.
|
|
|
|
"""
|
2011-09-23 17:14:57 +00:00
|
|
|
if self.ref.is_ref:
|
2013-04-28 18:12:08 +00:00
|
|
|
return False
|
2009-06-01 09:55:11 +00:00
|
|
|
try:
|
|
|
|
self.ordered.remove(with_dupe)
|
|
|
|
self.ordered.insert(0, with_dupe)
|
|
|
|
self._percentage = None
|
|
|
|
self._matches_for_ref = None
|
2013-04-28 18:12:08 +00:00
|
|
|
return True
|
2009-06-01 09:55:11 +00:00
|
|
|
except ValueError:
|
2013-04-28 18:12:08 +00:00
|
|
|
return False
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
dupes = property(lambda self: self[1:])
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
@property
|
|
|
|
def percentage(self):
|
|
|
|
if self._percentage is None:
|
|
|
|
if self.dupes:
|
|
|
|
matches = self._get_matches_for_ref()
|
2020-01-01 02:16:27 +00:00
|
|
|
self._percentage = sum(match.percentage for match in matches) // len(
|
|
|
|
matches
|
|
|
|
)
|
2009-06-01 09:55:11 +00:00
|
|
|
else:
|
|
|
|
self._percentage = 0
|
|
|
|
return self._percentage
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
@property
|
|
|
|
def ref(self):
|
|
|
|
if self:
|
|
|
|
return self[0]
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2016-06-08 16:24:35 +00:00
|
|
|
def get_groups(matches):
|
2013-08-21 02:52:43 +00:00
|
|
|
"""Returns a list of :class:`Group` from ``matches``.
|
2014-10-05 20:31:16 +00:00
|
|
|
|
2013-08-21 02:52:43 +00:00
|
|
|
Create groups out of match pairs in the smartest way possible.
|
|
|
|
"""
|
2009-06-01 09:55:11 +00:00
|
|
|
matches.sort(key=lambda match: -match.percentage)
|
|
|
|
dupe2group = {}
|
|
|
|
groups = []
|
2009-10-14 14:42:18 +00:00
|
|
|
try:
|
2016-06-08 16:24:35 +00:00
|
|
|
for match in matches:
|
2009-10-14 14:42:18 +00:00
|
|
|
first, second, _ = match
|
|
|
|
first_group = dupe2group.get(first)
|
|
|
|
second_group = dupe2group.get(second)
|
|
|
|
if first_group:
|
|
|
|
if second_group:
|
|
|
|
if first_group is second_group:
|
|
|
|
target_group = first_group
|
|
|
|
else:
|
|
|
|
continue
|
2009-06-01 09:55:11 +00:00
|
|
|
else:
|
2009-10-14 14:42:18 +00:00
|
|
|
target_group = first_group
|
|
|
|
dupe2group[second] = target_group
|
2009-06-01 09:55:11 +00:00
|
|
|
else:
|
2009-10-14 14:42:18 +00:00
|
|
|
if second_group:
|
|
|
|
target_group = second_group
|
|
|
|
dupe2group[first] = target_group
|
|
|
|
else:
|
|
|
|
target_group = Group()
|
|
|
|
groups.append(target_group)
|
|
|
|
dupe2group[first] = target_group
|
|
|
|
dupe2group[second] = target_group
|
|
|
|
target_group.add_match(match)
|
|
|
|
except MemoryError:
|
|
|
|
del dupe2group
|
|
|
|
del matches
|
|
|
|
# should free enough memory to continue
|
2020-01-01 02:16:27 +00:00
|
|
|
logging.warning("Memory Overflow. Groups: {0}".format(len(groups)))
|
2009-09-05 14:58:35 +00:00
|
|
|
# Now that we have a group, we have to discard groups' matches and see if there're any "orphan"
|
|
|
|
# matches, that is, matches that were candidate in a group but that none of their 2 files were
|
|
|
|
# accepted in the group. With these orphan groups, it's safe to build additional groups
|
|
|
|
matched_files = set(flatten(groups))
|
|
|
|
orphan_matches = []
|
2009-06-01 09:55:11 +00:00
|
|
|
for group in groups:
|
2014-10-13 19:08:59 +00:00
|
|
|
orphan_matches += {
|
2020-01-01 02:16:27 +00:00
|
|
|
m
|
|
|
|
for m in group.discard_matches()
|
2014-10-13 19:08:59 +00:00
|
|
|
if not any(obj in matched_files for obj in [m.first, m.second])
|
|
|
|
}
|
2009-09-05 14:58:35 +00:00
|
|
|
if groups and orphan_matches:
|
2020-01-01 02:16:27 +00:00
|
|
|
groups += get_groups(
|
|
|
|
orphan_matches
|
|
|
|
) # no job, as it isn't supposed to take a long time
|
2009-06-01 09:55:11 +00:00
|
|
|
return groups
|