发现关键短语的子串是否存在于句子中

2024-09-29 00:15:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在检查给定的句子中是否存在密钥

但是在句子中,键可以是混乱的,或者在它们之间可以有特殊字符,或者键之间可以有一些单词。 现在我不担心区分大小写

例如,我想检查一下:

  • “电机101”键存在于语句“设备电机_101非常高”中,或
  • “Motor101”键存在于语句“101电机设备非常高”中或
  • “Motor101”键存在于“Motor device 101非常高”语句中

我从一个字符一个字符地比较键和语句开始,但我没能解决这个问题

基于这些评论,我很想写代码

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)

我能够得到输出为“['MOTOR'、'otr'、'O'、'TOR'、'OR'、'R'、'101'、'1'、'01']”。 请让我知道,如果需要任何更改或改进可以做。 从这里,我在这里试图检查是否有任何单词组合导致“MOTOR101”


Tags: keytextinlenmatch语句字符单词
2条回答

您正在字符串中搜索两个单独的项。如果将它们拆分为一个列表,那么下面的代码将找到它们


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}' )

此代码将搜索并查找两个以上的项目


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}' )

好的,我发现单词的重复会导致误报,所以。。。 这是我的第三次尝试


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}' )

您需要分别搜索Motor101,然后返回状态(如果两者都存在或不存在)

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

相关问题 更多 >