forked from chrisjmccormick/simsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseMHC.py
More file actions
65 lines (42 loc) · 1.61 KB
/
Copy pathparseMHC.py
File metadata and controls
65 lines (42 loc) · 1.61 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
from corpusbuilder import CorpusBuilder
from simsearch import SimSearch
from os import makedirs
from os.path import exists
# Create the CorpusBuilder.
cb = CorpusBuilder()
# Set the list of stop words.
cb.setStopList('stop_words_mhc.txt')
# Match blank lines as the separator between "documents".
cb.setDocStartPattern(r'^\s*$')
#The MHC text includes a number of patterns which need to be filtered out...
sub_patterns = []
# Remove line breaks of the form "___________"
sub_patterns.append((r'_+', ' '))
# Remove verse references like "ver. 5-7" or "ver. 25, 26"
sub_patterns.append((r'ver\. \d+(-|, )*\d*', ' '))
# Remove verse numbers of the form "18.", "2.", etc., as well as any other
# remaining numbers.
sub_patterns.append((r'\d+\.?', ' '))
cb.setSubstitutions(sub_patterns)
print 'Parsing Matthew Henry\'s Commentary...'
# Parse all of the text files in the directory.
cb.addDirectory('./mhc/')
print 'Done.'
print 'Building corpus...'
cb.buildCorpus()
# Initialize a KeySearch object from the corpus.
ksearch = cb.toKeySearch()
# Print the top 30 most common words.
ksearch.printTopNWords(topn=30)
print '\nVocabulary contains', ksearch.getVocabSize(), 'unique words.'
print 'Corpus contains', len(ksearch.corpus_tfidf), '"documents" represented by tf-idf vectors.'
# Initialize a SimSearch object from the KeySearch.
ssearch = SimSearch(ksearch)
# Train LSI with 100 topics.
print '\nTraining LSI...'
ssearch.trainLSI(num_topics=300)
print '\nSaving to disk...'
if not exists('./mhc_corpus/'):
makedirs('./mhc_corpus/')
ssearch.save(save_dir='./mhc_corpus/')
print 'Done!'