AssignWords.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. int AssignWordsFromTest(std::string pathName, std::vector<tuple< string, string>> &oneList, std::vector<tuple< string, string>> &twoList,
  180. std::vector<tuple< string, string>> &threeList, std::vector<tuple< string, string>> &fourList, int &ret)
  181. {
  182. int nHangCount = 1;
  183. // 词性处理
  184. HMODULE module = GetModuleHandle(0);
  185. TCHAR pFileName[MAX_PATH + 2] = { 0 };
  186. GetModuleFileName(module, pFileName, MAX_PATH);
  187. CString csFullPath(pFileName);
  188. int nPos = csFullPath.ReverseFind(_T('\\'));
  189. CString filepath = csFullPath.Left(nPos);
  190. CString FilePath1 = filepath + L"\\config.ini";
  191. //CString FilePath2 = filepath + L"\\words.txt";
  192. WCHAR wflagWord[10],wFourColScaleParam[10],wThreeColScaleParam[10],wTwoColScaleParam[10];
  193. GetPrivateProfileString(L"USER", L"flagWord", L"1", wflagWord, 10, FilePath1);
  194. GetPrivateProfileString(L"USER", L"TwoColScaleParam", L"6", wTwoColScaleParam, 10, FilePath1);
  195. GetPrivateProfileString(L"USER", L"ThreeColScaleParam", L"9", wThreeColScaleParam, 10, FilePath1);
  196. GetPrivateProfileString(L"USER", L"FourColScaleParam", L"16", wFourColScaleParam, 10, FilePath1);
  197. int nFourColScaleParam = 0, nThreeColScaleParam = 0, nTwoColScaleParam = 0;
  198. nFourColScaleParam = _wtoi(wFourColScaleParam);
  199. nFourColScaleParam = nFourColScaleParam > 0 ? nFourColScaleParam : 16;
  200. nThreeColScaleParam = _wtoi(wThreeColScaleParam);
  201. nThreeColScaleParam = nThreeColScaleParam > 0 ? nThreeColScaleParam : 9;
  202. nTwoColScaleParam = _wtoi(wTwoColScaleParam);
  203. nTwoColScaleParam = nTwoColScaleParam > 0 ? nTwoColScaleParam : 6;
  204. CString lpszflagWord(wflagWord);
  205. int cout = 0;
  206. if (_access(pathName.c_str(), 00) != -1)
  207. {
  208. std::string otherPathName;
  209. otherPathName = pathName;
  210. size_t lenthOtherName = otherPathName.length();
  211. otherPathName.replace(lenthOtherName - 4, lenthOtherName, "-other.txt");
  212. if (_access(otherPathName.c_str(), 00) == -1)
  213. {
  214. return -1;
  215. }
  216. int maxOneWith = main_wdith;
  217. int maxTwoWith = main_wdith / 2 - main_wdith / nTwoColScaleParam - chk_width;
  218. int maxTreeWith = main_wdith / 3 - main_wdith / nThreeColScaleParam - chk_width;
  219. int maxFourWith = main_wdith / 4 - main_wdith/ nFourColScaleParam - chk_width;
  220. std::ifstream fin(pathName.c_str(), std::ios::in);
  221. std::ifstream finOther(otherPathName.c_str(), std::ios::in);
  222. char line[4096] = { 0 };
  223. char lineOther[4096] = { 0 };
  224. while (fin.getline(line, sizeof(line)))
  225. {
  226. if (!finOther.getline(lineOther, sizeof(lineOther)))
  227. {
  228. return -2;
  229. }
  230. std::string strTemp = line,strTempAnother = lineOther;
  231. UTF8toANSI(strTemp);
  232. UTF8toANSI(strTempAnother);
  233. if (strTemp == "" && strTempAnother=="")
  234. continue;
  235. else if (strTemp == "")
  236. {
  237. ret = nHangCount;
  238. return -3;
  239. }
  240. else if (strTempAnother == "")
  241. {
  242. ret = nHangCount;
  243. return -4;
  244. }
  245. cv::Size WordsWith = GetTextSize(strTemp.c_str(), font_size, font_family);
  246. if (WordsWith.width < maxTwoWith)
  247. {
  248. auto test2 = make_tuple(strTemp, strTempAnother);
  249. twoList.push_back(test2);
  250. }
  251. else if (WordsWith.width < maxOneWith)
  252. {
  253. auto test2 = make_tuple(strTemp, strTempAnother);
  254. oneList.push_back(test2);
  255. }
  256. else
  257. {
  258. auto test2 = make_tuple(strTemp, strTempAnother);
  259. threeList.push_back(test2);
  260. }
  261. memset(line, 0, 4096);
  262. memset(lineOther, 0, 4096);
  263. nHangCount++;
  264. }
  265. // 源文件结束,查看答案文件是否结束
  266. if (finOther.getline(lineOther, sizeof(lineOther)))
  267. {
  268. return -2;
  269. }
  270. }
  271. return cout;
  272. }