-
Notifications
You must be signed in to change notification settings - Fork 2
/
moreSentences.py
166 lines (147 loc) · 4.31 KB
/
moreSentences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# Get more sentences and output them to more_sentences.txt.
# Sentences with in the same context but has non-allowed words will be printed in console.
import re
import nltk
# run the following line only at the first time to download nltk.books
# nltk.download()
from nltk.book import *
# Extract allowed words from the file.
allowedWordsFile = open("allowed_words.txt", "r")
allowedWords = set([allowedWord.rstrip('\n').lower() for allowedWord in allowedWordsFile.readlines()])
def prohibitedWords(words):
"""
Checks if words are allowed.
"""
wordSet = set([word.lower() for word in words])
return wordSet - allowedWords
def wordsToSentences(words):
"""
Takes a list of words and returns a list of sentences (lists of words).
"""
# First format the punctuation marks.
# For example, split "?!" into "?" and "!"
puncPattern = re.compile(r'[?!,.()"\'\[\]:;-]+')
oldWords = words
words = []
for word in oldWords:
if puncPattern.match(word):
curWord = ""
for char in word:
if curWord:
if char == curWord[0]:
curWord += char
else:
words.append(curWord)
curWord = ""
curWord = curWord + char
else:
curWord = curWord + char
words.append(curWord)
else:
words.append(word)
# Then take out all the plots.
# Exclude the following:
# a. "SCENE 1 :"
# b. "[ thud ]"
# c. "SOLDIER # 1 :" or "HISTORIAN ' S WIFE" or "CART - MASTER :" or "VILLAGER # 2 and 3 :":
plots = []
curPlot = []
potentialTitle = []
isDescription = False
isTitle = False
for word in words:
if word == "[":
isDescription = True
if curPlot:
plots.append(curPlot)
curPlot = []
continue
if word == "]":
isDescription = False
continue
if isTitle:
if word == ":":
isTitle = False
potentialTitle = []
# The last plot terminates before the this title.
if curPlot:
plots.append(curPlot)
curPlot = []
continue
elif word.isupper() or word.isdigit() or word in {"#", ",", "-", "and"}:
# still potentially inside a title (a character name or a scene title)
potentialTitle.append(word)
continue
else:
# actually inside a sentence
isTitle = False
curPlot = curPlot + potentialTitle
potentialTitle = []
elif word.isupper():
isTitle = True
potentialTitle = [word]
if not isDescription and not isTitle:
curPlot.append(word)
if curPlot:
plots.append(curPlot)
# Finally, separate plots into sentences.
sentenceTerminals = {"?", ".", "!"}
sentences = []
for plot in plots:
l = len(plot)
i = 0
j = 1
while j < l:
if plot[j - 1] in sentenceTerminals and plot[j][0].isupper():
# Sentences are not separated simply by punctuations in case there are consecutive puncs.
# For example, "This is a sentence ! ? Yes, it is ... . Ha"
sentences.append(plot[i : j])
i = j
j += 1
sentences.append(plot[i : j])
# Deal with special cases.
# ' ve -> 've
# ' re -> 're
# ' m -> 'm
# ' s -> 's
# ' d -> 'd
# ' ll -> 'll
# ain ' t -> ain't
# can ' t -> can't
# xxxn ' t -> xxx n't
oldSentences = sentences
sentences = []
for oldSentence in oldSentences:
i = 0
l = len(oldSentence)
sentence = []
while i < l:
if i + 1 < l and oldSentence[i] == "'" and oldSentence[i + 1] in {"ve", "re", "m", "s", "d", "ll"}:
sentence.append("'" + oldSentence[i + 1])
i += 1
elif i + 2 < l and oldSentence[i + 1] == "'" and oldSentence[i + 2] == "t":
if oldSentence[i] == "ain":
sentence.append("ain't")
elif oldSentence[i] == "can":
sentence.append("can't")
else:
sentence.append(oldSentence[i][0:-1])
sentence.append("n't")
i += 2
else:
sentence.append(oldSentence[i])
i += 1
sentences.append(sentence)
return sentences
def formattedSentence(words):
return ' '.join(words)
f = open("more_sentences.txt", "w+")
for sentence in wordsToSentences(text6):
invalidWords = prohibitedWords(sentence)
if invalidWords:
print(formattedSentence(sentence))
print(invalidWords)
else:
f.write(formattedSentence(sentence))
f.write("\n")
f.close()