Page MenuHomec4science

cpp_ngram.cpp
No OneTemporary

File Metadata

Created
Sat, Nov 9, 06:17

cpp_ngram.cpp

/*
Copyright (c) 2009 Noé Cuneo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <vector>
#include <string>
#include <map>
using namespace std;
struct NGCountStruct{
// Stores the number of occurances of a n-gram in both strings
unsigned int counts[2];
NGCountStruct(){
counts[0] = 0; //number of occurances in the left string
counts[1] = 0; //number of occurances in the right string
}
};
template<class stringType>
class NgramComparator{
map<stringType, NGCountStruct> comp_map;
stringType theStrings[2];
public:
NgramComparator(const stringType &s1, const stringType &s2, unsigned int n){
//Initialization with two strings
theStrings[0] = s1;
theStrings[1] = s2;
//For both strings... (w=0 for s0, w=1 for s1)
for(int w = 0; w <= 1; w++){
int size = theStrings[w].size();
// for example: "bami" => "b", "ba", "bam", "ami", "mi", "i"
for(int i = 1 - n; i <= size - 1; i++){
int start = i;
int stop = i + n;
// Truncate if necessary
if(start < 0) start = 0;
if(stop > size) stop = size;
if(stop > start){
// Build the key
stringType key = theStrings[w].substr(start, stop-start);
// Try to find the ngram key.
// "typename" is just here to let the compilator know that
// map<stringType, NGCountStruct>::const_iterator is a type!
typename map<stringType, NGCountStruct>::const_iterator it = comp_map.find(key);
// If we do not have this key yet, we create it
if(it == comp_map.end()){
// initialize a 0-0 NGCountStruct
comp_map[key];
};
// Then increment it
comp_map[key].counts[w] += 1;
};
};
}
};
vector<int> getScoreArray(){
// returns [left_ngrams, right_ngrams, common_ngrams]
vector<int> score(3, 0);
for (typename map<stringType, NGCountStruct>::const_iterator it=comp_map.begin() ; it != comp_map.end(); it++ ){
int lefts(it->second.counts[0]), rights(it->second.counts[1]);
score[0] += lefts;
score[1] += rights;
score[2] += min(lefts, rights);
};
return score;
};
};
// Now we implement explicit methods for both types (string and wstring)
// so that we can expose them to Python with minimal object conversion...
// For Python scripts which need special calculations, we expose get_totals
vector<int> unicode_get_totals(wstring s1, wstring s2, unsigned int n = 3){
// returns [left_ngrams, right_ngrams, common_ngrams]
NgramComparator<wstring> ngc(s1, s2, n);
return ngc.getScoreArray();
}
vector<int> ascii_get_totals(string s1, string s2, unsigned int n = 3){
NgramComparator<string> ngc(s1, s2, n);
return ngc.getScoreArray();
}
// For standard ngram comparison, we expose the faster methods that follow
float unicode_compare(wstring s1, wstring s2, unsigned int n = 3){
// returns common_ngrams/avg_ngrams_count
NgramComparator<wstring> ngc(s1, s2, n);
vector<int> score = ngc.getScoreArray();
return float(score[2])*2.0/(float(score[0] + score[1]));
}
float ascii_compare(string s1, string s2, unsigned int n = 3){
// returns common_ngrams/avg_ngrams_count
NgramComparator<string> ngc(s1, s2, n);
vector<int> score = ngc.getScoreArray();
return float(score[2])*2.0/(float(score[0] + score[1]));
}

Event Timeline