I have a problem. I am trying to find device names in a string. All the device names that I am looking for are stored in a List. There is one thing very important in what I want:
- A command can have multiple devices!!!
Now the problem I have is this:
I have two devices (Fan
and Fan Light
). When I give the command: Turn on Fan Light
both devices have been found, but I want only Fan Light
to be found. I tried checking all the devices that have been found and set the longest one as found device like this:
# Create 2 dummy devices
device1 = {
"name": "fan"
}
device2 = {
"name": "fan light"
}
# Add devices to list
devices = []
devices.append(device1)
devices.append(device2)
# Given command
command = "Turn on fan light"
foundDevices = []
# Search devices in sentence
for device in devices:
# Splits a device name if it has multiple words
deviceSplit = device["name"].split()
numOfSubNames = len(deviceSplit)
# Checks for every sub-name if it is found in the string
i = 0
for subName in deviceSplit:
if subName in command:
i += 1
# Checks if all names where located in string
if i == numOfSubNames:
foundDevices.append(device["name"])
# Checks if multiple devices have been found
if len(foundDevices) >= 2:
largestNameLength = 0
# Checks which device has the largest name
for device in foundDevices:
if (len(device) > largestNameLength):
largestName = device
largestNameLength = len(device)
# Clears list and only add longest one
foundDevices.clear()
foundDevices.append(largestName)
print(foundDevices)
But that gives a problem when I say for example: "Turn on Fan Light and the Fan", because that command does contain multiple devices. How can I scan for devices the way I want?