AssignWords.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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<std::string> &twoList, std::vector<std::string> &threeList, std::vector<std::string> &fourList)
  180. {
  181. // 词性处理
  182. HMODULE module = GetModuleHandle(0);
  183. TCHAR pFileName[MAX_PATH + 2] = { 0 };
  184. GetModuleFileName(module, pFileName, MAX_PATH);
  185. CString csFullPath(pFileName);
  186. int nPos = csFullPath.ReverseFind(_T('\\'));
  187. CString filepath = csFullPath.Left(nPos);
  188. CString FilePath1 = filepath + L"\\config.ini";
  189. CString FilePath2 = filepath + L"\\words.txt";
  190. WCHAR wflagWord[10],wFourColScaleParam[10],wThreeColScaleParam[10],wTwoColScaleParam[10];
  191. GetPrivateProfileString(L"USER", L"flagWord", L"1", wflagWord, 10, FilePath1);
  192. GetPrivateProfileString(L"USER", L"TwoColScaleParam", L"6", wTwoColScaleParam, 10, FilePath1);
  193. GetPrivateProfileString(L"USER", L"ThreeColScaleParam", L"9", wThreeColScaleParam, 10, FilePath1);
  194. GetPrivateProfileString(L"USER", L"FourColScaleParam", L"16", wFourColScaleParam, 10, FilePath1);
  195. int nFourColScaleParam = 0, nThreeColScaleParam = 0, nTwoColScaleParam = 0;
  196. nFourColScaleParam = _wtoi(wFourColScaleParam);
  197. nFourColScaleParam = nFourColScaleParam > 0 ? nFourColScaleParam : 16;
  198. nThreeColScaleParam = _wtoi(wThreeColScaleParam);
  199. nThreeColScaleParam = nThreeColScaleParam > 0 ? nThreeColScaleParam : 9;
  200. nTwoColScaleParam = _wtoi(wTwoColScaleParam);
  201. nTwoColScaleParam = nTwoColScaleParam > 0 ? nTwoColScaleParam : 6;
  202. CString lpszflagWord(wflagWord);
  203. std::vector<std::string> WordList;
  204. WordList.push_back("vt.");
  205. WordList.push_back("vi.");
  206. WordList.push_back("n.");
  207. WordList.push_back("pron.");
  208. WordList.push_back("adj.");
  209. WordList.push_back("num.");
  210. WordList.push_back("v.");
  211. WordList.push_back("adv.");
  212. WordList.push_back("art.");
  213. WordList.push_back("prep.");
  214. WordList.push_back("conj.");
  215. WordList.push_back("int.");
  216. // 随机替换规则
  217. std::vector<std::string> rePlaceList;
  218. std::string strFilePath2 = CT2A(FilePath2);
  219. if (_access(strFilePath2.c_str(), 00) != -1)
  220. {
  221. char line[100] = { 0 };
  222. std::ifstream finfile(strFilePath2.c_str(), std::ios::in);
  223. while (finfile.getline(line, sizeof(line)))
  224. {
  225. std::vector<std::string> retlist;
  226. std::string strTemp = line;
  227. UTF8toANSI(strTemp);
  228. split(strTemp, "@", &retlist);
  229. if (retlist.size() == 2)
  230. {
  231. int x = atoi(retlist[1].c_str());
  232. if (x > 0)
  233. {
  234. for (int i = 0; i < x; i++)
  235. {
  236. rePlaceList.push_back(retlist[0]);
  237. }
  238. }
  239. }
  240. }
  241. memset(line, 0, 100);
  242. }
  243. int cout = 0;
  244. if (_access(pathName.c_str(), 00) != -1)
  245. {
  246. int maxTwoWith = main_wdith / 2 - main_wdith / nTwoColScaleParam - chk_width;
  247. int maxTreeWith = main_wdith / 3 - main_wdith / nThreeColScaleParam - chk_width;
  248. int maxFourWith = main_wdith / 4 - main_wdith/ nFourColScaleParam - chk_width;
  249. std::ifstream fin(pathName.c_str(), std::ios::in);
  250. char line[4096] = { 0 };
  251. while (fin.getline(line, sizeof(line)))
  252. {
  253. std::string strTemp = line;
  254. UTF8toANSI(strTemp);
  255. if (lpszflagWord == "0")
  256. {
  257. ReplaceFlagWord(WordList, strTemp);
  258. }
  259. if (strTemp == "")
  260. continue;
  261. cv::Size WordsWith = GetTextSize(strTemp.c_str(), font_size, font_family);
  262. if (WordsWith.width < maxFourWith)
  263. {
  264. if (rePlaceList.size() > 0)
  265. {
  266. RandReplaceStr(rePlaceList, strTemp);
  267. }
  268. fourList.push_back(strTemp);
  269. }
  270. else if (WordsWith.width < maxTreeWith)
  271. {
  272. if (rePlaceList.size() > 0)
  273. {
  274. RandReplaceStr(rePlaceList, strTemp);
  275. }
  276. threeList.push_back(strTemp);
  277. }
  278. else if (WordsWith.width < maxTwoWith)
  279. {
  280. if (rePlaceList.size() > 0)
  281. {
  282. RandReplaceStr(rePlaceList, strTemp);
  283. }
  284. twoList.push_back(strTemp);
  285. }
  286. else
  287. {
  288. char left[2048] = { 0 }, right[2048] = { 0 };
  289. DivideEquallyStr(strTemp.c_str(), strTemp.length(), left,right);
  290. AssignWordsFromString(left, maxTwoWith, maxTreeWith, maxFourWith, rePlaceList, twoList, threeList, fourList);
  291. AssignWordsFromString(right, maxTwoWith, maxTreeWith, maxFourWith, rePlaceList, twoList, threeList, fourList);
  292. }
  293. memset(line, 0, 4096);
  294. }
  295. }
  296. return cout;
  297. }