2017-10-21 140 views
-3

我想算「端的句子」如句號,感嘆號和問號。有沒有更好的方法來計算句子中的標點符號?

我已經寫了一個小環要做到這一點,但我不知道是否有更好的方法。不允許使用內置函數。

for line in textContent: 
    numberOfFullStops += line.count(".") 
    numberOfQuestionMarks += line.count("?") 
    numberOfQuestionMarks += line.count("!") 

numberOfSentences = numberOfFullStops + numberOfQuestionMarks + numberOfExclamationMarks 
+0

你需要爲他們單獨計數?這裏有什麼用途 - 計算標點符號或計算句子的數量? 「因爲這不是5句話!!!!!」 –

+0

在這裏什麼是「內置函數」?你可以使用標準庫模塊嗎? –

+0

對不起。我的意思是方法。舉個例子,如果有一些可能會自動剝離它,然後我們是不允許使用它e.g如果我們選,我們不允許使用variable.sort() –

回答

0

假設你想在一個句子計算終端標點符號,我們可以通過遍歷每個字符串的字符及過濾標點符號產生的字典(字符,計數)對。

演示

這裏有三個選項呈現自上而下與中程對初學者級別的數據結構:

import collections as ct 


sentence = "Here is a sentence, and it has some exclamations!!" 
terminals = ".?!" 

# Option 1 - Counter and Dictionary Comprehension 
cd = {c:val for c, val in ct.Counter(sentence).items() if c in terminals} 
cd 
# Out: {'!': 2} 


# Option 2 - Default Dictionary 
dd = ct.defaultdict(int) 
for c in sentence: 
    if c in terminals: 
     dd[c] += 1 
dd 
# Out: defaultdict(int, {'!': 2}) 


# Option 3 - Regular Dictionary 
d = {} 
for c in sentence: 
    if c in terminals: 
     if c not in d: 
      d[c] = 0 
     d[c] += 1 
d 
# Out: {'!': 2} 

爲了進一步擴展,爲獨立sentences列表,一環周圍後者的選擇。

for sentence in sentences: 
    # add option here 

注:總結每個句子的總標點符號,總的dict.values(),例如sum(cd.values())


更新假設你要通過終端punctutation拆分句子,使用正則表達式:

import re 


line = "Here is a string of sentences. How do we split them up? Try regular expressions!!!" 


# Option - Regular Expression and List Comprehension 
pattern = r"[.?!]" 
sentences = [sentence for sentence in re.split(pattern, line) if sentence] 
sentences 
# Out: ['Here is a string of sentences', ' How do we split them up', ' Try regular expressions'] 

len(sentences) 
# Out: 3 

通知line有5個終端,但是隻有3句。因此正則表達式是一種更可靠的方法。

參考

+0

謝謝你。 :-)大幫忙。 –

+0

沒問題。我已根據您的意見更新了該帖子,希望更接近您要查找的內容。而且,隨着答案的不斷髮布,不要忘記提供對您有幫助的答案並接受最終解決方案。 – pylang

相關問題