Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added new files
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def load(): | ||
f = open("words3.txt", "r") | ||
lines = f.readlines() | ||
words = [] | ||
scores = [] | ||
for line in lines: | ||
try: | ||
(word, score) = line.rstrip().split(" ") | ||
if not (word in words): | ||
words.append(word) | ||
if score == "p": | ||
scores.append(1) | ||
elif score == "n": | ||
scores.append(-1) | ||
else: | ||
print "FUCK", word | ||
exit() | ||
except: | ||
pass | ||
|
||
return (words, scores) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import math | ||
import nltk | ||
|
||
# document is assumed to be tokenized (a list of words) | ||
# documents is a list of tokenized docs | ||
def tfidf(term, document, documents): | ||
all_doc_appearances = 0 # number of documents in which term appears | ||
for doc in documents: | ||
if term in doc: | ||
all_doc_appearances += 1 | ||
doc_appearances = 0 # number of appearances of term in this document | ||
for word in document: | ||
if term == word: | ||
doc_appearances += 1 | ||
num_docs = len(documents) # number of documents in the collection | ||
if doc_appearances == 0: | ||
#This happens sometimes, probably due to inconsistent splitting/tokenizing. | ||
#print "Error: no occurrences of", term | ||
return 0 | ||
elif all_doc_appearances == 0: | ||
#print "Error: fuck,", term | ||
return 0 | ||
else: | ||
tfidf = (1 + math.log(doc_appearances,10)) * math.log((float(num_docs)/all_doc_appearances), 10) | ||
return tfidf |