I adapted the script quite extensively in the mean time. It takes into
account the language of the names (if possible) and it works quite well for
the bilingual situation of Brussels.

Of course, what it proposes in the edit box, is only a suggestion. It will
still turn all 'De' in 'de', which is probably not what you want when this
'De' is part of somebody's name. In that case, simply press the Cancel
button and it will not ask the question about streets with that name again.

What is more important than the specifics of what this script does though,
is that is now possible to code functionality in Python. Doing something
with a selection or with all the elements of a specific type. Adding the
actions to a list and then adding that list to the undoRedo buffer.

What I also found, is that it's best to start JOSM from within a command
prompt, or from a cmd-file:

"C:\Program Files (x86)\java\jre6\bin\java.exe" -jar -Xmx500m
"C:\Users\Jo\Downloads\josm-latest.jar"

That way Python print commands print to this command prompt and also when an
exception occurs (an error), it's possible to see what it's all about in
that output (and also in which line of the script it got stuck). At first we
were working 'in the dark', which is not very convenient.


#!/bin/jython
> #
> # Spell checking.py  - Helps to locate probable spell errors in name fields
> #
> from javax.swing import JOptionPane
> from org.openstreetmap.josm import Main
> import org.openstreetmap.josm.command as Command
> import org.openstreetmap.josm.data.osm.Node as Node
> import org.openstreetmap.josm.data.osm.Way as Way
> import org.openstreetmap.josm.data.osm.TagCollection as TagCollection
> import org.openstreetmap.josm.data.osm.DataSet as DataSet
>
> corrections = {'fr': [('Dr. ', 'Docteur '),('R. ', 'Rue '), ('Av. ',
> 'Avenue '), ('Bd. ', 'Boulevard '),
>                       ('Sq.', 'Square'), ('Pl.', 'Place'),
>                       (' De ', ' de '), (' Le ', ' le '), (' La ', ' la '),
> (' Du ', ' du '), (' Des ', ' des '), (' Les ', ' les '),
>                       (' Au ', ' au '),(' Aux ', ' aux '),('À', 'à'),(' Den
> ',' den '), (" Sur ", " sur "),
>                       (" D'"," d'"), (" L'"," l'"), ("' ","'"),
>                       ("Ecole ","École "), ("Eglise", "Église"),
> ("Chateau", "Château"), ("Cable", "Câble"), ("General", "Général")],
>                'nl': [(" Voor ", " voor "), (" Op ", " op "), (" Pour ", "
> pour "), (" Naar ", " naar "), (" Ter ", " ter "), (" En ", " en "), (" Van
> ", " van "),
>                       ("'T ", "'t "), ("'S ", "'s "), ("-Ter-", "-ter-"),
> (" Het ", " het "),
>                       (" Straat", "straat"), (" Weg", "weg"), (" Laan",
> "laan"), (" Steenweg", "steenweg"),
>                       (" Baan", "baan"), ("Oudebaan", "Oude Baan"),
> ("Grotebaan", "Grote Baan"),
>                       ("de Lijn", "De Lijn")]}
>
> commandsList = []
> streetnames = {}
>
> def getMapView():
>     if Main.main and Main.main.map:
>         return Main.main.map.mapView
>     else:
>         return None
>
> def myOwnCapitalize(word):
>     # JOptionPane.showMessageDialog(Main.parent, word.decode('utf-8'))
>     if word:
>         return word[0].upper() + word[1:]
>     else:
>         return u""
>
> mv = getMapView()
>
> if mv and mv.editLayer and mv.editLayer.data:
>     selectedNodes = mv.editLayer.data.getSelectedNodes()
>     selectedWays = mv.editLayer.data.getWays()
>     selectedRelations = mv.editLayer.data.getSelectedRelations()
>
>     if not(selectedNodes or selectedWays or selectedRelations):
>         JOptionPane.showMessageDialog(Main.parent, "Please select
> something")
>     else:
>         for way in selectedWays:
>             for isoLang in ['nl', 'fr', '']:
>                 correctedName = result = u''
>                 if isoLang:
>                     nameColonIso = 'name:' + isoLang
>                 else:
>                     nameColonIso = 'name'
>                 if way.hasKey(nameColonIso):
>                     name=str(way.get(nameColonIso).encode('utf-8'))
>                     if name in streetnames:
>                         if streetnames[name] == 'ignore':
>                             continue
>                         else:
>                             correctedName = streetnames[name]
>                     else:
>                         Main.main.getCurrentDataSet().setSelected(way)
>                         # dummy = mv.editLayer.data.getSelected()
>                         #
> mv.zoomTo(Main.main.getEditLayer().data.getSelected())
>                         # JOptionPane.showMessageDialog(Main.parent,
> name.decode('utf-8'))
>                         for subname in name.split(";"):
>                             for word in subname.split(" "):
>                                 if word:
>                                     if "-" in word and len(word)>1:
>                                         dashes = word.split("-")
>
>                                         correctedName +=
> myOwnCapitalize(dashes[0])
>                                         for dash in dashes[1:]:
>                                             # if dash[0] == ' ':
>                                                 # correctedName += u"-" +
> myOwnCapitalize(dash[1:])
>                                             # else:
>                                                 correctedName += u"-" +
> myOwnCapitalize(dash.strip())
>                                     elif "'" in word and not("." in word):
>                                         apo=word.split("'")
>                                         if apo[0]: correctedName +=
> myOwnCapitalize(apo[0])
>                                         correctedName += "'"
>                                         if apo[1]: correctedName +=
> myOwnCapitalize(apo[1])
>                                     elif "." in word or len(word)>1 and
> word[1]==word[1].upper() or len(word)>2 and word[2]==word[2].upper():
>                                         correctedName += word
>                                     else:
>                                         correctedName +=
> myOwnCapitalize(word)
>                                     correctedName += ' '
>                             correctedName = correctedName.strip() + ';'
>                         if correctedName and correctedName[-1] == ';':
> correctedName = correctedName[0:-1]
>                         for lang in corrections:
>                             if isoLang and isoLang != lang:
>                                 continue
>                             elif not(isoLang) and way.hasKey('name:fr') and
> way.hasKey('name:nl'):
>                                 correctedName =
> str(way.get('name:fr').encode('utf-8')) + ' - ' +
> str(way.get('name:nl').encode('utf-8'))
>                             else:
>                                 for wrongspelling, correction in
> corrections[lang]:
>                                     correctedName =
> correctedName.replace(wrongspelling, correction)
>                         correctedName = correctedName.strip()
>                     if name != correctedName:
>                         try:
>                             result =
> JOptionPane.showInputDialog(Main.parent,
>                                     "Previous name: " +
> name.decode('utf-8'),
>                                     'Change spelling?',
>                                     JOptionPane.QUESTION_MESSAGE,
>                                     None,
>                                     None,
>                                     correctedName.decode('utf-8'))
>                         except UnicodeDecodeError:
>                             pass
>                         if result: result = result.strip(' -')
>                         print
>                         print nameColonIso
>                         print name
>                         print result
>                         if not(result):
>                             streetnames[name] = 'ignore'
>                         elif result.lower() == u'stop':
>                             break
>                         else:
>                             streetnames[name] = result
>                             newWay = Way(way)
>                             newWay.put(nameColonIso, result)
>                             commandsList.append(Command.ChangeCommand(way,
> newWay))
>                 if commandsList:
>
> Main.main.undoRedo.add(Command.SequenceCommand("Spelling " + nameColonIso +
> ' ' + result, commandsList))
>                     commandsList = []
>             if result and result.lower() == u'stop':
>                 break
>

It still needs some work. I'd prefer a better edit box with custom buttons
(So the Cancel button can be named Ignore) and where all languages are shown
in dedicated edit boxes. Maybe I find out how to accomplish that later. I
also still need to determine whether a primitive is within a given closed
way/area, to establish whether the name should be bilingual, or not (special
case of Brussels Region).

Polyglot
_______________________________________________
Talk-be mailing list
Talk-be@openstreetmap.org
http://lists.openstreetmap.org/listinfo/talk-be

Reply via email to