-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngramsIO.py
More file actions
82 lines (67 loc) · 3.94 KB
/
Copy pathngramsIO.py
File metadata and controls
82 lines (67 loc) · 3.94 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
#################################################################################
# #
# ngramsIO #
# #
# Input and Output for deducing ngrams. #
# #
#################################################################################
import nltk
#################################################################################
# #
# LoadWordlistFromFiles(Wordlist, filename) #
# #
# Create a list of all the words in the given files. #
# #
#################################################################################
def LoadWordlistFromFiles(Wordlist, filename):
file = open(filename, "r");
words = []
for line in file:
words = nltk.word_tokenize(line)
for word in words:
if word.isalpha():
#print(word)
Wordlist.append(word.lower())
#################################################################################
# #
# CreateNGramsList(DictionaryReverse, EdgeCount, NGramsList) #
# #
# Create a list of the results, suitable for human consumption. #
# #
#################################################################################
def CreateNGramsList(DictionaryReverse, EdgeCount, NGramsList):
for edge in EdgeCount:
edgeString = str("%04d" % edge.count)
edgeString += " "
edgeString += DictionaryReverse.find(edge.n1)
edgeString += " "
edgeString += DictionaryReverse.find(edge.n2)
edgeString += " "
edgeString += DictionaryReverse.find(edge.n3)
if edge.count > 6: # List only ngrams who's edge has been drawn more than six times.
NGramsList.append(edgeString);
NGramsList.sort()
#################################################################################
# #
# OutputNGrams(NGramsList) #
# #
# Print the human readable results to standard out. #
# #
#################################################################################
def OutputNGrams(NGramsList):
for ngram in NGramsList:
print(ngram)
#################################################################################
# #
# WriteNGramsToFileFormattedRDataSet(NGramsList, filename) #
# #
# Write to given file the results formatted as an r language data set. #
# #
#################################################################################
def OutputNGramsFormattedRDataSet(NGramsList, filename):
file = open(filename, "w");
file.write("occurrences word1 word2 word3\n")
for ngram in NGramsList:
file.write(ngram)
file.write('\n')
file.close()