-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtextinput.py
More file actions
153 lines (118 loc) · 4.91 KB
/
Copy pathtextinput.py
File metadata and controls
153 lines (118 loc) · 4.91 KB
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
#
# Author: Christopher Minson
# www.christopherminson.com
#
#
import sys
import os
import string
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
BIBLE_TXT = './TEXT/bible.txt'
STOP_WORDS = './TEXT/STOPWORDS.txt'
MIN_WORD_SIZE = 2
BOOKS_OLD_TESTAMENT = [
'Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy', 'Joshua', 'Judges', 'Ruth',
'1 Samuel', '2 Samuel', '1 Kings', '2 Kings', '1 Chronicles', '2 Chronicles', 'Ezra', 'Nehemiah',
'Esther', 'Job', 'Psalms', 'Proverbs', 'Ecclesiastes', 'Song of Solomon', 'Isaiah', 'Jeremiah',
'Lamentations', 'Ezekiel', 'Daniel', 'Hosea', 'Joel', 'Amos', 'Obadiah', 'Jonah',
'Micah', 'Nahum', 'Habakkuk', 'Zephaniah', 'Haggai', 'Zechariah', 'Malachi'
]
BOOKS_NEW_TESTAMENT = [
'Matthew', 'Mark', 'Luke', 'John', 'Acts', 'Romans', '1 Corinthians', '2 Corinthians',
'Galatians', 'Ephesians', 'Philippians', 'Colossians', '1 Thessalonians', '2 Thessalonians', '1 Timothy', '2 Timothy',
'Titus', 'Philemon', 'Hebrews', 'James', '1 Peter', '2 Peter', '1 John', '2 John',
'3 John', 'Jude', 'Revelation'
]
ALL_BOOKS = BOOKS_OLD_TESTAMENT + BOOKS_NEW_TESTAMENT
AllBooks = {}
AllStoppedWords = []
AllSentences = []
AllStoppedSentences = []
AllCitations = []
BookSentencesDict = {}
BookSentencesList = []
def load_bible():
print('loading text ...')
# get all the stop words
#stop_words = set(nltk.corpus.stopwords.words('english'))
stop_words = set(stopwords.words('english'))
with open(STOP_WORDS, 'r') as fd:
while True:
word = fd.readline().strip().lower()
if not word: break;
stop_words.add(word)
# read in data
with open(BIBLE_TXT, 'r') as fd:
lines = fd.readlines()
for line in lines:
citation, raw_sentence = line.replace('\n', '').split('\t')
lowered_sentence = raw_sentence.lower().strip()
table = lowered_sentence.maketrans(dict.fromkeys(string.punctuation))
lowered_sentence = lowered_sentence.translate(table)
citation_parts = citation.split(' ')
citation = citation_parts[-1]
del citation_parts[-1]
book = ' '.join(citation_parts)
AllSentences.append(lowered_sentence)
stopped_words = [w.lower() for w in word_tokenize(lowered_sentence) if not w in stop_words and len(w) > MIN_WORD_SIZE]
AllStoppedWords.append(stopped_words)
stopped_sentence = ' '.join(stopped_words)
AllStoppedSentences.append(stopped_sentence)
AllCitations.append(book + ' ' + citation)
if book in AllBooks:
AllBooks[book].append((citation, raw_sentence, stopped_sentence))
else:
AllBooks[book] = [(citation, raw_sentence, stopped_sentence)]
if book in BookSentencesDict:
BookSentencesDict[book] += lowered_sentence
BookSentencesDict[book] += ' '
else:
BookSentencesDict[book] = lowered_sentence
BookSentencesDict[book] += ' '
for book, sentences in BookSentencesDict.items():
BookSentencesList.append(sentences)
def load_chapter(chapter):
print('loading chapter text ...')
# get all the stop words
#stop_words = set(nltk.corpus.stopwords.words('english'))
stop_words = set(stopwords.words('english'))
with open(STOP_WORDS, 'r') as fd:
while True:
word = fd.readline().strip().lower()
if not word: break;
stop_words.add(word)
# read in data
with open(BIBLE_TXT, 'r') as fd:
lines = fd.readlines()
for line in lines:
if chapter not in line: continue
citation, raw_sentence = line.replace('\n', '').split('\t')
lowered_sentence = raw_sentence.lower().strip()
table = lowered_sentence.maketrans(dict.fromkeys(string.punctuation))
lowered_sentence = lowered_sentence.translate(table)
citation_parts = citation.split(' ')
citation = citation_parts[-1]
del citation_parts[-1]
book = ' '.join(citation_parts)
AllSentences.append(lowered_sentence)
stopped_words = [w.lower() for w in word_tokenize(lowered_sentence) if not w in stop_words and len(w) > MIN_WORD_SIZE]
AllStoppedWords.append(stopped_words)
stopped_sentence = ' '.join(stopped_words)
AllStoppedSentences.append(stopped_sentence)
AllCitations.append(book + ' ' + citation)
if book in AllBooks:
AllBooks[book].append((citation, raw_sentence, stopped_sentence))
else:
AllBooks[book] = [(citation, raw_sentence, stopped_sentence)]
if book in BookSentencesDict:
BookSentencesDict[book] += lowered_sentence
BookSentencesDict[book] += ' '
else:
BookSentencesDict[book] = lowered_sentence
BookSentencesDict[book] += ' '
for book, sentences in BookSentencesDict.items():
BookSentencesList.append(sentences)
if __name__ == '__main__':
load_bible()
print('done')