net weights implementation WIP

This commit is contained in:
2025-03-22 01:02:37 +03:00
parent d83f106a91
commit 59315f9445
12 changed files with 104 additions and 94 deletions

View File

@@ -73,10 +73,17 @@ bool vector_contains_element(const std::vector<T>& vector, const T& to_find) {
}
return false;
}
template<class T>
bool areAllSizesEqual(const std::vector<T>& v1, const std::vector<T>& v2,
const std::vector<T>& v3, const std::vector<T>& v4) {
return (v1.size() == v2.size() && v2.size() == v3.size() && v3.size() == v4.size());
}
//ужас
template bool vector_contains_element<std::string>(const std::vector<std::string>& vector, const std::string& to_find);
template bool areAllSizesEqual(const std::vector<std::string>& v1, const std::vector<std::string>& v2,
const std::vector<std::string>& v3, const std::vector<std::string>& v4);
std::vector<std::string> split(std::string s, std::string delimiter) {
std::vector<std::string> result;
size_t pos = 0;
@@ -147,7 +154,7 @@ std::vector<std::wstring> find_in_html(std::string& html, std::string regex, std
}
std::vector<std::wstring> find_products_in_html(std::string html) {
return find_in_html(html, "<div class=\"ifw-col ifw-col-1 text-left\"><b>.*<\\/b><\\/div>", "<div class=\"ifw-col ifw-col-1 text-left\"><b>", "<\\/b><\\/div>");
return find_in_html(html, "<div class=\"ifw-col ifw-col-1 text-left\"><b>.{10,100}<\\/b><\\/div>", "<div class=\"ifw-col ifw-col-1 text-left\"><b>", "<\\/b><\\/div>");
}
std::vector<std::wstring> find_amounts_in_html(std::string html) {
@@ -159,6 +166,19 @@ std::vector<std::wstring> find_amounts_in_html(std::string html) {
return founds;
}
std::vector<std::wstring> find_net_weights_in_names(std::vector<std::wstring> &names) {
std::vector<std::wstring> result;
for (std::wstring &name : names ) {
boost::wregex regexp(from_utf8("((\\d+(.|,)?\\d?((м|)л|(к|м|)г|т|ц|шт|(pc|)s|(m|k|)g|(m|)l|t)).?)+"), boost::regex_constants::collate);
for (boost::wsregex_iterator it{name.begin(), name.end(), regexp}, end{}; it != end;
it++) {
result.push_back(it->str());
break;
}
}
return result;
}
std::vector<std::wstring> find_prices_in_html(std::string html) {
std::vector<std::wstring> founds = find_in_html(html, "X <\\/span><span>\\d+(\\.|,)\\d{2}<\\/span>", "X <\\/span><span>", "<\\/span>");
for (auto &found : founds) {
@@ -168,7 +188,7 @@ std::vector<std::wstring> find_prices_in_html(std::string html) {
return founds;
}
void dumpVectorsToStderr(std::vector<std::wstring> &products, std::vector<std::wstring> &amounts, std::vector<std::wstring> &prices) {
void dumpVectorsToStderr(std::vector<std::wstring> &products, std::vector<std::wstring> &amounts, std::vector<std::wstring> &net_weights, std::vector<std::wstring> &prices) {
std::cerr << "Products: ";
for (auto &product : products) {
std::cerr << to_utf8(product) << "|[]|";
@@ -181,6 +201,12 @@ void dumpVectorsToStderr(std::vector<std::wstring> &products, std::vector<std::w
}
std::cerr << std::endl;
std::cerr << "Net weights: ";
for (auto &net_weight : net_weights) {
std::wcerr << net_weight << " ";
}
std::cerr << std::endl;
std::cerr << "Prices: ";
for (auto &price : prices) {
std::wcerr << price << " ";
@@ -195,6 +221,7 @@ Check parseOfdRuAnswer(std::string html) {
std::vector<std::wstring> products = find_products_in_html(trimmed);
std::vector<std::wstring> amounts = find_amounts_in_html(trimmed);
std::vector<std::wstring> net_weights = find_net_weights_in_names(products);
std::vector<std::wstring> prices = find_prices_in_html(trimmed);
if ((products.size() + amounts.size() + prices.size()) == 0) {
@@ -209,7 +236,7 @@ Check parseOfdRuAnswer(std::string html) {
}
if ((products.size() + amounts.size() + prices.size())/products.size() != 3) {
dumpVectorsToStderr(products, amounts, prices);
dumpVectorsToStderr(products, amounts, net_weights, prices);
//TOOD: make new setting "app_home" and get all path using it.
std::ofstream error_log(get_path_relative_to_home(".local/share/checks_parser/error_log.txt"), std::ios_base::app);
error_log << trimmed << std::endl;
@@ -220,7 +247,7 @@ Check parseOfdRuAnswer(std::string html) {
Check c;
for (int i = 0; i < products.size(); i ++) {
Goods goods(to_utf8(products[i]), std::stod(prices[i]), std::stod(amounts[i]));
Goods goods(to_utf8(products[i]), std::stod(prices[i]), to_utf8(net_weights[i]), std::stod(amounts[i]));
c.add_goods(goods);
}
@@ -254,3 +281,4 @@ void generate_qr_code(std::string data) {
QRcode_free(qrCode);
}
#endif // ifdef BUILD_OFD_BINARYEYE_SCAN

View File

@@ -12,6 +12,9 @@ std::string get_path_relative_to_home(std::string path);
template <typename T>
bool vector_contains_element(const std::vector<T> &vector, const T &to_find);
template <class T>
bool areAllSizesEqual(const std::vector<T>& v1, const std::vector<T>& v2,
const std::vector<T>& v3, const std::vector<T>& v4);
std::vector<std::string> split(std::string, std::string);
@@ -26,4 +29,6 @@ void generate_qr_code(std::string data);
std::string get_local_ip_address();
#endif
#endif // UTILS_H