summaryrefslogtreecommitdiff
path: root/plugins/Boltun/src/Engine/WordsList.cpp
blob: 310eafa6432965c16f7f07ef5f47a51617a779ef (plain)
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
//***********************************************************
//	Copyright © 2008 Valentin Pavlyuchenko
//
//	This file is part of Boltun.
//
//    Boltun is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    Boltun is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//	  along with Boltun. If not, see <http://www.gnu.org/licenses/>.
//
//***********************************************************

#include "../stdafx.h"

using namespace std;

WordsList::WordsList(const wstring &data/*, bool allowUnstrict*/)
{
	Parse(data/*, allowUnstrict*/);
}

WordsList::operator wstring() const
{
	wstring res;
	//if (unstrict)
	//	res = L"~";
	set<wstring>::const_iterator it = words.begin();
	if (!words.empty())
		while (true)
		{
			res += *it;
			++it;
			if (it != words.end())
				res += L" ";
			else
				break;
		}
	res += isQuestion ? L"?" : L".";
	return res;
}

WordsList& WordsList::operator= (const wstring& s)
{
	Parse(s);
	return *this;
};

void WordsList::Parse(wstring s/*, bool allowUnstrict*/)
{
	isQuestion = false;
	/*if (allowUnstrict && s.length() && s[0] == '~')
	{
	s = s.substr(1, s.npos);
	unstrict = true;
	}
	else
	unstrict = false;*/
	int len = (int)s.length() - 1;
	while (len != -1 && iswspace(s[len]))
		len--;
	if (len < 0)
		return;
	if (s[len] == '?')
		isQuestion = true;
	else
		if (s[len] != '.')
			return;
	s.resize(len);
	int it = 0;
	while (it != len)
	{
		while (it != len && iswspace(s[it]))
			it++;
		if (it == len)
			break;
		int start = it;
		while (it != len && !iswspace(s[it]))
			it++;
		words.insert(s.substr(start, it - start));
	}
}

bool WordsList::MatchesAll(const vector<wstring>& s/*, bool& WasStrict*/, float& priority) const
{
	std::set<wstring> temp;
	//WasStrict = true;
	for (vector<wstring>::const_iterator it = s.begin(); it != s.end(); ++it)
	{
		/*		if (words.find(*it) == words.end())
					if (unstrict)
					{
					WasStrict = false;
					continue;
					}
					else
					return false;
					temp.insert((*it));*/
		if (words.find(*it) != words.end())
			temp.insert((*it));
	}
	if (temp.size() != words.size())
		return false;
	priority = words.size() * words.size() / (float)s.size();
	return temp.size() == words.size();
}

bool WordsList::MatchesAny(const vector<wstring>& s) const
{
	for (vector<wstring>::const_iterator it = s.begin(); it != s.end(); ++it)
		if (words.find(*it) != words.end())
			return true;
	return false;
}

vector<wstring> WordsList::ConsistsOf(const set<wstring>& list) const
{
	vector<wstring> res;
	for (set<wstring>::const_iterator it = words.begin(); it != words.end(); ++it)
		if (list.find(*it) == list.end())
			res.push_back(*it);
	return res;
}

vector<wstring> WordsList::DoesntIncludeAny(const set<wstring>& list) const
{
	vector<wstring> res;
	for (set<wstring>::const_iterator it = words.begin(); it != words.end(); ++it)
		if (list.find(*it) != list.end())
			res.push_back(*it);
	return res;
}

bool WordsList::operator<(const WordsList& value) const
{
	return (wstring)*this < (wstring)value;
}

bool WordsList::operator!=(const WordsList& value) const
{
	return (wstring)*this != (wstring)value;
}

bool WordsList::operator==(const WordsList& value) const
{
	return (wstring)*this == (wstring)value;
}

size_t WordsList::Size() const
{
	return words.size();
}

bool WordsList::IsQuestion() const
{
	return isQuestion;
}

bool WordsList::IsEmpty() const
{
	return words.size() == 0;
}