Python 如何计算句子中的单词数,忽略数字、标点符号和空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19410018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:40:33  来源:igfitidea点击:

How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

pythonlisttextcounterpython-textprocessing

提问by HossBender

How would I go about counting the words in a sentence? I'm using Python.

我将如何计算句子中的单词?我正在使用 Python。

For example, I might have the string:

例如,我可能有这样的字符串:

string = "I     am having  a   very  nice  23!@$      day. "

That would be 7 words. I'm having trouble with the random amount of spaces after/before each word as well as when numbers or symbols are involved.

那将是7个字。我在每个单词之后/之前以及涉及数字或符号时遇到随机数量的空格问题。

采纳答案by arshajii

str.split()without any arguments splits on runs of whitespace characters:

str.split()在空白字符运行时没有任何参数拆分:

>>> s = 'I am having a very nice day.'
>>> 
>>> len(s.split())
7

From the linked documentation:

从链接的文档:

If sepis not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

如果未指定sep或 is None,则应用不同的拆分算法:连续空格的运行被视为单个分隔符,如果字符串有前导或尾随空格,则结果将在开头或结尾不包含空字符串。

回答by karthikr

You can use regex.findall():

您可以使用regex.findall()

import re
line = " I am having a very nice day."
count = len(re.findall(r'\w+', line))
print (count)

回答by JadedTuna

Ok here is my version of doing this. I noticed that you want your output to be 7, which means you dont want to count special characters and numbers. So here is regex pattern:

好的,这是我这样做的版本。我注意到您希望输出为7,这意味着您不想计算特殊字符和数字。所以这是正则表达式模式:

re.findall("[a-zA-Z_]+", string)

Where [a-zA-Z_]means it will match anycharacter beetwen a-z(lowercase) and A-Z(upper case).

其中[a-zA-Z_]意味着它将匹配任何字符 beetwen a-z(小写)和A-Z(大写)。



About spaces. If you want to remove all extra spaces, just do:

关于空格。如果要删除所有多余的空格,只需执行以下操作:

string = string.rstrip().lstrip() # Remove all extra spaces at the start and at the end of the string
while "  " in string: # While  there are 2 spaces beetwen words in our string...
    string = string.replace("  ", " ") # ... replace them by one space!

回答by Aliyar

This is a simple word counter using regex. The script includes a loop which you can terminate it when you're done.

这是一个使用正则表达式的简单单词计数器。该脚本包含一个循环,您可以在完成后终止它。

#word counter using regex
import re
while True:
    string =raw_input("Enter the string: ")
    count = len(re.findall("[a-zA-Z_]+", string))
    if line == "Done": #command to terminate the loop
        break
    print (count)
print ("Terminated")

回答by Darrell White

    def wordCount(mystring):  
        tempcount = 0  
        count = 1  

        try:  
            for character in mystring:  
                if character == " ":  
                    tempcount +=1  
                    if tempcount ==1:  
                        count +=1  

                    else:  
                        tempcount +=1
                 else:
                     tempcount=0

             return count  

         except Exception:  
             error = "Not a string"  
             return error  

    mystring = "I   am having   a    very nice 23!@$      day."           

    print(wordCount(mystring))  

output is 8

输出为 8

回答by boon kwee

s = "I     am having  a   very  nice  23!@$      day. "
sum([i.strip(string.punctuation).isalpha() for i in s.split()])

The statement above will go through each chunk of text and remove punctuations before verifying if the chunk is really string of alphabets.

上面的语句将遍历每个文本块并在验证块是否真的是字母串之前删除标点符号。

回答by Anto

How about using a simple loop to count the occurrences of number of spaces!?

使用一个简单的循环来计算空格数的出现如何!?

txt = "Just an example here move along" 
count = 1
for i in txt:
if i == " ":
   count += 1
print(count)

回答by Adam

import string 

sentence = "I     am having  a   very  nice  23!@$      day. "
# Remove all punctuations
sentence = sentence.translate(str.maketrans('', '', string.punctuation))
# Remove all numbers"
sentence = ''.join([word for word in sentence if not word.isdigit()])
count = 0;
for index in range(len(sentence)-1) :
    if sentence[index+1].isspace() and not sentence[index].isspace():
        count += 1 
print(count)