【问题标题】:Find capitalized words in a text在文本中查找大写单词
【发布时间】:2021-12-25 20:58:15
【问题描述】:

如何指定以大写字母开头的单词以及该单词在文本中的编号?如果在文本中找不到具有此属性的单词,则将其打印在 None 输出中。不应考虑句子开头的单词。不应考虑数字,如果分号位于单词的末尾,则应省略该分号。

像下面的例子:

输入:

The University of Edinburgh is a public research university in Edinburgh, Scotland. The University of Texas was included in the Association of American Universities in 1929.

输出:

2:University
4:Edinburgh
11:Edinburgh
12:Scotland
14:University
16:Texas
21:Association
23:American
24:Universities

【问题讨论】:

    标签: python word capitalize


    【解决方案1】:

    句首不考虑

    这使过程变得更加困难,因为您首先应该确定句子是如何分开的。一个句子可以以. or ! or ? 之类的标点符号结尾。但是您没有用句号结束​​示例中的最后一句话。为此,您的语料库必须首先进行预处理!


    抛开这个问题,假设这种情况:

    import re
    
    inp = "The University of Edinburgh is a public research university in Edinburgh, Scotland. The University of Texas was included in the Association of American Universities in 1929! The last Sentence."
    
    sentences = re.findall(r"[\w\s,]*[\.\!\?]",inp)
    counter = 0
    for sentence in sentences:
        sentence = re.sub(r"\W", " ",sentence)
        sentence = re.sub(r"\s+", " ", sentence)
        words = re.split(r"\s", sentence)
        words = [w for w in words if w!=""]
        for i, word in enumerate(words):
            if word != "" and i != 0:
                if re.search(r"[A-Z]+", word):
                    print("%d:%s" % (counter+i+1, word))
        counter += len(words)
    

    这段代码正是你想要的。这不是最佳实践,但它是一个紧凑而简单的代码。注意,首先需要为输入句指定每句末尾的标点符号!!!


    输出:

    2:University                                                                                                                          
    4:Edinburgh                                                                                                                           
    11:Edinburgh                                                                                                                          
    12:Scotland                                                                                                                           
    14:University                                                                                                                         
    16:Texas                                                                                                                              
    21:Association                                                                                                                        
    23:American                                                                                                                           
    24:Universities                                                                                                                       
    29:Sentence 
    

    【讨论】:

    • 非常感谢。你的代码很完美。
    • @M_92 如果对我有帮助,请投票支持我的答案并将其作为答案:)))))
    【解决方案2】:

    这是代码。您可以添加任何其他字符来剥离,它应该从单词的末尾删除它。您还可以将最后的打印更改为您想要的任何内容。

    import numpy as np
    
    s1="The University of Edinburgh is a public research university in Edinburgh, Scotland. The University of Texas was included in the Association of American Universities in 1929."
    
    n = []
    
    for index, word in enumerate(s1.split()):
        if word[0].isupper():
            if string[index-1][-1] == ".": #check that previous word does not end in a ".". 
                continue
            print(f"""{index+1}:{word.strip(",.;:")}""") #python index is one number lower, so add one to it to get the numbers you requested
            n.append(word) #this is just to be able to print something if no words have capital letters
    if len(n) == 0:
        print("None")
    

    【讨论】:

      【解决方案3】:

      试试这个代码

      只是你必须在字符串上使用.istitle()方法来检查它是否以大写字母开头,其余的都是小写

      并且使用正则表达式,您可以取出单词,不包括末尾的符号(假设您不想包含您提到的符号以忽略单词末尾的分号)

      import re
      
      inp = 'The University; of Edinburgh is a public research university in Edinburgh, Scotland. The University of Texas was included in the Association of American Universities in 1929'
      inp2 = ''
      
      def capitalized_words_in_a_text(inp):
          lst = inp.split(' ')[1:]
          res = [f"{i}: {re.match(r'^[A-Za-z]+', j).group()}" for i,j in enumerate(lst, start=2) if j.istitle()]
      
          if len(res) == 0:
              return
          return '\n'.join(res)
      
      print(capitalized_words_in_a_text(inp))
      print(capitalized_words_in_a_text(inp.lower()))
      

      输出:

      2: University
      4: Edinburgh
      11: Edinburgh
      12: Scotland
      13: The
      14: University
      16: Texas
      21: Association
      23: American
      24: Universities
      None # this is from the inp.lower() line, as there's no capital letters in the string
      

      如果它不起作用,请告诉我...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-28
        • 1970-01-01
        • 2021-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多