-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyLibrary.cpp
More file actions
83 lines (74 loc) · 2.42 KB
/
myLibrary.cpp
File metadata and controls
83 lines (74 loc) · 2.42 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
#include <fstream>
#include <string>
#include <algorithm>
#include "myLibrary.h"
#include <codecvt>
#include <locale>
#include <functional>
#include <cctype>
#include <exception>
#include <iostream>
// function to convert char * to wstring in lowercase
std::wstring strToWstringLower(std::string word)
{
std::wstring wordWString;
try
{
setlocale(LC_ALL, "ru_RU.UTF-8");
//setup converter
using convert_type = std::codecvt_utf8<typename std::wstring::value_type>;
std::wstring_convert<convert_type, typename std::wstring::value_type> converter;
//use converter
wordWString = converter.from_bytes(word);
// transform to lowecase
std::transform(wordWString.begin(), wordWString.end(), wordWString.begin(),
[](wchar_t c)
{
return std::tolower<wchar_t>(c, std::locale("ru_RU.UTF-8"));
}
);
// remove non alpha character in end of word
if (!std::isalpha(*std::prev(wordWString.end()), std::locale("ru_RU.UTF-8")))
{
wordWString.erase(std::prev(wordWString.end()));
}
} catch (const std::exception& e)
{
std::cout << "Error while converting char * to std::wstring" << std::endl;
exit(0);
}
return wordWString;
}
// function to search for a pair of words at a given distance
int findWordPair (std::ifstream &file, std::wstring firstWord, std::wstring secondWord, int dist)
{
std::string curWord;
std::wstring curWString;
int pairCnt = 0, curDist = -1; // word pair counter and distance counter
while (file >> curWord)
{
curWString = strToWstringLower(curWord);
if (!curWString.compare(firstWord)) // first word found
{
curDist = 0; // start counting the distance
} else if (!curWString.compare(secondWord)) // second word found
{
if (curDist != -1) // found the first word at a distance less than given
{
pairCnt++;
curDist = -1; // initial state
}
} else
{
if (curDist != -1) // found the first word earlier
{
curDist++;
}
if (curDist > dist) // too many words after first
{
curDist = -1;
}
}
}
return pairCnt;
}