-1

These two functions are given.

def split_on_separators(original, separators):
""" (str, str) -> list of str

Return a list of non-empty, non-blank strings from the original string
determined by splitting the string on any of the separators.
separators is a string of single-character separators.

>>> split_on_separators("Hooray! Finally, we're done.", "!,")
['Hooray', ' Finally', " we're done."]
"""

# To do: Complete this function's body to meet its specification.
# You are not required to keep the two lines below but you may find
# them helpful. (Hint)
for i in separators:
    original = original.replace(i,"<*)))>{")
    ret = original.split("<*)))>{")
return ret

def clean_up(s):
""" (str) -> str

Return a new string based on s in which all letters have been
converted to lowercase and punctuation characters have been stripped 
from both ends. Inner punctuation is left untouched. 

>>> clean_up('Happy Birthday!!!')
'happy birthday'
>>> clean_up("-> It's on your left-hand side.")
" it's on your left-hand side"
"""

punctuation = """!"',;:.-?)([]<>*#\n\t\r"""
result = s.lower().strip(punctuation)
return result

I should return average number of phrases per sentence. Here is the function I wrote

def avg_sentence_complexity(text):
""" (list of str) -> float

Return the average number of phrases per sentence.

A sentence is defined as a non-empty string of non-terminating
punctuation surrounded by terminating punctuation
or beginning or end of file. Terminating punctuation is defined as !?.
Phrases are substrings of sentences, separated by one or more of the
following delimiters ,;: 

>>> text = ['The time has come, the Walrus said\n',
     'To talk of many things: of shoes - and ships - and sealing wax,\n',
     'Of cabbages; and kings.\n',
     'And why the sea is boiling hot;\n',
     'and whether pigs have wings.\n']
>>> avg_sentence_complexity(text)
3.5
"""

huge_str = ''
clean_sentences = []
for lines in text:
    huge_str += lines   
list_of_sentences = split_on_separators(huge_str, '?!.')    
for strings in list_of_sentences:
    cleaned = clean_up(strings)
    clean_sentences.append(cleaned) 
    if '' in clean_sentences:
        clean_sentences.remove('')  
num_sentences = len(clean_sentences)

large = ''
for phrases in text:
    large += phrases
list_of_phrases = split_on_separators(large, ',;:')
num_phrases = len(list_of_phrases)

asc =  num_phrases / num_sentences
return asc

This only gives me 3.0 which is total phrases divided by total sentences. My question is how do I calculate (total phrases in first sentences)/(total sentences) + (total phrases in second sentences)/(total sentences) + …

1 Answers1

1

I mean technically as you've described you'd just be calculating 1/total_sentances*num_phrases which is equal to num_phrases/total_sentances, since each phrase is only 1 as I understand it.

What you actually want to do is count the number of phrases in each sentence. You can then use numpy.mean on a list of phrase counts to find the average phrase count.

I'm not going to be more specific than that because this is clearly a homework assignment :p

aruisdante
  • 8,875
  • 2
  • 30
  • 37