forked from typecode/wikileaks
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
elishowk
committed
Dec 23, 2010
1 parent
6ca8ae7
commit 1c080ac
Showing
4 changed files
with
115 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (C) 2010 elishowk@nonutc.fr | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
import logging | ||
logging.basicConfig(level=logging.DEBUG, format="%(levelname)-8s %(message)s") | ||
|
||
from bson.code import Code | ||
|
||
class CoocNetwork(object): | ||
""" | ||
Reads all database entries to produce a network | ||
""" | ||
def __init__(self, storage, config, minoccs=1): | ||
self.storage = storage | ||
self.config = config | ||
self.map_reduce(minoccs) | ||
|
||
def map_reduce(self, ngramizer, filters, postagger, minoccs): | ||
""" | ||
execute a map-reduce operation on mongodb documents to produce the coocurrence edges matrix | ||
""" | ||
result = self.storage.cables.map_reduce( self.get_mapper(), self.get_reducer() ) | ||
logging.info("CableExtractor.map_reduce is done") | ||
|
||
def get_mapper(self): | ||
return Code( | ||
function() { | ||
for (var ngramid in this.edges.NGram) { | ||
var coocslice = {}; | ||
for (var neighbourid in this.edges.NGram) { | ||
if (neighbourid != ngramid) { | ||
coocslice[neighbourid] = 1; | ||
} | ||
} | ||
emit(ngramid, coocslice); | ||
} | ||
} | ||
) | ||
|
||
def get_reducer(self): | ||
return Code( | ||
function(ngramid, coocslices) { | ||
totalcooc = {}; | ||
for ( var slice in coocslices ) { | ||
for ( var neighbourid in slice ) { | ||
if ( neighbourid in totalcooc ) | ||
totalcooc[neighbourid] += slice[neighbourid] | ||
else | ||
totalcooc[neighbourid] = slice[neighbourid] | ||
} | ||
} | ||
return totalcooc; | ||
} | ||
) |
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