123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #include "pch.h"
- #include "AssignWords.h"
- #include "CvxText.h"
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <io.h>
- #include "BaseUtility.h"
- /*
- string& replace_str(string& str, const string& to_replaced, const string& newchars)
- {
- for (string::size_type pos(0); pos != string::npos; pos += newchars.length())
- {
- pos = str.find(to_replaced, pos);
- if (pos != string::npos)
- str.replace(pos, to_replaced.length(), newchars);
- else
- break;
- }
- return str;
- }
- */
- void split(const std::string& s, const std::string& delim, std::vector< std::string >* ret)
- {
- size_t last = 0;
- size_t index = s.find_first_of(delim, last);
- while (index != std::string::npos)
- {
- ret->push_back(s.substr(last, index - last));
- last = index + 1;
- index = s.find_first_of(delim, last);
- }
- if (index - last > 0)
- {
- ret->push_back(s.substr(last, index - last));
- }
- }
- void deleteFlagWord(std::string& str, const std::string& to_replaced)
- {
- std::string::size_type pos = str.find(to_replaced, 0);
- if (pos == 0)
- str.replace(pos, to_replaced.length(), "");
- }
- void ReplaceFlagWord(std::vector<std::string> &list, std::string &strRet)
- {
- for (std::vector<std::string>::iterator it = list.begin(); it != list.end(); it++)
- {
- deleteFlagWord(strRet, (*it));
- }
- }
- void DivideEquallyStr(const char *buff, int len, char left[], char right[])
- {
- std::string strtemp = buff;
- std::size_t keyindex1 = std::string::npos, keyindex2 = std::string::npos, keyindex;
- int i = 0;
- for (i = 0; i < len / 2; i++)
- {
- if (!(buff[i] < 0x80 && buff[i] >= 0x0))
- {
- i++;
- }
- }
- strncpy(left, buff, i);
- strncpy(right, buff + i, len - i);
- return;
- /* 直接中间分开,不用特殊处理*/
- std::string strKey1 = ";", strKey2 = " ";
- std::size_t found = strtemp.find(strKey1);
- bool flag1 = false, flag2 = false;
- while (found != std::string::npos && found < strtemp.length() / 2)
- {
- keyindex1 = found;
- found = strtemp.find(strKey1, found + 1);
- flag1 = true;
- }
- int w = 1;
- found = strtemp.find(strKey2);
- while (found != std::string::npos && found < strtemp.length() / 2)
- {
- keyindex2 = found;
- found = strtemp.find(strKey2, found + 1);
- flag2 = true;
- }
- if (flag1 && flag2)
- {
- if (keyindex1 > keyindex2)
- {
- keyindex = keyindex1;
- w = 2;
- }
- else
- {
- keyindex = keyindex2;
- }
- }
- else if (flag1)
- {
- keyindex = keyindex1;
- w = 2;
- }
- else if (flag2)
- {
- keyindex = keyindex2;
- }
- else
- {
- int i = 0;
- for (i = 0; i < len / 2; i++)
- {
- if (!(buff[i] < 0x80 && buff[i] >= 0x0))
- {
- i++;
- }
- }
- strncpy(left, buff, i);
- strncpy(right, buff + i, len - i);
- return;
- }
- std::string ttm = strtemp.substr(0, keyindex + w);
- std::string ttm2 = strtemp.substr(keyindex + w, strtemp.size());
- strncpy(left, ttm.c_str(), ttm.length());
- strncpy(right, ttm2.c_str(), ttm2.length());
- }
- void RandReplaceStr(std::vector<std::string> list, std::string &ret)
- {
- std::string to_replaced = ";";
- srand((unsigned)time(NULL));
- std::string newchars;
- int index = rand() % list.size();
- newchars = list[index];
- for (string::size_type pos(0); pos != string::npos; )
- {
- pos = ret.find(to_replaced, pos);
- if (pos != string::npos)
- ret.replace(pos, to_replaced.length(), newchars);
- else
- break;
- pos += newchars.length();
- index = rand() % list.size();
- newchars = list[index];
- }
- }
- void AssignWordsFromString(std::string strContent, int maxTwoWith, int maxTreeWith, int maxFourWith, std::vector<std::string> &rePlaceList,
- std::vector<std::string> &twoList, std::vector<std::string> &threeList, std::vector<std::string> &fourList)
- {
- cv::Size WordsWith = GetTextSize(strContent.c_str(), font_size, font_family);
- if (WordsWith.width < maxFourWith)
- {
- if (rePlaceList.size() > 0)
- {
- RandReplaceStr(rePlaceList, strContent);
- }
- fourList.push_back(strContent);
- }
- else if (WordsWith.width < maxTreeWith)
- {
- if (rePlaceList.size() > 0)
- {
- RandReplaceStr(rePlaceList, strContent);
- }
- threeList.push_back(strContent);
- }
- else if (WordsWith.width < maxTwoWith)
- {
- if (rePlaceList.size() > 0)
- {
- RandReplaceStr(rePlaceList, strContent);
- }
- twoList.push_back(strContent);
- }
- else
- {
- char left[2048] = { 0 }, right[2048] = { 0 };
- DivideEquallyStr(strContent.c_str(), strContent.length(), left, right);
- AssignWordsFromString(left, maxTwoWith, maxTreeWith, maxFourWith, rePlaceList, twoList, threeList, fourList);
- AssignWordsFromString(right, maxTwoWith, maxTreeWith, maxFourWith, rePlaceList, twoList, threeList, fourList);
- }
- }
- int AssignWordsFromTest(std::string pathName, std::vector<tuple< string, string>> &oneList, std::vector<tuple< string, string>> &twoList,
- std::vector<tuple< string, string>> &threeList, std::vector<tuple< string, string>> &fourList, int &ret)
- {
- int nHangCount = 1;
- // 词性处理
- HMODULE module = GetModuleHandle(0);
- TCHAR pFileName[MAX_PATH + 2] = { 0 };
- GetModuleFileName(module, pFileName, MAX_PATH);
- CString csFullPath(pFileName);
- int nPos = csFullPath.ReverseFind(_T('\\'));
- CString filepath = csFullPath.Left(nPos);
- CString FilePath1 = filepath + L"\\config.ini";
- //CString FilePath2 = filepath + L"\\words.txt";
- WCHAR wflagWord[10],wFourColScaleParam[10],wThreeColScaleParam[10],wTwoColScaleParam[10];
- GetPrivateProfileString(L"USER", L"flagWord", L"1", wflagWord, 10, FilePath1);
- GetPrivateProfileString(L"USER", L"TwoColScaleParam", L"6", wTwoColScaleParam, 10, FilePath1);
- GetPrivateProfileString(L"USER", L"ThreeColScaleParam", L"9", wThreeColScaleParam, 10, FilePath1);
- GetPrivateProfileString(L"USER", L"FourColScaleParam", L"16", wFourColScaleParam, 10, FilePath1);
- int nFourColScaleParam = 0, nThreeColScaleParam = 0, nTwoColScaleParam = 0;
- nFourColScaleParam = _wtoi(wFourColScaleParam);
- nFourColScaleParam = nFourColScaleParam > 0 ? nFourColScaleParam : 16;
- nThreeColScaleParam = _wtoi(wThreeColScaleParam);
- nThreeColScaleParam = nThreeColScaleParam > 0 ? nThreeColScaleParam : 9;
- nTwoColScaleParam = _wtoi(wTwoColScaleParam);
- nTwoColScaleParam = nTwoColScaleParam > 0 ? nTwoColScaleParam : 6;
- CString lpszflagWord(wflagWord);
-
- int cout = 0;
- if (_access(pathName.c_str(), 00) != -1)
- {
- std::string otherPathName;
- otherPathName = pathName;
- size_t lenthOtherName = otherPathName.length();
- otherPathName.replace(lenthOtherName - 4, lenthOtherName, "-other.txt");
- if (_access(otherPathName.c_str(), 00) == -1)
- {
- return -1;
- }
- int maxOneWith = main_wdith;
- int maxTwoWith = main_wdith / 2 - main_wdith / nTwoColScaleParam - chk_width;
- int maxTreeWith = main_wdith / 3 - main_wdith / nThreeColScaleParam - chk_width;
- int maxFourWith = main_wdith / 4 - main_wdith/ nFourColScaleParam - chk_width;
- std::ifstream fin(pathName.c_str(), std::ios::in);
- std::ifstream finOther(otherPathName.c_str(), std::ios::in);
- char line[4096] = { 0 };
- char lineOther[4096] = { 0 };
- while (fin.getline(line, sizeof(line)))
- {
- if (!finOther.getline(lineOther, sizeof(lineOther)))
- {
- return -2;
- }
-
- std::string strTemp = line,strTempAnother = lineOther;
- UTF8toANSI(strTemp);
- UTF8toANSI(strTempAnother);
- if (strTemp == "" && strTempAnother=="")
- continue;
- else if (strTemp == "")
- {
- ret = nHangCount;
- return -3;
- }
- else if (strTempAnother == "")
- {
- ret = nHangCount;
- return -4;
- }
-
- cv::Size WordsWith = GetTextSize(strTemp.c_str(), font_size, font_family);
- if (WordsWith.width < maxTwoWith)
- {
- auto test2 = make_tuple(strTemp, strTempAnother);
- twoList.push_back(test2);
- }
- else if (WordsWith.width < maxOneWith)
- {
- auto test2 = make_tuple(strTemp, strTempAnother);
- oneList.push_back(test2);
- }
- else
- {
- auto test2 = make_tuple(strTemp, strTempAnother);
- threeList.push_back(test2);
- }
- memset(line, 0, 4096);
- memset(lineOther, 0, 4096);
- nHangCount++;
- }
- // 源文件结束,查看答案文件是否结束
- if (finOther.getline(lineOther, sizeof(lineOther)))
- {
- return -2;
- }
-
- }
- return cout;
- }
|