File indexing completed on 2026-04-09 07:49:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <iostream>
0021 #include <fstream>
0022 #include <iomanip>
0023 #include <algorithm>
0024 #include <cassert>
0025
0026 #include "SPath.hh"
0027 #include "SASCII.hh"
0028 #include "SAbbrev.hh"
0029 #include "SDice.hh"
0030
0031 #include "SLOG.hh"
0032
0033 const plog::Severity SAbbrev::LEVEL = SLOG::EnvLevel("SAbbrev", "DEBUG") ;
0034
0035 SAbbrev* SAbbrev::FromString(const char* str)
0036 {
0037 std::vector<std::string>* nms = new std::vector<std::string>() ;
0038
0039 std::stringstream ss(str) ;
0040 std::string line ;
0041 while (std::getline(ss, line))
0042 {
0043 if(line.empty()) continue ;
0044
0045 nms->push_back(line);
0046 }
0047
0048 const std::vector<std::string>& names = *nms ;
0049 return new SAbbrev(names);
0050 }
0051
0052
0053 SAbbrev* SAbbrev::Load(const char* path_)
0054 {
0055 const char* path = SPath::Resolve(path_, 0);
0056 std::ifstream fp(path, std::ios::in);
0057 if(fp.fail()) return nullptr ;
0058
0059 std::vector<std::string>* nms = new std::vector<std::string>() ;
0060
0061 std::stringstream ss ;
0062 std::string line ;
0063 while (std::getline(fp, line))
0064 {
0065 nms->push_back(line);
0066 }
0067
0068 const std::vector<std::string>& names = *nms ;
0069 return new SAbbrev(names);
0070 }
0071
0072
0073
0074
0075
0076 SAbbrev::SAbbrev( const std::vector<std::string>& names_ )
0077 :
0078 names(names_)
0079 {
0080 init();
0081 }
0082
0083 void SAbbrev::save(const char* fold, const char* name) const
0084 {
0085 const char* path = SPath::Resolve(fold, name, 1);
0086 save(path);
0087 }
0088
0089 void SAbbrev::save(const char* path_) const
0090 {
0091 const char* path = SPath::Resolve(path_, 1);
0092 LOG(LEVEL) << path ;
0093 std::ofstream fp(path, std::ios::out);
0094 for(unsigned i=0 ; i < names.size() ; i++ ) fp << names[i] << std::endl ;
0095 }
0096
0097
0098
0099
0100 bool SAbbrev::isFree(const std::string& ab) const
0101 {
0102 return std::find( abbrev.begin(), abbrev.end(), ab ) == abbrev.end() ;
0103 }
0104
0105 void SAbbrev::init()
0106 {
0107 for(unsigned i=0 ; i < names.size() ; i++) LOG(LEVEL) << names[i].c_str();
0108
0109 for(unsigned i=0 ; i < names.size() ; i++)
0110 {
0111 const char* name = names[i].c_str();
0112 SASCII* n = new SASCII(name);
0113
0114 int chars_after_other = n->other == 1 ? strlen(name) - n->first_other_index - 1 : -1 ;
0115 bool underscored = chars_after_other > 2 ;
0116
0117
0118 if( underscored )
0119 {
0120 int idx = n->first_other_index ;
0121 delete n ;
0122 n = new SASCII(name+idx+1) ;
0123 }
0124
0125 std::string ab = form_candidate(n);
0126
0127 bool is_now_free = isFree(ab) ;
0128
0129 if(!is_now_free)
0130 {
0131 save("$TMP", "SAbbrev_FAIL.txt");
0132 }
0133
0134 LOG(LEVEL)
0135 << " name [" << name << "]"
0136 << " ab [" << ab << "]"
0137 << " is_now_free " << is_now_free
0138 ;
0139
0140 assert( is_now_free && "failed to abbreviate ");
0141 abbrev.push_back(ab) ;
0142 }
0143 }
0144
0145
0146 std::string SAbbrev::form_candidate( const SASCII* n )
0147 {
0148 std::string ab ;
0149
0150 if( n->upper > 0 && n->number > 0 )
0151 {
0152 int iu = n->first_upper_index ;
0153 int in = n->first_number_index ;
0154 ab = n->getTwoChar( iu < in ? iu : in , iu < in ? in : iu );
0155 }
0156 else if( n->upper > 1 )
0157 {
0158 ab = n->getFirstUpper(2) ;
0159 }
0160 else
0161 {
0162 ab = n->getFirst(2) ;
0163 }
0164
0165 LOG(LEVEL)
0166 << " n.s [" << n->s << "]"
0167 << " ab [" << ab << "]"
0168 ;
0169
0170 if(!isFree(ab))
0171 {
0172 ab = n->getFirstLast();
0173 }
0174
0175
0176 SDice<26> rng ;
0177
0178 unsigned count = 0 ;
0179 while(!isFree(ab) && count < 100)
0180 {
0181 ab = n->getTwoRandom(rng);
0182 count += 1 ;
0183 }
0184
0185 return ab ;
0186 }
0187
0188
0189
0190
0191
0192 void SAbbrev::dump() const
0193 {
0194 for(unsigned i=0 ; i < names.size() ; i++)
0195 {
0196 std::cout
0197 << std::setw(30) << names[i]
0198 << " : "
0199 << std::setw(2) << abbrev[i]
0200 << std::endl
0201 ;
0202 }
0203 }
0204
0205
0206
0207