Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. import java.util.regex.*;
  5.  
  6. public class Address {
  7. private final String street, house, floor, side, postcode, city;
  8.  
  9. private Address(String _street, String _house, String _floor, String _side, String _postcode, String _city) {
  10. street = _street;
  11. house = _house;
  12. floor = _floor;
  13. side = _side;
  14. postcode = _postcode;
  15. city = _city;
  16. }
  17.  
  18. public String toString() {
  19. String floorInfo = "";
  20. if(floor != null){
  21. floorInfo += " " + floor + " ";
  22. if(side != null){
  23. floorInfo += side + " ";
  24. }
  25. }
  26.  
  27. return street + " " + house + floorInfo + "\n" +
  28. postcode + " " + city;
  29. }
  30.  
  31. public static class Builder {
  32. private String street = "Unknown", house, floor, side, postcode, city;
  33. public Builder street(String _street) { street = _street; return this; }
  34. public Builder house(String _house) { house = _house; return this; }
  35. public Builder floor(String _floor) { floor = _floor; return this; }
  36. public Builder side(String _side) { side = _side; return this; }
  37. public Builder postcode(String _postcode) { postcode = _postcode; return this; }
  38. public Builder city(String _city) { city = _city; return this; }
  39. public Address build() {
  40. return new Address(street, house, floor, side, postcode, city);
  41. }
  42. }
  43.  
  44. private final static String zipcodeRegex = "(?<zipcode>[0-9]{4}) (?<city>.*)";
  45. private final static Pattern zipcodePattern = Pattern.compile(zipcodeRegex);
  46. private static HashMap<Integer, String> zipcodeHashM;
  47.  
  48. private static String getCityFromZipCode(String zipcode){
  49.  
  50. if (zipcodeHashM == null){
  51. zipcodeHashM = new HashMap<>();
  52.  
  53. try{
  54.  
  55. //FileReader file = new FileReader("postnumre.txt");
  56. InputStream is = Address.class.getClassLoader().getResourceAsStream("postnumre.txt");
  57. System.out.println(is != null);
  58. InputStreamReader isr = new InputStreamReader(is, "UTF-8");
  59. BufferedReader reader = new BufferedReader(isr);
  60. String LineReader = reader.readLine();
  61. Matcher match;
  62.  
  63. while(LineReader != null){
  64. match = zipcodePattern.matcher(LineReader);
  65. if (match.matches()){
  66. zipcodeHashM.put(Integer.parseInt(match.group("zipcode")), match.group("city"));
  67. }
  68. LineReader = reader.readLine();
  69. }
  70. }catch (IOException e){
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. try{
  76. int _zipcode = Integer.parseInt(zipcode);
  77. return zipcodeHashM.get(_zipcode);
  78. }catch (Exception e){
  79. return null;
  80. }
  81.  
  82. }
  83.  
  84. private final static String cityRegex = "(?<zipcode>[0-9]{4}) (?<city>.*)";
  85. private final static Pattern cityPattern = Pattern.compile(cityRegex);
  86. private static HashMap<String, Integer> cityHashM;
  87.  
  88. private static String getZipcodeFromCity(String city){
  89.  
  90. if (cityHashM == null){
  91. cityHashM = new HashMap<>();
  92.  
  93. try{
  94.  
  95. //FileReader file = new FileReader("postnumre.txt");
  96. InputStream is = Address.class.getResourceAsStream("postnumre.txt");
  97. InputStreamReader isr = new InputStreamReader(is, "UTF-8");
  98. BufferedReader reader = new BufferedReader(isr);
  99. String LineReader = reader.readLine();
  100. Matcher match;
  101.  
  102. while(LineReader != null){
  103. match = cityPattern.matcher(LineReader);
  104. if (match.matches()){
  105. cityHashM.put(match.group("city"), Integer.parseInt(match.group("zipcode")));
  106. }
  107. LineReader = reader.readLine();
  108. }
  109. }catch (IOException e){
  110. e.printStackTrace();
  111. }
  112. }
  113.  
  114. try{
  115. String _city = city;
  116. return String.valueOf(cityHashM.get(_city));
  117. }catch (Exception e){
  118. return null;
  119. }
  120. }
  121.  
  122.  
  123.  
  124. public String street() { return street; }
  125. public String house() { return house; }
  126. public String floor() { return floor; }
  127. public String side() { return side; }
  128. public String postcode() { return postcode; }
  129. public String city() { return city; }
  130.  
  131.  
  132. //REGEX1 - Street|number|floor|side|zipcode|city
  133. private final static String REGEX1 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?) +(?<floor>[0-9|st]+)[?., ]+ *(?<side>[tv|mf|th]+)[?., ]++(?<zipcode>[0-9]{4}) +(?<city>[\\p{L} ]+)";
  134. private final static Pattern PATTERN1 = Pattern.compile(REGEX1);
  135.  
  136.  
  137. //REGEX2 - Street|number|floor|side|zipcode
  138. private final static String REGEX2 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?) +(?<floor>[0-9|st]+)[?., ]+ *(?<side>[tv|mf|th]+)[?., ]++(?<zipcode>[0-9]{4})";
  139. private final static Pattern PATTERN2 = Pattern.compile(REGEX2);
  140.  
  141.  
  142. //REGEX3 - Street|number|floor|zipcode|city
  143. private final static String REGEX3 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?) +(?<floor>[0-9|st]+)[?., ]++(?<zipcode>[0-9]{4}) +(?<city>[\\p{L} ]+)";
  144. private final static Pattern PATTERN3 = Pattern.compile(REGEX3);
  145.  
  146.  
  147. //REGEX4 - Street|number|floor|zipcode
  148. private final static String REGEX4 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?) +(?<floor>[0-9|st]+)[?., ]++(?<zipcode>[0-9]{4})";
  149. private final static Pattern PATTERN4 = Pattern.compile(REGEX4);
  150.  
  151. //REGEX5 - Street|number|zipcode|city
  152. private final static String REGEX5 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?)[?., ]++(?<zipcode>[0-9]{4}) +(?<city>[\\p{L} ]+)";
  153. private final static Pattern PATTERN5 = Pattern.compile(REGEX5);
  154.  
  155. //REGEX6 - Street|number|city
  156. private final static String REGEX6 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?)[?., ]++(?<city>[\\p{L} ]+)";
  157. private final static Pattern PATTERN6 = Pattern.compile(REGEX6);
  158.  
  159.  
  160. //REGEX7 - Street|number|zipcode
  161. private final static String REGEX7 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?)[?., ]++(?<zipcode>[0-9]{4})";
  162. private final static Pattern PATTERN7 = Pattern.compile(REGEX7);
  163.  
  164.  
  165.  
  166.  
  167. public static Address parse(String s) {
  168. Builder b = new Builder();
  169.  
  170. //REGEX1 - Street|number|floor|side|zipcode|city
  171. Matcher matcher = PATTERN1.matcher(s);
  172. if (matcher.matches()) {
  173. b.street(matcher.group("street"))
  174. .house(matcher.group("number"))
  175. .floor(matcher.group("floor"))
  176. .side(matcher.group("side"))
  177. .postcode(matcher.group("zipcode"))
  178. .city(matcher.group("city"));
  179. }
  180.  
  181. //REGEX2 - Street|number|floor|side|zipcode
  182. matcher = PATTERN2.matcher(s);
  183. if (matcher.matches()) {
  184. b.street(matcher.group("street"))
  185. .house(matcher.group("number"))
  186. .floor(matcher.group("floor"))
  187. .side(matcher.group("side"))
  188. .postcode(matcher.group("zipcode"));
  189. }
  190.  
  191. //REGEX3 - Street|number|floor|zipcode|city
  192. matcher = PATTERN3.matcher(s);
  193. if(matcher.matches()){
  194. b.street(matcher.group("street"))
  195. .house(matcher.group("number"))
  196. .floor(matcher.group("floor"))
  197. .postcode(matcher.group("zipcode"))
  198. .city(matcher.group("city"));
  199. }
  200.  
  201. //REGEX4 - Street|number|floor|zipcode
  202. matcher = PATTERN4.matcher(s);
  203. if(matcher.matches()){
  204. b.street(matcher.group("street"))
  205. .house(matcher.group("number"))
  206. .floor(matcher.group("floor"))
  207. .postcode(matcher.group("zipcode"));
  208. }
  209.  
  210. //REGEX5 - Street|number|zipcode|city
  211. matcher = PATTERN5.matcher(s);
  212. if(matcher.matches()){
  213. b.street(matcher.group("street"))
  214. .house(matcher.group("number"))
  215. .postcode(matcher.group("zipcode"))
  216. .city(matcher.group("city"));
  217. }
  218.  
  219. //REGEX6 - Street|number|city
  220. matcher = PATTERN6.matcher(s);
  221. if(matcher.matches()){
  222. b.street(matcher.group("street"))
  223. .house(matcher.group("number"))
  224. .city(matcher.group("city"));
  225. }
  226.  
  227. //REGEX7 - Street|number|zipcode
  228. matcher = PATTERN7.matcher(s);
  229. if(matcher.matches()){
  230. b.street(matcher.group("street"))
  231. .house(matcher.group("number"))
  232. .postcode(matcher.group("zipcode"));
  233. }
  234.  
  235. if(b.city == null && b.postcode != null){
  236. b.city(getCityFromZipCode(b.postcode));
  237. }
  238. if(b.postcode == null && b.city != null){
  239. b.postcode(String.valueOf(getZipcodeFromCity(b.city)));
  240. }
  241. return b.build();
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement