-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchEngine.cpp
More file actions
144 lines (125 loc) · 3.68 KB
/
SearchEngine.cpp
File metadata and controls
144 lines (125 loc) · 3.68 KB
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
// SearchEngine.cpp: implementation of the CSearchEngine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ARS.h"
#include "ARSDocument.h"
#include "KeyWord.h"
#include "IndexTermsInDoc.h"
#include "DBManpulation.h"
#include "CommonWord.h"
#include "Indexer.h"
#include "SVD.h"
#include "SVDDel.h"
#include "Query.h"
#include "SearchEngine.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern CSVD ARSsvd;
extern CQuery ARSquery;
extern CDBManpulation ARSdb;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSearchEngine::CSearchEngine()
{
}
CSearchEngine::~CSearchEngine()
{
}
BOOL CSearchEngine::FindSimilarDoc(ItemList* SimDocList, mwArray& Vk)
{
double fResultSim = 0;
int DocNum,Index = 0;
memset( SimDocList, -1, (ARSdb.MaxDoc+1) * sizeof(ItemList) );
for(DocNum = 1; DocNum <= ARSdb.MaxDoc; DocNum++)
{
fResultSim = CosineMeasure( ARSquery.Query_hh, Vk(DocNum, colon()) );
if( fResultSim >= fSimValue )
{
SimDocList[Index].nItem = DocNum;
SimDocList[Index].fSim = fResultSim;
Index++;
}
}
if(!Index)
AfxMessageBox("áã íÊã ÇáÚ辄 Úáì æËÇÆÞ ÊÔÇÈå ÇáÅÓÊÝÓÇÑ ãÚ äÓÈÉ ÇáÊÔÇÈå ÇáãÍÏÏÉ Ýì ÇáÅÓÊÝÓÇÑ");
//::MessageBox(NULL, "could not find any similar documents","", MB_OK );
else
qsort( (void *)SimDocList, (size_t)Index, sizeof( ItemList ), compare );
///////////////////////////////////////////////////////////////////////////////////
/*
{ // block for testing the output data from every phase
CStdioFile constfile("_sortdoc.txt", CFile::modeCreate | CFile::modeWrite | CFile::typeText);
char strLine[80];
//int CollectionCount = WordCountCollection.GetCount();
for(int i = 0;i<Index;i++)
{
double x = SimDocList[i].fSim;
sprintf(strLine,"%d,%f\n",SimDocList[i].nItem, x);
constfile.WriteString (strLine);
}
constfile.Close();
}
/*///////////////////////////////////////////////////////////////////////*/
return Index;
}
BOOL CSearchEngine::FindSimilarTerm(ItemList* SimTermList, mwArray& Uk)
{
double fResultSim = 0;
int TermNum,Index = 0;
memset( SimTermList, -1, (ARSdb.MaxTerm+1) * sizeof(ItemList) );
for(TermNum = 1; TermNum <= ARSdb.MaxTerm; TermNum++)
{
fResultSim = CosineMeasure( ARSquery.Query_hh, Uk(TermNum, colon()) );
if( fResultSim >= fSimValue )
{
SimTermList[Index].nItem = TermNum;
SimTermList[Index].fSim = fResultSim;
Index++;
}
}
if(!Index)
AfxMessageBox("áã íÊã ÇáÚ辄 Úáì ßáãÇÊ ÊÔÇÈå ÇáÅÓÊÝÓÇÑ ãÚ äÓÈÉ ÇáÊÔÇÈå ÇáãÍÏÏÉ Ýì ÇáÅÓÊÝÓÇÑ");
else
qsort( (void *)SimTermList, (size_t)Index, sizeof( ItemList ), compare );
return Index;
}
void CSearchEngine::SetSimilarity(float SimValue)
{
fSimValue = SimValue;
}
void CSearchEngine::SetSearchLimit(int nLimitValue)
{
nSearchLimit = nLimitValue;
}
double CSearchEngine::GetSimilarity()
{
return fSimValue;
}
int CSearchEngine::GetSearchLimit()
{
return nSearchLimit;
}
double CSearchEngine::CosineMeasure(const mwArray &Q_hh, const mwArray &VU)
{
mwArray res_Arr = ( dot(Q_hh, VU) / (norm(Q_hh) * norm(VU)) );
double *result = mxGetPr(res_Arr.GetData());
return (*result);
}
//////////////////////////////////////////////////////////////////////////////
int compare( const void *arg1, const void *arg2 )
{
ItemList *Item1 = (ItemList*)arg1;
ItemList *Item2 = (ItemList*)arg2;
if( Item1->fSim > Item2->fSim )
return -1;
else
if( Item1->fSim < Item2->fSim )
return 1;
else
return 0;
}