Reputation: 53
I'm trying to check if key exists in given sentence or not.
But in the sentence, the key can be jumbled, or there can be special characters in between, or the key can have some words between them. Right now I'm not worried about case sensitive.
For example, I want to check if:
I started with comparing character-by-character from key to statement, but I was not able to solve the problem.
Based on comments, I tired to come up with code.
def getMatch(text,key):
matchedwords=list()
i=0
for j in range(0, len(text)):
for i in range(0,len(key)):
match = ""
keyindex=i
textindex=j
while(keyindex<len(key) and textindex<len(text) and key[keyindex]==text[textindex] ):
match+=text[textindex]
keyindex+=1
textindex+=1
if(len(match)>0):
if(match not in matchedwords):
matchedwords.append(match)
print(matchedwords)
text="MOTOR Device 101 is high"
key="MOTOR101"
getMatch(text,key)
I was able to get the output as"['MOTOR', 'OTOR', 'O', 'TOR', 'OR', 'R', '101', '1', '01']". Please let me know if any changes is required or improvement can be done. From here I'm here trying to check if any combination of words leads to "MOTOR101".
Upvotes: 0
Views: 447
Reputation: 2244
You are searching for two separate items in string. If you split them into a list then the following code will find them.
lk = [ 'Motor','101' ]
s1 = [ 'Device Motor_101 is very high',
'101Motor Device is very high',
'Motor device 101 is very is high' ]
for i, a in enumerate( s1 ):
result = -1
for b in a.split():
if lk[0] in b:
result = i
if lk[1] in b:
if result > -1:
print( f'found in {a}' )
This code will search and find more than just two items
lk = [ 'Motor','101' ]
ln = len( lk )
s1 = [ 'Device Motor_101 is very high',
'101Motor Device is very high',
'Motor device 101 is very is high' ]
found = []
for i, a in enumerate( s1 ):
for b in a.split():
result = -1
for gp in range( ln ):
if lk[ gp ] in b:
result += 1
if result > -1 and found.count( a ) == 0:
found.append( a )
print( f'found in {a}' )
print( f'{found}' )
Ok I've found that duplications of words cause false positives so... Here is my third attempt
lk = [ 'sky', '101', 'Motor', 'very' ]
ln = len( lk )
s1 = [ 'Device Motor_101 is very high in sky',
'101Motor Device is sky sky very',
'Motor device is very is high very very' ]
found = []
for i, a in enumerate( s1 ):
result = 0
sa = a.split( )
for b in a.split():
for gp in range( ln ):
if lk[ gp ] in b:
if b in sa:
sa.remove( b )
result += 1
if result == ln-1:
found.append( a )
print( f'{found}' )
Upvotes: 1
Reputation: 4789
You need to search for Motor
and 101
individually and then return the status if both are present or not.
def get_match(s):
lookup = ['Motor', '101']
count = 0
for i in lookup:
if i in s:
count += 1
if count == len(lookup):
return 'Strings Present'
return 'Not Present'
print(get_match('Device Motor_101 is very high'))
print(get_match('Stackoverflow 101'))
print(get_match('101Motor Device is very high'))
print(get_match('Motor device 101 is very is high'))
print(get_match('Motorbikes are good'))
Output:
Strings Present
Not Present
Strings Present
Strings Present
Not Present
Upvotes: 0