summaryrefslogtreecommitdiff
path: root/ept/popcon/local.cc
blob: 308a9e40e0a1f57a8ce2faa1ae54da9d3481ae46 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/** @file
 * @author Enrico Zini <enrico@enricozini.org>
 * Correlate popcon data with local popcon information
 */

/*
 * Copyright (C) 2007  Enrico Zini <enrico@debian.org>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <ept/popcon/local.h>
#include <ept/popcon/popcon.h>
#include <ept/popcon/maint/path.h>

#include <wibble/exception.h>

#include <algorithm>
#include <fstream>
#include <cmath>

//#include <iostream>

using namespace std;

namespace ept {
namespace popcon {

// Split a string where there are separators
static vector<string> split(const std::string& str, char sep = ' ')
{
	vector<string> res;
	size_t start = 0;
	while (start < str.size())
	{
		size_t end = str.find(sep, start);
		if (end == string::npos)
		{
			res.push_back(str.substr(start));
			break;
		}
		else
		{
			res.push_back(str.substr(start, end-start));
			start = end + 1;
		}
	}
	return res;
}

// Reverse sort pairs by comparing their second element
struct secondsort
{
	bool operator()(const pair<string, float>& a, const pair<string, float>& b) const
	{
		if (a.second == b.second)
			return a.first > b.first;
		else
			return a.second > b.second;
	}
};

Local::Local(const std::string& file)
{
	m_timestamp = Path::timestamp(file);
	if (m_timestamp == 0)
		return;
	
	ifstream in;
	in.open(file.c_str());
	if (!in.good())
		throw wibble::exception::File(file, "opening file for reading");

	while (!in.eof())
	{
		std::string line;
		getline(in, line);
		if (line.substr(0, 10) == "POPULARITY")
			continue;
		if (line.substr(0, 14) == "END-POPULARITY")
			continue;
		vector<string> data = split(line);
		if (data.size() < 4)
			continue;
		if (data[3] == "<NOFILES>")
			// This is an empty / virtual package
			m_scores.insert(make_pair(data[2], 0.1));
		else if (data.size() == 4)
			// Package normally in use
			m_scores.insert(make_pair(data[2], 1.0));
		else if (data[4] == "<OLD>")
			// Unused packages
			m_scores.insert(make_pair(data[2], 0.3));
		else if (data[4] == "<RECENT-CTIME>")
			// Recently installed packages
			m_scores.insert(make_pair(data[2], 0.5));
	}
}

float Local::score(const std::string& pkg) const
{
	std::map<std::string, float>::const_iterator i = m_scores.find(pkg);
	if (i == m_scores.end())
		return 0;
	else
		return i->second;
}

/**
 * Return the TFIDF score of the package computed against the popcon
 * information.
 */
float Local::tfidf(const Popcon& popcon, const std::string& pkg) const
{
	float popconScore = popcon.score(pkg);
	//cerr << pkg << ": " << score(pkg) << " * log(" << (float)popcon.submissions() << " / " << popconScore << ") = " << score(pkg) * log((float)popcon.submissions() / popconScore) << endl;
	if (popconScore == 0)
		return 0;
	else
		return score(pkg) * log((float)popcon.submissions() / popconScore);
	
}

std::vector< std::pair<std::string, float> > Local::scores() const
{
	vector< pair<string, float> > res;
	// Copy the scores in res
	copy(m_scores.begin(), m_scores.end(), back_inserter(res));
	// Sort res by score
	sort(res.begin(), res.end(), secondsort());
	return res;
}

std::vector< std::pair<std::string, float> > Local::tfidf(const Popcon& popcon) const
{
	vector< pair<string, float> > res;
	// Compute the tfidf scores and store them into res
	for (std::map<std::string, float>::const_iterator i = m_scores.begin();
			i != m_scores.end(); ++i)
	{
		float popconScore = popcon.score(i->first);
		if (popconScore == 0)
			res.push_back(make_pair(i->first, 0.0f));
		else
			res.push_back(make_pair(i->first,
						i->second * log((float)popcon.submissions() / popconScore)));
	}
	// Sort res by score
	sort(res.begin(), res.end(), secondsort());
	return res;
}

}
}

// vim:set ts=4 sw=4: