mirror of
				https://github.com/avatao-content/baseimage-tutorial-framework
				synced 2025-11-04 01:52:55 +00:00 
			
		
		
		
	Improve CommandsEqual with fuzzy logic
This commit is contained in:
		@@ -3,17 +3,23 @@
 | 
			
		||||
 | 
			
		||||
from shlex import split
 | 
			
		||||
 | 
			
		||||
from tfw.decorators.lazy_property import lazy_property
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CommandsEqual:
 | 
			
		||||
    def __init__(self, command_1, command_2):
 | 
			
		||||
        self.command_1 = command_1
 | 
			
		||||
        self.command_2 = command_2
 | 
			
		||||
    def __init__(self, command_1, command_2, fuzzyness=1):
 | 
			
		||||
        self.command_1 = split(command_1)
 | 
			
		||||
        self.command_2 = split(command_2)
 | 
			
		||||
        self.fuzzyness = fuzzyness
 | 
			
		||||
 | 
			
		||||
    def __bool__(self):
 | 
			
		||||
        parts_1 = split(self.command_1)
 | 
			
		||||
        parts_2 = split(self.command_2)
 | 
			
		||||
        return (
 | 
			
		||||
            parts_1[0] == parts_2[0]
 | 
			
		||||
            and set(parts_1) == set(parts_2)
 | 
			
		||||
        )
 | 
			
		||||
        return self.similarity >= self.fuzzyness
 | 
			
		||||
 | 
			
		||||
    @lazy_property
 | 
			
		||||
    def similarity(self):
 | 
			
		||||
        parts_1 = set(self.command_1)
 | 
			
		||||
        parts_2 = set(self.command_2)
 | 
			
		||||
 | 
			
		||||
        difference = parts_1 - parts_2
 | 
			
		||||
        deviance = len(difference) / len(max(parts_1, parts_2))
 | 
			
		||||
        return 1 - deviance
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user