Check if a string contains a number

后端 未结 16 1595
無奈伤痛
無奈伤痛 2020-11-22 11:14

Most of the questions I\'ve found are biased on the fact they\'re looking for letters in their numbers, whereas I\'m looking for numbers in what I\'d like to be a numberless

16条回答
  •  再見小時候
    2020-11-22 11:43

    You can use NLTK method for it.

    This will find both '1' and 'One' in the text:

    import nltk 
    
    def existence_of_numeric_data(text):
        text=nltk.word_tokenize(text)
        pos = nltk.pos_tag(text)
        count = 0
        for i in range(len(pos)):
            word , pos_tag = pos[i]
            if pos_tag == 'CD':
                return True
        return False
    
    existence_of_numeric_data('We are going out. Just five you and me.')
    

提交回复
热议问题