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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
// Copyright 2018 David Sietz and [`test-data-generator` contributors](https://github.com/dsietz/test-data-generator/blob/master/CONTRIBUTORS.md).
// Licensed under the MIT license
// (see LICENSE or <https://opensource.org/licenses/Apache-2.0>)
//
//!
//! The are multiple ways to use the Test Data Generation library. It all depends on your intent.
//!
//! ### Profile
//!
//! The easiest way is to use a Profile. The `profile` module provides functionality to create a profile on a data sample (Strings).
//! Once a profile has been made, data can be generated by calling the _pre_generate()_ and _generate()_ functions, in that order.
//!
//! ```
//! extern crate test_data_generation;
//!
//! use test_data_generation::Profile;
//!
//! fn main() {
//! // analyze the dataset
//! let mut data_profile = Profile::new();
//!
//! // analyze the dataset
//! data_profile.analyze("Smith, John");
//! data_profile.analyze("Doe, John");
//! data_profile.analyze("Dale, Danny");
//! data_profile.analyze("Rickets, Ronney");
//!
//! // confirm 4 data samples were analyzed
//! assert_eq!(data_profile.patterns.len(), 4);
//!
//! // prepare the generator
//! data_profile.pre_generate();
//!
//! // generate some data
//! println!("The generated name is {:?}", data_profile.generate());
//! }
//! ```
//!
//! You can also export (archive as JSON file) the profile for later use.
//! This allows for the algorithm to be retrieved without having to store the actual data that was analyzed.
//!
//! ```
//! extern crate test_data_generation;
//!
//! use test_data_generation::Profile;
//!
//! fn main() {
//! //create a profile and analyze some data
//! let mut old_profile = Profile::new();
//! old_profile.analyze("Smith, John");
//! old_profile.analyze("O'Brian, Henny");
//! old_profile.analyze("Dale, Danny");
//! old_profile.analyze("Rickets, Ronney");
//!
//! old_profile.pre_generate();
//!
//! //save the profile for later
//! assert_eq!(old_profile.save("./tests/samples/sample-00-profile").unwrap(), true);
//!
//! // create a new profile from the archive json file
//! let mut new_profile = Profile::from_file("./tests/samples/sample-00-profile");
//!
//! // generate some data. NOTE that the pre-generate() was already called prior to saving
//! println!("The generated name is {:?}", new_profile.generate());
//! }
//! ```
//!
//! ### Data Sample Parser
//!
//! If you are using CSV files of data samples, then you may wish to use a Data Sample Parser.
//! The `data_sample_parser` module provides functionality to read sample data, parse and analyze it, so that test data can be generated based on profiles.
//!
//! ```
//! extern crate test_data_generation;
//! use test_data_generation::data_sample_parser::DataSampleParser;
//!
//! fn main() {
//! let mut dsp = DataSampleParser::new();
//! dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap();
//!
//! println!("My new name is {} {}", dsp.generate_record()[0], dsp.generate_record()[1]);
//! // My new name is Abbon Aady
//! }
//! ```
//!
//! You can also save the Data Sample Parser (the algorithm) as an archive file (json) ...
//!
//! ```
//! extern crate test_data_generation;
//! use test_data_generation::data_sample_parser::DataSampleParser;
//!
//! fn main() {
//! let mut dsp = DataSampleParser::new();
//! dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap();
//!
//! assert_eq!(dsp.save(&String::from("./tests/samples/sample-01-dsp")).unwrap(), true);
//! }
//! ```
//!
//! and use it at a later time.
//!
//! ```
//! extern crate test_data_generation;
//! use test_data_generation::data_sample_parser::DataSampleParser;
//!
//! fn main() {
//! let mut dsp = DataSampleParser::from_file(&String::from("./tests/samples/sample-01-dsp"));
//!
//! println!("Sample data is {:?}", dsp.generate_record()[0]);
//! }
//! ```
//!
//! You can also generate a new csv file based on the data sample provided.
//!
//! ```
//! extern crate test_data_generation;
//!
//! use test_data_generation::data_sample_parser::DataSampleParser;
//!
//! fn main() {
//! let mut dsp = DataSampleParser::new();
//!
//! // Use the default delimiter (comma)
//! dsp.analyze_csv_file(&String::from("./tests/samples/sample-01.csv"), None).unwrap();
//! dsp.generate_csv(100, &String::from("./tests/samples/generated-01.csv"), None).unwrap();
//! }
//! ```
#![crate_type = "lib"]
#![crate_name = "test_data_generation"]
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
extern crate crossbeam;
extern crate csv;
extern crate indexmap;
extern crate levenshtein;
extern crate rand;
extern crate regex;
extern crate serde;
extern crate serde_json;
extern crate serde_yaml;
extern crate yaml_rust;
use crate::engine::{Fact, PatternDefinition};
use std::collections::BTreeMap;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::Write;
use std::ops::AddAssign;
type PatternMap = BTreeMap<String, u32>;
type SizeMap = BTreeMap<u32, u32>;
type SizeRankMap = BTreeMap<u32, f64>;
#[derive(Clone, Serialize, Deserialize, Debug)]
/// Represents a Profile for sample data that has been analyzed and can be used to generate realistic data
pub struct Profile {
/// An identifier (not necessarily unique) that is used to differentiate profiles from one another
pub id: Option<String>,
/// A list of symbolic patterns with a distinct count of occurrences
pub patterns: PatternMap,
/// The total number of patterns in the profile
pub pattern_total: u32,
/// A list of symbolic patterns in the profile
/// (used for temporary storage due to lifetime issues)
pub pattern_keys: Vec<String>,
/// A list of distinct counts for patterns in the profile
/// (used for temporary storage due to lifetime issues)
pub pattern_vals: Vec<u32>,
/// A list of symbolic patterns with their percent chance of occurrence
pub pattern_percentages: Vec<(String, f64)>,
/// A list of symbolic patterns with a running total of percent chance of occurrence, in increasing order
pub pattern_ranks: Vec<(String, f64)>,
/// A list of pattern lengths with a distinct count of occurrence
pub sizes: SizeMap,
/// the total number of pattern sizes (lengths) in the profile
pub size_total: u32,
/// A list of pattern sizes (lengths) with a running total of their percent chance of occurrence, in increasing order
pub size_ranks: Vec<(u32, f64)>,
/// The number of processors used to distribute the work load (multi-thread) while finding Facts to generate data
pub processors: u8,
/// A list of processors (which are lists of Facts) that store all the Facts in the profile
pub facts: Vec<Vec<Fact>>,
}
impl Profile {
/// Constructs a new Profile
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let placeholder = Profile::new();
/// }
/// ```
pub fn new() -> Profile {
Profile {
id: None,
patterns: PatternMap::new(),
pattern_total: 0,
pattern_keys: Vec::new(),
pattern_vals: Vec::new(),
pattern_percentages: Vec::new(),
pattern_ranks: Vec::new(),
sizes: SizeMap::new(),
size_total: 0,
size_ranks: Vec::new(),
processors: 4,
facts: Profile::new_facts(4),
}
}
/// Constructs a new Profile using an identifier
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let placeholder = Profile::new_with_id("12345".to_string());
/// }
/// ```
pub fn new_with_id(id: String) -> Profile {
Profile {
id: Some(id),
patterns: PatternMap::new(),
pattern_total: 0,
pattern_keys: Vec::new(),
pattern_vals: Vec::new(),
pattern_percentages: Vec::new(),
pattern_ranks: Vec::new(),
sizes: SizeMap::new(),
size_total: 0,
size_ranks: Vec::new(),
processors: 4,
facts: Profile::new_facts(4),
}
}
/// Constructs a new Profile with a specified number of processors to analyze the data.
/// Each processor shares the load of generating the data based on the Facts it has been assigned to manage.
///
/// # Arguments
///
/// * `p: u8` - A number that sets the number of processors to start up to manage the Facts.</br>
/// Increasing the number of processors will speed up the generator be distributing the workload.
/// The recommended number of processors is 1 per 10K data points (e.g.: profiling 20K names should be handled by 2 processors)</br>
/// NOTE: The default number of processors is 4.
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let processors: u8 = 10;
/// let placeholder = Profile::new_with_processors(processors);
/// }
/// ```
pub fn new_with_processors(p: u8) -> Profile {
Profile {
id: None,
patterns: PatternMap::new(),
pattern_total: 0,
pattern_keys: Vec::new(),
pattern_vals: Vec::new(),
pattern_percentages: Vec::new(),
pattern_ranks: Vec::new(),
sizes: SizeMap::new(),
size_total: 0,
size_ranks: Vec::new(),
processors: p,
facts: Profile::new_facts(p),
}
}
/// Constructs a new Profile from an exported JSON file. This is used when restoring from "archive"
///
/// # Arguments
///
/// * `path: &str` - The full path of the export file , excluding the file extension, (e.g.: "./test/data/custom-names").</br>
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::from_file("./tests/samples/sample-00-profile");
///
/// profile.pre_generate();
///
/// println!("The generated name is {:?}", profile.generate());
/// }
/// ```
pub fn from_file(path: &'static str) -> Profile {
// open the archive file
let mut file = match File::open(format!("{}.json", &path)) {
Err(_e) => {
error!("Could not open file {:?}", &path.to_string());
panic!("Could not open file {:?}", &path.to_string());
}
Ok(f) => {
info!("Successfully opened file {:?}", &path.to_string());
f
}
};
//read the archive file
let mut serialized = String::new();
match file.read_to_string(&mut serialized) {
Err(e) => {
error!(
"Could not read file {:?} because of {:?}",
&path.to_string(),
e.to_string()
);
panic!(
"Could not read file {:?} because of {:?}",
&path.to_string(),
e.to_string()
);
}
Ok(s) => {
info!("Successfully read file {:?}", &path.to_string());
s
}
};
//serde_json::from_str(&serialized).unwrap()
Self::from_serialized(&serialized)
}
/// Constructs a new Profile from a serialized (JSON) string of the Profile object. This is used when restoring from "archive"
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let serialized = "{\"patterns\":{\"VC\":1},\"pattern_total\":1,\"pattern_keys\":[\"VC\"],\"pattern_vals\":[1],\"pattern_percentages\":[],\"pattern_ranks\":[],\"sizes\":{\"2\":1},\"size_total\":1,\"size_ranks\":[],\"processors\":4,\"facts\":[[{\"key\":\"O\",\"prior_key\":null,\"next_key\":\"K\",\"pattern_placeholder\":\"V\",\"starts_with\":1,\"ends_with\":0,\"index_offset\":0}],[{\"key\":\"K\",\"prior_key\":\"O\",\"next_key\":null,\"pattern_placeholder\":\"C\",\"starts_with\":0,\"ends_with\":1,\"index_offset\":1}],[],[]]}";
/// let mut profile = Profile::from_serialized(&serialized);
///
/// profile.pre_generate();
///
/// println!("The generated name is {:?}", profile.generate());
/// }
/// ```
pub fn from_serialized(serialized: &str) -> Profile {
serde_json::from_str(&serialized).unwrap()
}
/// This function converts an data point (&str) to a pattern and adds it to the profile
///
/// # Arguments
///
/// * `entity: String` - The textual str of the value to analyze.</br>
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
/// profile.analyze("One");
/// profile.analyze("Two");
/// profile.analyze("Three");
/// profile.analyze("Four");
///
/// assert_eq!(profile.patterns.len(), 4);
/// }
/// ```
pub fn analyze(&mut self, entity: &str) {
let rslt = PatternDefinition::new().analyze(entity);
let _t = self.apply_facts(rslt.0, rslt.1).map_err(|e| {
error!(
"Warning: Couldn't apply the pattern and facts for the entity {}!",
entity
);
e.to_string()
});
}
/// This function applies the pattern and list of Facts to the profile
///
/// # Arguments
///
/// * `pattern: String` - The string the represents the pattern of the entity that was analyzed.</br>
/// * `facts: Vec<Fact>` - A Vector containing the Facts based on the analysis (one for each char in the entity).</br>
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::engine::{Fact, PatternDefinition};
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
/// let results = PatternDefinition::new().analyze("Word");
///
/// assert_eq!(profile.apply_facts(results.0, results.1).unwrap(), 1);
/// }
/// ```
#[inline]
pub fn apply_facts(&mut self, pattern: String, facts: Vec<Fact>) -> Result<i32, String> {
// balance the storing of facts across all the vectors that can be processed in parallel
let mut i = 0;
for f in facts.into_iter() {
if i == self.processors {
i = 0;
}
self.facts[i as usize].push(f);
i += 1;
}
// store the pattern
AddAssign::add_assign(self.patterns.entry(pattern.to_string()).or_insert(0), 1);
// store the total number of patterns generated so far
self.pattern_total = self.patterns.values().sum::<u32>();
// analyze sizes
AddAssign::add_assign(self.sizes.entry(pattern.len() as u32).or_insert(0), 1);
self.size_total = self.sizes.values().sum::<u32>();
self.pattern_keys = self.patterns.keys().cloned().collect();
self.pattern_vals = self.patterns.values().cloned().collect();
Ok(1)
}
/// This function calculates the patterns to use by the chance they will occur (as cumulative percentage) in decreasing order
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
///
/// profile.analyze("Smith, John");
/// profile.analyze("O'Brian, Henny");
/// profile.analyze("Dale, Danny");
/// profile.analyze("Rickets, Ronnae");
/// profile.analyze("Richard, Richie");
/// profile.analyze("Roberts, Blake");
/// profile.analyze("Conways, Sephen");
///
/// profile.pre_generate();
/// let test = [("CvccvccpSCvccvv".to_string(), 28.57142857142857 as f64), ("CcvccpSCvcc".to_string(), 42.857142857142854 as f64), ("CvccvccpSCvccvc".to_string(), 57.14285714285714 as f64), ("CvcvcccpSCcvcv".to_string(), 71.42857142857142 as f64), ("CvcvpSCvccc".to_string(), 85.7142857142857 as f64), ("V@CcvvcpSCvccc".to_string(), 99.99999999999997 as f64)];
///
/// assert_eq!(profile.pattern_ranks, test);
/// }
/// ```
#[inline]
pub fn cum_patternmap(&mut self) {
// Reference: https://users.rust-lang.org/t/cannot-infer-an-appropriate-lifetime-for-autoref/13360/3
debug!("calculating the cumulative percentage of occurences for data point patterns...");
// calculate the percentage by patterns
// -> {"CcvccpSCvcc": 14.285714285714285, "CvccvccpSCvccvc": 14.285714285714285, "CvccvccpSCvccvv": 28.57142857142857, "CvcvcccpSCcvcv": 14.285714285714285, "CvcvpSCvccc": 14.285714285714285, "V~CcvvcpSCvccc": 14.285714285714285}
let n = self.patterns.len();
// see issue: https://github.com/dsietz/test-data-generation/issues/88
self.pattern_percentages.clear();
for m in 0..n {
self.pattern_percentages.push((
self.pattern_keys[m].clone(),
(self.pattern_vals[m] as f64 / self.pattern_total as f64) * 100.0,
));
}
// sort the ranks by percentages in decreasing order
// -> [("CvccvccpSCvccvv", 28.57142857142857), ("CcvccpSCvcc", 14.285714285714285), ("CvccvccpSCvccvc", 14.285714285714285), ("CvcvcccpSCcvcv", 14.285714285714285), ("CvcvpSCvccc", 14.285714285714285), ("V~CcvvcpSCvccc", 14.285714285714285)]
self.pattern_percentages
.sort_by(|&(_, a), &(_, b)| b.partial_cmp(&a).unwrap());
// calculate the cumulative sum of the pattern rankings
// -> [("CvccvccpSCvccvv", 28.57142857142857), ("CcvccpSCvcc", 42.857142857142854), ("CvccvccpSCvccvc", 57.14285714285714), ("CvcvcccpSCcvcv", 71.42857142857142), ("CvcvpSCvccc", 85.7142857142857), ("V~CcvvcpSCvccc", 99.99999999999997)]
let mut rank: f64 = 0.00;
// see issue: https://github.com/dsietz/test-data-generation/issues/88
self.pattern_ranks.clear();
for pttrn in self.pattern_percentages.iter() {
let tmp = pttrn.1 + rank;
self.pattern_ranks.push((pttrn.0.clone(), tmp));
rank = tmp;
}
}
/// This function calculates the sizes to use by the chance they will occur (as cumulative percentage) in decreasing order
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
/// profile.analyze("One");
/// profile.analyze("Two");
/// profile.analyze("Three");
/// profile.analyze("Four");
/// profile.analyze("Five");
/// profile.analyze("Six");
///
/// profile.cum_sizemap();
///
/// print!("The size ranks are {:?}", profile.size_ranks);
/// // The size ranks are [(3, 50), (4, 83.33333333333333), (5, 100)]
/// }
/// ```
#[inline]
pub fn cum_sizemap(&mut self) {
debug!("calculating the cumulative percentage of occurences for data point sizes...");
// calculate the percentage by sizes
// -> {11: 28.57142857142857, 14: 14.285714285714285, 15: 57.14285714285714}
let mut size_ranks = SizeRankMap::new();
for key in self.sizes.keys() {
size_ranks.insert(
*key,
(*self.sizes.get(key).unwrap() as f64 / self.size_total as f64) * 100.0,
);
}
// sort the ranks by percentages in decreasing order
// -> [(15, 57.14285714285714), (11, 28.57142857142857), (14, 14.285714285714285)]
let mut sizes = size_ranks.iter().collect::<Vec<_>>();
sizes.sort_by(|&(_, a), &(_, b)| b.partial_cmp(a).unwrap());
// calculate the cumulative sum of the size rankings
// -> [(15, 57.14285714285714), (11, 85.71428571428571), (14, 100)]
self.size_ranks = sizes
.iter()
.scan((0_u32, 0.00_f64), |state, &(&k, &v)| {
*state = (k, state.1 + &v);
Some(*state)
})
.collect::<Vec<(_, _)>>();
}
/// This function generates realistic test data based on the sampel data that was analyzed.
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
///
/// profile.analyze("One");
/// profile.analyze("Two");
/// profile.analyze("Three");
/// profile.analyze("Four");
/// profile.analyze("Five");
///
/// profile.pre_generate();
///
/// print!("The test data {:?} was generated.", profile.generate());
/// }
/// ```
#[inline]
pub fn generate(&mut self) -> String {
// 1. get a random number
let s: f64 = random_percentage!();
// 2. find the first pattern that falls within the percentage chance of occurring
// NOTE: The following 2 lines has been commented out because this doesn't need to
// happen since the patterns are already ranks by percent chance of occurring
// and therefore sizes (lengths) as well since the patterns include the full
// length of the entitiy analyzed.
//let size = self.size_ranks.iter().find(|&&x|&x.1 >= &s).unwrap().0;
//let pattern = self.pattern_ranks.iter().find(|x|&x.1 >= &s && x.0.len() == size as usize).unwrap().clone();
let pattern = self
.pattern_ranks
.iter()
.find(|x| &x.1 >= &s)
.unwrap()
.clone();
// lastly, generate the test data using facts that adhere to the pattern
self.generate_from_pattern(pattern.0)
}
/// This function generates realistic test data based on the sample data that was analyzed.
///
/// # Arguments
///
/// * `pattern: String` - The pattern to reference when generating the test data.</br>
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
///
/// profile.analyze("01/13/2017");
/// profile.analyze("11/24/2017");
/// profile.analyze("08/05/2017");
///
/// profile.pre_generate();
///
/// let generated = profile.generate_from_pattern("##p##p####".to_string());
///
/// assert_eq!(generated.len(), 10);
/// }
/// ```
#[inline]
pub fn generate_from_pattern(&self, pattern: String) -> String {
let pattern_chars = pattern.chars().collect::<Vec<char>>();
let mut generated = String::new();
let prev_char = ' ';
// iterate through the chars in the pattern string
for (idx, ch) in pattern_chars.iter().enumerate() {
match crossbeam::scope(|scope| {
let c = ch;
let starts = if idx == 0 { 1 } else { 0 };
let ends = if idx == pattern_chars.len() - 1 { 1 } else { 0 };
let mut fact_options = vec![];
let prior_char = prev_char;
// iterate through the processors (vec) that hold the lists (vec) of facts
for v in &self.facts {
let selected_facts = scope.spawn(move |_| {
let mut facts = vec![];
// iterate through the list of facts
for value in v {
if value.starts_with == starts
&& value.ends_with == ends
&& value.pattern_placeholder == *c
&& value.index_offset == idx as u32
{
facts.push(value.key);
// if the value.key's prior char matches the prior generated char, then weight the value.key
// to increase the chance of it being used when generated
if value.prior_key.unwrap_or(' ') == prior_char {
facts.push(value.key);
facts.push(value.key);
}
// if the value.key's index_offset matches the current index, then weight the value.key
// to increase the chance of it being used when generated
if value.index_offset == idx as u32 {
facts.push(value.key);
facts.push(value.key);
}
}
}
facts
});
//append the selected_facts to the fact_options
//fact_options.extend_from_slice(&selected_facts.join());
match selected_facts.join() {
Ok(sf) => fact_options.extend_from_slice(&sf),
Err(err) => {
error!("{:?}", err);
panic!("{:?}", err);
}
}
}
//select a fact to use as the generated char
let rnd_start = 0;
let rnd_end = fact_options.len() - 1;
if rnd_start >= rnd_end {
//generated.push(fact_options[0 as usize]);
fact_options[0_usize]
} else {
let x: u32 = random_between!(rnd_start, rnd_end);
//prev_char = fact_options[x as usize];
//generated.push(prev_char);
fact_options[x as usize]
}
}) {
Ok(c) => generated.push(c),
Err(err) => {
error!("{:?}", err);
panic!("{:?}", err);
}
}
}
generated
}
/// This function learns by measuring how realistic the test data it generates to the sample data that was provided.
///
/// # Arguments
///
/// * `control_list: Vec<String>` - The list of strings to compare against. This would be the real data from the data sample.</br>
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profil = Profile::new();
/// let sample_data = vec!("Smith, John".to_string(),"Doe, John".to_string(),"Dale, Danny".to_string(),"Rickets, Ronney".to_string());
///
/// for sample in sample_data.iter().clone() {
/// profil.analyze(&sample);
/// }
///
/// // in order to learn the profile must be prepared with pre_genrate()
/// // so it can generate data to learn from
/// profil.pre_generate();
///
/// let learning = profil.learn_from_entity(sample_data).unwrap();
///
/// assert_eq!(learning, true);
/// }
/// ```
pub fn learn_from_entity(&mut self, control_list: Vec<String>) -> Result<bool, String> {
for _n in 0..10 {
let experiment = self.generate();
let mut percent_similarity: Vec<f64> = Vec::new();
for control in control_list.iter().clone() {
debug!("Comparing {} with {} ...", &control, &experiment);
percent_similarity.push(self.realistic_test(&control, &experiment));
}
let percent =
percent_similarity.iter().sum::<f64>() as f64 / percent_similarity.len() as f64;
debug!("Percent similarity is {} ...", &percent);
if percent >= 80_f64 {
self.analyze(&experiment);
}
}
Ok(true)
}
/// This function calculates the levenshtein distance between 2 strings.
/// See: https://crates.io/crates/levenshtein
///
/// # Arguments
///
/// * `control: &String` - The string to compare against. This would be the real data from the data sample.</br>
/// * `experiment: &String` - The string to compare. This would be the generated data for which you want to find the distance.</br>
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
///
/// assert_eq!(profile.levenshtein_distance(&"kitten".to_string(), &"sitting".to_string()), 3 as usize);
/// }
///
pub fn levenshtein_distance(&mut self, control: &String, experiment: &String) -> usize {
// https://docs.rs/levenshtein/1.0.3/levenshtein/fn.levenshtein.html
levenshtein_distance!(control, experiment)
}
/// This function calculates the percent difference between 2 strings.
///
/// # Arguments
///
/// * `control: &str` - The string to compare against. This would be the real data from the data sample.</br>
/// * `experiment: &str` - The string to compare. This would be the generated data for which you want to find the percent difference.</br>
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
///
/// assert_eq!(profile.realistic_test(&"kitten".to_string(), &"sitting".to_string()), 76.92307692307692 as f64);
/// }
///
#[inline]
pub fn realistic_test(&mut self, control: &str, experiment: &str) -> f64 {
realistic_test!(control, experiment)
}
/// This function is called from within the implementated structure and returns a list processors (Vec) with empty lists (Vec) for their Facts.
/// Each processor shares the load of generating the data based on the Facts it has been assigned to manage.
///
/// # Arguments
///
/// * `p: u8` - A number that sets the number of processors to start up to manage the Facts.</br>
/// Increasing the number of processors will speed up the generator be ditributing the workload.
/// The recommended number of processors is 1 per 10K data points (e.g.: profiling 20K names should be handled by 2 processors)</br>
/// NOTE: The default number of processors is 4.
///
#[inline]
fn new_facts(p: u8) -> Vec<Vec<Fact>> {
let mut vec_main = Vec::new();
for _ in 0..p {
vec_main.push(Vec::new());
}
vec_main
}
/// This function prepares the size a pattern accumulated percentages order by percentage increasing
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
/// profile.analyze("One");
/// profile.analyze("Two");
/// profile.analyze("Three");
/// profile.analyze("Four");
/// profile.analyze("Five");
/// profile.analyze("Six");
///
/// profile.pre_generate();
///
/// print!("The size ranks are {:?}", profile.size_ranks);
/// // The size ranks are [(3, 50), (4, 83.33333333333333), (5, 100)]
/// }
/// ```
pub fn pre_generate(&mut self) {
info!("Preparing the profile for data generation...");
self.cum_sizemap();
self.cum_patternmap();
info!("Profile: preparing generator...");
}
/// This function resets the patterns that the Profile has analyzed.
/// Call this method whenever you wish to "clear" the Profile
///
/// # Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// let mut profile = Profile::new();
///
/// profile.analyze("One");
/// profile.analyze("Two");
/// profile.analyze("Three");
///
/// let x = profile.patterns.len();
///
/// profile.reset_analyze();
///
/// profile.analyze("Four");
/// profile.analyze("Five");
/// profile.analyze("Six");
/// profile.analyze("Seven");
/// profile.analyze("Eight");
/// profile.analyze("Nine");
/// profile.analyze("Ten");
///
/// let y = profile.patterns.len();
///
/// assert_eq!(x, 3);
/// assert_eq!(y, 5);
/// }
/// ```
pub fn reset_analyze(&mut self) {
info!("Resetting the profile ...");
self.patterns = PatternMap::new();
info!("Profile: patterns have been reset ...");
}
/// This function saves (exports) the Profile to a JSON file.
/// This is useful when you wish to reuse the algorithm to generate more test data later.
///
/// # Arguments
///
/// * `field: String` - The full path of the export file , excluding the file extension, (e.g.: "./test/data/custom-names").</br>
///
/// #Errors
/// If this function encounters any form of I/O or other error, an error variant will be returned.
/// Otherwise, the function returns Ok(true).</br>
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// // analyze the dataset
/// let mut profile = Profile::new();
/// profile.analyze("Smith, John");
/// profile.analyze("O'Brian, Henny");
/// profile.analyze("Dale, Danny");
/// profile.analyze("Rickets, Ronney");
///
/// profile.pre_generate();
///
/// assert_eq!(profile.save("./tests/samples/sample-00-profile").unwrap(), true);
/// }
///
pub fn save(&mut self, path: &'static str) -> Result<bool, io::Error> {
let dsp_json = serde_json::to_string(&self).unwrap();
// Create the archive file
let mut file = match File::create(format!("{}.json", &path)) {
Err(e) => {
error!("Could not create file {:?}", &path.to_string());
return Err(e);
}
Ok(f) => {
info!("Successfully exported to {:?}", &path.to_string());
f
}
};
// Write the json string to file, returns io::Result<()>
match file.write_all(dsp_json.as_bytes()) {
Err(e) => {
error!("Could not write to file {}", &path.to_string());
return Err(e);
}
Ok(_) => {
info!("Successfully exported to {}", &path.to_string());
}
};
Ok(true)
}
/// This function converts the Profile to a serialize JSON string.
///
/// #Example
///
/// ```rust
/// extern crate test_data_generation;
///
/// use test_data_generation::Profile;
///
/// fn main() {
/// // analyze the dataset
/// let mut data_profile = Profile::new();
///
/// // analyze the dataset
/// data_profile.analyze("OK");
///
/// println!("{}", data_profile.serialize());
/// // {"patterns":{"VC":1},"pattern_total":1,"pattern_keys":["VC"],"pattern_vals":[1],"pattern_percentages":[],"pattern_ranks":[],"sizes":{"2":1},"size_total":1,"size_ranks":[],"processors":4,"facts":[[{"key":"O","prior_key":null,"next_key":"K","pattern_placeholder":"V","starts_with":1,"ends_with":0,"index_offset":0}],[{"key":"K","prior_key":"O","next_key":null,"pattern_placeholder":"C","starts_with":0,"ends_with":1,"index_offset":1}],[],[]]}
/// }
///
pub fn serialize(&mut self) -> String {
serde_json::to_string(&self).unwrap()
}
}
#[macro_use]
pub mod macros;
pub mod configs;
pub mod data_sample_parser;
pub mod engine;
pub mod shared;
// Unit Tests
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn apply_facts() {
let mut profile = Profile::new();
let results = PatternDefinition::new().analyze("Word");
assert_eq!(profile.apply_facts(results.0, results.1).unwrap(), 1);
}
#[test]
fn levenshtein_test() {
let mut profil = Profile::new();
assert_eq!(
profil.levenshtein_distance(&"kitten".to_string(), &"sitting".to_string()),
3 as usize
);
}
#[test]
fn realistic_data_test() {
let mut profil = Profile::new();
assert_eq!(
profil.realistic_test(&"kitten".to_string(), &"sitting".to_string()),
76.92307692307692 as f64
);
}
#[test]
fn learn_from_entity() {
let mut profil = Profile::new();
let sample_data = vec![
"Smith, John".to_string(),
"Doe, John".to_string(),
"Dale, Danny".to_string(),
"Rickets, Ronney".to_string(),
];
for sample in sample_data.iter().clone() {
profil.analyze(&sample);
}
profil.pre_generate();
let learning = profil.learn_from_entity(sample_data).unwrap();
assert_eq!(learning, true);
}
#[test]
fn logging_test() {
let mut profile = Profile::new();
profile.reset_analyze();
assert!(true);
}
#[test]
fn new_profile_with_id() {
let mut profile = Profile::new_with_id("12345".to_string());
profile.pre_generate();
assert_eq!(profile.id.unwrap(), "12345".to_string());
}
#[test]
fn new_profile_from_file() {
let mut profile = Profile::from_file("./tests/samples/sample-00-profile");
profile.pre_generate();
assert!(profile.generate().len() > 0);
}
#[test]
#[should_panic]
fn new_profile_from_file_bad_data() {
let mut profile = Profile::from_file("./tests/samples/not-readable");
profile.pre_generate();
assert!(profile.generate().len() > 0);
}
#[test]
#[should_panic(expected = "Could not open file \"./tests/samples/bad-path\"")]
fn new_profile_from_file_bad_path() {
let mut profile = Profile::from_file("./tests/samples/bad-path");
profile.pre_generate();
assert!(profile.generate().len() > 0);
}
#[test]
fn new_profile_from_serialized() {
let serialized = "{\"patterns\":{\"VC\":1},\"pattern_total\":1,\"pattern_keys\":[\"VC\"],\"pattern_vals\":[1],\"pattern_percentages\":[],\"pattern_ranks\":[],\"sizes\":{\"2\":1},\"size_total\":1,\"size_ranks\":[],\"processors\":4,\"facts\":[[{\"key\":\"O\",\"prior_key\":null,\"next_key\":\"K\",\"pattern_placeholder\":\"V\",\"starts_with\":1,\"ends_with\":0,\"index_offset\":0}],[{\"key\":\"K\",\"prior_key\":\"O\",\"next_key\":null,\"pattern_placeholder\":\"C\",\"starts_with\":0,\"ends_with\":1,\"index_offset\":1}],[],[]]}";
let mut profile = Profile::from_serialized(&serialized);
profile.pre_generate();
assert_eq!(profile.generate(), "OK");
}
#[test]
fn new_profile_new_with() {
let profile = Profile::new_with_processors(10);
assert_eq!(profile.processors, 10);
}
#[test]
// ensure Profile is analyzing all the sample data points
fn profile_analyze() {
let mut profil = Profile::new();
profil.analyze("Smith, John");
profil.analyze("O'Brian, Henny");
profil.analyze("Dale, Danny");
profil.analyze("Rickets, Ronney");
assert_eq!(profil.patterns.len(), 4);
}
#[test]
// ensure Profile is able to find the facts that relate to a pattern
// NOTE: Dates need work! e.g.: 00/15/0027
fn profile_generate_from_pattern_date() {
let mut profil = Profile::new();
profil.analyze("01/13/2017");
profil.analyze("11/24/2017");
profil.analyze("08/05/2017");
profil.pre_generate();
let generated = profil.generate_from_pattern("##p##p####".to_string());
assert_eq!(10, generated.len());
}
#[test]
// ensure Profile is able to find the facts that relate to a pattern
fn profile_generate_from_pattern_string() {
let mut profil = Profile::new();
profil.analyze("First");
profil.analyze("Next");
profil.analyze("Last");
profil.pre_generate();
let generated = profil.generate_from_pattern("Cvcc".to_string());
assert_eq!(4, generated.len());
}
#[test]
// ensure Profile is generating correct test data
fn profile_generate() {
let mut profil = Profile::new();
profil.analyze("Smith, John");
profil.analyze("O'Brian, Henny");
profil.analyze("Dale, Danny");
profil.analyze("Rickets, Ronnae");
profil.analyze("Richard, Richie");
profil.analyze("Roberts, Blake");
profil.analyze("Conways, Sephen");
profil.pre_generate();
assert!(profil.generate().len() > 10);
}
#[test]
// issue #31
// ensure Profile doesn't generate a name with a backslash preceding an apostrophe
fn profile_generate_with_apostrophe() {
let mut profil = Profile::new();
profil.analyze("O'Brien");
profil.pre_generate();
let generated = profil.generate();
assert_eq!(generated, "O'Brien");
}
#[test]
// ensure Profile is providing the correct pattern ranks after analyzing the sample data
fn profile_pregenerate_patterns() {
let mut profil = Profile::new();
profil.analyze("Smith, John");
profil.analyze("O'Brian, Henny");
profil.analyze("Dale, Danny");
profil.analyze("Rickets, Ronnae");
profil.analyze("Richard, Richie");
profil.analyze("Roberts, Blake");
profil.analyze("Conways, Sephen");
profil.pre_generate();
let test = [
("CvccvccpSCvccvv".to_string(), 28.57142857142857 as f64),
("CcvccpSCvcc".to_string(), 42.857142857142854 as f64),
("CvccvccpSCvccvc".to_string(), 57.14285714285714 as f64),
("CvcvcccpSCcvcv".to_string(), 71.42857142857142 as f64),
("CvcvpSCvccc".to_string(), 85.7142857142857 as f64),
("V@CcvvcpSCvccc".to_string(), 99.99999999999997 as f64),
];
assert_eq!(profil.pattern_ranks, test);
}
#[test]
// ensure Profile is providing the correct pattern ranks after analyzing the sample data
fn profile_pregenerate_sizes() {
let mut profil = Profile::new();
profil.analyze("Smith, Johny"); //12
profil.analyze("O'Brian, Hen"); //12
profil.analyze("Dale, Danny"); //11
profil.analyze("O'Henry, Al"); //11
profil.analyze("Rickets, Ro"); //11
profil.analyze("Mr. Wilbers"); //11
profil.analyze("Po, Al"); //6
profil.pre_generate();
let test = [
(11, 57.14285714285714),
(12, 85.71428571428571),
(6, 100 as f64),
];
assert_eq!(profil.size_ranks, test);
}
#[test]
fn save_profile() {
let mut profile = Profile::new();
profile.analyze("Smith, John");
profile.analyze("O'Brian, Henny");
profile.analyze("Dale, Danny");
profile.analyze("Rickets, Ronney");
profile.pre_generate();
assert_eq!(
profile.save("./tests/samples/sample-00-profile").unwrap(),
true
);
}
#[test]
// ensure a Profile can be exported (to be archived) as JSON
fn serialize() {
let mut profil = Profile::new();
// analyze the dataset
profil.analyze("OK");
let serialized = profil.serialize();
assert_eq!(serialized, "{\"id\":null,\"patterns\":{\"VC\":1},\"pattern_total\":1,\"pattern_keys\":[\"VC\"],\"pattern_vals\":[1],\"pattern_percentages\":[],\"pattern_ranks\":[],\"sizes\":{\"2\":1},\"size_total\":1,\"size_ranks\":[],\"processors\":4,\"facts\":[[{\"key\":\"O\",\"prior_key\":null,\"next_key\":\"K\",\"pattern_placeholder\":\"V\",\"starts_with\":1,\"ends_with\":0,\"index_offset\":0}],[{\"key\":\"K\",\"prior_key\":\"O\",\"next_key\":null,\"pattern_placeholder\":\"C\",\"starts_with\":0,\"ends_with\":1,\"index_offset\":1}],[],[]]}");
}
}