2018-08-03 11:40:34 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
|
|
|
from shlex import split
|
|
|
|
|
2018-08-03 13:01:44 +00:00
|
|
|
from tfw.decorators.lazy_property import lazy_property
|
|
|
|
|
2018-08-03 11:40:34 +00:00
|
|
|
|
|
|
|
class CommandsEqual:
|
2018-08-03 13:13:02 +00:00
|
|
|
def __init__(self, command_1, command_2, fuzzyness=1, must_begin_similarly=True):
|
2018-08-03 13:01:44 +00:00
|
|
|
self.command_1 = split(command_1)
|
|
|
|
self.command_2 = split(command_2)
|
|
|
|
self.fuzzyness = fuzzyness
|
2018-08-03 13:13:02 +00:00
|
|
|
self.must_begin_similarly = must_begin_similarly
|
2018-08-03 11:40:34 +00:00
|
|
|
|
|
|
|
def __bool__(self):
|
2018-08-03 13:13:02 +00:00
|
|
|
if self.must_begin_similarly and not self.beginnings_are_equal:
|
|
|
|
return False
|
|
|
|
|
2018-08-03 13:01:44 +00:00
|
|
|
return self.similarity >= self.fuzzyness
|
|
|
|
|
2018-08-03 13:13:02 +00:00
|
|
|
@lazy_property
|
|
|
|
def beginnings_are_equal(self):
|
|
|
|
return self.command_1[0] == self.command_2[0]
|
|
|
|
|
2018-08-03 13:01:44 +00:00
|
|
|
@lazy_property
|
|
|
|
def similarity(self):
|
|
|
|
parts_1 = set(self.command_1)
|
|
|
|
parts_2 = set(self.command_2)
|
2018-08-03 11:40:34 +00:00
|
|
|
|
2018-08-03 13:01:44 +00:00
|
|
|
difference = parts_1 - parts_2
|
|
|
|
deviance = len(difference) / len(max(parts_1, parts_2))
|
|
|
|
return 1 - deviance
|