Ignore:
Timestamp:
2009-06-08T01:23:58+02:00 (15 years ago)
Author:
rcernoch
Message:

CzechAddress: Version 0.2.2 released. Fixes 2 bugs and improves speed by cca 15%.

Location:
applications/editors/josm/plugins/czechaddress/src/org/openstreetmap/josm/plugins/czechaddress
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/czechaddress/src/org/openstreetmap/josm/plugins/czechaddress/CzechAddressPlugin.java

    r15649 r15763  
    6969                    continue;
    7070
    71                 System.err.println(name);
    7271                Logger.getLogger(name).setLevel(Level.FINE);
    7372                Logger.getLogger(name).addHandler(fileHandler);
     
    8382    public CzechAddressPlugin() {
    8483
     84        /*boolean x;
     85        x = StringUtils.matchAbbrev("Ahoj lidi", "Ahoj lidi");
     86        System.out.println(x ? "Match" : "Differ");
     87        x = StringUtils.matchAbbrev("Bož. Němcové", "Boženy Němca.");
     88        System.out.println(x ? "Match" : "Differ");*/
     89
    8590        addStatusListener(this);
    8691       
     
    102107
    103108        // Fill the database in separate thread.
    104         Thread t = new Thread() { @Override public void run() {
     109        Thread t = new Thread("CzechAddress: DB preload") {
     110          @Override public void run() {
    105111            super.run();
    106112            try {
  • applications/editors/josm/plugins/czechaddress/src/org/openstreetmap/josm/plugins/czechaddress/StringUtils.java

    r15582 r15763  
    22
    33import java.text.Normalizer;
     4import java.util.ArrayList;
     5import java.util.List;
    46import org.openstreetmap.josm.data.coor.LatLon;
    57
     
    5759     */
    5860    public static boolean matchAbbrev(String s1, String s2) {
    59         String[] parts1 = anglicize(s1).split(" +");
    60         String[] parts2 = anglicize(s2).split(" +");
    6161
    62         if (parts1.length != parts2.length)
     62        s1 = anglicize(s1);
     63        s2 = anglicize(s2);
     64        List<Integer> beg1 = new ArrayList<Integer>(4);
     65        List<Integer> beg2 = new ArrayList<Integer>(4);
     66
     67        char lastChar = ' ';
     68        for (int i=0; i<s1.length(); i++) {
     69            if (s1.charAt(i) != ' ' && lastChar == ' ')
     70                beg1.add(i);
     71            lastChar = s1.charAt(i);
     72        }
     73
     74        lastChar = ' ';
     75        for (int i=0; i<s2.length(); i++) {
     76            if (s2.charAt(i) != ' ' && lastChar == ' ')
     77                beg2.add(i);
     78            lastChar = s2.charAt(i);
     79        }
     80
     81        if (beg1.size() != beg2.size())
    6382            return false;
    6483
    65         for (int i=0; i<parts1.length; i++) {
    66             String part1 = parts1[i];
    67             String part2 = parts2[i];
     84        for (int i=0; i<beg1.size(); i++) {
    6885
    69             if (part1.charAt(part1.length()-1) == '.')
    70                 part1 = part1.substring(0, part1.length()-1);
     86            int pos1 = beg1.get(i);
     87            int pos2 = beg2.get(i);
    7188
    72             if (part2.charAt(part2.length()-1) == '.')
    73                 part2 = part2.substring(0, part2.length()-1);
     89            boolean doContinue = false;
     90            while (pos1 < s1.length() && pos2 < s2.length()) {
     91                if (s1.charAt(pos1) == '.' || s2.charAt(pos2) == '.')
     92                    {doContinue = true; break;}
     93                if (s1.charAt(pos1) == ' ' && s2.charAt(pos2) == ' ')
     94                     {doContinue = true; break;}
    7495
    75             int minLen = Math.min(part1.length(), part2.length());
    76             part1 = part1.substring(0, minLen).toUpperCase();
    77             part2 = part2.substring(0, minLen).toUpperCase();
     96                if (Character.toUpperCase(s1.charAt(pos1)) !=
     97                    Character.toUpperCase(s2.charAt(pos2)))
     98                    return false;
    7899
    79             if (!part1.equals(part2))
     100                pos1++;
     101                pos2++;
     102            }
     103            if (doContinue) continue;
     104
     105            if (pos1 >= s1.length() ^ pos2 >= s2.length())
    80106                return false;
    81107        }
     108       
    82109        return true;
    83110    }
     
    93120     */
    94121    public static String capitalize(String s) {
     122        if (s == null) return null;
    95123
    96         if (s == null)
    97             return s;
    98 
    99         String result = "";
    100 
     124        char[] charr = s.toCharArray();
    101125        char last = ' ';
    102         for (char ch : s.toCharArray()) {
     126        char ch = last;
     127        for (int i=0; i<charr.length; i++) {
     128            ch = charr[i];
    103129            if ((last >= 'a') && (last <= 'ž') ||
    104130                (last >= 'A') && (last <= 'Ž'))
     
    107133                ch = Character.toTitleCase(ch);
    108134
    109             last = ch;
    110             result = result + ch;
     135            last = charr[i] = ch;
    111136        }
    112137
    113138        String[] noCapitalize = { "Nad", "Pod", "U", "Na", "Z" };
     139        String result = String.valueOf(charr);
     140
    114141        for (String noc : noCapitalize)
    115142            result = result.replaceAll(" "+noc+" ", " "+noc.toLowerCase()+" ");
    116 
    117143        return result;
    118144    }
     
    129155    public static String anglicize(String str) {
    130156        String strNFD = Normalizer.normalize(str, Normalizer.Form.NFD);
    131         StringBuilder sb = new StringBuilder();
     157        StringBuilder sb = new StringBuilder(str.length());
    132158        for (char ch : strNFD.toCharArray()) {
    133159            if (Character.getType(ch) != Character.NON_SPACING_MARK) {
  • applications/editors/josm/plugins/czechaddress/src/org/openstreetmap/josm/plugins/czechaddress/addressdatabase/ElementWithHouses.java

    r15582 r15763  
    1818    }
    1919
    20     protected List<House> houses = new ArrayList<House>();
     20    protected List<House> houses = new ArrayList<House>(30);
    2121
    2222    /**
  • applications/editors/josm/plugins/czechaddress/src/org/openstreetmap/josm/plugins/czechaddress/intelligence/Reasoner.java

    r15582 r15763  
    312312     */
    313313    private int getQ(OsmPrimitive prim, AddressElement elem) {
     314
     315        // TODO: This is a workaround. We should not be here at all.
     316        if (elemMatchIndex.get(elem) == null) return MATCH_NOMATCH;
     317        if (primMatchIndex.get(prim) == null) return MATCH_NOMATCH;
     318
    314319        assert primMatchIndex.get(prim).get(elem)
    315320            == elemMatchIndex.get(elem).get(prim);
     
    466471    public Set<AddressElement> getCandidates(OsmPrimitive prim) {
    467472
     473        Set<AddressElement> result = new HashSet<AddressElement>();
     474        if (primMatchIndex.get(prim) == null) return result;
     475
    468476        int best = MATCH_NOMATCH;
    469477        for (AddressElement elem : primMatchIndex.get(prim).keySet()) {
     
    473481        }
    474482
    475         Set<AddressElement> result = new HashSet<AddressElement>();
    476 
    477483        for (AddressElement elem : primMatchIndex.get(prim).keySet()) {
    478484            int cand = primMatchIndex.get(prim).get(elem);
     
    499505    public Set<OsmPrimitive> getCandidates(AddressElement elem) {
    500506
     507        Set<OsmPrimitive> result = new HashSet<OsmPrimitive>();
     508        if (elemMatchIndex.get(elem) == null) return result;
     509
    501510        int best = MATCH_NOMATCH;
    502511        for (OsmPrimitive prim : elemMatchIndex.get(elem).keySet()) {
     
    505514                best = cand;
    506515        }
    507 
    508         Set<OsmPrimitive> result = new HashSet<OsmPrimitive>();
    509516       
    510517        for (OsmPrimitive prim : elemMatchIndex.get(elem).keySet()) {
     
    539546
    540547        Map<AddressElement, Integer> matches = primMatchIndex.get(prim);
     548        if (matches == null) return null;
    541549        AddressElement bestE = null;
    542550        int bestQ = MATCH_NOMATCH;
     
    578586
    579587        Map<OsmPrimitive, Integer> matches = elemMatchIndex.get(elem);
     588        if (matches == null) return null;
    580589        OsmPrimitive bestE = null;
    581590        int bestQ = MATCH_NOMATCH;
  • applications/editors/josm/plugins/czechaddress/src/org/openstreetmap/josm/plugins/czechaddress/parser/MvcrParser.java

    r15582 r15763  
    4040                                Attributes attributes) throws SAXException {
    4141
     42        // ========== PARSING HOUSE ========== //
     43        if (name.equals("a")) {
     44
     45            String cp = attributes.getValue("p");
     46            String co = attributes.getValue("o");
     47            if ((cp == null) && (co == null))
     48                return;
     49
     50            ElementWithHouses    topElem = curStreet;
     51            if (topElem == null) topElem = curSuburb;
     52            if (topElem == null) topElem = curViToCi;
     53            if (topElem == null) topElem = curRegion;
     54
     55            topElem.addHouse(new House(cp, co));
     56            return;
     57        }
     58       
     59        // ========== PARSING STREET ========== //
     60        if (name.equals("ulice")) {
     61            String nazev = attributes.getValue("nazev");
     62
     63            // If the street filter is on, apply it!
     64            if (filStreet != null && !nazev.equals(filStreet)) {
     65                curStreet = null;
     66                return;
     67            }
     68
     69            ElementWithStreets   topElem = curSuburb;
     70            if (topElem == null) topElem = curViToCi;
     71            if (topElem == null) topElem = curRegion;
     72
     73            //curStreet = topElem.findStreet(attributes.getValue("nazev"));
     74            //if (curStreet == null) {
     75                curStreet = new Street(capitalize(nazev));
     76            //    System.out.println("Parser: " + curStreet);
     77                topElem.addStreet(curStreet);
     78            //}
     79            return;
     80        }
     81
     82        // ========== PARSING SUBURB ========== //
     83        if (name.equals("cast")) {
     84            if (curViToCi == null)
     85                return;
     86
     87            String nazev = attributes.getValue("nazev");
     88
     89            // If the suburb filter is on, apply it!
     90            if (filSuburb != null && !nazev.equals(filSuburb)) {
     91                curSuburb = null;
     92                curStreet = null;
     93                return;
     94            }
     95
     96            //curSuburb = curViToCi.findSuburb(attributes.getValue("nazev"));
     97            //if (curSuburb == null) {
     98                curSuburb = new Suburb(capitalize(nazev));
     99            //    System.out.println("Parser: " + curSuburb);
     100                curViToCi.addSuburb(curSuburb);
     101            //}
     102            return;
     103        }
     104
     105        // ========== PARSING ViToCi ========== //
     106        if (name.equals("obec")) {
     107
     108            String nazev = attributes.getValue("nazev");
     109
     110            // If the viToCi filter is on, apply it!
     111            if (filViToCi != null && !nazev.equals(filViToCi)) {
     112                curViToCi = null;
     113                curSuburb = null;
     114                curStreet = null;
     115                return;
     116            }
     117
     118            //curViToCi = curRegion.findViToCi(attributes.getValue("nazev"));
     119            //if (curViToCi == null) {
     120                curViToCi = new ViToCi(capitalize(nazev));
     121            //    System.out.println("Parser: " + curViToCi);
     122                curRegion.addViToCi(curViToCi);
     123            //}
     124            return;
     125        }
     126
    42127        // ========== PARSING REGION ========== //
    43128        if (name.equals("oblast")) {
     
    65150                target.regions.add(curRegion);
    66151            //}
    67         }
    68 
    69         // Everything must belong to some region
    70         if (curRegion == null)
    71             return;
    72 
    73         // ========== PARSING ViToCi ========== //
    74         if (name.equals("obec")) {
    75 
    76             // If the viToCi filter is on, apply it!
    77             if (filViToCi != null && !attributes.getValue("nazev").equals(filViToCi)) {
    78                 curViToCi = null;
    79                 curSuburb = null;
    80                 curStreet = null;
    81                 return;
    82             }
    83 
    84             //curViToCi = curRegion.findViToCi(attributes.getValue("nazev"));
    85             //if (curViToCi == null) {
    86                 curViToCi = new ViToCi(capitalize(attributes.getValue("nazev")));
    87             //    System.out.println("Parser: " + curViToCi);
    88                 curRegion.addViToCi(curViToCi);
    89             //}
    90         }
    91 
    92         // ========== PARSING SUBURB ========== //
    93         if (name.equals("cast")) {
    94             if (curViToCi == null)
    95                 return;
    96 
    97             // If the suburb filter is on, apply it!
    98             if (filSuburb != null && !attributes.getValue("nazev").equals(filSuburb)) {
    99                 curSuburb = null;
    100                 curStreet = null;
    101                 return;
    102             }
    103 
    104             //curSuburb = curViToCi.findSuburb(attributes.getValue("nazev"));
    105             //if (curSuburb == null) {
    106                 curSuburb = new Suburb(capitalize(attributes.getValue("nazev")));
    107             //    System.out.println("Parser: " + curSuburb);
    108                 curViToCi.addSuburb(curSuburb);
    109             //}
    110         }
    111 
    112         // ========== PARSING STREET ========== //
    113         if (name.equals("ulice")) {
    114 
    115             // If the street filter is on, apply it!
    116             if (filStreet != null && !attributes.getValue("nazev").equals(filStreet)) {
    117                 curStreet = null;
    118                 return;
    119             }
    120 
    121             ElementWithStreets   topElem = curSuburb;
    122             if (topElem == null) topElem = curViToCi;
    123             if (topElem == null) topElem = curRegion;
    124 
    125             //curStreet = topElem.findStreet(attributes.getValue("nazev"));
    126             //if (curStreet == null) {
    127                 curStreet = new Street(capitalize(attributes.getValue("nazev")));
    128             //    System.out.println("Parser: " + curStreet);
    129                 topElem.addStreet(curStreet);
    130             //}
    131 
    132         }
    133 
    134         // ========== PARSING HOUSE ========== //
    135         if (name.equals("a")) {
    136 
    137             if (   (attributes.getValue("p") == null)
    138                 && (attributes.getValue("o") == null))
    139                 return;
    140 
    141             ElementWithHouses    topElem = curStreet;
    142             if (topElem == null) topElem = curSuburb;
    143             if (topElem == null) topElem = curViToCi;
    144             if (topElem == null) topElem = curRegion;
    145 
    146             topElem.addHouse(new House(attributes.getValue("p"),
    147                                        attributes.getValue("o")));
     152            return;
    148153        }
    149154    }
Note: See TracChangeset for help on using the changeset viewer.