AssignWords.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include "pch.h"
  2. #include "AssignWords.h"
  3. #include "CvxText.h"
  4. #include <iostream>
  5. #include <sstream>
  6. #include <fstream>
  7. #include <io.h>
  8. #include "BaseUtility.h"
  9. /*
  10. string& replace_str(string& str, const string& to_replaced, const string& newchars)
  11. {
  12. for (string::size_type pos(0); pos != string::npos; pos += newchars.length())
  13. {
  14. pos = str.find(to_replaced, pos);
  15. if (pos != string::npos)
  16. str.replace(pos, to_replaced.length(), newchars);
  17. else
  18. break;
  19. }
  20. return str;
  21. }
  22. */
  23. void split(const std::string& s, const std::string& delim, std::vector< std::string >* ret)
  24. {
  25. size_t last = 0;
  26. size_t index = s.find_first_of(delim, last);
  27. while (index != std::string::npos)
  28. {
  29. ret->push_back(s.substr(last, index - last));
  30. last = index + 1;
  31. index = s.find_first_of(delim, last);
  32. }
  33. if (index - last > 0)
  34. {
  35. ret->push_back(s.substr(last, index - last));
  36. }
  37. }
  38. void deleteFlagWord(std::string& str, const std::string& to_replaced)
  39. {
  40. std::string::size_type pos = str.find(to_replaced, 0);
  41. if (pos == 0)
  42. str.replace(pos, to_replaced.length(), "");
  43. }
  44. void ReplaceFlagWord(std::vector<std::string> &list, std::string &strRet)
  45. {
  46. for (std::vector<std::string>::iterator it = list.begin(); it != list.end(); it++)
  47. {
  48. deleteFlagWord(strRet, (*it));
  49. }
  50. }
  51. void DivideEquallyStr(const char *buff, int len, char left[], char right[])
  52. {
  53. std::string strtemp = buff;
  54. std::size_t keyindex1 = std::string::npos, keyindex2 = std::string::npos, keyindex;
  55. int i = 0;
  56. for (i = 0; i < len / 2; i++)
  57. {
  58. if (!(buff[i] < 0x80 && buff[i] >= 0x0))
  59. {
  60. i++;
  61. }
  62. }
  63. strncpy(left, buff, i);
  64. strncpy(right, buff + i, len - i);
  65. return;
  66. /* 直接中间分开,不用特殊处理*/
  67. std::string strKey1 = ";", strKey2 = " ";
  68. std::size_t found = strtemp.find(strKey1);
  69. bool flag1 = false, flag2 = false;
  70. while (found != std::string::npos && found < strtemp.length() / 2)
  71. {
  72. keyindex1 = found;
  73. found = strtemp.find(strKey1, found + 1);
  74. flag1 = true;
  75. }
  76. int w = 1;
  77. found = strtemp.find(strKey2);
  78. while (found != std::string::npos && found < strtemp.length() / 2)
  79. {
  80. keyindex2 = found;
  81. found = strtemp.find(strKey2, found + 1);
  82. flag2 = true;
  83. }
  84. if (flag1 && flag2)
  85. {
  86. if (keyindex1 > keyindex2)
  87. {
  88. keyindex = keyindex1;
  89. w = 2;
  90. }
  91. else
  92. {
  93. keyindex = keyindex2;
  94. }
  95. }
  96. else if (flag1)
  97. {
  98. keyindex = keyindex1;
  99. w = 2;
  100. }
  101. else if (flag2)
  102. {
  103. keyindex = keyindex2;
  104. }
  105. else
  106. {
  107. int i = 0;
  108. for (i = 0; i < len / 2; i++)
  109. {
  110. if (!(buff[i] < 0x80 && buff[i] >= 0x0))
  111. {
  112. i++;
  113. }
  114. }
  115. strncpy(left, buff, i);
  116. strncpy(right, buff + i, len - i);
  117. return;
  118. }
  119. std::string ttm = strtemp.substr(0, keyindex + w);
  120. std::string ttm2 = strtemp.substr(keyindex + w, strtemp.size());
  121. strncpy(left, ttm.c_str(), ttm.length());
  122. strncpy(right, ttm2.c_str(), ttm2.length());
  123. }
  124. void RandReplaceStr(std::vector<std::string> list, std::string &ret)
  125. {
  126. std::string to_replaced = ";";
  127. srand((unsigned)time(NULL));
  128. std::string newchars;
  129. int index = rand() % list.size();
  130. newchars = list[index];
  131. for (string::size_type pos(0); pos != string::npos; )
  132. {
  133. pos = ret.find(to_replaced, pos);
  134. if (pos != string::npos)
  135. ret.replace(pos, to_replaced.length(), newchars);
  136. else
  137. break;
  138. pos += newchars.length();
  139. index = rand() % list.size();
  140. newchars = list[index];
  141. }
  142. }
  143. void AssignWordsFromString(std::string strContent, int maxTwoWith, int maxTreeWith, int maxFourWith, std::vector<std::string> &rePlaceList,
  144. std::vector<std::string> &twoList, std::vector<std::string> &threeList, std::vector<std::string> &fourList)
  145. {
  146. cv::Size WordsWith = GetTextSize(strContent.c_str(), font_size, font_family);
  147. if (WordsWith.width < maxFourWith)
  148. {
  149. if (rePlaceList.size() > 0)
  150. {
  151. RandReplaceStr(rePlaceList, strContent);
  152. }
  153. fourList.push_back(strContent);
  154. }
  155. else if (WordsWith.width < maxTreeWith)
  156. {
  157. if (rePlaceList.size() > 0)
  158. {
  159. RandReplaceStr(rePlaceList, strContent);
  160. }
  161. threeList.push_back(strContent);
  162. }
  163. else if (WordsWith.width < maxTwoWith)
  164. {
  165. if (rePlaceList.size() > 0)
  166. {
  167. RandReplaceStr(rePlaceList, strContent);
  168. }
  169. twoList.push_back(strContent);
  170. }
  171. else
  172. {
  173. char left[2048] = { 0 }, right[2048] = { 0 };
  174. DivideEquallyStr(strContent.c_str(), strContent.length(), left, right);
  175. AssignWordsFromString(left, maxTwoWith, maxTreeWith, maxFourWith, rePlaceList, twoList, threeList, fourList);
  176. AssignWordsFromString(right, maxTwoWith, maxTreeWith, maxFourWith, rePlaceList, twoList, threeList, fourList);
  177. }
  178. }
  179. // 判断是否是utf-8
  180. int preNUm(unsigned char byte) {
  181. unsigned char mask = 0x80;
  182. int num = 0;
  183. for (int i = 0; i < 8; i++) {
  184. if ((byte & mask) == mask) {
  185. mask = mask >> 1;
  186. num++;
  187. }
  188. else {
  189. break;
  190. }
  191. }
  192. return num;
  193. }
  194. bool isUtf8(unsigned char* data, int len) {
  195. int num = 0;
  196. int i = 0;
  197. while (i < len) {
  198. if ((data[i] & 0x80) == 0x00) {
  199. // 0XXX_XXXX
  200. i++;
  201. continue;
  202. }
  203. else if ((num = preNUm(data[i])) > 2) {
  204. // 110X_XXXX 10XX_XXXX
  205. // 1110_XXXX 10XX_XXXX 10XX_XXXX
  206. // 1111_0XXX 10XX_XXXX 10XX_XXXX 10XX_XXXX
  207. // 1111_10XX 10XX_XXXX 10XX_XXXX 10XX_XXXX 10XX_XXXX
  208. // 1111_110X 10XX_XXXX 10XX_XXXX 10XX_XXXX 10XX_XXXX 10XX_XXXX
  209. // preNUm() 返回首个字节8个bits中首�?0bit前面1bit的个数,该数量也是该字符所使用的字节数
  210. i++;
  211. for (int j = 0; j < num - 1; j++) {
  212. //判断后面num - 1 个字节是不是都是10开
  213. if ((data[i] & 0xc0) != 0x80) {
  214. return false;
  215. }
  216. i++;
  217. }
  218. }
  219. else {
  220. //其他情况说明不是utf-8
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. int AssignWordsFromTest(std::string pathName, std::vector<tuple< string, string>> &oneList, std::vector<tuple< string, string>> &twoList,
  227. std::vector<tuple< string, string>> &threeList, std::vector<tuple< string, string>> &fourList, int &ret)
  228. {
  229. int nHangCount = 1;
  230. // 词性处理
  231. HMODULE module = GetModuleHandle(0);
  232. TCHAR pFileName[MAX_PATH + 2] = { 0 };
  233. GetModuleFileName(module, pFileName, MAX_PATH);
  234. CString csFullPath(pFileName);
  235. int nPos = csFullPath.ReverseFind(_T('\\'));
  236. CString filepath = csFullPath.Left(nPos);
  237. CString FilePath1 = filepath + L"\\config.ini";
  238. //CString FilePath2 = filepath + L"\\words.txt";
  239. WCHAR wflagWord[10],wFourColScaleParam[10],wThreeColScaleParam[10],wTwoColScaleParam[10];
  240. GetPrivateProfileString(L"USER", L"flagWord", L"1", wflagWord, 10, FilePath1);
  241. GetPrivateProfileString(L"USER", L"TwoColScaleParam", L"6", wTwoColScaleParam, 10, FilePath1);
  242. GetPrivateProfileString(L"USER", L"ThreeColScaleParam", L"9", wThreeColScaleParam, 10, FilePath1);
  243. GetPrivateProfileString(L"USER", L"FourColScaleParam", L"16", wFourColScaleParam, 10, FilePath1);
  244. int nFourColScaleParam = 0, nThreeColScaleParam = 0, nTwoColScaleParam = 0;
  245. nFourColScaleParam = _wtoi(wFourColScaleParam);
  246. nFourColScaleParam = nFourColScaleParam > 0 ? nFourColScaleParam : 16;
  247. nThreeColScaleParam = _wtoi(wThreeColScaleParam);
  248. nThreeColScaleParam = nThreeColScaleParam > 0 ? nThreeColScaleParam : 9;
  249. nTwoColScaleParam = _wtoi(wTwoColScaleParam);
  250. nTwoColScaleParam = nTwoColScaleParam > 0 ? nTwoColScaleParam : 6;
  251. CString lpszflagWord(wflagWord);
  252. int cout = 0;
  253. if (_access(pathName.c_str(), 00) != -1)
  254. {
  255. std::string otherPathName;
  256. otherPathName = pathName;
  257. size_t lenthOtherName = otherPathName.length();
  258. otherPathName.replace(lenthOtherName - 4, lenthOtherName, "-other.txt");
  259. if (_access(otherPathName.c_str(), 00) == -1)
  260. {
  261. return -1;
  262. }
  263. int maxOneWith = main_wdith;
  264. int maxTwoWith = main_wdith / 2 - main_wdith / nTwoColScaleParam - chk_width;
  265. int maxTreeWith = main_wdith / 3 - main_wdith / nThreeColScaleParam - chk_width;
  266. int maxFourWith = main_wdith / 4 - main_wdith/ nFourColScaleParam - chk_width;
  267. std::ifstream fin(pathName.c_str(), std::ios::in);
  268. std::ifstream finOther(otherPathName.c_str(), std::ios::in);
  269. char line[4096] = { 0 };
  270. char lineOther[4096] = { 0 };
  271. while (fin.getline(line, sizeof(line)))
  272. {
  273. if (!finOther.getline(lineOther, sizeof(lineOther)))
  274. {
  275. return -2;
  276. }
  277. std::string strTemp = line,strTempAnother = lineOther;
  278. if (isUtf8((unsigned char*)strTemp.c_str(), strTemp.length()))
  279. UTF8toANSI(strTemp);
  280. //UTF8toANSI(strTempAnother);
  281. if(isUtf8((unsigned char*)strTempAnother.c_str(), strTempAnother.length()))
  282. UTF8toANSI(strTempAnother);
  283. //strTempAnother;
  284. if (strTemp == "" && strTempAnother=="")
  285. continue;
  286. else if (strTemp == "")
  287. {
  288. ret = nHangCount;
  289. return -3;
  290. }
  291. else if (strTempAnother == "")
  292. {
  293. ret = nHangCount;
  294. return -4;
  295. }
  296. cv::Size WordsWith = GetTextSize(strTemp.c_str(), font_size, font_family);
  297. if (WordsWith.width < maxTwoWith)
  298. {
  299. auto test2 = make_tuple(strTemp, strTempAnother);
  300. twoList.push_back(test2);
  301. }
  302. else if (WordsWith.width < maxOneWith)
  303. {
  304. auto test2 = make_tuple(strTemp, strTempAnother);
  305. oneList.push_back(test2);
  306. }
  307. else
  308. {
  309. auto test2 = make_tuple(strTemp, strTempAnother);
  310. threeList.push_back(test2);
  311. }
  312. memset(line, 0, 4096);
  313. memset(lineOther, 0, 4096);
  314. nHangCount++;
  315. }
  316. // 源文件结束,查看答案文件是否结束
  317. if (finOther.getline(lineOther, sizeof(lineOther)))
  318. {
  319. return -2;
  320. }
  321. }
  322. return cout;
  323. }