|
1 | 1 | #ifndef TEXTCAT_HXX |
2 | 2 | #define TEXTCAT_HXX |
3 | 3 | #include <iterator> |
| 4 | + |
4 | 5 | namespace textcat { |
| 6 | + // storing ngrams for 1 <= n <= N |
| 7 | + template<unsigned char N> |
| 8 | + struct ngram { |
| 9 | + static unsigned char const size=N; |
| 10 | + ngram(char32_t init=0):data(static_cast<data_type>(init)){} |
| 11 | + char32_t operator()(char32_t to_push) { |
| 12 | + char32_t const to_pop( data >> (21*(N-1))); |
| 13 | + data= ((data << 21) & ( (static_cast<data_type>(1)<< (21*N))-1)) |
| 14 | + | static_cast<data_type>(to_push); |
| 15 | + return to_pop; |
| 16 | + } |
| 17 | + std::size_t hash() const { |
| 18 | + return static_cast<std::size_t>(data) |
| 19 | + ^ static_cast<std::size_t>(data>>(8*sizeof(std::size_t))); |
| 20 | + } |
| 21 | + friend bool operator<(ngram<N> const& n1, ngram<N> const& n2); |
| 22 | + friend bool operator==(ngram<N> const& n1, ngram<N> const& n2); |
| 23 | + template<typename Os> Os& operator<<(Os& os, ngram<N> const& n); |
| 24 | + typedef __int128 data_type; |
| 25 | + data_type data; |
| 26 | + }; |
| 27 | + bool operator<(ngram<N> const& n1, ngram<N> const& n2) |
| 28 | + { return n1.data < n2.data; } |
| 29 | + bool operator==(ngram<N> const& n1, ngram<N> const& n2) |
| 30 | + { return n1.data == n2.data; } |
| 31 | + |
| 32 | + template<typename Os> Os& operator<<(Os& os, ngram<N> const& n) { |
| 33 | + for(unsigned char i(0); i != N; ++i){ |
| 34 | + char32_t const c (static_cast<char32_t>(n.data >> 21*i) & ((1 << 21)-1)); |
| 35 | + if(c) { to_utf8(c, os);} |
| 36 | + else { |
| 37 | + os << '\0'; |
| 38 | + break; |
| 39 | + } |
| 40 | + } |
| 41 | + return os; |
| 42 | +} |
| 43 | +namespace std { |
| 44 | + namespace tr1 { |
| 45 | + template<unsigned char N> hash<textcat::ngram<N> > { |
| 46 | + std::size_t operator()(textcat::ngram<N> const& ng) const |
| 47 | + { return ng.hash() ; } |
| 48 | + }; |
| 49 | + } |
| 50 | +} |
| 51 | +namespace textcat { |
| 52 | + |
| 53 | + template<unsigned char N, typedef SizeType= std::size_t> |
| 54 | + struct counts { |
| 55 | + typedef ngram<N> ngram_type; |
| 56 | + typedef SizeType size_type; |
| 57 | + typedef std::tr1::map<ngram_type, size_type> map_type; |
| 58 | + typedef typename map_type::value_type value_type; |
| 59 | + typedef std::vector<value_type> fingerprint_type; |
| 60 | + }; |
| 61 | + |
| 62 | + template<unsigned char N, typename In> |
| 63 | + typename counts<N>::map_type make_counts(In b, In e, std::size_t max_n){ |
| 64 | + typename counts<N>::map_type counts; |
| 65 | + typedef typename counts<N>::ngram_type ngram_type; |
| 66 | + ngram_type ng; |
| 67 | + std::size_t n; |
| 68 | + for( n=0, ng(U'_'), was_invalid=true; (b != e) && (n != max_ngrams) |
| 69 | + ; was_invalid= invalid) { |
| 70 | + char32_t const new_codepoint(to_normal(from_utf8(b, e))); |
| 71 | + invalid= (new_char == U'_'); |
| 72 | + if(! (invalid && was_invalid)) { |
| 73 | + ng(new_codepoint); |
| 74 | + ++counts[ng]; |
| 75 | + ++n; |
| 76 | + } |
| 77 | + if(invalid && !was_invalid) |
| 78 | + { ng= ngram_type(U'_');} |
| 79 | + } |
| 80 | + return counts; |
| 81 | + } |
| 82 | + |
| 83 | + template<unsigned char N> |
| 84 | + typename counts<N>::fingerprint_type |
| 85 | + to_vector(typename counts<N>::map_type const& c, std::size_t maxngrams){ |
| 86 | + typedef typename counts<N>::value_type value_type; |
| 87 | + typename counts<N>::fingerprint_type result(c.begin(), c.end()); |
| 88 | + typedef typename value_type::second_type size_type; |
| 89 | + if(maxngrams > tmp.size()) { maxngrams= tmp.size(); } |
| 90 | + std::partial_sort(tmp.begin(), tmp.begin()+maxngrams, tmp.end() |
| 91 | + , std::tr1::bind(std::greater<size_type>() |
| 92 | + , std::tr1::bind<size_type>(&pair_type::second |
| 93 | + ,std::tr1::placeholders::_1) |
| 94 | + , std::tr1::bind<size_type>(&pair_type::second |
| 95 | + ,std::tr1::placeholders::_2))); |
| 96 | + result.resize(maxngrams); |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | + template<typename InOut> InOut count_to_rank(InOut b, InOut e){ |
| 101 | + if(b!= e){ |
| 102 | + for(std::iterator_traits<InOut>::value_type last((*b).second), rnk(0) |
| 103 | + ; b != e; ++b) { |
| 104 | + if((*b).second != last){ last= (*b).second; ++rnk;} |
| 105 | + (*b).second= rnk; |
| 106 | + } |
| 107 | + } |
| 108 | + return b; |
| 109 | + } |
| 110 | + |
| 111 | +// Input Stream data is supposed to be already sorted |
| 112 | +// imbue maxngrams and >> ? |
| 113 | + template<unsigned char N, typename Is> typename counts<N>::fingerprint_type read(Is& is, std::size_t maxngrams) { |
| 114 | + typename counts<N>::fingerprint_type result; |
| 115 | + std::string tmp; |
| 116 | + std::size_t count; |
| 117 | + while(std::getline(is, tmp, '\t') && (maxngrams--)){ |
| 118 | + is >> count; |
| 119 | + if(!is) { break; } |
| 120 | + ngram<N> ng; |
| 121 | + result.push_back(std::for_each(tmp.begin(), tmp.end(), ng), count); |
| 122 | + } |
| 123 | + count_to_rank(result.begin(), result.end()); |
| 124 | + return result; |
| 125 | + } |
| 126 | + |
| 127 | + template<unsigned char N, typename Os> Os& operator<<(Os& os, typename counts<N>::fingerprint_type fp) { |
| 128 | + for(typename counts<N>::fingerprint_type::const_iterator it(fp.begin()); it != fp.end(); ++it){ |
| 129 | + os<<(*it).first << '\t' << (*it).second << '\n'; |
| 130 | + } |
| 131 | + return os; |
| 132 | + } |
| 133 | + |
| 134 | + template<unsigned char N > |
| 135 | + struct scorer { |
| 136 | + scorer(std::size_t out_of_place= 400 |
| 137 | + , std::size_t max_score= std::numeric_limits<std::size_t>::max() |
| 138 | + , float treshold_ratio=1.03): |
| 139 | + out_of_place(out_of_place), max_score(max_score) |
| 140 | + , cutoff(max_score), treshold_ratio(treshold_ratio){} |
| 141 | + |
| 142 | + std::size_t operator()(typename score<N>::fingerprint_type const& f1 |
| 143 | + ,typename score<N>::fingerprint_type const& f2){ |
| 144 | + typedef typename score<N>::fingerprint_type fp_type; |
| 145 | + std::size_t result(0); |
| 146 | + for(typename fp_type::const_iterator it1(f1.begin()), it2(f2.begin()) |
| 147 | + ; (it1 != f1.end()) && (it2 != f2.end()) && (result != max_score) |
| 148 | + ; ) { |
| 149 | + if( (*it1).first < (*it2).first) { ++it1; } |
| 150 | + else { |
| 151 | + result += ((*it1).first == (*it2).first) |
| 152 | + ? std::abs((*it1++).second - (*it2).second); |
| 153 | + : out_of_place; |
| 154 | + if(result > cutoff) { result= max_score; } |
| 155 | + ++it2; |
| 156 | + } |
| 157 | + } |
| 158 | + cutoff= std::min(cutoff, result* treshold_ratio);// check no overflow |
| 159 | + return result; |
| 160 | + } |
| 161 | + std::size_t const out_of_place, max_score; |
| 162 | + std::size_t cutoff; |
| 163 | + float treshold_ratio; |
| 164 | + }; |
| 165 | + |
| 166 | + template<unsigned char N> struct classifier { |
| 167 | + template<typename In> classifier(In b, In e) |
| 168 | + std::size_t score( typename counts<N>::fingerprint_type const& |
| 169 | + std::array<CountsType::mapped_type::size * 4> string_type; |
| 170 | + |
5 | 171 | struct fingerprint { |
6 | 172 | template<typename In> |
7 | | - fingerprint(In src, std::size_t max_ngrams){ |
| 173 | + fingerprint(In b, In e, std::size_t max_ngrams){ |
| 174 | + bool invalid, was_invalid; |
| 175 | + typedef ngram<5> ngram_type; |
| 176 | + ngram_type ng; |
| 177 | + std::size_t n; |
| 178 | + for( n=0, ng(U'_'), was_invalid=true; (b != e) && (n != max_ngrams); was_invalid= invalid) { |
| 179 | + char32_t const new_codepoint(to_normal(from_utf8(b, e))); |
| 180 | + invalid= (new_char == U'_'); |
| 181 | + if(! (invalid && was_invalid)) { |
| 182 | + ng(new_codepoint); |
| 183 | + ++counts[ng]; |
| 184 | + ++n; |
| 185 | + } |
| 186 | + if(invalid && !was_invalid) |
| 187 | + { ng= ngram_type(U'_');} |
| 188 | + } |
| 189 | + } |
| 190 | + template<typename Out> Out ranks(Out o ) const { |
| 191 | + std::vector<counts_type::value_type> |
| 192 | + std::copy(counts.begin(), counts.end(), |
8 | 193 | } |
9 | 194 | }; |
10 | 195 | template<typename Os> Os& operator<<(Os& os, fingerprint const& fp){ |
|
0 commit comments