forked from sd16spring/TextMining
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_analysis_practice.py
More file actions
117 lines (84 loc) · 2.84 KB
/
text_analysis_practice.py
File metadata and controls
117 lines (84 loc) · 2.84 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
import string
import random
import pickle
from pickle import dump, load
from text_processing import processing_file, any_lowercase
def process_file(filename):
hist = dict()
fp = processing_file(filename)
for line in fp.split('\n'):
process_line(line, hist)
return hist
def process_line(line, hist):
#gets rid of white space and punctuation and puts words into a dictionary
line = line.replace('-', ' ')
for word in line.split():
word = word.strip(string.whitespace + string.punctuation)
word = word.lower()
hist[word] = hist.get(word, 0) + 1
def word_count(hist):
'''total number of words in the dictionary of story'''
return sum(hist.values())
def diff_words(hist):
'''total number of different words'''
return len(hist)
def word_freq(hist):
t = []
for word, count in hist.iteritems():
t.append((count, word))
t.sort(reverse = True)
#print t
res = []
for count, word in t[0:len(hist)]:
res.append(word)
print res[0:100]
return res
def random_word_list(hist):
length = random.randint(8, 20)
sentence = []
print length
for i in range(length):
sentence.append(random.choice(word_freq(hist)))
print sentence
from bisect import bisect
def random_word(hist):
"""Chooses a random word from a histogram.
The probability of each word is proportional to its frequency.
"""
words = []
freqs = []
total_freq = 0
# make a list of words and a list of cumulative frequencies
for word, freq in hist.items():
total_freq += freq
words.append(word)
freqs.append(total_freq)
# choose a random value and find its location in the cumulative list
x = random.randint(0, total_freq-1)
index = bisect(freqs, x)
print words[index]
def subtract(d1, d2, d3):
'''does the comparing between the dictionaries'''
res = dict()
for key in d1:
if key not in d2:
if key not in d3:
res[key] = d1[key]
return res
def story_comparison(filename, d1, d2, d3):
'''find the difference between the stories: what words only appear in one of the fairytale books?'''
fp = open(filename, 'w')
difference = subtract(d1, d2, d3)
return word_freq(difference)
fp.write(new_word_list)
fp.close()
input_file = open('grimm_fairytales.pickle','r')
reloaded_copy_of_texts = pickle.load(input_file)
list_stories = ['grimm_fairytales.txt', 'scottish_fairytales.txt']
hist1 = process_file('grimm_fairytales.txt')
hist2 = process_file('scottish_fairytales.txt')
hist3 = process_file('japanese_fairytales.txt')
list_hists = [hist1, hist2, hist3]
story_comparison('grimm_.txt', hist1, hist2, hist3)
story_comparison('scottish.txt', hist2, hist1, hist3)
story_comparison('japan.txt', hist3, hist2, hist1)