查找句子中的第一个数字单词

时间:2019-05-23 16:43:03

标签: python

假设我有一个段落,例如

I have two years left for my school to end. 
I have four apples at home. My school sucks but it is the second best school in town.

I have three years left for my school to end. 
I have four apples at home. My school sucks but it is the second best school in town.

我想执行以下操作:

1)将段落分解为一个句子
2)找出其中包含“学校”一词的句子,然后提取该句子
3)对于所有提取的句子,找出其中有一个数字(例如,一,二,三等)
4)找出第一个数字并提取该数字

目前,我有以下代码,但是我敢肯定,有一种更好的更优雅的方式解决此问题

txt = 'I have two years left for my school to end. 
I have four apples at home. My school sucks but it is the second best school in town. I have three years left for my school to end. 
I have four apples at home. My school sucks but it is the second best school in town.'
words = 'school'.split(' ')
sentences = re.findall(r"([^.]*\.)" ,txt)  
for sentence in sentences:
    if any(word in sentence for word in words):
        print(sentence.split('\n'))

上面代码的输出

['I have two years left for my school to end.']
[' My school sucks but it is the second best school in town.']
[' I have three years left for my school to end.']
[' My school sucks but it is the second best school in town.']

所需的输出为

two
three

1 个答案:

答案 0 :(得分:0)

虽然您的尝试不准确,但我认为您至少已尝试过,因此以下是一种解决方案:  -创建您要过滤的单词列表,在下面的数字[]并进行核对

txt = "I have two years left for my school to end.I have four apples at home. My school sucks but it is the second best school in town. I have three years left for my school to end./I have four apples at home. My school sucks but it is the second best school in town."
numbers = ["one","two","three", "four","five","six","seven","eight","nine"]
sentences = txt.split('.')
words = []

for i in sentences:
    words = i.split()

    # below checks whether the words in the sentence are in the list, numbers[]
    num = [k for k in words if k in numbers]

    if "school" in words and any(num):
        print(num[0])

输出:

two
three