-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews_Sort.cpp
169 lines (169 loc) · 4.41 KB
/
News_Sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
#include<sys/stat.h>
#include<map>
#include<direct.h>
#include<windows.h>
#include<io.h>
#include<fstream>
#include<cstdlib>
using namespace std;
const string m=".\\Model";
map<string,int> num;
map<string,map<string,int>> book;
ifstream FI;
ofstream FO;
void Print_Sort(){
string file=m+"\\sort";
FO.open(file,ios::out);
for(map<string,int>::iterator iter=num.begin();iter!=num.end();iter++){
FO<<iter->first<<" "<<iter->second<<endl;
}
FO.close();
}
void Print_Single(string file,string $){
FO.open(file,ios::out);
for(map<string,int>::iterator iter=book[$].begin();iter!=book[$].end();iter++){
FO<<iter->first<<" "<<iter->second<<endl;
}
FO.close();
}
void Get_Output_Folder(string folder){
if (_access(folder.c_str(),0)==-1) _mkdir(folder.c_str());
}
inline bool Exists_Test(const string& name) {
struct stat test;
return (stat(name.c_str(),&test)==0);
}
struct Word{
string s;
int ti;
const bool operator <(const Word &b){
return s<b.s;
}
};
inline void File_Create(string file){
FO.open(file,ios::out);
FO.close();
}
void Previous_Read(string $){//Model data to book[$]
string file=m+"\\"+$+".dat";
if(!Exists_Test(file)) File_Create(file);
FI.open(file,ios::in);
string w;
int tt;
while(FI>>w>>tt) book[$][w]=tt;
FI.close();
}
string rd[100000];int siz;
void File_To_rd(string file){//Data file to rd string
string Command="python -u Divide.py "+file+" temp";
system(Command.c_str());
FI.open("temp",ios::in);
siz=1;
while(FI>>rd[siz]) siz++;
siz--;
FI.close();
sort(rd+1,rd+1+siz);
siz=unique(rd+1,rd+1+siz)-rd-1;
remove(".\\temp");
}
void Update_Single_File(string file,string $){
File_To_rd(file);
num[$]++;
for(int i=1;i<=siz;i++) book[$][rd[i]]++;
}
void getFileNames(string path, vector<string>& files)
{
string command="python -u GetFileName.py "+path;
system(command.c_str());
string r;
FI.open("temp",ios::in);
while(FI>>r) files.push_back(path+"\\"+r);
FI.close();
remove(".\\temp");
}
void Update_Model(){
puts("Enter the folder name.");
string Folder;
cin>>Folder;
puts("Enter the folder sort.");
string $;//A little joke.
cin>>$;
Previous_Read($);
vector<string> names;
getFileNames(Folder,names);
for(const auto &ph:names) Update_Single_File(ph,$);
Print_Sort();
Print_Single(m+"\\"+$+".dat",$);
}
void Begin(){
Get_Output_Folder(m);
string file=m+"\\sort";
if(!Exists_Test(file)) File_Create(file);
FI.open(file,ios::in);
string q;int e;
while(FI>>q>>e) num[q]=e;
FI.close();
}
void Check_Single(string file,bool flag){//true no i/o
File_To_rd(file);
map<string,long double> p;
for(map<string,int>::iterator iter=num.begin();iter!=num.end();iter++) p[iter->first]=1.0/num.size();
for(int i=1;i<=siz;i++){
double B=0.0;
for(map<string,int>::iterator iter=num.begin();iter!=num.end();iter++){
B+=(p[iter->first]*=(long double)(book[iter->first][rd[i]]+1)/(iter->second+2));
}
for(map<string,int>::iterator iter=num.begin();iter!=num.end();iter++){
p[iter->first]/=B;
}
}
map<string,int>::iterator ans=num.begin();
for(map<string,int>::iterator iter=num.begin();iter!=num.end();iter++){
if(p[iter->first]>p[ans->first]) ans=iter;
}
if(flag) cout<<ans->first<<" "<<p[ans->first]<<endl;
else FO<<ans->first<<endl;
}
void Load_Model(){
for(map<string,int>::iterator iter=num.begin();iter!=num.end();iter++){
Previous_Read(iter->first);
}
puts("Enter 'S' to check one file or 'M' to check the whole folder.");
char ch;
cin>>ch;
if(ch=='S'){
puts("Enter the file name.");
string A;
cin>>A;
Check_Single(A,1);
}
if(ch=='M'){
puts("Enter the folder name");
string f;
cin>>f;
vector<string> v;
getFileNames(f,v);
puts("Enter the result file name.");
string res;
cin>>res;
FO.open(res,ios::out);
for(const auto &ph:v) Check_Single(ph,0);
FO.close();
}
}
int main()
{
Begin();
puts("Enter 'U' to update model or 'L' to load model");
char ch;
cin>>ch;
if(ch=='U') Update_Model();
if(ch=='L') Load_Model();
return 0;
}