-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
7 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
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
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,59 @@ | ||
from guineapig import * | ||
|
||
# compute TFIDF in Guineapig | ||
# | ||
# Optimized to use sideviews for the relations that | ||
# are only the size of the vocabulary | ||
# | ||
# sample invocation: | ||
# % python smallvoc-tfidf-simplified.py --params input:dbpedia/withIds,output:dbpedia/docvec.gp --store docvec | ||
# | ||
|
||
import sys | ||
import math | ||
|
||
def loadAsDict(view): | ||
result = {} | ||
for (key,val) in GPig.rowsOf(view): | ||
result[key] = val | ||
return result | ||
|
||
class TFIDF(Planner): | ||
|
||
data = ReadLines('idcorpus.txt') \ | ||
| Map(by=lambda line:line.strip().split("\t")) \ | ||
| Map(by=lambda (docid,doc): (docid,doc.lower().split())) \ | ||
| FlatMap(by=lambda (docid,words): map(lambda w:(docid,w),words)) | ||
|
||
#compute document frequency and inverse doc freq | ||
docFreq = Distinct(data) \ | ||
| Group(by=lambda (docid,term):term, \ | ||
retaining=lambda x:1, \ | ||
reducingTo=ReduceToSum()) | ||
|
||
# definitely use combiners when you aggregate | ||
ndoc = Map(data, by=lambda (docid,term):docid) \ | ||
| Distinct() \ | ||
| Group(by=lambda row:'ndoc', retaining=lambda x:1, combiningTo=ReduceToSum(), reducingTo=ReduceToSum()) | ||
|
||
# convert raw docFreq to idf | ||
inverseDocFreq = Augment(docFreq, sideview=ndoc, loadedBy=lambda v:GPig.onlyRowOf(v)) \ | ||
| Map(by=lambda((term,df),(dummy,ndoc)):(term,math.log(ndoc/df))) | ||
|
||
#compute unweighted document vectors with a map-side join | ||
udocvec = Augment(data, sideview=inverseDocFreq, loadedBy=loadAsDict) \ | ||
| Map(by=lambda ((docid,term),idfDict):(docid,term,idfDict[term])) | ||
|
||
#normalize | ||
norm = Group(udocvec, | ||
by=lambda(docid,term,weight):docid, | ||
retaining=lambda(docid,term,weight):weight*weight, | ||
reducingTo=ReduceToSum() ) | ||
|
||
docvec = Augment(udocvec, sideview=norm, loadedBy=loadAsDict) \ | ||
| Map( by=lambda ((docid,term,weight),normDict): (docid,term,weight/math.sqrt(normDict[docid]))) | ||
|
||
## always end like this | ||
if __name__ == "__main__": | ||
p = TFIDF() | ||
p.main(sys.argv) |
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