0

I'm fairly new to coding, and I've looked around and haven't found an answer to this (maybe I'm wording it wrong?). I'm working on a code that finds numbers in comments, converts them to floats, and adds them to the end of a list (float_numbers).

Currently, if someone says, "2 Thousand," then it'll place it at the end of the float_numbers list as [2, 1000], because it reads that as being two separate numbers.

I'm currently using the module "word2number" to convert the number words to number numbers.

I'll also need to convert numbers formatted as "3k" "4b" etc...

Here's the process in my code that sorts numbers out of comments.

def extract_numbers_from_text(text):
    # Use regular expressions to search for numeric patterns
    numbers = re.findall(r'\b\d+(?:[.,]\d+)?\b|\b[a-z]+\b', text)
    float_numbers = []
    
    # Filter out some numbers
    
    for number in numbers:
        if number in ["infinite", "infinity"]:
            continue
        if 'http' in comment.body:
            return
        try:
            #if number is already a float, add it to the list
            float_numbers.append(float(number))
        except ValueError:
            try:
                #if number is string, convert it to a float and add it to the list
                float_numbers.append(w2n.word_to_num(number))
            except ValueError:
                #if word cannot be converted to number, ignore it
                pass
    return float_numbers
macropod
  • 12,757
  • 2
  • 9
  • 21
  • Looks like the answer by totalhack might fit, but it's also quite bulky. I wonder if it can be trimmed down using the word2number module. Edit, it's almost 3:30am here, so this might need to wait till tomorrow hahaha – ShiP wReCk Jan 21 '23 at 08:21
  • @ShiPwReCk Is [Billion](https://en.wikipedia.org/wiki/Billion) 1,000,000,000, or 1,000,000,000,000? There is no universal value in English although 1,000,000,000 is common. – chux - Reinstate Monica Jan 23 '23 at 06:32
  • --> What about `"5m"`? Is that 5 million, 5 Mega- or 5 milli-? Is 1k going to be 1,000 or 1,024? `"2t"`, is that 2,000 or 2 trillion or 2 tera? – chux - Reinstate Monica Jan 23 '23 at 06:37
  • I'm actually really surprised, this is the first I've heard that trillion can be billion. Good points, being that I'm sorting through reddit comments, I don't think many people are using "m" as "mega" or "milli" but it probably more regularly means "meters," which is a bigger issue. But this does seem to be a bigger task than I realized. Without natural language processing, I don't think I could easily figure it out. – ShiP wReCk Jan 30 '23 at 02:04

0 Answers0