Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.util.regex.*;
- public class Address {
- private final String street, house, floor, side, postcode, city;
- private Address(String _street, String _house, String _floor, String _side, String _postcode, String _city) {
- street = _street;
- house = _house;
- floor = _floor;
- side = _side;
- postcode = _postcode;
- city = _city;
- }
- public String toString() {
- String floorInfo = "";
- if(floor != null){
- floorInfo += " " + floor + " ";
- if(side != null){
- floorInfo += side + " ";
- }
- }
- return street + " " + house + floorInfo + "\n" +
- postcode + " " + city;
- }
- public static class Builder {
- private String street = "Unknown", house, floor, side, postcode, city;
- public Builder street(String _street) { street = _street; return this; }
- public Builder house(String _house) { house = _house; return this; }
- public Builder floor(String _floor) { floor = _floor; return this; }
- public Builder side(String _side) { side = _side; return this; }
- public Builder postcode(String _postcode) { postcode = _postcode; return this; }
- public Builder city(String _city) { city = _city; return this; }
- public Address build() {
- return new Address(street, house, floor, side, postcode, city);
- }
- }
- private final static String zipcodeRegex = "(?<zipcode>[0-9]{4}) (?<city>.*)";
- private final static Pattern zipcodePattern = Pattern.compile(zipcodeRegex);
- private static HashMap<Integer, String> zipcodeHashM;
- private static String getCityFromZipCode(String zipcode){
- if (zipcodeHashM == null){
- zipcodeHashM = new HashMap<>();
- try{
- //FileReader file = new FileReader("postnumre.txt");
- InputStream is = Address.class.getClassLoader().getResourceAsStream("postnumre.txt");
- System.out.println(is != null);
- InputStreamReader isr = new InputStreamReader(is, "UTF-8");
- BufferedReader reader = new BufferedReader(isr);
- String LineReader = reader.readLine();
- Matcher match;
- while(LineReader != null){
- match = zipcodePattern.matcher(LineReader);
- if (match.matches()){
- zipcodeHashM.put(Integer.parseInt(match.group("zipcode")), match.group("city"));
- }
- LineReader = reader.readLine();
- }
- }catch (IOException e){
- e.printStackTrace();
- }
- }
- try{
- int _zipcode = Integer.parseInt(zipcode);
- return zipcodeHashM.get(_zipcode);
- }catch (Exception e){
- return null;
- }
- }
- private final static String cityRegex = "(?<zipcode>[0-9]{4}) (?<city>.*)";
- private final static Pattern cityPattern = Pattern.compile(cityRegex);
- private static HashMap<String, Integer> cityHashM;
- private static String getZipcodeFromCity(String city){
- if (cityHashM == null){
- cityHashM = new HashMap<>();
- try{
- //FileReader file = new FileReader("postnumre.txt");
- InputStream is = Address.class.getResourceAsStream("postnumre.txt");
- InputStreamReader isr = new InputStreamReader(is, "UTF-8");
- BufferedReader reader = new BufferedReader(isr);
- String LineReader = reader.readLine();
- Matcher match;
- while(LineReader != null){
- match = cityPattern.matcher(LineReader);
- if (match.matches()){
- cityHashM.put(match.group("city"), Integer.parseInt(match.group("zipcode")));
- }
- LineReader = reader.readLine();
- }
- }catch (IOException e){
- e.printStackTrace();
- }
- }
- try{
- String _city = city;
- return String.valueOf(cityHashM.get(_city));
- }catch (Exception e){
- return null;
- }
- }
- public String street() { return street; }
- public String house() { return house; }
- public String floor() { return floor; }
- public String side() { return side; }
- public String postcode() { return postcode; }
- public String city() { return city; }
- //REGEX1 - Street|number|floor|side|zipcode|city
- 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} ]+)";
- private final static Pattern PATTERN1 = Pattern.compile(REGEX1);
- //REGEX2 - Street|number|floor|side|zipcode
- 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})";
- private final static Pattern PATTERN2 = Pattern.compile(REGEX2);
- //REGEX3 - Street|number|floor|zipcode|city
- private final static String REGEX3 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?) +(?<floor>[0-9|st]+)[?., ]++(?<zipcode>[0-9]{4}) +(?<city>[\\p{L} ]+)";
- private final static Pattern PATTERN3 = Pattern.compile(REGEX3);
- //REGEX4 - Street|number|floor|zipcode
- private final static String REGEX4 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?) +(?<floor>[0-9|st]+)[?., ]++(?<zipcode>[0-9]{4})";
- private final static Pattern PATTERN4 = Pattern.compile(REGEX4);
- //REGEX5 - Street|number|zipcode|city
- private final static String REGEX5 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?)[?., ]++(?<zipcode>[0-9]{4}) +(?<city>[\\p{L} ]+)";
- private final static Pattern PATTERN5 = Pattern.compile(REGEX5);
- //REGEX6 - Street|number|city
- private final static String REGEX6 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?)[?., ]++(?<city>[\\p{L} ]+)";
- private final static Pattern PATTERN6 = Pattern.compile(REGEX6);
- //REGEX7 - Street|number|zipcode
- private final static String REGEX7 = "(?<street>^[\\p{L} ]+) +(?<number>[0-9]+[\\p{L}]?)[?., ]++(?<zipcode>[0-9]{4})";
- private final static Pattern PATTERN7 = Pattern.compile(REGEX7);
- public static Address parse(String s) {
- Builder b = new Builder();
- //REGEX1 - Street|number|floor|side|zipcode|city
- Matcher matcher = PATTERN1.matcher(s);
- if (matcher.matches()) {
- b.street(matcher.group("street"))
- .house(matcher.group("number"))
- .floor(matcher.group("floor"))
- .side(matcher.group("side"))
- .postcode(matcher.group("zipcode"))
- .city(matcher.group("city"));
- }
- //REGEX2 - Street|number|floor|side|zipcode
- matcher = PATTERN2.matcher(s);
- if (matcher.matches()) {
- b.street(matcher.group("street"))
- .house(matcher.group("number"))
- .floor(matcher.group("floor"))
- .side(matcher.group("side"))
- .postcode(matcher.group("zipcode"));
- }
- //REGEX3 - Street|number|floor|zipcode|city
- matcher = PATTERN3.matcher(s);
- if(matcher.matches()){
- b.street(matcher.group("street"))
- .house(matcher.group("number"))
- .floor(matcher.group("floor"))
- .postcode(matcher.group("zipcode"))
- .city(matcher.group("city"));
- }
- //REGEX4 - Street|number|floor|zipcode
- matcher = PATTERN4.matcher(s);
- if(matcher.matches()){
- b.street(matcher.group("street"))
- .house(matcher.group("number"))
- .floor(matcher.group("floor"))
- .postcode(matcher.group("zipcode"));
- }
- //REGEX5 - Street|number|zipcode|city
- matcher = PATTERN5.matcher(s);
- if(matcher.matches()){
- b.street(matcher.group("street"))
- .house(matcher.group("number"))
- .postcode(matcher.group("zipcode"))
- .city(matcher.group("city"));
- }
- //REGEX6 - Street|number|city
- matcher = PATTERN6.matcher(s);
- if(matcher.matches()){
- b.street(matcher.group("street"))
- .house(matcher.group("number"))
- .city(matcher.group("city"));
- }
- //REGEX7 - Street|number|zipcode
- matcher = PATTERN7.matcher(s);
- if(matcher.matches()){
- b.street(matcher.group("street"))
- .house(matcher.group("number"))
- .postcode(matcher.group("zipcode"));
- }
- if(b.city == null && b.postcode != null){
- b.city(getCityFromZipCode(b.postcode));
- }
- if(b.postcode == null && b.city != null){
- b.postcode(String.valueOf(getZipcodeFromCity(b.city)));
- }
- return b.build();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement