-
Notifications
You must be signed in to change notification settings - Fork 13
/
MM.cpp
188 lines (179 loc) · 4.43 KB
/
MM.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
#include <fstream>
#include <sstream>
#include "matrix.h"
#define VEC_LEN 100000
using namespace std;
struct DICOS
{
string dic[VEC_LEN];
int len;
};
DICOS dicos;
void createVocabList(dataToMatrix dtm)
{
int i,j,k,vl;
int dic_len=0;
Data *p;
p=dtm.dataSet->next;
for(i=0; i<dtm.col; i++)
{
for(j=0; p->attr_string[j]!="#"; j++)
{
for(vl=0; vl<dic_len; vl++)
{
if(!p->attr_string[j].compare(dicos.dic[vl]))
break;
}
if(vl==dic_len)
{
dicos.dic[vl]=p->attr_string[j];
dic_len++;
}
}
p=p->next;
}
dicos.len=dic_len;
cout<<"vec_len="<<dic_len<<endl;
}
int Bmm(dataToMatrix testDtm)
{
int i,j,k;
Data *p;
int st;
int len;
int num;
bool flag;
string tmp;
p=testDtm.dataSet->next;
for(i=0; testDtm.col&&p!=NULL; i++)
{
len=p->attr_string[0].length();
st=len;
flag=false;
while(st!=0&&!flag)
{
flag=true;
for(num=st>10?10:st; num>0&&flag; num-=1)
{
for(k=0; k<dicos.len&&flag; k++)
{
if(!p->attr_string[0].substr(st-num,num).compare(dicos.dic[k]))
{
tmp.append(p->attr_string[0].substr(st-num,num));
//cout<<p->attr_string[0].substr(st-num,num)<<'/';
tmp.append(1,'/');
st=st-num;
flag=false;
}
}
}
if(st>10&&flag)
{
tmp.append(p->attr_string[0].substr(st-2,2));
tmp.append(1,'/');
//cout<<cout<<p->attr_string[0].substr(st-1,2)<<'/';
st-=2;
flag=false;
}
}
j=tmp.size();
for(i=tmp.size()-1; i>0; i--)
{
if(tmp[i]=='/')
{
cout<<tmp.substr(i+1,j-i);
j=i;
}
}
cout<<tmp.substr(0,j)<<endl;
tmp="";
p=p->next;
}
}
int Fmm(dataToMatrix testDtm)
{
int i,j,k;
Data *p;
int st;
int len;
int num;
bool flag;
string tmp;
p=testDtm.dataSet->next;
for(i=0; testDtm.col&&p!=NULL; i++)
{
st=0;
len=p->attr_string[0].length();
flag=false;
while(st!=len&&!flag)
{
flag=true;
for(num=len-st>10?10:len-st; num>0&&flag; num-=2)
{
for(k=0; k<dicos.len&&flag; k++)
{
if(!p->attr_string[0].substr(st,num).compare(dicos.dic[k]))
{
tmp.append(p->attr_string[0].substr(st,num));
tmp.append("/");
cout<<p->attr_string[0].substr(st,num)<<'/';
st=st+num;
flag=false;
}
}
}
if(len-st>10&&flag)
//if()出现为登入词可以采用字构词,即先把其按字划分,后采用字构词
{
tmp.append(p->attr_string[0].substr(st,2));
tmp.append(1,'/');
cout<<p->attr_string[0].substr(st,2)<<'/';
st+=2;
flag=false;
}
}
//cout<<endl;
//cout<<tmp<<endl;
tmp="";
p=p->next;
cout<<endl;
}
}
int main()
{
int k,i,j;
cout<<"loadData"<<endl;
char file[20]="data\\dicfile.txt";
ifstream infile;
infile.open(file,ios::in);
string tmpstrline;
j=0;
//cout<<"--------------------"<<endl;
while(!infile.eof())
{
getline(infile,tmpstrline,'\n');//读取文件中一行的数据,保存为string类型
stringstream input(tmpstrline);
if(tmpstrline!="\0")////由于读取文件结束符同样会继续该操作
{
while(input>>dicos.dic[j])
{
j++;
}
}
}
dicos.len=j;
infile.close();
//cout<<"load dic ok"<<endl;
dataToMatrix testDtm;
//cout<<"loadData"<<endl;
//cout<<"----------------------"<<endl;
testDtm.loadData(&testDtm,"data\\stest.txt");
//cout<<"load test ok"<<endl;
testDtm.print(testDtm);
Fmm(testDtm);
Bmm(testDtm);
}