2017-10-05 37 views
0

我想正確計算文本文件中的元音和輔音的數量,但目前我迷了路。我有其他需要查找的部分。如何計算文本文件中元音和輔音的數量?

# Home work 4 
from string import punctuation 

fname = raw_input("Enter name of the file: ") 
fvar = open(fname, "r") 
punctuationList = "!#$%&'(),.:;?" 
numLines = 0 
numWords = 0 
numChars = 0 
numPunc = 0 
numVowl = 0 
numCons = 0 


if line in "aeiou": 
    numVowl = + 1 
else: 
    numCons += 1 

for line in fvar: 
    wordsList = line.split() 
    numLines += 1 
    numWords += len(wordsList) 
    numChars += len(line) 
for punctuation in punctuationList: 
    numPunc += 1 


print "Lines %d" % numLines 
print "Words %d" % numWords 
print "The amount of charcters is %d" % numChars 
print "The amount of punctuation is %d" % numPunc 
print "The amount of vowls is %d" % numVowl 
print "The amount of consonants is %d" % numCons 
+3

究竟是什麼問題? – Mureinik

+0

作爲第一步,嘗試編寫一個函數,給定一個**字符串**,返回輔音字母和元音字母的數量。然後從那裏工作。例如,'count(「hello world」)''可能會返回'(7,3)' – sam

+0

您在fvar'循環中的'for line'外有'if line'語句。這應該如何工作? – Barmar

回答

-1

你不是很清楚你的問題是什麼。但是你可以做這樣的事情:

string = 'The quick brown fox jumped over the lazy dog' 
vow='aeiou' 
sum([string.count(i) for i in vow]) 

,並按照聲母相同的過程太

+0

爲什麼看這個numpy? –

+0

這就是我的不好。將編輯它。我更習慣於在我的項目中使用numpy數組。它通常更快與numpy數組一起工作。我認爲numpy.sum實際上比列表的本地總和慢。 @AdamSmith – frank

0

你需要循環的所有字符,測試他們是否是元音,輔音,或標點符號。

for line in fvar: 
    wordsList = line.split() 
    numLines += 1 
    numWords += len(wordsList) 
    numChars += len(line) 
    for char in line: 
     if char in 'aeiou': 
      numVowl += 1 
     elif char in 'bcdfghjklmnpqrstvwxyz' 
      numCons += 1 
     else: 
      numPunc += 1 
0

你可以試試這個:

f = [i.strip('\n').split() for i in open('file.txt')] 
new_lines = [[sum(b in 'bcdfghjklmnpqrstvwxyz' for b in i), sum(b in "aeiou" for b in i)] for i in f] 
total_consonants = sum(a for a, b in new_lines) 
total_vowels = sum(b for a, b in new_lines) 
0

我會寫一個返回你所關心的時候給出一個字符串計數的3元組的功能。

import string 

def count_helper(s) -> ("vowel count", "consonant count", "punctuation count"): 
    vowels = set('aeiou') 
    consonants = set(string.ascii_lowercase).difference(vowels) 
    # you could also do set('bcdfghjklmnpqrstvwxyz'), but I recommend this approach 
    # because it's more obviously correct (you can't possibly typo and miss a letter) 
    c_vowel = c_consonant = c_punctuation = 0 
    for ch in s: 
     if ch in vowels: c_vowel += 1 
     elif ch in consonants: c_consonant += 1 
     else: c_punctuation += 1 
    return (c_vowel, c_consonant, c_punctuation) 

然後,當您遍歷文件時,將每行傳遞給count_helper

counts = {'vowels': 0, 'consonants': 0, 'punctuation': 0} 
for line in f: 
    v, c, p = count_helper(line) 
    counts['vowels'] += v 
    counts['consonants'] += c 
    counts['punctuation'] += p 
+0

爲了好玩(和練習)我在Haskell中構建了這個玩具程序https://gist.github.com/NotTheEconomist/0b3ed35e140d000e3ce614d0ce5a327b –