Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include<fstream>
  6. #include<cstdlib>
  7. #include<ctime>
  8. #include<dirent.h>
  9. using namespace std;
  10. vector<string> dir_list;
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. #define all(a) a.begin(),a.end()
  18. #define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
  19. #define pSS pair <string, string>
  20.  
  21. struct MyData {
  22. string Sentence, Level, Valid;
  23. MyData() {}
  24. MyData(string a, string b, string c) {
  25. Sentence = a;
  26. Level = b;
  27. Valid = c;
  28. }
  29. };
  30.  
  31. void Modify(string &s) {
  32. string nw;
  33. int l, r;
  34. l = 0;
  35. r = s.size() - 1;
  36. for(int i = 0; i < s.size(); i++) {
  37. l = i;
  38. if(s[i] != '\n' && s[i] != ' ') break;
  39. }
  40. for(int i = r; i >= 0; i--) {
  41. r = i;
  42. if(s[i] != '\n' && s[i] != ' ') break;
  43. }
  44. for(int i = l; i <= r; i++) nw += s[i];
  45. s = nw;
  46. }
  47.  
  48. void Writing() {
  49. vector <MyData> Data;
  50. int sz;
  51. srand(time(0));
  52. map <string, vector <pSS> > Mp;
  53. // ifstream input("D:\\Input\\original.txt");
  54. ifstream input("output.csv");
  55. if(!input.is_open()) {
  56. cerr << "ERROR : File can't be Opened\n";
  57. exit(0);
  58. }
  59. string Sentence;
  60. string Level;
  61. string Valid;
  62. while(input.good()) {
  63. getline(input, Sentence, ',');
  64. getline(input, Level, ',');
  65. getline(input, Valid, ',');
  66. string tp;
  67. Modify(Sentence);
  68. Modify(Level);
  69. Modify(Valid);
  70. // cerr << "Data : " << Sentence << '\n';
  71. // cerr << "Level : " << Level << '\n';
  72. // cerr << "Valid : " << Valid << '\n';
  73. // cerr << "----------------------------\n";
  74. Mp[Sentence].push_back(make_pair(Level, Valid));
  75. Data.push_back(MyData(Sentence, Level,Valid));
  76. }
  77. double fivePercent = Data.size() / 100.0;
  78. int First85 = fivePercent * 85;
  79. int First95 = ceil(fivePercent * 95);
  80. sz = Data.size();
  81. vector <MyData> Front, Mid, Last;
  82. ofstream outFile;
  83. outFile.open("First85.csv");
  84. random_shuffle(all(Data));
  85. for(int i = 0; i < First85; i++) {
  86. outFile << Data[i].Sentence << "," << Data[i].Level << "," << Data[i].Valid << '\n';
  87. // cerr << Data[i].Sentence << ' ' << Data[i].Level << " " << Data[i].Valid << "\n";
  88. }
  89. outFile.close();
  90.  
  91. outFile.open("Mid10.csv");
  92. vector <string> Temp;
  93. for(int i = First85; i < First95; i++) {
  94. Temp.push_back(Data[i].Sentence);
  95. } Unique(Temp);
  96.  
  97. for(int i = 0; i < Temp.size(); i++) {
  98. vector < pSS > &OtherStuff = Mp[Temp[i]];
  99. vector <string> str;
  100. string org = "-1";
  101. for(int j = 0; j < OtherStuff.size(); j++) {
  102. // cout << Temp[i] << " : (" << OtherStuff[i].first << ") : (" << OtherStuff[i].second << ")" << "\n";
  103. if(OtherStuff[j].second == "1") {
  104. org = OtherStuff[j].first;
  105. }
  106. else str.push_back(OtherStuff[j].first);
  107. }
  108. // cout << Temp[i] << " " << org << '\n';
  109. outFile << Temp[i] << "," << org;
  110. for(int j = 0; j < str.size(); j++) outFile << "," << str[j];
  111. outFile << "\n";
  112. }
  113.  
  114. outFile.close();
  115. outFile.open("Last5.csv");
  116. Temp.clear();
  117. for(int i = First95; i < Data.size(); i++) {
  118. Temp.push_back(Data[i].Sentence);
  119. } Unique(Temp);
  120.  
  121. for(int i = 0; i < Temp.size(); i++) {
  122. vector < pSS > &OtherStuff = Mp[Temp[i]];
  123. vector <string> str;
  124. string org;
  125. for(int j = 0; j < OtherStuff.size(); j++) {
  126. if(OtherStuff[j].second == "1") org = OtherStuff[j].first;
  127. else str.push_back(OtherStuff[j].first);
  128. }
  129. outFile << Temp[i] << "," << org;
  130. for(int j = 0; j < str.size(); j++) outFile << "," << str[j];
  131. outFile << "\n";
  132. }
  133. outFile.close();
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. void func(char *ss){
  143. ofstream myfile;
  144. myfile.open("dir.txt");
  145. DIR *dir;
  146. string create;
  147. for(int i=0;ss[i];i++)create+=ss[i];
  148. //ss+="\\" ;
  149. struct dirent *ent;
  150. if ((dir = opendir (ss)) != NULL) {
  151. /* print all the files and directories within directory */
  152. while ((ent = readdir (dir)) != NULL) {
  153. //printf ("%s\n", ent->d_name);
  154. myfile<<ss<<"\\"<<(ent->d_name)<<endl;
  155.  
  156. }
  157. closedir (dir);
  158. }
  159. else {
  160. /* could not open directory */
  161. perror ("");
  162. return ;
  163. }
  164.  
  165.  
  166. }
  167.  
  168. struct data{
  169. string line;
  170. int file;
  171. int k;
  172.  
  173. };
  174. vector<data> input,output;
  175. void Task1(){
  176. ifstream infile;
  177. ofstream myfile;
  178. string s;
  179. data temp,temp2;
  180. infile.open("dir.txt");
  181. myfile.open("output.csv");
  182. if(!infile){
  183. cout<<"Unable to open dir file"<<endl;
  184. return ;
  185. }
  186.  
  187. while(getline(infile,s)){
  188. dir_list.push_back(s);
  189. }
  190. infile.close();
  191. for(int i=0;i<dir_list.size();i++){
  192. s=dir_list[i];
  193. infile.open(s);
  194. if(!infile){
  195. cout<<"Unable to open file : "<<s<<endl;
  196. continue;
  197. }
  198. while(getline(infile,s)){
  199. temp.line=s;
  200. temp.file=i+1;
  201. temp.k=0;
  202. input.push_back(temp);
  203. }
  204. infile.close();
  205. }
  206. int len=dir_list.size();
  207. srand((unsigned)time(0));
  208. for(int i=0;i<input.size();i++){
  209. temp=input[i];
  210. for(int j=0;j<9;j++){
  211. int r=temp.file;
  212. while(r==temp.file){
  213. r=rand();
  214. r=(r%len)+1;
  215. }
  216. temp2=temp;
  217. temp2.file=r;
  218. output.push_back(temp2);
  219. }
  220. temp.k=1;
  221. output.push_back(temp);
  222. }
  223. for(int i=0;i<output.size();i++){
  224. int r=rand();
  225. r%=(output.size());
  226. swap(output[i],output[r]);
  227. }
  228. for(int i=0;i<output.size();i++){
  229. if(i>0) myfile<<", ";
  230. myfile<<output[i].line<<", "<<output[i].file<<", "<<output[i].k;
  231. }
  232. myfile.close();
  233. return ;
  234.  
  235. }
  236.  
  237.  
  238. int main(){
  239. char dir[100];
  240. gets(dir);
  241. func(dir);
  242. Task1();
  243. Writing();
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement