如何分割字符串中的特定单词?

时间:2019-11-22 18:58:16

标签: python python-3.x list split

我有一个大学项目。我想将单词拆分为503并将其转换为503。 我从文本文件中提取了字符串,但是我不怎么分割它。

我要转换为测试的句子

there is five hundred three people

我想这样分裂

there, is, five hundred three, people

并接受列表以使用字典将其转换为

there is 503 people

我搜索了很多网站,但找不到任何相关信息。我尝试了 .split(),但它会分割每个单词,因此我无法将其用于项目。

3 个答案:

答案 0 :(得分:0)

它是python,因此有一个用于此的库:https://github.com/careless25/text2digits

但是,如果您不喜欢使用库,则此方法(来自库)可以完全满足您的要求:

def text2int (textnum, numwords={}):
    if not numwords:
        units = [
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
        "sixteen", "seventeen", "eighteen", "nineteen",
        ]

        tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

        scales = ["hundred", "thousand", "million", "billion", "trillion"]

        numwords["and"] = (1, 0)
        for idx, word in enumerate(units):  numwords[word] = (1, idx)
        for idx, word in enumerate(tens):       numwords[word] = (1, idx * 10)
        for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)

    ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
    ordinal_endings = [('ieth', 'y'), ('th', '')]

    textnum = textnum.replace('-', ' ')

    current = result = 0
    curstring = ""
    onnumber = False
    for word in textnum.split():
        if word in ordinal_words:
            scale, increment = (1, ordinal_words[word])
            current = current * scale + increment
            if scale > 100:
                result += current
                current = 0
            onnumber = True
        else:
            for ending, replacement in ordinal_endings:
                if word.endswith(ending):
                    word = "%s%s" % (word[:-len(ending)], replacement)

            if word not in numwords:
                if onnumber:
                    curstring += repr(result + current) + " "
                curstring += word + " "
                result = current = 0
                onnumber = False
            else:
                scale, increment = numwords[word]

                current = current * scale + increment
                if scale > 100:
                    result += current
                    current = 0
                onnumber = True

    if onnumber:
        curstring += repr(result + current)

    return curstring

您可以像这样使用它:

>>> text2int("I want fifty five hot dogs for two hundred dollars.")
 I want 55 hot dogs for 200 dollars.

答案 1 :(得分:0)

您可以通过以下方式安装text2digits软件包:

pip install text2digits

然后使用以下程序包处理示例:

from text2digits import text2digits
t2d = text2digits.Text2Digits()
print t2d.convert("there is five hundred three people")

输出为:

>>> 
there is 503 people

答案 2 :(得分:-1)

您将不得不使用列出的数字列表,然后在字符串中搜索所有数字并将其替换。

就是这样

strings["one", "two", "three"...]      #list of numbers represented as strings
numbers[1, 2, 3...]                    #corrasponding numbers 

def replaceNumbers(string):            #function to replace numbers
    for x in range(len(strings)):      #loop through strings 
        #replace string with number
        string= string[:string.find(x)] + str(numbers[x]) + string[string.find(x) + len(x):] 
    return string

然后,您需要弄清楚如何处理成百上千的东西

相关问题