@@ -0,0 +1,715 @@
+#!/usr/bin/env python%0A# -*- coding: UTF-8 -*-%0A#%0A# This is a simple NMI tool based on MI%0A#%0A# Reference: http://en.wikipedia.org/wiki/Pointwise_mutual_information %0A# Paper: Normalized(Pointwise) Mutual Information in Collocation Extraction%0A#%0A# @author: Jason Wu (Jasonwbw@yahoo.com)%0A%0Afrom pmi import MI%0Aimport math%0A%0Aclass NMI(MI):%0A%0A%09def compute_mi(self, sentence1, sentence2):%0A%09%09# NMI(t1, t2) = mi / -sum_x_y_in_s12( p(x, y)logp(x, y))%0A%09%09mi = MI.compute_mi(self, sentence1, sentence2)%0A%09%09denominator = 0.0%0A%09%09for t1 in sentence1:%0A%09%09%09for t2 in sentence2:%0A%09%09%09%09p = self.iindex.concurrence(t1, t2) / self.iindex.get_num_docs()%0A%09%09%09%09if p %3E 0:%0A%09%09%09%09%09denominator += p * math.log(p, 2)%0A%09%09return mi != 0 and -mi / denominator or 0

add nmi tool