Skip to content

Commit

Permalink
feat: added body of function to avgwordlen
Browse files Browse the repository at this point in the history
  • Loading branch information
kphaterp committed Jan 20, 2022
1 parent fcb72f2 commit baa406d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/textfeatureinfo/textfeatureinfo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Count number of punctuations
def count_punc(text):
"""
Expand Down Expand Up @@ -48,6 +49,31 @@ def avg_word_len(text):
>>> avg_word_len("Hello, World!")
5
"""
if not isinstance(text, str):
raise TypeError("'text' should be of type 'String'")

try:
from string import punctuation
except ImportError:
raise ImportError("punctuation from string module failed to import")

new_text = ""
for char in text:
if char not in punctuation:
new_text = new_text + char
else:
new_text = new_text + " "

word_len = [len(word) for word in new_text.split()]

if len(word_len) == 0:
average_len = 0
else:
average_len = sum(word_len) / len(word_len)

return average_len



# Count percentage of fully capitalised words
def perc_cap_words(text):
Expand Down

0 comments on commit baa406d

Please sign in to comment.