Browse Source

Added "approximate_regex"

Dmitry Yu Okunev 3 years ago
parent
commit
c2ea6b57c4

+ 3 - 0
.gitignore

@@ -22,3 +22,6 @@
 
 /config/application.rb
 /config/database.yml
+
+*.orig
+/3rdparty/approximate_regex/approximate_regex

+ 13 - 0
3rdparty/approximate_regex/GNUmakefile

@@ -0,0 +1,13 @@
+
+all:
+	$(CC) -O2 -ggdb3 -ltre main.c -o approximate_regex
+
+prepare:
+	
+
+format:
+	astyle --style=linux --indent=tab --indent-cases --indent-switches --indent-preproc-define --break-blocks --pad-oper --pad-paren --delete-empty-lines *.c
+
+test:
+	./approximate_regex /tmp/cps_keys.csv ОкунвДмитрийЮрьевич
+

+ 83 - 0
3rdparty/approximate_regex/main.c

@@ -0,0 +1,83 @@
+/*
+    approximate_regex - just an execution wrapper of libtre for
+                        fixok.ut.emphi.ru
+
+    Copyright (C) 2016 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
+
+    This program 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 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <tre/regex.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+
+int main ( int argc, char *argv[] )
+{
+	if ( argc != 3 ) {
+		fprintf ( stderr, "syntax: %s key_list_file key\n", argv[0] );
+		return EINVAL;
+	}
+
+	const char *const key_list_path = argv[1];
+	const char *const key           = argv[2];
+	FILE *key_list_file = fopen ( key_list_path, "r" );
+
+	if ( key_list_file == NULL ) {
+		fprintf ( stderr, "Cannot open file \"%s\" for reading: %s\n", key_list_path, strerror ( errno ) );
+		return errno;
+	}
+
+	regex_t reg;
+	int rc = tre_regcomp ( &reg, key, REG_LITERAL );
+
+	if ( rc != 0 ) {
+		fprintf ( stderr, "regcomp() error: %i\n", rc );
+		return EINVAL;
+	}
+
+	regamatch_t match;
+	match.nmatch = 0;
+	match.pmatch = NULL;
+	regaparams_t params;
+	params.cost_ins   = 1;
+	params.cost_del   = 1;
+	params.cost_subst = 1;
+	params.max_cost   = 4;
+	params.max_ins    = 4;
+	params.max_del    = 4;
+	params.max_subst  = 4;
+	params.max_err    = 4;
+	char   *line = NULL;
+	size_t  size = 0;
+
+	while ( getline ( &line, &size, key_list_file ) > 0 ) {
+		switch ( tre_regaexec ( &reg, line, &match, params, 0 ) ) {
+			case REG_NOMATCH:
+				break;
+
+			case 0: // found
+				printf ( "%s", line );
+				return 0;
+
+			case REG_ESPACE:
+				fprintf ( stderr, "Cannot allocate memory\n" );
+				return ENOMEM;
+		}
+	}
+
+	return 1;
+}
+

+ 11 - 7
app/controllers/people_controller.rb

@@ -69,24 +69,28 @@ class PeopleController < ApplicationController
 
 		employees = Hash.new
 		json["employees"].each do |id, employee|
-			key="#{employee["lastname"]}#{employee["firstname"]}#{employee["middlename"]}".delete('?').strip
+			key="#{employee["lastname"]}#{employee["firstname"]}#{employee["middlename"]}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
 			employees[key] = Array.new if employees[key].nil?
 			employees[key] << employee
 		end
 
 		ofz_links = Hash.new
-		json["ofz_link_table"].each do |row|
-			key="#{row["Lastname"]}#{row["Firstname"]}#{row["Patronymic"]}".delete('?').strip
-			ofz_links[key] = Array.new if ofz_links[key].nil?
-			ofz_links[key] << row["UserID"]
-		end
+		File.open('/tmp/cps_keys.csv', 'w') { |file|
+			json["ofz_link_table"].each do |row|
+				key="#{row["Lastname"]}#{row["Firstname"]}#{row["Patronymic"]}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
+				ofz_links[key] = Array.new if ofz_links[key].nil?
+				ofz_links[key] << row["UserID"]
+				file.write("#{key}\n")
+			end
+		}
+
 
 		employees.each do |key, curEmployees|
 			next if curEmployees.size != 1
 			employee = curEmployees[0]
 
 			ofz_key = key
-			ofz_key = "#{employee["lastname"]}#{employee["firstname"]}".delete('?').strip if ofz_links[ofz_key].nil? == 0
+			ofz_key = "#{employee["lastname"]}#{employee["firstname"]}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip if ofz_links[ofz_key].nil?
 			next if ofz_links[ofz_key].nil?
 			next if ofz_links[ofz_key].size != 1