In [86]:
from bs4 import BeautifulSoup  # For processing XMLfrom BeautifulSoup
import nltk
import re

In [87]:
doc = open('garden-party.xml').read()
soup = BeautifulSoup(doc, 'lxml')

In [88]:
segs = soup.findAll('seg')

In [89]:
text = ""
for seg in segs: 
    text += seg.text + " "

In [90]:
def cleanText(text): 
    text = text.replace('\n', ' ') # change newlines to spaces
    text = text.replace('\t', ' ') # change tabs to spaces
    text = re.sub('\s+', ' ', text).strip() # remove redundant whitespace
    return text

text = cleanText(text)

In [91]:
sents = nltk.sent_tokenize(text) # break the text up into sentences

In [92]:
len(sents) # how many sentences?


In [85]:
ands = [sent for sent in sents if'^And', sent) is not None]
ands # sentences that start with "And"

['And after all the weather was ideal.',
 'And now she looked at the others, they were smiling too.',
 'And what a beautiful morning!',
 "And she pointed to the lily-lawn with the hand that didn't hold the bread-and-butter.",
 'And they were so lovely, with their broad, gleaming leaves, and their clusters of yellow fruit.',
 'And Laura sat back.',
 'And now there came a long, chuckling absurd sound.',
 'And there were two tiny spots of sun, one on the inkpot, one on a silver photograph frame, playing too.',
 'And I suddenly thought for once in my life I shall have enough canna lilies.',
 "And the children knew by her face that she hadn't got them.",
 'And she said to Sadie firmly, "Tell cook I\'ll let her have them in ten minutes."',
 'And—and, Jose, pacify cook if you do go into the kitchen, will you?',
 'And he said to the cook, "He\'s left a wife and five little ones."',
 'And Mrs. Sheridan turned round from her dressing-table.',
 'And she held up her hand-mirror.',
 'And it\'s not very sympathetic to spoil everybody\'s enjoyment as you\'re doing now."',
 'And now she hoped her mother was right.',
 'And somehow that seemed quite the best plan...',
 'And she followed him into the hall.',
 'And Laura, glowing, answered softly, "Have you had tea?',
 'And the perfect afternoon slowly ripened, slowly faded, slowly its petals closed.',
 'And they all of them sat down in the deserted marquee.',
 "And she's sure to have neighbours calling in and so on.",
 'And, Laura!',
 'And it seemed to her that kisses, voices, tinkling spoons, laughter, the smell of crushed grass were somehow inside her.',
 'And the big hat with the velvet streamer—if only it was another hat!',
 'And the poor face puckered up again.',
 'And again she began, "You\'ll excuse her, miss, I\'m sure," and her face, swollen too, tried an oily smile.',
 "And this time she didn't wait for Em's sister."]

In [81]:
len(ands) # number of sentences that start with "And"


In [84]:
proportionOfAnds = (len(ands) / len(sents)) * 100 
proportionOfAnds # percentage of sentences that start with "And"


Now let's find sentences that begin with "but."

In [94]:
buts = [sent for sent in sents if'^But', sent) is not None]
buts # sentences that start with "But"

['But Meg could not possibly go and supervise the men.',
 'But that sounded so fearfully affected that she was ashamed, and stammered like a little girl, "Oh—er—have you come—is it about the marquee?"',
 'But she did quite follow him.',
 'But the tall fellow interrupted.',
 'But the air!',
 'But at that moment Mrs. Sheridan joined them.',
 'But at the word "Good-bye," and although the piano sounded more desperate than ever, her face broke into a brilliant, dreadfully unsympathetic smile.',
 'But now Sadie interrupted them.',
 "But the back door was blocked by cook, Sadie, Godber's man and Hans.",
 "But Godber's man wasn't going to have his story snatched from under his very nose.",
 'But Jose was still more amazed.',
 'But since they were grown up, Laura and Laurie on their prowls sometimes walked through.',
 'But still one must go everywhere; one must see everything.',
 'But it all seemed blurred, unreal, like a picture in the newspaper.',
 'But oh, these parties, these parties!',
 'But to her horror the woman answered, "Walk in please, miss," and she was shut in the passage.',
 "But all the same you had to cry, and she couldn't go out of the room without saying something to him.",
 'But Laurie—" She stopped, she looked at her brother.']

In [95]:


In [96]:
(len(buts) / len(sents)) * 100


In [ ]: