Changeset 32404 in osm for applications/editors/josm/plugins/OSMRecPlugin/src
- Timestamp:
- 2016-06-25T14:41:52+02:00 (9 years ago)
- Location:
- applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec
- Files:
-
- 1 deleted
- 24 edited
- 5 moved
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/MenuExportAction.java
r32278 r32404 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.osmrec; 2 3 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 4 6 import java.awt.event.ActionEvent; 7 5 8 import org.openstreetmap.josm.actions.JosmAction; 6 9 7 10 /** 8 11 * Exports the OSMRec toggle dialog. 9 * 12 * 10 13 * @author imis-nkarag 11 14 * … … 16 19 * Constructs a new {@code MenuExportAction}. 17 20 */ 18 public MenuExportAction(){ 19 super(tr("OSM Recommendation"), "dialogs/logo-osmrec.png", 20 tr("Recommend categories to your newly created instances."), null, false); 21 public MenuExportAction() { 22 super(tr("OSM Recommendation"), "dialogs/logo-osmrec.png", 23 tr("Recommend categories to your newly created instances."), null, false); 21 24 } 22 25 23 26 @Override 24 27 public void actionPerformed(ActionEvent arg0) { … … 26 29 OSMRecToggleDialog pro = new OSMRecToggleDialog(); 27 30 OSMRecPlugin.getCurrentMapFrame().addToggleDialog(pro); 28 } 31 } 29 32 } 30 33 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecPlugin.java
r31554 r32404 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.plugins.osmrec; 3 3 … … 6 6 import org.openstreetmap.josm.plugins.Plugin; 7 7 import org.openstreetmap.josm.plugins.PluginInformation; 8 //import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog;9 8 10 /** 11 * 9 /** 10 * 12 11 * @author imis-nkarag 13 12 */ 13 public class OSMRecPlugin extends Plugin { 14 14 15 public class OSMRecPlugin extends Plugin{16 17 15 private final MenuExportAction menuExportAction; 18 16 private static MapFrame mapFrame; 19 17 public OSMRecPlugin plugin; 20 21 22 public OSMRecPlugin(PluginInformation info) { 18 19 public OSMRecPlugin(PluginInformation info) { // NO_UCD (unused code) 23 20 super(info); 24 System.out.println(getPluginDir());25 21 menuExportAction = new MenuExportAction(); 26 22 Main.main.menu.toolsMenu.add(menuExportAction); 27 23 } 28 29 /** 30 * Called when the JOSM map frame is created or destroyed. 31 * @param oldFrame 32 * @param newFrame 33 */ 24 34 25 @Override 35 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 26 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 36 27 if (oldFrame == null && newFrame != null) { // map frame added 37 System.out.println("mapFrameInitialized");38 28 setCurrentMapFrame(newFrame); 39 29 setState(this); 40 30 } 41 31 } 42 43 private void setCurrentMapFrame(MapFrame newFrame){ 32 33 private void setCurrentMapFrame(MapFrame newFrame) { 44 34 OSMRecPlugin.mapFrame = newFrame; 45 35 } 46 36 47 37 public static MapFrame getCurrentMapFrame() { 48 38 return mapFrame; 49 39 } 50 51 private void setState(OSMRecPlugin plugin){ 40 41 private void setState(OSMRecPlugin plugin) { 52 42 this.plugin = plugin; 53 43 } 54 55 public OSMRecPlugin getState(){ 44 45 public OSMRecPlugin getState() { 56 46 return plugin; 57 47 } 58 59 // @Override 60 // public PreferenceSetting getPreferenceSetting() { 61 // return new PreferenceEditor(); 62 // } 48 49 // @Override 50 // public PreferenceSetting getPreferenceSetting() { 51 // return new PreferenceEditor(); 52 // } 63 53 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecPluginHelper.java
r31627 r32404 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.osmrec; 2 3 3 import com.vividsolutions.jts.geom.Coordinate;4 import com.vividsolutions.jts.geom.Geometry;5 import com.vividsolutions.jts.geom.GeometryFactory;6 import com.vividsolutions.jts.geom.LineString;7 import com.vividsolutions.jts.geom.LinearRing;8 import com.vividsolutions.jts.geom.Point;9 import com.vividsolutions.jts.geom.Polygon;10 import de.bwaldvogel.liblinear.FeatureNode;11 import de.bwaldvogel.liblinear.Linear;12 import de.bwaldvogel.liblinear.Model;13 4 import static org.openstreetmap.josm.tools.I18n.tr; 14 5 import static org.openstreetmap.josm.tools.I18n.trn; … … 19 10 import java.awt.Cursor; 20 11 import java.awt.Dimension; 12 import java.awt.EventQueue; 21 13 import java.awt.FlowLayout; 22 import java.awt.Font;23 14 import java.awt.GridBagConstraints; 24 15 import java.awt.GridBagLayout; 25 import java.awt.Toolkit;26 import java.awt.datatransfer.Clipboard;27 import java.awt.datatransfer.Transferable;28 16 import java.awt.event.ActionEvent; 29 17 import java.awt.event.FocusAdapter; … … 32 20 import java.awt.event.MouseAdapter; 33 21 import java.awt.event.MouseEvent; 34 import java.awt.event.WindowAdapter;35 import java.awt.event.WindowEvent;36 22 import java.awt.image.BufferedImage; 37 23 import java.beans.PropertyChangeEvent; … … 68 54 import javax.swing.BoxLayout; 69 55 import javax.swing.ButtonGroup; 70 import javax.swing.DefaultListCellRenderer;71 56 import javax.swing.DefaultListModel; 72 57 import javax.swing.GroupLayout; … … 88 73 import javax.swing.JScrollPane; 89 74 import javax.swing.JTextField; 90 import javax.swing.ListCellRenderer;91 75 import javax.swing.SwingConstants; 92 76 import javax.swing.border.EtchedBorder; … … 96 80 import javax.swing.table.DefaultTableModel; 97 81 import javax.swing.text.JTextComponent; 82 98 83 import org.geotools.geometry.jts.JTS; 99 84 import org.geotools.referencing.CRS; … … 105 90 import org.opengis.referencing.operation.MathTransform; 106 91 import org.opengis.referencing.operation.TransformException; 107 108 92 import org.openstreetmap.josm.Main; 109 93 import org.openstreetmap.josm.actions.JosmAction; … … 124 108 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 125 109 import org.openstreetmap.josm.io.XmlWriter; 126 import org.openstreetmap.josm.plugins.container.OSMWay; 127 import org.openstreetmap.josm.plugins.core.TrainWorker; 128 import org.openstreetmap.josm.plugins.extractor.LanguageDetector; 129 import org.openstreetmap.josm.plugins.extractor.SampleModelsExtractor; 130 import org.openstreetmap.josm.plugins.features.ClassFeatures; 131 import org.openstreetmap.josm.plugins.features.GeometryFeatures; 132 import org.openstreetmap.josm.plugins.features.OSMClassification; 133 import org.openstreetmap.josm.plugins.features.TextualFeatures; 110 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 111 import org.openstreetmap.josm.plugins.osmrec.core.TrainWorker; 112 import org.openstreetmap.josm.plugins.osmrec.extractor.LanguageDetector; 113 import org.openstreetmap.josm.plugins.osmrec.extractor.SampleModelsExtractor; 114 import org.openstreetmap.josm.plugins.osmrec.features.ClassFeatures; 115 import org.openstreetmap.josm.plugins.osmrec.features.GeometryFeatures; 116 import org.openstreetmap.josm.plugins.osmrec.features.OSMClassification; 117 import org.openstreetmap.josm.plugins.osmrec.features.TextualFeatures; 118 import org.openstreetmap.josm.plugins.osmrec.parsers.Mapper; 119 import org.openstreetmap.josm.plugins.osmrec.parsers.OSMParser; 120 import org.openstreetmap.josm.plugins.osmrec.parsers.Ontology; 121 import org.openstreetmap.josm.plugins.osmrec.parsers.TextualStatistics; 134 122 import org.openstreetmap.josm.plugins.osmrec.personalization.UserDataExtractAndTrainWorker; 135 import org.openstreetmap.josm.plugins.parsers.Mapper;136 import org.openstreetmap.josm.plugins.parsers.OSMParser;137 import org.openstreetmap.josm.plugins.parsers.Ontology;138 import org.openstreetmap.josm.plugins.parsers.TextualStatistics;139 123 import org.openstreetmap.josm.tools.GBC; 140 124 import org.openstreetmap.josm.tools.Shortcut; 141 125 import org.openstreetmap.josm.tools.WindowGeometry; 142 126 127 import com.vividsolutions.jts.geom.Coordinate; 128 import com.vividsolutions.jts.geom.Geometry; 129 import com.vividsolutions.jts.geom.GeometryFactory; 130 import com.vividsolutions.jts.geom.LineString; 131 import com.vividsolutions.jts.geom.LinearRing; 132 import com.vividsolutions.jts.geom.Point; 133 import com.vividsolutions.jts.geom.Polygon; 134 135 import de.bwaldvogel.liblinear.FeatureNode; 136 import de.bwaldvogel.liblinear.Linear; 137 import de.bwaldvogel.liblinear.Model; 138 143 139 /** 144 140 * Modified PropertiesDialog(since 5633) to serve the plugin functionality. 145 * 141 * 146 142 * @author imis-nkarag 147 143 */ 148 149 144 class OSMRecPluginHelper { 150 145 151 146 private final DefaultTableModel tagData; 152 private final Map<String, Map<String, Integer>> valueCount;153 147 private static Collection<String> fileHistory; 154 148 private Map<File, Double> filesAndWeights = new HashMap<>(); 155 private boolean useCombinedModel = false;156 157 //the most recent file from history. 149 private boolean useCombinedModel; 150 151 //the most recent file from history. 158 152 //all necessary files will reside in a directory of this file 159 153 private static String MAIN_PATH; 160 //private static final String MODEL_PATH = MAIN_PATH.substring(0, MAIN_PATH.lastIndexOf("/")) + "OSMRec_models/";161 154 private static String MODEL_PATH; 162 155 private static String TEXTUAL_LIST_PATH; … … 165 158 private static LanguageDetector languageDetector; 166 159 private static String bestModelPath; 167 private boolean modelWithClasses; // = false;160 private boolean modelWithClasses; 168 161 private final String modelWithClassesPath; 169 162 private boolean useCustomSVMModel = false; … … 171 164 private final String combinedModelClasses; 172 165 private final String combinedModel; 173 166 174 167 // Selection that we are editing by using both dialogs 175 168 Collection<OsmPrimitive> sel; 176 169 177 170 private String changedKey; 178 private String objKey;179 171 180 172 Comparator<AutoCompletionListItem> defaultACItemComparator = new Comparator<AutoCompletionListItem>() { … … 199 191 }; 200 192 201 202 193 OSMRecPluginHelper(DefaultTableModel propertyData, Map<String, Map<String, Integer>> valueCount) { 203 194 this.tagData = propertyData; 204 this.valueCount = valueCount;205 195 fileHistory = Main.pref.getCollection("file-open.history"); 206 if(!fileHistory.isEmpty()){ 207 MAIN_PATH = (String) fileHistory.toArray()[0]; 208 } 209 else{ 196 if (!fileHistory.isEmpty()) { 197 MAIN_PATH = (String) fileHistory.toArray()[0]; 198 } else { 210 199 MAIN_PATH = System.getProperty("user.home"); 211 200 } … … 220 209 221 210 SampleModelsExtractor sampleModelsExtractor = new SampleModelsExtractor(); 222 211 223 212 sampleModelsExtractor.extractSampleSVMmodel("best_model", bestModelPath); 224 213 sampleModelsExtractor.extractSampleSVMmodel("model_with_classes", modelWithClassesPath); 225 226 214 } 227 215 228 216 /** 229 * Open the add selection dialog and add a new key/value to the table (and 230 * to the dataset, of course). 217 * Open the add selection dialog and add a new key/value to the table (and to the dataset, of course). 231 218 */ 232 219 public void addTag() { … … 236 223 237 224 final AddTagsDialog addDialog = new AddTagsDialog(); 238 225 239 226 addDialog.showDialog(); 240 227 … … 247 234 248 235 /** 249 * Edit the value in the tags table row. 250 * @param row The row of the table from which the value is edited. 251 * @param focusOnKey Determines if the initial focus should be set on key instead of value 252 * @since 5653 253 */ 236 * Edit the value in the tags table row. 237 * @param row The row of the table from which the value is edited. 238 * @param focusOnKey Determines if the initial focus should be set on key instead of value 239 * @since 5653 240 */ 254 241 public void editTag(final int row, boolean focusOnKey) { 255 242 changedKey = null; 256 243 sel = Main.main.getInProgressSelection(); 257 //if (sel == null || sel.isEmpty()) return;258 // String key = tagData.getValueAt(row, 0).toString();259 244 String key = ""; 260 // objKey=key;261 245 262 246 @SuppressWarnings("unchecked") … … 264 248 dumPar.put(" ", -1); 265 249 final TrainingDialog editDialog = new TrainingDialog(key, row, 266 dumPar, focusOnKey); 250 dumPar, focusOnKey); 267 251 editDialog.showDialog(); 268 //if (editDialog.getValue() !=1 ) return;269 //editDialog.performTagEdit();270 252 } 271 253 … … 277 259 public String getChangedKey() { 278 260 return changedKey; 279 }280 281 public void resetChangedKey() {282 changedKey = null;283 261 } 284 262 … … 318 296 public void saveTagsIfNeeded() { 319 297 if (PROPERTY_REMEMBER_TAGS.get() && !recentTags.isEmpty()) { 320 List<String> c = new ArrayList<>( 298 List<String> c = new ArrayList<>(recentTags.size()*2); 321 299 for (Tag t: recentTags.keySet()) { 322 300 c.add(t.getKey()); … … 349 327 public final class TrainingDialog extends AbstractTagsDialog { 350 328 351 ListCellRenderer<AutoCompletionListItem> cellRenderer = new ListCellRenderer<AutoCompletionListItem>() {352 final DefaultListCellRenderer def = new DefaultListCellRenderer();353 @Override354 public Component getListCellRendererComponent(JList<? extends AutoCompletionListItem> list,355 AutoCompletionListItem value, int index, boolean isSelected, boolean cellHasFocus){356 Component c = def.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);357 if (c instanceof JLabel) {358 String str = value.getValue();359 if (valueCount.containsKey(objKey)) {360 Map<String, Integer> m = valueCount.get(objKey);361 if (m.containsKey(str)) {362 str = tr("{0} ({1})", str, m.get(str));363 c.setFont(c.getFont().deriveFont(Font.ITALIC + Font.BOLD));364 }365 }366 ((JLabel) c).setText(str);367 }368 return c;369 }370 };371 329 private static final int FIELD_COLUMNS = 4; 372 330 private final JTextField inputFileField; … … 375 333 private final JTextField cParameterField; 376 334 private final JTextField frequencyField; 377 335 378 336 private final JButton fileBrowseButton; 379 337 private final JButton acceptConfigButton; … … 393 351 private final JLabel frequencyErrorMessageLabel; 394 352 private final JProgressBar trainingProgressBar; 395 //private final JLabel userHistoryLabel;396 353 private final JRadioButton byAreaRadioButton; 397 354 private final JRadioButton byTimeRadioButton; … … 406 363 private TrainWorker trainWorker; 407 364 private UserDataExtractAndTrainWorker userDataExtractAndTrainWorker; 408 365 409 366 private TrainingDialog(String key, int row, Map<String, Integer> map, final boolean initialFocusOnKey) { 410 //super(Main.parent, tr("Training process configuration"), new String[] {tr("Start Training"),tr("Cancel")}); 411 super(Main.parent, tr("Training process configuration"), 412 new String[] {tr("Cancel")}); 413 414 setButtonIcons(new String[] {"ok","cancel"}); 367 super(Main.parent, tr("Training process configuration"), new String[] {tr("Cancel")}); 368 369 setButtonIcons(new String[] {"ok", "cancel"}); 415 370 setCancelButton(2); 416 417 JPanel mainPanel = new JPanel(new BorderLayout(10,10)); //6,6 418 JPanel configPanel = new JPanel(new BorderLayout(10,10)); //6,6 //at NORTH of mainPanel 371 372 JPanel mainPanel = new JPanel(new BorderLayout(10, 10)); //6,6 373 JPanel configPanel = new JPanel(new BorderLayout(10, 10)); //6,6 //at NORTH of mainPanel 419 374 JPanel inputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //NORTH at config panel 420 JPanel paramPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //WEST at config panel //config panel has EAST free 421 375 JPanel paramPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //WEST at config panel //config panel has EAST free 376 422 377 JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //SOUTH at config panel 423 378 userHistoryPanel = new JPanel(); //CENTER of config 424 425 trainingProgressBar = new javax.swing.JProgressBar(0,100);379 380 trainingProgressBar = new JProgressBar(0, 100); 426 381 427 382 ButtonGroup paramGroup = new ButtonGroup(); 428 383 ButtonGroup userGroup = new ButtonGroup(); 429 inputFileLabel = new javax.swing.JLabel();430 inputFileField = new javax.swing.JTextField();431 cParameterField = new javax.swing.JTextField();432 topKField = new javax.swing.JTextField();433 frequencyField = new javax.swing.JTextField();434 384 inputFileLabel = new JLabel(); 385 inputFileField = new JTextField(); 386 cParameterField = new JTextField(); 387 topKField = new JTextField(); 388 frequencyField = new JTextField(); 389 435 390 cParameterCheckBox = new JCheckBox("Define C parameter"); 436 391 topKButton = new JRadioButton("Top-K terms"); 437 392 frequencyButton = new JRadioButton("Max-Frequency"); 438 fileBrowseButton = new javax.swing.JButton();439 acceptConfigButton = new javax.swing.JButton("Accept Configuration");440 resetConfigButton = new javax.swing.JButton("Reset Configuration/Cancel training");441 startTrainingButton = new javax.swing.JButton("Train Model");442 443 inputFileErrorMessageLabel = new javax.swing.JLabel("");444 cErrorMessageLabel = new javax.swing.JLabel("");445 topKErrorMessageLabel = new javax.swing.JLabel("");446 frequencyErrorMessageLabel = new javax.swing.JLabel("");447 393 fileBrowseButton = new JButton(); 394 acceptConfigButton = new JButton("Accept Configuration"); 395 resetConfigButton = new JButton("Reset Configuration/Cancel training"); 396 startTrainingButton = new JButton("Train Model"); 397 398 inputFileErrorMessageLabel = new JLabel(""); 399 cErrorMessageLabel = new JLabel(""); 400 topKErrorMessageLabel = new JLabel(""); 401 frequencyErrorMessageLabel = new JLabel(""); 402 448 403 trainFromUserCheckBox = new JCheckBox("Train Model From User"); 449 404 byAreaRadioButton = new JRadioButton("By Area"); 450 405 byTimeRadioButton = new JRadioButton("By Time"); 451 userNameLabel = new javax.swing.JLabel("Username:");452 userNameField = new javax.swing.JTextField();453 454 daysLabel = new javax.swing.JLabel("Days: ");455 daysField = new javax.swing.JTextField();456 406 userNameLabel = new JLabel("Username:"); 407 userNameField = new JTextField(); 408 409 daysLabel = new JLabel("Days: "); 410 daysField = new JTextField(); 411 457 412 cParameterCheckBox.setSelected(true); 458 413 userHistoryPanel.setEnabled(false); … … 465 420 daysField.setEnabled(false); 466 421 userNameField.setColumns(FIELD_COLUMNS); 467 daysField.setColumns(FIELD_COLUMNS); 468 422 daysField.setColumns(FIELD_COLUMNS); 423 469 424 Collection<String> fileHistory = Main.pref.getCollection("file-open.history"); 470 if(!fileHistory.isEmpty()){ 471 //System.out.println(fileName); 425 if (!fileHistory.isEmpty()) { 472 426 inputFileField.setText(MAIN_PATH); 473 427 } 474 428 475 429 fileBrowseButton.setText("..."); 476 430 inputFileLabel.setText("OSM filepath: "); … … 480 434 frequencyField.setText("200"); 481 435 cParameterField.setText("0.01"); 482 436 483 437 cParameterField.setColumns(FIELD_COLUMNS); 484 438 cParameterField.setEditable(false); 485 439 cParameterField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 486 440 cErrorMessageLabel.setForeground(Color.RED); 487 cErrorMessageLabel.setMinimumSize(new Dimension(150,10)); 488 489 topKButton.setSelected(true); 441 cErrorMessageLabel.setMinimumSize(new Dimension(150, 10)); 442 443 topKButton.setSelected(true); 490 444 topKField.setColumns(FIELD_COLUMNS); 491 445 topKField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 492 446 topKErrorMessageLabel.setForeground(Color.RED); 493 447 494 448 frequencyField.setEditable(false); 495 449 frequencyField.setColumns(FIELD_COLUMNS); 496 450 frequencyField.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); 497 451 frequencyErrorMessageLabel.setForeground(Color.RED); 498 452 499 453 fileBrowseButton.addActionListener(new java.awt.event.ActionListener() { 500 454 @Override … … 506 460 @Override 507 461 public void actionPerformed(java.awt.event.ActionEvent evt) { 508 if(topKButton.isSelected()) {462 if (topKButton.isSelected()) { 509 463 frequencyField.setEditable(false); 510 464 topKField.setEditable(true); 511 } 512 else{ 465 } else { 513 466 frequencyField.setEditable(true); 514 467 topKField.setEditable(false); … … 519 472 @Override 520 473 public void actionPerformed(java.awt.event.ActionEvent evt) { 521 if(frequencyButton.isSelected()){ 474 if (frequencyButton.isSelected()) { 522 475 topKField.setEditable(false); 523 476 frequencyField.setEditable(true); 524 } 525 else{ 477 } else { 526 478 topKField.setEditable(true); 527 479 frequencyField.setEditable(false); 528 480 } 529 481 } 530 }); 482 }); 531 483 cParameterCheckBox.addActionListener(new java.awt.event.ActionListener() { 532 484 @Override 533 485 public void actionPerformed(java.awt.event.ActionEvent evt) { 534 if(cParameterCheckBox.isSelected()){ 535 cParameterField.setEditable(true); 536 } 537 else{ 486 if (cParameterCheckBox.isSelected()) { 487 cParameterField.setEditable(true); 488 } else { 538 489 cParameterField.setEditable(false); 539 490 } … … 550 501 public void actionPerformed(java.awt.event.ActionEvent evt) { 551 502 resetConfigButtonActionPerformed(); 552 553 503 } 554 504 }); 555 505 556 506 startTrainingButton.addActionListener(new java.awt.event.ActionListener() { 557 507 @Override 558 508 public void actionPerformed(java.awt.event.ActionEvent evt) { 559 if(!acceptConfigButton.isEnabled()){ 509 if (!acceptConfigButton.isEnabled()) { 560 510 startTraining(); 561 } 562 else{ 511 } else { 563 512 System.out.println("Set values first!"); 564 513 } 565 514 } 566 515 }); 567 516 568 517 trainFromUserCheckBox.addActionListener(new java.awt.event.ActionListener() { 569 518 @Override 570 519 public void actionPerformed(java.awt.event.ActionEvent evt) { 571 if(trainFromUserCheckBox.isSelected()){ 520 if (trainFromUserCheckBox.isSelected()) { 572 521 userHistoryPanel.setEnabled(true); 573 522 byAreaRadioButton.setEnabled(true); … … 577 526 daysLabel.setEnabled(true); 578 527 daysField.setEnabled(true); 579 } 580 else{ 528 } else { 581 529 userHistoryPanel.setEnabled(false); 582 530 byAreaRadioButton.setEnabled(false); … … 585 533 userNameField.setEnabled(false); 586 534 daysLabel.setEnabled(false); 587 daysField.setEnabled(false); 588 } 589 } 590 }); 591 535 daysField.setEnabled(false); 536 } 537 } 538 }); 539 592 540 byAreaRadioButton.addActionListener(new java.awt.event.ActionListener() { 593 541 @Override 594 542 public void actionPerformed(java.awt.event.ActionEvent evt) { 595 if(byAreaRadioButton.isSelected()){ 596 daysField.setEditable(false); 597 } 598 else{ 599 daysField.setEditable(true); 600 } 601 } 602 }); 603 543 if (byAreaRadioButton.isSelected()) { 544 daysField.setEditable(false); 545 } else { 546 daysField.setEditable(true); 547 } 548 } 549 }); 550 604 551 byTimeRadioButton.addActionListener(new java.awt.event.ActionListener() { 605 552 @Override 606 553 public void actionPerformed(java.awt.event.ActionEvent evt) { 607 if(byTimeRadioButton.isSelected()){ 608 daysField.setEditable(true); 609 } 610 else{ 611 daysField.setEditable(false); 612 } 613 } 614 }); 615 554 if (byTimeRadioButton.isSelected()) { 555 daysField.setEditable(true); 556 } else { 557 daysField.setEditable(false); 558 } 559 } 560 }); 561 616 562 //grouplayout for input panel 563 buildInputPanelGroupLayout(inputPanel); 564 565 //grouplayout for param panel 566 buildParamPanelGroupLayout(paramPanel); 567 568 inputPanel.add(inputFileLabel); 569 inputPanel.add(inputFileField); 570 inputPanel.add(fileBrowseButton); 571 inputPanel.add(inputFileErrorMessageLabel); 572 573 paramGroup.add(topKButton); 574 paramGroup.add(frequencyButton); 575 576 paramPanel.add(cParameterCheckBox); 577 paramPanel.add(cParameterField); 578 paramPanel.add(cErrorMessageLabel); 579 paramPanel.add(topKButton); 580 paramPanel.add(topKField); 581 paramPanel.add(topKErrorMessageLabel); 582 paramPanel.add(frequencyButton); 583 paramPanel.add(frequencyField); 584 paramPanel.add(frequencyErrorMessageLabel); 585 586 southPanel.add(acceptConfigButton); 587 southPanel.add(resetConfigButton); 588 southPanel.add(trainFromUserCheckBox); 589 590 userGroup.add(byAreaRadioButton); 591 userGroup.add(byTimeRadioButton); 592 userHistoryPanel.add(byAreaRadioButton); 593 userHistoryPanel.add(byTimeRadioButton); 594 userHistoryPanel.add(daysLabel); 595 userHistoryPanel.add(daysField); 596 userHistoryPanel.add(userNameLabel); 597 userHistoryPanel.add(userNameField); 598 599 //grouplayout for user history panel 600 /* 601 userNameLabel userField 602 arearadiobutton 603 timeradiobutton daysLabel daysField 604 */ 605 buildUserHistoryPanelGroupLayout(); 606 607 configPanel.add(inputPanel, BorderLayout.NORTH); 608 configPanel.add(userHistoryPanel, BorderLayout.EAST); 609 configPanel.add(paramPanel, BorderLayout.WEST); 610 configPanel.add(southPanel, BorderLayout.SOUTH); 611 612 userHistoryPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory 613 .createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE), "Train by user History")); 614 paramPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory 615 .createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE), "SVM Configuration")); 616 inputPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 617 configPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 618 619 mainPanel.add(configPanel, BorderLayout.NORTH); 620 mainPanel.add(startTrainingButton, BorderLayout.CENTER); 621 mainPanel.add(trainingProgressBar, BorderLayout.SOUTH); 622 623 AutoCompletionManager autocomplete = Main.getLayerManager().getEditLayer().data.getAutoCompletionManager(); 624 List<AutoCompletionListItem> keyList = autocomplete.getKeys(); 625 Collections.sort(keyList, defaultACItemComparator); 626 627 setContent(mainPanel, false); 628 } 629 630 private void buildInputPanelGroupLayout(JPanel inputPanel) { 617 631 GroupLayout inputGroupLayout = new GroupLayout(inputPanel); 618 632 inputPanel.setLayout(inputGroupLayout); 619 633 inputGroupLayout.setAutoCreateGaps(true); 620 634 inputGroupLayout.setAutoCreateContainerGaps(true); 621 635 622 636 GroupLayout.SequentialGroup inputHorGroup = inputGroupLayout.createSequentialGroup(); 623 637 inputHorGroup.addGroup(inputGroupLayout.createParallelGroup().addComponent(inputFileLabel). … … 626 640 inputHorGroup.addGroup(inputGroupLayout.createParallelGroup().addComponent(fileBrowseButton)); 627 641 inputGroupLayout.setHorizontalGroup(inputHorGroup); 628 642 629 643 GroupLayout.SequentialGroup inputVerGroup = inputGroupLayout.createSequentialGroup(); 630 644 inputVerGroup.addGroup(inputGroupLayout.createParallelGroup(Alignment.LEADING).addComponent(inputFileLabel). … … 633 647 addComponent(inputFileErrorMessageLabel)); 634 648 inputGroupLayout.setVerticalGroup(inputVerGroup); 635 636 637 //grouplayout for param panel649 } 650 651 private void buildParamPanelGroupLayout(JPanel paramPanel) { 638 652 GroupLayout paramGroupLayout = new GroupLayout(paramPanel); 639 653 paramPanel.setLayout(paramGroupLayout); 640 654 paramGroupLayout.setAutoCreateGaps(true); 641 655 paramGroupLayout.setAutoCreateContainerGaps(true); 642 656 643 657 GroupLayout.SequentialGroup paramHorGroup = paramGroupLayout.createSequentialGroup(); 644 658 paramHorGroup.addGroup(paramGroupLayout.createParallelGroup().addComponent(topKButton). 645 659 addComponent(frequencyButton).addComponent(cParameterCheckBox)); 646 660 paramHorGroup.addGroup(paramGroupLayout.createParallelGroup().addComponent(cParameterField). 647 addComponent(topKField).addComponent(frequencyField)); 661 addComponent(topKField).addComponent(frequencyField)); 648 662 paramHorGroup.addGroup(paramGroupLayout.createParallelGroup().addComponent(cErrorMessageLabel). 649 663 addComponent(topKErrorMessageLabel).addComponent(frequencyErrorMessageLabel)); 650 664 paramGroupLayout.setHorizontalGroup(paramHorGroup); 651 665 652 666 GroupLayout.SequentialGroup paramVerGroup = paramGroupLayout.createSequentialGroup(); 653 667 paramVerGroup.addGroup(paramGroupLayout.createParallelGroup(Alignment.BASELINE). … … 657 671 paramVerGroup.addGroup(paramGroupLayout.createParallelGroup(Alignment.BASELINE). 658 672 addComponent(frequencyButton).addComponent(frequencyField).addComponent(frequencyErrorMessageLabel)); 659 paramGroupLayout.setVerticalGroup(paramVerGroup); 660 661 inputPanel.add(inputFileLabel); 662 inputPanel.add(inputFileField); 663 inputPanel.add(fileBrowseButton); 664 inputPanel.add(inputFileErrorMessageLabel); 665 666 paramGroup.add(topKButton); 667 paramGroup.add(frequencyButton); 668 669 paramPanel.add(cParameterCheckBox); 670 paramPanel.add(cParameterField); 671 paramPanel.add(cErrorMessageLabel); 672 paramPanel.add(topKButton); 673 paramPanel.add(topKField); 674 paramPanel.add(topKErrorMessageLabel); 675 paramPanel.add(frequencyButton); 676 paramPanel.add(frequencyField); 677 paramPanel.add(frequencyErrorMessageLabel); 678 679 southPanel.add(acceptConfigButton); 680 southPanel.add(resetConfigButton); 681 southPanel.add(trainFromUserCheckBox); 682 683 userGroup.add(byAreaRadioButton); 684 userGroup.add(byTimeRadioButton); 685 userHistoryPanel.add(byAreaRadioButton); 686 userHistoryPanel.add(byTimeRadioButton); 687 userHistoryPanel.add(daysLabel); 688 userHistoryPanel.add(daysField); 689 userHistoryPanel.add(userNameLabel); 690 userHistoryPanel.add(userNameField); 691 //userHistoryPanel.add(trainFromUserCheckBox); 692 693 //grouplayout for user history panel 694 /* 695 userNameLabel userField 696 arearadiobutton 697 timeradiobutton daysLabel daysField 698 */ 673 paramGroupLayout.setVerticalGroup(paramVerGroup); 674 } 675 676 private void buildUserHistoryPanelGroupLayout() { 699 677 GroupLayout userHistoryGroupLayout = new GroupLayout(userHistoryPanel); 700 //userHistoryGroupLayout.se701 678 userHistoryPanel.setLayout(userHistoryGroupLayout); 702 679 userHistoryGroupLayout.setAutoCreateGaps(true); 703 userHistoryGroupLayout.setAutoCreateContainerGaps(true); 704 userHistoryGroupLayout.linkSize(SwingConstants.HORIZONTAL, userNameField, daysLabel,daysField); 705 680 userHistoryGroupLayout.setAutoCreateContainerGaps(true); 681 userHistoryGroupLayout.linkSize(SwingConstants.HORIZONTAL, userNameField, daysLabel, daysField); 682 706 683 GroupLayout.SequentialGroup userHistoryHorGroup = userHistoryGroupLayout.createSequentialGroup(); 707 684 708 685 userHistoryHorGroup.addGroup(userHistoryGroupLayout.createParallelGroup().addComponent(userNameLabel) 709 .addComponent(byAreaRadioButton).addComponent(byTimeRadioButton)); 686 .addComponent(byAreaRadioButton).addComponent(byTimeRadioButton)); 710 687 userHistoryHorGroup.addGroup(userHistoryGroupLayout.createParallelGroup().addComponent(userNameField) 711 .addComponent(daysLabel)); 688 .addComponent(daysLabel)); 712 689 userHistoryHorGroup.addGroup(userHistoryGroupLayout.createParallelGroup().addComponent(daysField)); 713 690 userHistoryGroupLayout.setHorizontalGroup(userHistoryHorGroup); 714 691 715 692 GroupLayout.SequentialGroup userHistoryVerGroup = userHistoryGroupLayout.createSequentialGroup(); 716 693 userHistoryVerGroup.addGroup(userHistoryGroupLayout.createParallelGroup(Alignment.BASELINE). 717 addComponent(userNameLabel).addComponent(userNameField)); 694 addComponent(userNameLabel).addComponent(userNameField)); 718 695 userHistoryVerGroup.addGroup(userHistoryGroupLayout.createParallelGroup(Alignment.BASELINE). 719 addComponent(byAreaRadioButton)); 696 addComponent(byAreaRadioButton)); 720 697 userHistoryVerGroup.addGroup(userHistoryGroupLayout.createParallelGroup(Alignment.BASELINE). 721 addComponent(byTimeRadioButton).addComponent(daysLabel).addComponent(daysField)); 698 addComponent(byTimeRadioButton).addComponent(daysLabel).addComponent(daysField)); 722 699 userHistoryGroupLayout.setVerticalGroup(userHistoryVerGroup); 723 724 configPanel.add(inputPanel, BorderLayout.NORTH); 725 configPanel.add(userHistoryPanel, BorderLayout.EAST); 726 configPanel.add(paramPanel, BorderLayout.WEST); 727 configPanel.add(southPanel, BorderLayout.SOUTH); 728 729 userHistoryPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory 730 .createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE), "Train by user History")); 731 paramPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory 732 .createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE), "SVM Configuration")); 733 inputPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 734 configPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 735 736 mainPanel.add(configPanel, BorderLayout.NORTH); 737 mainPanel.add(startTrainingButton, BorderLayout.CENTER); 738 mainPanel.add(trainingProgressBar, BorderLayout.SOUTH); 739 740 AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager(); 741 List<AutoCompletionListItem> keyList = autocomplete.getKeys(); 742 Collections.sort(keyList, defaultACItemComparator); 743 744 setContent(mainPanel, false); 745 746 addWindowListener(new WindowAdapter() { 747 @Override 748 public void windowOpened(WindowEvent e) { 749 if (initialFocusOnKey) { 750 selectKeysComboBox(); 751 } else { 752 selectValuesCombobox(); 753 } 754 } 755 }); 756 } 757 758 private void inputFileChooserButtonActionPerformed(java.awt.event.ActionEvent evt) { 700 } 701 702 private void inputFileChooserButtonActionPerformed(ActionEvent evt) { 759 703 try { 760 704 final File file = new File(inputFileField.getText()); … … 762 706 763 707 final int returnVal = fileChooser.showOpenDialog(this); 764 if(returnVal == JFileChooser.APPROVE_OPTION) { 708 if (returnVal == JFileChooser.APPROVE_OPTION) { 765 709 inputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath()); 766 710 } 767 } 768 catch (RuntimeException ex) { 711 } catch (RuntimeException ex) { 769 712 Main.warn(ex); 770 //ex.printStackTrace(); 771 } 772 } 773 774 private void acceptConfigButtonActionPerformed(java.awt.event.ActionEvent evt) { 713 } 714 } 715 716 private void acceptConfigButtonActionPerformed(ActionEvent evt) { 775 717 //parse values 776 718 inputFileValue = inputFileField.getText(); 777 778 if(!new File(inputFileValue).exists()){ 719 720 if (!new File(inputFileValue).exists()) { 779 721 inputFileErrorMessageLabel.setText("OSM file does not exist"); 780 722 resetConfigButtonActionPerformed(); 781 723 return; 782 724 } 783 784 if(cParameterCheckBox.isSelected()){ 725 726 if (cParameterCheckBox.isSelected()) { 785 727 String c = cParameterField.getText(); 786 728 try { 787 cParameterValue = Double.parseDouble(c.replace(",",".")); 729 cParameterValue = Double.parseDouble(c.replace(",", ".")); 788 730 cErrorMessageLabel.setText(""); 789 } 790 catch(NumberFormatException ex){ 791 731 } catch (NumberFormatException ex) { 792 732 cErrorMessageLabel.setText("Must be a number!"); 793 System.out.println("c must be a number!" + ex); //make empty textLabel beside c param to notify errors 733 System.out.println("c must be a number!" + ex); //make empty textLabel beside c param to notify errors 794 734 resetConfigButtonActionPerformed(); 795 return; 796 } 797 crossValidateFlag = false; 798 } 799 else{ 735 return; 736 } 737 crossValidateFlag = false; 738 } else { 800 739 crossValidateFlag = true; 801 740 } 802 803 if(topKButton.isSelected()){ 741 742 if (topKButton.isSelected()) { 804 743 String k = topKField.getText(); 805 744 try { 806 topKvalue = Integer.parseInt(k); 745 topKvalue = Integer.parseInt(k); 807 746 topKErrorMessageLabel.setText(""); 808 } 809 catch(NumberFormatException ex){ 810 topKErrorMessageLabel.setText("Must be an Integer!"); 811 resetConfigButtonActionPerformed(); 812 return; 813 } 814 } 815 else{ 816 String f = frequencyField.getText(); 817 try{ 818 frequencyValue = Integer.parseInt(f); 819 frequencyErrorMessageLabel.setText(""); 820 } 821 catch(NumberFormatException ex){ 822 frequencyErrorMessageLabel.setText("Must be an Integer!"); 747 } catch (NumberFormatException ex) { 748 topKErrorMessageLabel.setText("Must be an Integer!"); 823 749 resetConfigButtonActionPerformed(); 824 750 return; 825 751 } 826 } 827 828 if(trainFromUserCheckBox.isSelected()){ 752 } else { 753 String f = frequencyField.getText(); 754 try { 755 frequencyValue = Integer.parseInt(f); 756 frequencyErrorMessageLabel.setText(""); 757 } catch (NumberFormatException ex) { 758 frequencyErrorMessageLabel.setText("Must be an Integer!"); 759 resetConfigButtonActionPerformed(); 760 return; 761 } 762 } 763 764 if (trainFromUserCheckBox.isSelected()) { 829 765 usernameValue = userNameField.getText(); 830 if(byTimeRadioButton.isSelected()){ 831 try{ 766 if (byTimeRadioButton.isSelected()) { 767 try { 832 768 daysValue = Integer.parseInt(daysField.getText()); 833 } 834 catch (NumberFormatException ex){ 769 } catch (NumberFormatException ex) { 835 770 daysField.setText("Integer!"); 836 771 Main.warn(ex); 837 } 838 } 839 772 } 773 } 774 840 775 userHistoryPanel.setEnabled(false); 841 776 byAreaRadioButton.setEnabled(false); … … 844 779 userNameField.setEnabled(false); 845 780 daysLabel.setEnabled(false); 846 daysField.setEnabled(false); 847 } 848 849 System.out.println("Running configuration:" + "\nC parameter: " + cParameterValue +" \ntopK: " + topKvalue 850 + "\nMax Frequency: " + frequencyValue + "\nCross Validate?: " + crossValidateFlag); 851 781 daysField.setEnabled(false); 782 } 783 784 System.out.println("Running configuration:" + "\nC parameter: " + cParameterValue +" \ntopK: " + topKvalue 785 + "\nMax Frequency: " + frequencyValue + "\nCross Validate?: " + crossValidateFlag); 786 852 787 trainFromUserCheckBox.setEnabled(false); 853 788 inputFileField.setEditable(false); 854 cParameterField.setEditable(false); 789 cParameterField.setEditable(false); 855 790 topKField.setEditable(false); 856 frequencyField.setEditable(false); 857 cParameterCheckBox.setEnabled(false); 791 frequencyField.setEditable(false); 792 cParameterCheckBox.setEnabled(false); 858 793 topKButton.setEnabled(false); 859 794 frequencyButton.setEnabled(false); 860 acceptConfigButton.setEnabled(false); 861 fileBrowseButton.setEnabled(false); 862 } 863 864 private void resetConfigButtonActionPerformed() { 865 if(trainWorker != null){ 795 acceptConfigButton.setEnabled(false); 796 fileBrowseButton.setEnabled(false); 797 } 798 799 private void resetConfigButtonActionPerformed() { 800 if (trainWorker != null) { 866 801 try { 867 802 trainWorker.cancel(true); 868 } 869 catch(CancellationException ex){ 870 startTrainingButton.setEnabled(true); 871 System.out.println(ex); 872 } 873 } 874 if(userDataExtractAndTrainWorker != null){ 803 } catch (CancellationException ex) { 804 startTrainingButton.setEnabled(true); 805 System.out.println(ex); 806 } 807 } 808 if (userDataExtractAndTrainWorker != null) { 875 809 try { 876 810 userDataExtractAndTrainWorker.cancel(true); 877 } 878 catch(CancellationException ex){ 879 startTrainingButton.setEnabled(true); 880 System.out.println(ex); 881 } 811 } catch (CancellationException ex) { 812 startTrainingButton.setEnabled(true); 813 System.out.println(ex); 814 } 882 815 } 883 816 inputFileField.setEditable(true); 884 cParameterField.setEditable(true); 817 cParameterField.setEditable(true); 885 818 topKField.setEditable(true); 886 frequencyField.setEditable(true); 887 cParameterCheckBox.setEnabled(true); 819 frequencyField.setEditable(true); 820 cParameterCheckBox.setEnabled(true); 888 821 topKButton.setEnabled(true); 889 822 frequencyButton.setEnabled(true); 890 acceptConfigButton.setEnabled(true); 823 acceptConfigButton.setEnabled(true); 891 824 fileBrowseButton.setEnabled(true); 892 825 trainFromUserCheckBox.setEnabled(true); 893 894 if(trainFromUserCheckBox.isSelected()){ 826 827 if (trainFromUserCheckBox.isSelected()) { 895 828 userHistoryPanel.setEnabled(true); 896 829 byAreaRadioButton.setEnabled(true); … … 899 832 userNameField.setEnabled(true); 900 833 daysLabel.setEnabled(true); 901 daysField.setEnabled(true); 902 } 903 } 904 834 daysField.setEnabled(true); 835 } 836 } 837 905 838 private void startTraining() { 906 839 startTrainingButton.setEnabled(false); 907 908 if(trainFromUserCheckBox.isSelected()){ //if user training. train by area or days 909 910 java.awt.EventQueue.invokeLater(new Runnable() { 840 841 if (trainFromUserCheckBox.isSelected()) { //if user training. train by area or days 842 EventQueue.invokeLater(new Runnable() { 911 843 @Override 912 844 public void run() { 913 845 914 userDataExtractAndTrainWorker = new UserDataExtractAndTrainWorker 915 (inputFileValue, usernameValue, daysValue, byAreaRadioButton.isSelected(), 916 crossValidateFlag, cParameterValue, topKvalue, frequencyValue, 917 topKButton.isSelected(), languageDetector); 918 846 userDataExtractAndTrainWorker = new UserDataExtractAndTrainWorker(inputFileValue, usernameValue, daysValue, 847 byAreaRadioButton.isSelected(), crossValidateFlag, cParameterValue, topKvalue, frequencyValue, 848 topKButton.isSelected(), languageDetector); 849 919 850 userDataExtractAndTrainWorker.addPropertyChangeListener(new PropertyChangeListener() { 920 921 @Override 851 @Override 922 852 public void propertyChange(PropertyChangeEvent evt) { 923 if("progress".equals(evt.getPropertyName())) { 853 if ("progress".equals(evt.getPropertyName())) { 924 854 int progress = (Integer) evt.getNewValue(); 925 855 trainingProgressBar.setValue(progress); 926 if(progress == 100){ 856 if (progress == 100) { 927 857 startTrainingButton.setEnabled(true); 928 858 } 929 859 } 930 } 931 }); 860 } 861 }); 932 862 933 863 try { … … 936 866 } catch (Exception ex) { 937 867 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); 938 } 939 } 940 }); 941 } 942 else { 943 944 java.awt.EventQueue.invokeLater(new Runnable() { 945 //private TrainWorker trainWorker; 868 } 869 } 870 }); 871 } else { 872 EventQueue.invokeLater(new Runnable() { 946 873 @Override 947 874 public void run() { 948 949 trainWorker = new TrainWorker 950 (inputFileValue, crossValidateFlag, cParameterValue, topKvalue, frequencyValue, 951 topKButton.isSelected(), languageDetector); 952 875 trainWorker = new TrainWorker(inputFileValue, crossValidateFlag, cParameterValue, topKvalue, frequencyValue, 876 topKButton.isSelected(), languageDetector); 877 953 878 trainWorker.addPropertyChangeListener(new PropertyChangeListener() { 954 @Override 879 @Override 955 880 public void propertyChange(PropertyChangeEvent evt) { 956 if("progress".equals(evt.getPropertyName())) { 881 if ("progress".equals(evt.getPropertyName())) { 957 882 int progress = (Integer) evt.getNewValue(); 958 883 trainingProgressBar.setValue(progress); 959 if(progress == 100){ 884 if (progress == 100) { 960 885 startTrainingButton.setEnabled(true); 961 886 } 962 887 } 963 // if(cancelled){ 964 // trainWorker.cancel(true); 965 // } 966 } 967 }); 888 } 889 }); 968 890 969 891 try { … … 971 893 } catch (Exception ex) { 972 894 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); 973 } 974 } 975 }); 895 } 896 } 897 }); 976 898 } 977 899 } 978 900 } 979 901 980 public static final BooleanProperty PROPERTY_FIX_TAG_LOCALE = new BooleanProperty("properties.fix-tag-combobox-locale", false); 981 public static final BooleanProperty PROPERTY_REMEMBER_TAGS = new BooleanProperty("properties.remember-recently-added-tags", true); 982 public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = new IntegerProperty("properties.recently-added-tags", DEFAULT_LRU_TAGS_NUMBER); 983 984 abstract class AbstractTagsDialog extends ExtendedDialog { 902 public static final BooleanProperty PROPERTY_FIX_TAG_LOCALE = 903 new BooleanProperty("properties.fix-tag-combobox-locale", false); 904 public static final BooleanProperty PROPERTY_REMEMBER_TAGS = 905 new BooleanProperty("properties.remember-recently-added-tags", true); 906 public static final IntegerProperty PROPERTY_RECENT_TAGS_NUMBER = 907 new IntegerProperty("properties.recently-added-tags", DEFAULT_LRU_TAGS_NUMBER); 908 909 abstract static class AbstractTagsDialog extends ExtendedDialog { 985 910 AutoCompletingComboBox keys; 986 911 AutoCompletingComboBox values; 987 Component componentUnderMouse; 988 989 public AbstractTagsDialog(Component parent, String title, String[] buttonTexts) { 912 913 AbstractTagsDialog(Component parent, String title, String[] buttonTexts) { 990 914 super(parent, title, buttonTexts); 991 915 addMouseListener(new PopupMenuLauncher(popupMenu)); … … 1004 928 1005 929 setRememberWindowGeometry(getClass().getName() + ".geometry", 1006 WindowGeometry.centerInWindow(Main.parent, size)); 930 WindowGeometry.centerInWindow(Main.parent, size)); 1007 931 } 1008 932 … … 1024 948 rememberWindowGeometry(geometry); 1025 949 } 1026 if(keys != null){ 1027 keys.setFixedLocale(PROPERTY_FIX_TAG_LOCALE.get()); 1028 } 950 if (keys != null) { 951 keys.setFixedLocale(PROPERTY_FIX_TAG_LOCALE.get()); 952 } 1029 953 } 1030 954 super.setVisible(visible); 1031 955 } 1032 956 1033 private void selectACComboBoxSavingUnixBuffer(AutoCompletingComboBox cb) {1034 // select combobox with saving unix system selection (middle mouse paste)1035 Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();1036 if(sysSel != null) {1037 Transferable old = sysSel.getContents(null);1038 cb.requestFocusInWindow();1039 cb.getEditor().selectAll();1040 sysSel.setContents(old, null);1041 } else {1042 cb.requestFocusInWindow();1043 cb.getEditor().selectAll();1044 }1045 }1046 1047 public void selectKeysComboBox() {1048 //selectACComboBoxSavingUnixBuffer(keys);1049 }1050 1051 public void selectValuesCombobox() {1052 //selectACComboBoxSavingUnixBuffer(values);1053 }1054 1055 957 /** 1056 * Create a focus handling adapter and apply in to the editor component of value 1057 * autocompletion box. 1058 * @param autocomplete Manager handling the autocompletion 1059 * @param comparator Class to decide what values are offered on autocompletion 1060 * @return The created adapter 1061 */ 958 * Create a focus handling adapter and apply in to the editor component of value 959 * autocompletion box. 960 * @param autocomplete Manager handling the autocompletion 961 * @param comparator Class to decide what values are offered on autocompletion 962 * @return The created adapter 963 */ 1062 964 protected FocusAdapter addFocusAdapter(final AutoCompletionManager autocomplete, final Comparator<AutoCompletionListItem> comparator) { 1063 // get the combo box' editor component 1064 JTextComponent editor = (JTextComponent)values.getEditor().getEditorComponent(); 1065 // Refresh the values model when focus is gained 1066 FocusAdapter focus = new FocusAdapter() { 1067 @Override 1068 public void focusGained(FocusEvent e) { 1069 String key = keys.getEditor().getItem().toString(); 1070 1071 List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key)); 1072 Collections.sort(valueList, comparator); 1073 1074 values.setPossibleACItems(valueList); 1075 values.getEditor().selectAll(); 1076 objKey=key; 1077 } 1078 }; 1079 editor.addFocusListener(focus); 1080 return focus; 965 // get the combo box' editor component 966 JTextComponent editor = (JTextComponent) values.getEditor().getEditorComponent(); 967 // Refresh the values model when focus is gained 968 FocusAdapter focus = new FocusAdapter() { 969 @Override 970 public void focusGained(FocusEvent e) { 971 String key = keys.getEditor().getItem().toString(); 972 973 List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key)); 974 Collections.sort(valueList, comparator); 975 976 values.setPossibleACItems(valueList); 977 values.getEditor().selectAll(); 978 } 979 }; 980 editor.addFocusListener(focus); 981 return focus; 1081 982 } 1082 983 1083 984 protected JPopupMenu popupMenu = new JPopupMenu() { 1084 985 JCheckBoxMenuItem fixTagLanguageCb = new JCheckBoxMenuItem( 1085 new AbstractAction(tr("Use English language for tag by default")){ 1086 @Override 1087 public void actionPerformed(ActionEvent e) { 1088 boolean sel =((JCheckBoxMenuItem) e.getSource()).getState();1089 PROPERTY_FIX_TAG_LOCALE.put(sel); 1090 } 1091 }); 986 new AbstractAction(tr("Use English language for tag by default")) { 987 @Override 988 public void actionPerformed(ActionEvent e) { 989 boolean sel = ((JCheckBoxMenuItem) e.getSource()).getState(); 990 PROPERTY_FIX_TAG_LOCALE.put(sel); 991 } 992 }); 1092 993 { 1093 994 add(fixTagLanguageCb); … … 1097 998 } 1098 999 1099 class ModelSettingsDialog extends JPanel{ 1100 1000 class ModelSettingsDialog extends JPanel { 1001 1101 1002 private final JLabel chooseModelLabel; 1102 1003 private final JButton chooseModelButton; 1103 1004 private final JTextField chooseModelTextField; 1104 1005 1105 1006 private final DefaultListModel<String> combinationDefaultListModel = new DefaultListModel<>(); 1106 private final JList<String> modelCombinationList = new javax.swing.JList<>(combinationDefaultListModel);1007 private final JList<String> modelCombinationList = new JList<>(combinationDefaultListModel); 1107 1008 private final JPanel modelCombinationPanel; 1108 //private final JTextField weightTextField;1109 1009 private final JPanel weightsPanel; 1110 //private final int hei;1111 //private final int wi;1112 1010 private final JCheckBox useModelCombinationCheckbox; 1113 1011 private final JButton acceptWeightsButton; … … 1126 1024 private final TitledBorder weightTitle; 1127 1025 private final TitledBorder combineTitle; 1128 //private final BorderLayout mainBorderLayout;1129 //private final BorderLayout mainBorderLayoutDefault;1130 //private final Dimension combinationPanelDimension;1131 1026 private final Dimension singleSelectionDimension; 1132 1027 private final Dimension modelCombinationDimension; 1133 1028 private final Dimension mainPanelDimension; 1134 1135 1136 public ModelSettingsDialog(Collection<OsmPrimitive> sel1, final AddTagsDialog addDialog){ 1137 1029 1030 ModelSettingsDialog(Collection<OsmPrimitive> sel1, final AddTagsDialog addDialog) { 1031 1138 1032 loadPreviousCombinedSVMModel(); 1139 singleSelectionDimension = new Dimension(470,70); 1140 modelCombinationDimension = new Dimension(450,250); 1141 mainPanelDimension = new Dimension(600,350); 1142 1033 singleSelectionDimension = new Dimension(470, 70); 1034 modelCombinationDimension = new Dimension(450, 250); 1035 mainPanelDimension = new Dimension(600, 350); 1036 1143 1037 //------- <NORTH of main> ---------// 1144 mainPanel = new JPanel(new BorderLayout(10,10)); 1145 singleSelectionPanel = new JPanel(new BorderLayout(10,10)); 1038 mainPanel = new JPanel(new BorderLayout(10, 10)); 1039 singleSelectionPanel = new JPanel(new BorderLayout(10, 10)); 1146 1040 setResetWeightsPanel = new JPanel(); 1147 1148 chooseModelLabel = new javax.swing.JLabel("Choose a Model:");1149 chooseModelTextField = new javax.swing.JTextField();1150 chooseModelButton = new javax.swing.JButton("...");1151 chooseModelTextField.setText(MODEL_PATH); 1152 1153 singleSelectionPanel.add(chooseModelLabel, BorderLayout.NORTH); 1041 1042 chooseModelLabel = new JLabel("Choose a Model:"); 1043 chooseModelTextField = new JTextField(); 1044 chooseModelButton = new JButton("..."); 1045 chooseModelTextField.setText(MODEL_PATH); 1046 1047 singleSelectionPanel.add(chooseModelLabel, BorderLayout.NORTH); 1154 1048 singleSelectionPanel.add(chooseModelTextField, BorderLayout.WEST); 1155 singleSelectionPanel.add(chooseModelButton, BorderLayout.EAST); 1156 1049 singleSelectionPanel.add(chooseModelButton, BorderLayout.EAST); 1050 1157 1051 singleSelectionScrollPane = new JScrollPane(singleSelectionPanel); 1158 1052 singleSelectionScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 1159 1053 singleSelectionScrollPane.setPreferredSize(singleSelectionDimension); 1160 1054 1161 1055 //------- </NORTH of main> ---------// 1162 1056 1163 1057 //------- <WEST of main> ---------// 1164 //modelCombinationList = new javax.swing.JList<>(combinationDefaultListModel); 1165 modelCombinationList.setFixedCellHeight(20); 1058 modelCombinationList.setFixedCellHeight(20); 1166 1059 modelCombinationList.setEnabled(false); 1167 modelCombinationPanel = new JPanel(new BorderLayout(10, 10)); //new BorderLayout()1060 modelCombinationPanel = new JPanel(new BorderLayout(10, 10)); 1168 1061 1169 1062 weightsPanel = new JPanel(); 1170 weightsPanel.setLayout(new BoxLayout(weightsPanel, BoxLayout.Y_AXIS)); 1063 weightsPanel.setLayout(new BoxLayout(weightsPanel, BoxLayout.Y_AXIS)); 1171 1064 weightsPanel.setEnabled(false); 1172 1173 1174 acceptWeightsButton = new javax.swing.JButton("Set Weights/Normalize");1175 resetWeightsButton = new javax.swing.JButton("Reset Weights");1176 removeSelectedModelButton = new javax.swing.JButton("Remove Selected");1065 1066 1067 acceptWeightsButton = new JButton("Set Weights/Normalize"); 1068 resetWeightsButton = new JButton("Reset Weights"); 1069 removeSelectedModelButton = new JButton("Remove Selected"); 1177 1070 setResetWeightsPanel.add(acceptWeightsButton); 1178 1071 setResetWeightsPanel.add(resetWeightsButton); … … 1181 1074 acceptWeightsButton.setEnabled(false); 1182 1075 resetWeightsButton.setEnabled(false); 1183 1076 1184 1077 modelCombinationPanel.add(modelCombinationList, BorderLayout.CENTER); 1185 1078 modelCombinationPanel.add(weightsPanel, BorderLayout.EAST); 1186 modelCombinationPanel.add(setResetWeightsPanel, BorderLayout.SOUTH); 1079 modelCombinationPanel.add(setResetWeightsPanel, BorderLayout.SOUTH); 1187 1080 1188 1081 combinationScrollPane = new JScrollPane(modelCombinationPanel); 1189 1082 1190 1083 combinationScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 1191 combinationScrollPane.setPreferredSize(modelCombinationDimension); //new Dimension(450,250) // w/h 1084 combinationScrollPane.setPreferredSize(modelCombinationDimension); //new Dimension(450, 250) // w/h 1192 1085 1193 1086 //------- </WEST of main> ---------// … … 1195 1088 //------- <SOUTH of main> ---------// 1196 1089 useModelCombinationCheckbox = new JCheckBox("Combine different models?"); 1197 1090 1198 1091 //------- </SOUTH of main> ---------// 1199 1092 1200 1093 //------- <Borders> ---------// 1201 1094 modelTitle = BorderFactory.createTitledBorder("Models"); … … 1204 1097 modelCombinationList.setBorder(modelTitle); 1205 1098 weightsPanel.setBorder(weightTitle); 1206 1207 for(Entry<JTextField, String> entry : weightFieldsAndPaths.entrySet()){ 1208 //modelCombinationList.add(entry.getValue()); 1099 1100 for (Entry<JTextField, String> entry : weightFieldsAndPaths.entrySet()) { 1209 1101 combinationDefaultListModel.addElement(entry.getValue()); 1210 1211 JTextField weightTextField = new javax.swing.JTextField("0.00"); 1212 weightTextField.setMaximumSize(new Dimension(80,20)); 1213 weightsPanel.add(entry.getKey()); 1214 1215 //entry.getKey().setText("0.00"); 1216 } 1217 1102 1103 JTextField weightTextField = new JTextField("0.00"); 1104 weightTextField.setMaximumSize(new Dimension(80, 20)); 1105 weightsPanel.add(entry.getKey()); 1106 } 1107 1218 1108 //modelCombinationPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 1219 1109 modelCombinationPanel.setBorder(combineTitle); … … 1226 1116 modelChooserButtonActionPerformed(evt); 1227 1117 } 1228 }); 1229 1118 }); 1119 1230 1120 useModelCombinationCheckbox.addActionListener(new java.awt.event.ActionListener() { 1231 1121 @Override … … 1234 1124 } 1235 1125 }); 1236 1126 1237 1127 acceptWeightsButton.addActionListener(new java.awt.event.ActionListener() { 1238 1128 @Override … … 1240 1130 acceptWeightsButtonActionPerformed(evt); 1241 1131 } 1242 }); 1243 1132 }); 1133 1244 1134 resetWeightsButton.addActionListener(new java.awt.event.ActionListener() { 1245 1135 @Override … … 1247 1137 resetWeightsButtonActionPerformed(evt); 1248 1138 } 1249 }); 1250 1139 }); 1140 1251 1141 removeSelectedModelButton.addActionListener(new java.awt.event.ActionListener() { 1252 1142 @Override … … 1254 1144 removeSelectedModelButtonActionPerformed(evt); 1255 1145 } 1256 }); 1146 }); 1257 1147 mainPanel.add(singleSelectionScrollPane, BorderLayout.NORTH); 1258 mainPanel.add(combinationScrollPane, BorderLayout.CENTER); 1259 mainPanel.add(useModelCombinationCheckbox,BorderLayout.SOUTH); 1148 mainPanel.add(combinationScrollPane, BorderLayout.CENTER); 1149 mainPanel.add(useModelCombinationCheckbox, BorderLayout.SOUTH); 1260 1150 1261 1151 mainPanel.setPreferredSize(mainPanelDimension); 1262 1152 1263 1153 this.add(mainPanel); 1264 //pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); 1265 1266 //final JFrame frame = new JFrame(); 1154 1267 1155 pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION) { 1268 1156 @Override 1269 1157 public void setValue(Object newValue) { 1270 1158 super.setValue(newValue); 1271 if(newValue instanceof Integer && (int) newValue == 0 && useModelCombinationCheckbox.isSelected()){ 1159 if (newValue instanceof Integer && (int) newValue == 0 && useModelCombinationCheckbox.isSelected()) { 1272 1160 System.out.println("model settings button value: " + newValue); 1273 1161 System.out.println("\nUSE COMBINED MODEL\n"); 1274 1162 useCombinedModel = true; 1275 1163 useCustomSVMModel = false; 1276 1277 addDialog.loadSVMmodel(); 1278 addDialog.createOSMObject(sel); 1164 1165 addDialog.loadSVMmodel(); 1166 addDialog.createOSMObject(sel); 1279 1167 saveCombinedModel(); 1280 1168 dlg.setVisible(false); 1281 //this.setVisible(false); 1282 } 1283 else if(newValue instanceof Integer && (int) newValue == -1 && useModelCombinationCheckbox.isSelected()){ 1169 } else if (newValue instanceof Integer && (int) newValue == -1 && useModelCombinationCheckbox.isSelected()) { 1284 1170 System.out.println("model settings button value: " + newValue); 1285 1171 useCombinedModel = false; … … 1290 1176 addDialog.createOSMObject(sel); 1291 1177 dlg.setVisible(false); 1292 } 1293 else if(newValue instanceof Integer && (int) newValue == 0 && !useModelCombinationCheckbox.isSelected() ){ 1178 } else if (newValue instanceof Integer && (int) newValue == 0 && !useModelCombinationCheckbox.isSelected()) { 1294 1179 System.out.println("model settings button value: " + newValue); 1295 1180 System.out.println("Don t use combined model, use custom model"); … … 1299 1184 addDialog.createOSMObject(sel); 1300 1185 dlg.setVisible(false); 1301 } 1302 else if(newValue instanceof Integer && (int) newValue == -1 && !useModelCombinationCheckbox.isSelected() ){ 1186 } else if (newValue instanceof Integer && (int) newValue == -1 && !useModelCombinationCheckbox.isSelected()) { 1303 1187 System.out.println("model settings button value: " + newValue); 1304 1188 System.out.println("Don t use combined model, use custom model"); … … 1308 1192 addDialog.createOSMObject(sel); 1309 1193 dlg.setVisible(false); 1310 } 1311 else if(newValue == null || newValue.equals("uninitializedValue")){ 1194 } else if (newValue == null || newValue.equals("uninitializedValue")) { 1312 1195 System.out.println("uninitializedValue, do nothing"); 1313 1196 } 1314 //JOptionPane.showMessageDialog(frame.getContentPane(), "You have hit " + newValue); 1315 } 1316 }; 1197 } 1198 }; 1317 1199 1318 1200 dlg = pane.createDialog(Main.parent, tr("Model Settings")); 1319 dlg.setVisible(true);1320 1321 }1322 1323 public void makeVisible(boolean visible){1324 1201 dlg.setVisible(true); 1325 1202 } 1326 1327 private void modelChooserButtonActionPerformed(java.awt.event.ActionEvent evt) { 1328 1203 1204 public void makeVisible(boolean visible) { 1205 dlg.setVisible(true); 1206 } 1207 1208 private void modelChooserButtonActionPerformed(ActionEvent evt) { 1209 1329 1210 try { 1330 1211 final File file = new File(chooseModelTextField.getText()); 1331 1212 final JFileChooser fileChooser = new JFileChooser(file); 1332 1213 1333 1214 final int returnVal = fileChooser.showOpenDialog(this); 1334 if(returnVal == JFileChooser.APPROVE_OPTION) { 1215 if (returnVal == JFileChooser.APPROVE_OPTION) { 1335 1216 chooseModelTextField.setText(fileChooser.getSelectedFile().getAbsolutePath()); 1336 1217 useCustomSVMModel = true; 1337 1218 customSVMModelPath = fileChooser.getSelectedFile().getAbsolutePath(); 1338 1219 } 1339 1340 if(useModelCombinationCheckbox.isSelected()){ 1220 1221 if (useModelCombinationCheckbox.isSelected()) { 1341 1222 String svmModelPath = fileChooser.getSelectedFile().getAbsolutePath(); 1342 1223 String svmModelText; 1343 if(System.getProperty("os.name").contains("ux")){ 1344 if(svmModelPath.contains("/")){ 1224 if (System.getProperty("os.name").contains("ux")) { 1225 if (svmModelPath.contains("/")) { 1345 1226 svmModelText = svmModelPath.substring(svmModelPath.lastIndexOf("/")); 1346 } 1347 else{ 1227 } else { 1348 1228 svmModelText = svmModelPath; 1349 1229 } 1350 } 1351 else{ 1352 if(svmModelPath.contains("\\")){ 1230 } else { 1231 if (svmModelPath.contains("\\")) { 1353 1232 svmModelText = svmModelPath.substring(svmModelPath.lastIndexOf("\\")); 1354 } 1355 else{ 1233 } else { 1356 1234 svmModelText = svmModelPath; 1357 } 1235 } 1358 1236 } 1359 1237 combinationDefaultListModel.addElement(svmModelText); 1360 JTextField weightTextField = new javax.swing.JTextField("0.00");1238 JTextField weightTextField = new JTextField("0.00"); 1361 1239 weightFieldsAndPaths.put(weightTextField, svmModelPath); 1362 1240 System.out.println("weights size: " + weightFieldsAndPaths.size()); 1363 1364 weightTextField.setMaximumSize(new Dimension(80,20)); 1241 1242 weightTextField.setMaximumSize(new Dimension(80, 20)); 1365 1243 weightsPanel.add(weightTextField); 1366 1244 //add additional textbox 1367 } 1368 } 1369 catch (RuntimeException ex) { 1245 } 1246 } catch (RuntimeException ex) { 1370 1247 Main.warn(ex); 1371 //ex.printStackTrace(); 1372 } 1373 } 1374 1248 } 1249 } 1250 1375 1251 private void userCombinationCheckboxActionPerformed(java.awt.event.ActionEvent evt) { 1376 1252 1377 if(useModelCombinationCheckbox.isSelected()){ 1253 if (useModelCombinationCheckbox.isSelected()) { 1378 1254 useCombinedModel = true; 1379 1255 useCustomSVMModel = false; //reseting the selected custom SVM model only here … … 1381 1257 acceptWeightsButton.setEnabled(true); 1382 1258 resetWeightsButton.setEnabled(true); 1383 1259 1384 1260 chooseModelTextField.setEnabled(false); 1385 1261 modelCombinationList.setEnabled(true); … … 1389 1265 weightPanelComponent.setEnabled(true); 1390 1266 } 1391 1392 } 1393 else{ 1267 } else { 1394 1268 useCombinedModel = false; 1395 1269 useCustomSVMModel = true; … … 1397 1271 acceptWeightsButton.setEnabled(false); 1398 1272 resetWeightsButton.setEnabled(false); 1399 1273 1400 1274 chooseModelTextField.setEnabled(true); 1401 1275 modelCombinationList.setEnabled(false); … … 1407 1281 } 1408 1282 } 1409 1283 1410 1284 private void acceptWeightsButtonActionPerformed(ActionEvent evt) { 1411 1285 int weightsCount = 0; 1412 1286 removeSelectedModelButton.setEnabled(false); 1413 double weightSum = 0; 1414 for(JTextField weightField : weightFieldsAndPaths.keySet()){ 1415 if(weightField.getText().equals("")){ 1287 double weightSum = 0; 1288 for (JTextField weightField : weightFieldsAndPaths.keySet()) { 1289 if (weightField.getText().equals("")) { 1416 1290 weightField.setText("0.00"); 1417 1291 } 1418 1419 try{ 1292 1293 try { 1420 1294 //TODO replace "," with "." to parse doubles with commas 1421 1295 Double weightValue = Double.parseDouble(weightField.getText()); … … 1423 1297 weightValue = Math.abs(weightValue); 1424 1298 weightSum += weightValue; 1425 } 1426 catch (NumberFormatException ex){ 1299 } catch (NumberFormatException ex) { 1427 1300 Main.warn(ex); 1428 1301 } 1429 1302 weightsCount++; 1430 } 1431 1432 if(!filesAndWeights.isEmpty()){ 1303 } 1304 1305 if (!filesAndWeights.isEmpty()) { 1433 1306 filesAndWeights.clear(); 1434 1307 } 1435 1436 for(JTextField weightField : weightFieldsAndPaths.keySet()){ 1437 try{ 1308 1309 for (JTextField weightField : weightFieldsAndPaths.keySet()) { 1310 try { 1438 1311 Double weightValue = Double.parseDouble(weightField.getText()); 1439 1312 1440 1313 weightValue = Math.abs(weightValue)/weightSum; //normalize 1441 1442 if(weightSum == 0){ 1443 //System.out.println("Zero weights"); 1314 1315 if (weightSum == 0) { 1444 1316 weightValue = 1.0/weightsCount; 1445 } 1446 1317 } 1318 1447 1319 weightField.setText(new DecimalFormat("#.##").format(weightValue)); 1448 normalizedPathsAndWeights.put(weightFieldsAndPaths.get(weightField), weightValue); 1320 normalizedPathsAndWeights.put(weightFieldsAndPaths.get(weightField), weightValue); 1449 1321 filesAndWeights.put(new File(weightFieldsAndPaths.get(weightField)), weightValue); 1450 System.out.println("normalized: " + weightFieldsAndPaths.get(weightField) + "->" 1322 System.out.println("normalized: " + weightFieldsAndPaths.get(weightField) + "->" + weightValue); 1451 1323 weightField.setEnabled(false); 1452 1324 1453 } 1454 catch (NumberFormatException ex){ 1325 } catch (NumberFormatException ex) { 1455 1326 Main.warn(ex); 1456 } 1327 } 1457 1328 } 1458 1329 1459 1330 useCombinedModel = true; 1460 1331 useCustomSVMModel = false; 1461 1462 //filesAndWeights.putAll(normalizedPathsAndWeights); 1463 } 1464 1332 } 1333 1465 1334 private void resetWeightsButtonActionPerformed(ActionEvent evt) { 1466 1335 removeSelectedModelButton.setEnabled(true); 1467 for(JTextField weightField : weightFieldsAndPaths.keySet()){ 1336 for (JTextField weightField : weightFieldsAndPaths.keySet()) { 1468 1337 weightField.setEnabled(true); 1469 1338 } 1470 1339 } 1471 1340 1472 1341 private void removeSelectedModelButtonActionPerformed(ActionEvent evt) { 1473 int index 1342 int index = modelCombinationList.getSelectedIndex(); 1474 1343 String modelToBeRemoved = combinationDefaultListModel.get(index); 1475 combinationDefaultListModel.remove(index); 1344 combinationDefaultListModel.remove(index); 1476 1345 System.out.println("model to be removed: " + modelToBeRemoved); 1477 1346 1478 Iterator<Entry<JTextField, String>> it = weightFieldsAndPaths.entrySet().iterator(); 1347 Iterator<Entry<JTextField, String>> it = weightFieldsAndPaths.entrySet().iterator(); 1479 1348 while (it.hasNext()) { 1480 1349 Entry<JTextField, String> en = it.next(); 1481 if(en.getValue().equals(modelToBeRemoved)){ 1350 if (en.getValue().equals(modelToBeRemoved)) { 1482 1351 it.remove(); 1483 1352 } … … 1485 1354 System.out.println("model to be removed: " + modelToBeRemoved); 1486 1355 1487 weightsPanel.remove(index); 1356 weightsPanel.remove(index); 1488 1357 weightsPanel.revalidate(); 1489 1358 weightsPanel.repaint(); … … 1507 1376 } finally { 1508 1377 try { 1509 if (in!=null){1378 if (in != null) { 1510 1379 in.close(); 1511 1380 } 1512 if(fileIn !=null){1381 if (fileIn != null) { 1513 1382 fileIn.close(); 1514 1383 } 1515 1384 1516 1385 } catch (IOException ex) { 1517 1386 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); … … 1529 1398 private void saveCombinedModel() { 1530 1399 try (FileOutputStream fileOut = new FileOutputStream(combinedModelClasses); 1531 ObjectOutputStream out = new ObjectOutputStream(fileOut)) { 1400 ObjectOutputStream out = new ObjectOutputStream(fileOut)) { 1532 1401 out.writeObject(weightFieldsAndPaths); 1533 1402 } catch (IOException e) { … … 1536 1405 } 1537 1406 } 1538 1539 class AddTagsDialog extends 1407 1408 class AddTagsDialog extends AbstractTagsDialog { 1540 1409 List<JosmAction> recentTagsActions = new ArrayList<>(); 1541 1410 … … 1554 1423 private Map<Integer, String> idsWithMappings; 1555 1424 private List<String> textualList = new ArrayList<>(); 1556 //private boolean useClassFeatures = false; 1557 private final JCheckBox useTagsCheckBox; 1425 private final JCheckBox useTagsCheckBox; 1558 1426 private ModelSettingsDialog modelSettingsDialog; 1559 1427 private static final int RECOMMENDATIONS_SIZE = 10; 1560 1428 1561 publicAddTagsDialog() {1562 super(Main.parent, tr("Add value?"), new String[] {tr("OK"),tr("Cancel")}); 1563 setButtonIcons(new String[] {"ok","cancel"}); 1429 AddTagsDialog() { 1430 super(Main.parent, tr("Add value?"), new String[] {tr("OK"), tr("Cancel")}); 1431 setButtonIcons(new String[] {"ok", "cancel"}); 1564 1432 setCancelButton(2); 1565 1433 configureContextsensitiveHelp("/Dialog/AddValue", true /* show help button */); 1566 1434 final AddTagsDialog addTagsDialog = this; 1567 1435 1568 1436 loadOntology(); 1569 1437 //if the user did not train a model by running the training process … … 1572 1440 System.out.println("path for textual: " + TEXTUAL_LIST_PATH); 1573 1441 File textualListFile = new File(TEXTUAL_LIST_PATH); 1574 if(textualListFile.exists()){ 1442 if (textualListFile.exists()) { 1575 1443 loadTextualList(textualListFile); 1576 } 1577 else{ 1578 loadDefaultTextualList(); 1579 } 1580 1444 } else { 1445 loadDefaultTextualList(); 1446 } 1447 1581 1448 //if training process has not been performed, we use two sample SVM models, extracted from the jar 1582 //File bestModelFile = new File(bestModelPath); 1583 //File bestModelWithClassesFile = new File(modelWithClassesPath); 1584 1585 JPanel splitPanel = new JPanel(new BorderLayout(10,10)); 1586 JPanel mainPanel = new JPanel(new GridBagLayout()); //original panel, will be wrapped by the splitPanel 1587 JPanel recommendPanel = new JPanel(new BorderLayout(10,10)); //will contain listPanel, action panel 1588 JPanel listPanel = new JPanel(new BorderLayout(10,10)); //class recommend label, recommendation list 1449 1450 JPanel splitPanel = new JPanel(new BorderLayout(10, 10)); 1451 JPanel mainPanel = new JPanel(new GridBagLayout()); //original panel, will be wrapped by the splitPanel 1452 JPanel recommendPanel = new JPanel(new BorderLayout(10, 10)); //will contain listPanel, action panel 1453 JPanel listPanel = new JPanel(new BorderLayout(10, 10)); //class recommend label, recommendation list 1589 1454 JPanel actionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //model selection buttons or configuration 1590 1591 addAndContinueButton = new javax.swing.JButton("Add and continue");1592 modelSettingsButton = new javax.swing.JButton("Model Settings");1593 useTagsCheckBox = new javax.swing.JCheckBox("Predict using tags");1594 recommendedClassesLabel = new javax.swing.JLabel("Recommended Classes:");1595 1455 1456 addAndContinueButton = new JButton("Add and continue"); 1457 modelSettingsButton = new JButton("Model Settings"); 1458 useTagsCheckBox = new JCheckBox("Predict using tags"); 1459 recommendedClassesLabel = new JLabel("Recommended Classes:"); 1460 1596 1461 addAndContinueButton.addActionListener(new java.awt.event.ActionListener() { 1597 1462 @Override 1598 public void actionPerformed(java.awt.event.ActionEvent evt) { 1463 public void actionPerformed(java.awt.event.ActionEvent evt) { 1599 1464 String selectedClass = categoryList.getSelectedValue(); 1600 1465 addAndContinueButtonActionPerformed(evt, selectedClass); 1601 1466 1602 1467 //reconstruct vector for instance and use the model that was trained with classes here 1603 1468 1604 1469 List<OsmPrimitive> osmPrimitiveSelection = new ArrayList<>(sel); 1605 1470 OsmPrimitive s; 1606 1471 1607 1608 1472 //get a simple selection 1609 if(!osmPrimitiveSelection.isEmpty()){ 1610 s = osmPrimitiveSelection.get(0); 1611 if(s.getInterestingTags().isEmpty()){ 1473 if (!osmPrimitiveSelection.isEmpty()) { 1474 s = osmPrimitiveSelection.get(0); 1475 if (s.getInterestingTags().isEmpty()) { 1612 1476 //load original model 1613 1477 modelWithClasses = false; 1614 1478 loadSVMmodel(); 1615 1479 createOSMObject(sel); //create object without class features 1616 } 1617 else{ 1480 } else { 1618 1481 //recommend using tags: set the checkbox selected to avoid confusing the user 1619 1482 useTagsCheckBox.setSelected(true); 1620 1621 if(useTagsCheckBox.isSelected()){ 1483 1484 if (useTagsCheckBox.isSelected()) { 1622 1485 //load model with classes 1623 1486 modelWithClasses = true; 1624 1487 loadSVMmodel(); 1625 createOSMObject(sel); //create object including class features 1626 } 1627 else{ 1488 createOSMObject(sel); //create object including class features 1489 } else { 1628 1490 modelWithClasses = false; 1629 1491 loadSVMmodel(); 1630 createOSMObject(sel); //create object including class features 1492 createOSMObject(sel); //create object including class features 1631 1493 } 1632 } 1494 } 1633 1495 } 1634 1496 } 1635 1497 }); 1636 1498 1637 1499 modelSettingsButton.addActionListener(new java.awt.event.ActionListener() { 1638 1500 @Override 1639 1501 public void actionPerformed(java.awt.event.ActionEvent evt) { 1640 if(modelSettingsDialog == null){ 1502 if (modelSettingsDialog == null) { 1641 1503 System.out.println("new modelSettingsDialog"); 1642 modelSettingsDialog = new ModelSettingsDialog(sel, addTagsDialog); 1643 1644 //modelSettingsDialog.makeVisible(true); 1645 //modelSettingsDialog.setVisible(true); 1646 } 1647 else{ 1504 modelSettingsDialog = new ModelSettingsDialog(sel, addTagsDialog); 1505 } else { 1648 1506 System.out.println("set modelSettingsDialog visible"); 1649 1507 modelSettingsDialog.makeVisible(true); 1650 //modelSettingsDialog.setVisible(true); 1651 } 1652 } 1653 }); 1654 1655 useTagsCheckBox.addActionListener(new java.awt.event.ActionListener() { 1508 } 1509 } 1510 }); 1511 1512 useTagsCheckBox.addActionListener(new java.awt.event.ActionListener() { 1656 1513 @Override 1657 1514 public void actionPerformed(java.awt.event.ActionEvent evt) { 1658 1515 List<OsmPrimitive> osmPrimitiveSelection = new ArrayList<>(sel); 1659 1516 OsmPrimitive s; 1660 if(!osmPrimitiveSelection.isEmpty()){ 1661 s = osmPrimitiveSelection.get(0); 1662 if(s.getInterestingTags().isEmpty()){ 1517 if (!osmPrimitiveSelection.isEmpty()) { 1518 s = osmPrimitiveSelection.get(0); 1519 if (s.getInterestingTags().isEmpty()) { 1663 1520 //load original model 1664 1521 modelWithClasses = false; 1665 1522 loadSVMmodel(); 1666 1523 createOSMObject(sel); //create object without class features 1667 } 1668 else{ 1524 } else { 1669 1525 //useTagsCheckBox 1670 if(useTagsCheckBox.isSelected()){ 1526 if (useTagsCheckBox.isSelected()) { 1671 1527 //load model with classes 1672 1528 modelWithClasses = true; 1673 1529 loadSVMmodel(); 1674 createOSMObject(sel); //create object including class features 1675 } 1676 else{ 1530 createOSMObject(sel); //create object including class features 1531 } else { 1677 1532 modelWithClasses = false; 1678 1533 loadSVMmodel(); 1679 createOSMObject(sel); //create object including class features 1534 createOSMObject(sel); //create object including class features 1680 1535 } 1681 } 1682 } 1536 } 1537 } 1683 1538 } 1684 1539 }); 1685 1540 1686 1541 keys = new AutoCompletingComboBox(); 1687 1542 values = new AutoCompletingComboBox(); 1688 1543 1689 1544 mainPanel.add(new JLabel("<html>"+trn("This will change up to {0} object.", 1690 "This will change up to {0} objects.", sel.size(),sel.size()) 1691 1692 1693 AutoCompletionManager autocomplete = Main. main.getEditLayer().data.getAutoCompletionManager();1545 "This will change up to {0} objects.", sel.size(), sel.size()) 1546 +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL)); 1547 1548 AutoCompletionManager autocomplete = Main.getLayerManager().getEditLayer().data.getAutoCompletionManager(); 1694 1549 List<AutoCompletionListItem> keyList = autocomplete.getKeys(); 1695 1550 … … 1718 1573 1719 1574 mainPanel.add(keys, GBC.eop().fill()); 1720 mainPanel.add(new JLabel(tr("Please select a value")), GBC.eol()); 1721 1575 mainPanel.add(new JLabel(tr("Please select a value")), GBC.eol()); 1576 1722 1577 model = new DefaultListModel<>(); 1723 1724 parseTagsMappedToClasses(); 1725 1578 1579 parseTagsMappedToClasses(); 1580 1726 1581 List<OsmPrimitive> osmPrimitiveSelection = new ArrayList<>(sel); 1727 1582 OsmPrimitive s; 1728 1583 //get a simple selection 1729 if(!osmPrimitiveSelection.isEmpty()){ 1730 s = osmPrimitiveSelection.get(0); 1584 if (!osmPrimitiveSelection.isEmpty()) { 1585 s = osmPrimitiveSelection.get(0); 1731 1586 File modelDirectory = new File(MODEL_PATH); 1732 1587 String modelWithClassesPath = modelDirectory.getAbsolutePath() + "/model_with_classes"; 1733 File modelWithClassesFile = new File(modelWithClassesPath); 1734 if(s.getInterestingTags().isEmpty() || !modelWithClassesFile.exists()){ 1588 File modelWithClassesFile = new File(modelWithClassesPath); 1589 if (s.getInterestingTags().isEmpty() || !modelWithClassesFile.exists()) { 1735 1590 modelWithClasses = false; 1736 loadSVMmodel();//load original model 1737 createOSMObject(sel); //create object without class features 1738 } 1739 else{ 1591 loadSVMmodel(); //load original model 1592 createOSMObject(sel); //create object without class features 1593 } else { 1740 1594 //recommend using tags: set the checkbox selected to avoid confusing the user 1741 useTagsCheckBox.setSelected(true); 1595 useTagsCheckBox.setSelected(true); 1742 1596 modelWithClasses = true; 1743 loadSVMmodel();//load model with classes 1744 createOSMObject(sel); //create object including class features 1745 } 1746 } 1747 1597 loadSVMmodel(); //load model with classes 1598 createOSMObject(sel); //create object including class features 1599 } 1600 } 1601 1748 1602 categoryList = new JList<>(model); 1749 1603 1750 ListSelectionListener listSelectionListener = new ListSelectionListener() { 1604 ListSelectionListener listSelectionListener = new ListSelectionListener() { 1751 1605 @Override 1752 1606 public void valueChanged(ListSelectionEvent listSelectionEvent) { 1753 if (!listSelectionEvent.getValueIsAdjusting()) {//This prevents double events 1754 //System.out.println("tag selected: " + categoryList.getSelectedValue()); 1755 1607 if (!listSelectionEvent.getValueIsAdjusting()) { //This prevents double events 1608 1756 1609 String selectedClass = categoryList.getSelectedValue(); 1757 1610 1758 if(selectedClass != null){ //null check, because the model is cleared after a new recommendation 1759 1760 if((selectedClass.indexOf(" ")+1) > 0){ 1611 if (selectedClass != null) { //null check, because the model is cleared after a new recommendation 1612 //tags become unselected 1613 if ((selectedClass.indexOf(" ")+1) > 0) { 1761 1614 //add both key + value in tags 1762 1615 String keyTag = selectedClass.substring(0, selectedClass.indexOf(" ")); … … 1764 1617 keys.setSelectedItem(keyTag); //adding selected tags to textBoxes 1765 1618 values.setSelectedItem(valueTag); 1766 } 1767 else{ 1619 } else { 1768 1620 //value does not have a value, add the key tag only 1769 1621 String keyTag = selectedClass; //test it … … 1772 1624 } 1773 1625 } 1774 } 1626 } 1775 1627 } 1776 1628 }; 1777 1629 1778 1630 categoryList.addListSelectionListener(listSelectionListener); 1779 1631 categoryList.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); … … 1798 1650 } 1799 1651 1800 // Add tag on Shift-Enter1801 // mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(1802 // KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK), "addAndContinue");1803 // mainPanel.getActionMap().put("addAndContinue", new AbstractAction() {1804 // @Override1805 // public void actionPerformed(ActionEvent e) {1806 // performTagAdding();1807 // selectKeysComboBox();1808 // }1809 // });1810 1811 1652 suggestRecentlyAddedTags(mainPanel, recentTagsToShow, focus); 1812 1653 … … 1814 1655 listPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 1815 1656 splitPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 1816 1657 1817 1658 listPanel.add(recommendedClassesLabel, BorderLayout.NORTH); 1818 1659 listPanel.add(categoryList, BorderLayout.SOUTH); … … 1820 1661 actionsPanel.add(modelSettingsButton); 1821 1662 actionsPanel.add(useTagsCheckBox); 1822 1663 1823 1664 recommendPanel.add(actionsPanel, BorderLayout.WEST); 1824 1665 recommendPanel.add(listPanel, BorderLayout.NORTH); 1825 1666 1826 1667 splitPanel.add(mainPanel, BorderLayout.WEST); 1827 1668 splitPanel.add(recommendPanel, BorderLayout.EAST); 1828 1669 1829 1670 setContent(splitPanel, false); 1830 1831 selectKeysComboBox();1832 1671 1833 1672 popupMenu.add(new AbstractAction(tr("Set number of recently added tags")) { … … 1838 1677 }); 1839 1678 JCheckBoxMenuItem rememberLastTags = new JCheckBoxMenuItem( 1840 new AbstractAction(tr("Remember last used tags after a restart")){ 1841 @Override 1842 public void actionPerformed(ActionEvent e) { 1843 boolean sel =((JCheckBoxMenuItem) e.getSource()).getState();1844 PROPERTY_REMEMBER_TAGS.put(sel); 1845 if (sel) saveTagsIfNeeded(); 1846 } 1847 }); 1679 new AbstractAction(tr("Remember last used tags after a restart")) { 1680 @Override 1681 public void actionPerformed(ActionEvent e) { 1682 boolean sel = ((JCheckBoxMenuItem) e.getSource()).getState(); 1683 PROPERTY_REMEMBER_TAGS.put(sel); 1684 if (sel) saveTagsIfNeeded(); 1685 } 1686 }); 1848 1687 rememberLastTags.setState(PROPERTY_REMEMBER_TAGS.get()); 1849 1688 popupMenu.add(rememberLastTags); 1850 1851 } 1852 1689 } 1690 1853 1691 private void addAndContinueButtonActionPerformed(ActionEvent evt, String selectedClass) { 1854 1692 performTagAdding(); 1855 selectKeysComboBox(); 1856 } 1857 1693 } 1694 1858 1695 private void selectNumberOfTags() { 1859 1696 String s = JOptionPane.showInputDialog(this, tr("Please enter the number of recently added tags to display")); 1860 if (s !=null) try {1697 if (s != null) try { 1861 1698 int v = Integer.parseInt(s); 1862 if (v >=0 && v<=MAX_LRU_TAGS_NUMBER) {1699 if (v >= 0 && v <= MAX_LRU_TAGS_NUMBER) { 1863 1700 PROPERTY_RECENT_TAGS_NUMBER.put(v); 1864 1701 return; … … 1878 1715 int count = 1; 1879 1716 // We store the maximum number (9) of recent tags to allow dynamic change of number of tags shown in the preferences. 1880 // This implies to iterate in descending order, as the oldest elements will only be removed after we reach the maximum numbern and not the number of tags to show. 1881 // However, as Set does not allow to iterate in descending order, we need to copy its elements into a List we can access in reverse order. 1717 // This implies to iterate in descending order, 1718 // as the oldest elements will only be removed after we reach the maximum numbern and not the number of tags to show. 1719 // However, as Set does not allow to iterate in descending order, 1720 // we need to copy its elements into a List we can access in reverse order. 1882 1721 List<Tag> tags = new LinkedList<>(recentTags.keySet()); 1883 1722 for (int i = tags.size()-1; i >= 0 && count <= tagsToShow; i--, count++) { 1884 1723 final Tag t = tags.get(i); 1885 1724 // Create action for reusing the tag, with keyboard shortcut Ctrl+(1-5) 1886 String actionShortcutKey = "properties:recent:"+count;1887 String actionShortcutShiftKey = "properties:recent:shift:"+count;1888 Shortcut sc = Shortcut.registerShortcut( actionShortcutKey, tr("Choose recent tag {0}", count), KeyEvent.VK_0+count, Shortcut.CTRL);1889 final JosmAction action = new JosmAction( actionShortcutKey, null, tr("Use this tag again"), sc, false) {1725 String scKey = "properties:recent:"+count; 1726 String scsKey = "properties:recent:shift:"+count; 1727 Shortcut sc = Shortcut.registerShortcut(scKey, tr("Choose recent tag {0}", count), KeyEvent.VK_0+count, Shortcut.CTRL); 1728 final JosmAction action = new JosmAction(scKey, null, tr("Use this tag again"), sc, false) { 1890 1729 @Override 1891 1730 public void actionPerformed(ActionEvent e) { … … 1894 1733 focus.focusGained(null); 1895 1734 values.setSelectedItem(t.getValue()); 1896 selectValuesCombobox();1897 1735 } 1898 1736 }; 1899 Shortcut sc Shift= Shortcut.registerShortcut(actionShortcutShiftKey, tr("Apply recent tag {0}", count), KeyEvent.VK_0+count, Shortcut.CTRL_SHIFT);1900 final JosmAction actionShift = new JosmAction( actionShortcutShiftKey, null, tr("Use this tag again"), scShift, false) {1737 Shortcut scs = Shortcut.registerShortcut(scsKey, tr("Apply recent tag {0}", count), KeyEvent.VK_0+count, Shortcut.CTRL_SHIFT); 1738 final JosmAction actionShift = new JosmAction(scsKey, null, tr("Use this tag again"), scs, false) { 1901 1739 @Override 1902 1740 public void actionPerformed(ActionEvent e) { 1903 1741 action.actionPerformed(null); 1904 1742 performTagAdding(); 1905 selectKeysComboBox();1906 1743 } 1907 1744 }; … … 1915 1752 Map<String, String> map = new HashMap<>(); 1916 1753 map.put(t.getKey(), t.getValue()); 1917 // for (TaggingPreset tp : TaggingPreset.getMatchingPresets(null, map, false)) {1918 // icon = tp.getIcon();1919 // if (icon != null) {1920 // break;1921 // }1922 // }1923 1754 // If still nothing display an empty icon 1924 1755 if (icon == null) { … … 1932 1763 final String color = action.isEnabled() ? "" : "; color:gray"; 1933 1764 final JLabel tagLabel = new JLabel("<html>" 1934 + "<style>td{border:1px solid gray; font-weight:normal"+color+"}</style>" 1935 + "<table><tr><td>" + XmlWriter.encode(t.toString(), true) + "</td></tr></table></html>"); 1765 + "<style>td{border:1px solid gray; font-weight:normal"+color+"}</style>" 1766 + "<table><tr><td>" + XmlWriter.encode(t.toString(), true) + "</td></tr></table></html>"); 1936 1767 if (action.isEnabled()) { 1937 1768 // Register action 1938 mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), actionShortcutKey);1939 mainPanel.getActionMap().put( actionShortcutKey, action);1940 mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc Shift.getKeyStroke(),actionShortcutShiftKey);1941 mainPanel.getActionMap().put( actionShortcutShiftKey, actionShift);1769 mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), scKey); 1770 mainPanel.getActionMap().put(scKey, action); 1771 mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(scs.getKeyStroke(), scsKey); 1772 mainPanel.getActionMap().put(scsKey, actionShift); 1942 1773 // Make the tag label clickable and set tooltip to the action description (this displays also the keyboard shortcut) 1943 1774 tagLabel.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION)); … … 1948 1779 action.actionPerformed(null); 1949 1780 // add tags and close window on double-click 1950 1951 if (e.getClickCount() >1) {1781 1782 if (e.getClickCount() > 1) { 1952 1783 buttonAction(0, null); // emulate OK click and close the dialog 1953 1784 } … … 1955 1786 if (e.isShiftDown()) { 1956 1787 performTagAdding(); 1957 selectKeysComboBox();1958 1788 } 1959 1789 } … … 2021 1851 File modelDirectory = new File(MODEL_PATH); 2022 1852 File modelFile; 2023 if(useCombinedModel){ 2024 if(filesAndWeights.isEmpty()){ 1853 if (useCombinedModel) { 1854 if (filesAndWeights.isEmpty()) { 2025 1855 System.out.println("No models selected! Loading defaults.."); 2026 if(modelWithClasses){ 1856 if (modelWithClasses) { 2027 1857 System.out.println("Using default/last model with classes: " + modelDirectory.getAbsolutePath() + "/model_with_classes"); 2028 1858 modelFile = new File(modelDirectory.getAbsolutePath() + "/model_with_classes"); … … 2037 1867 modelSVMLabelSize = modelSVM.getLabels().length; 2038 1868 modelSVMLabels = modelSVM.getLabels(); 2039 } 2040 else{ 1869 } else { 2041 1870 System.out.println("Using default/last model without classes: " + modelDirectory.getAbsolutePath() + "/best_model"); 2042 1871 modelFile = new File(modelDirectory.getAbsolutePath() + "/best_model"); … … 2051 1880 modelSVMLabelSize = modelSVM.getLabels().length; 2052 1881 modelSVMLabels = modelSVM.getLabels(); 2053 } 2054 } 2055 if(modelWithClasses){ //check filenames to define if model with classes is selected 1882 } 1883 } 1884 if (modelWithClasses) { //check filenames to define if model with classes is selected 2056 1885 System.out.println("Using combined model with classes"); 2057 1886 useCombinedSVMmodels(sel, true); 2058 } 2059 else{ 1887 } else { 2060 1888 System.out.println("Using combined model without classes"); 2061 1889 useCombinedSVMmodels(sel, false); 2062 } 2063 2064 } 2065 else if(useCustomSVMModel){ 1890 } 1891 } else if (useCustomSVMModel) { 2066 1892 System.out.println("custom path: " + customSVMModelPath); 2067 File checkExistance = new File(customSVMModelPath); 2068 if(checkExistance.exists() && checkExistance.isFile()){ 1893 File checkExistance = new File(customSVMModelPath); 1894 if (checkExistance.exists() && checkExistance.isFile()) { 2069 1895 if (modelWithClasses) { 2070 1896 System.out.println("Using custom model with classes: "); 2071 1897 if (customSVMModelPath.endsWith(".0")) { 2072 //String customSVMModelPathWithClasses = customSVMModelPath.replace(".0", ".1");2073 1898 String customSVMModelPathWithClasses = customSVMModelPath.substring(0, customSVMModelPath.length() - 2) + ".1"; 2074 1899 2075 1900 modelFile = new File(customSVMModelPathWithClasses); 2076 1901 System.out.println(customSVMModelPathWithClasses); 2077 } 2078 else { 1902 } else { 2079 1903 modelFile = new File(customSVMModelPath); 2080 1904 } … … 2098 1922 } 2099 1923 modelSVMLabelSize = modelSVM.getLabels().length; 2100 modelSVMLabels = modelSVM.getLabels(); 2101 2102 } 2103 else { 1924 modelSVMLabels = modelSVM.getLabels(); 1925 1926 } else { 2104 1927 //user chose to use a custom model, but did not provide a path to a model: 2105 if(modelWithClasses){ 1928 if (modelWithClasses) { 2106 1929 System.out.println("Using default/last model with classes"); 2107 1930 modelFile = new File(modelDirectory.getAbsolutePath() + "/model_with_classes"); 2108 } 2109 else{ 1931 } else { 2110 1932 System.out.println("Using default/last model without classes"); 2111 1933 modelFile = new File(modelDirectory.getAbsolutePath() + "/best_model"); … … 2121 1943 } 2122 1944 modelSVMLabelSize = modelSVM.getLabels().length; 2123 modelSVMLabels = modelSVM.getLabels(); 2124 2125 } 2126 } 2127 else { 2128 if(modelWithClasses){ 1945 modelSVMLabels = modelSVM.getLabels(); 1946 1947 } 1948 } else { 1949 if (modelWithClasses) { 2129 1950 System.out.println("Using default/last model with classes"); 2130 1951 modelFile = new File(modelDirectory.getAbsolutePath() + "/model_with_classes"); 2131 } 2132 else{ 1952 } else { 2133 1953 System.out.println("Using default/last model without classes"); 2134 1954 modelFile = new File(modelDirectory.getAbsolutePath() + "/best_model"); 2135 } 2136 1955 } 1956 2137 1957 try { 2138 1958 System.out.println("try to load model: " + modelFile.getAbsolutePath()); … … 2144 1964 } 2145 1965 modelSVMLabelSize = modelSVM.getLabels().length; 2146 modelSVMLabels = modelSVM.getLabels(); 2147 } 2148 } 2149 2150 private void useCombinedSVMmodels(Collection<OsmPrimitive> sel, boolean useClassFeatures){ 2151 System.out.println("The system will combine " + filesAndWeights.size() + " SVM models."); 2152 1966 modelSVMLabels = modelSVM.getLabels(); 1967 } 1968 } 1969 1970 private void useCombinedSVMmodels(Collection<OsmPrimitive> sel, boolean useClassFeatures) { 1971 System.out.println("The system will combine " + filesAndWeights.size() + " SVM models."); 1972 2153 1973 MathTransform transform = null; 2154 1974 GeometryFactory geometryFactory = new GeometryFactory(); … … 2157 1977 try { 2158 1978 transform = CRS.findMathTransform(sourceCRS, targetCRS, true); 2159 1979 2160 1980 } catch (FactoryException ex) { 2161 1981 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); … … 2165 1985 List<OsmPrimitive> osmPrimitiveSelection = new ArrayList<>(sel); 2166 1986 OsmPrimitive s; 2167 1987 2168 1988 //get a simple selection 2169 if(!osmPrimitiveSelection.isEmpty()){ 1989 if (!osmPrimitiveSelection.isEmpty()) { 2170 1990 s = osmPrimitiveSelection.get(0); 2171 } 2172 else { 1991 } else { 2173 1992 return; 2174 1993 } 2175 1994 2176 1995 selectedInstance = new OSMWay(); 2177 for(Way selectedWay : s.getDataSet().getSelectedWays()){ 2178 // System.out.println(nod.getNodes()); 1996 for (Way selectedWay : s.getDataSet().getSelectedWays()) { 2179 1997 List<Node> selectedWayNodes = selectedWay.getNodes(); 2180 for(Node node : selectedWayNodes){ 1998 for (Node node : selectedWayNodes) { 2181 1999 node.getCoor(); 2182 //System.out.println(node.getCoor()); 2183 if(node.isLatLonKnown()){ 2000 if (node.isLatLonKnown()) { 2184 2001 double lat = node.getCoor().lat(); 2185 2002 double lon = node.getCoor().lon(); … … 2187 2004 Coordinate sourceCoordinate = new Coordinate(lon, lat); 2188 2005 Coordinate targetGeometry = null; 2189 try { 2006 try { 2190 2007 targetGeometry = JTS.transform(sourceCoordinate, null, transform); 2191 2008 } catch (MismatchedDimensionException | TransformException ex) { 2192 2009 Logger.getLogger(OSMParser.class.getName()).log(Level.SEVERE, null, ex); 2193 } 2194 2195 Geometry geom = geometryFactory.createPoint(new Coordinate(targetGeometry)); 2010 } 2011 2012 Geometry geom = geometryFactory.createPoint(new Coordinate(targetGeometry)); 2196 2013 selectedInstance.addNodeGeometry(geom); 2197 2014 } … … 2199 2016 } 2200 2017 Geometry fullGeom = geometryFactory.buildGeometry(selectedInstance.getNodeGeometries()); 2201 if((selectedInstance.getNodeGeometries().size() > 3) && 2202 selectedInstance.getNodeGeometries().get(0).equals(selectedInstance.getNodeGeometries() 2203 .get(selectedInstance.getNodeGeometries().size()-1))) 2204 { 2205 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 2206 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 2018 if ((selectedInstance.getNodeGeometries().size() > 3) && 2019 selectedInstance.getNodeGeometries().get(0).equals(selectedInstance.getNodeGeometries() 2020 .get(selectedInstance.getNodeGeometries().size()-1))) { 2021 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 2022 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 2207 2023 LinearRing linear = geometryFactory.createLinearRing(fullGeom.getCoordinates()); 2208 2024 Polygon poly = new Polygon(linear, null, geometryFactory); … … 2210 2026 2211 2027 System.out.println("\n\npolygon"); 2212 } 2213 else if (selectedInstance.getNodeGeometries().size()>1){ 2214 //it is an open geometry with more than one nodes, make it linestring 2028 } else if (selectedInstance.getNodeGeometries().size() > 1) { 2029 //it is an open geometry with more than one nodes, make it linestring 2215 2030 System.out.println("\n\nlinestring"); 2216 LineString lineString = geometryFactory.createLineString(fullGeom.getCoordinates()); 2217 selectedInstance.setGeometry(lineString); 2218 } 2219 else{ //we assume all the rest geometries are points 2031 LineString lineString = geometryFactory.createLineString(fullGeom.getCoordinates()); 2032 selectedInstance.setGeometry(lineString); 2033 } else { //we assume all the rest geometries are points 2220 2034 System.out.println("\n\npoint"); 2221 2035 Point point = geometryFactory.createPoint(fullGeom.getCoordinate()); … … 2224 2038 2225 2039 Map<String, String> selectedTags = s.getInterestingTags(); 2226 selectedInstance.setAllTags(selectedTags); 2227 2040 selectedInstance.setAllTags(selectedTags); 2041 2228 2042 //construct vector 2229 if(selectedInstance != null){ 2043 if (selectedInstance != null) { 2230 2044 int id; 2231 2045 2232 2046 OSMClassification classifier = new OSMClassification(); 2233 2047 classifier.calculateClasses(selectedInstance, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 2234 2235 if(useClassFeatures){ 2048 2049 if (useClassFeatures) { 2236 2050 ClassFeatures classFeatures = new ClassFeatures(); 2237 2051 classFeatures.createClassFeatures(selectedInstance, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 2238 2052 id = 1422; 2239 } 2240 else{ 2053 } else { 2241 2054 id = 1; 2242 2055 } 2243 2056 2244 2057 GeometryFeatures geometryFeatures = new GeometryFeatures(id); 2245 2058 geometryFeatures.createGeometryFeatures(selectedInstance); 2246 id =geometryFeatures.getLastID();2059 id = geometryFeatures.getLastID(); 2247 2060 TextualFeatures textualFeatures = new TextualFeatures(id, textualList, languageDetector); 2248 textualFeatures.createTextualFeatures(selectedInstance); 2249 2061 textualFeatures.createTextualFeatures(selectedInstance); 2062 2250 2063 List<FeatureNode> featureNodeList = selectedInstance.getFeatureNodeList(); 2251 //System.out.println(featureNodeList); 2252 2064 2253 2065 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 2254 2066 2255 2067 int i = 0; 2256 for(FeatureNode featureNode : featureNodeList){ 2257 2068 for (FeatureNode featureNode : featureNodeList) { 2258 2069 featureNodeArray[i] = featureNode; 2259 //System.out.println("i: " + i + " array:" + featureNodeArray[i]);2260 2070 i++; 2261 2262 } 2263 FeatureNode[] testInstance2 = featureNodeArray; 2264 2071 } 2072 FeatureNode[] testInstance2 = featureNodeArray; 2073 2265 2074 //compute prediction list for every model 2266 2075 int[] ranks = new int[10]; 2267 2268 for (int l=0; l<10; l++){2076 2077 for (int l = 0; l < 10; l++) { 2269 2078 ranks[l] = 10-l; //init from 10 to 1 2270 2079 } 2271 2080 2272 2081 Map<String, Double> scoreMap = new HashMap<>(); 2273 2082 2274 2083 Map<File, Double> alignedFilesAndWeights = getAlignedModels(filesAndWeights); 2275 2276 for(File modelFile : alignedFilesAndWeights.keySet()){ 2277 2084 2085 for (File modelFile : alignedFilesAndWeights.keySet()) { 2278 2086 2279 2087 try { 2280 2088 modelSVM = Model.load(modelFile); 2281 //System.out.println("model loaded: " + modelFile.getAbsolutePath());2282 2089 } catch (IOException ex) { 2283 2090 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 2284 2091 } 2285 2092 modelSVMLabelSize = modelSVM.getLabels().length; 2286 modelSVMLabels = modelSVM.getLabels(); 2287 2288 2093 modelSVMLabels = modelSVM.getLabels(); 2094 2289 2095 Map<Integer, Integer> mapLabelsToIDs = new HashMap<>(); 2290 for(int h =0; h < modelSVMLabelSize; h++){ 2291 2096 for (int h = 0; h < modelSVMLabelSize; h++) { 2292 2097 mapLabelsToIDs.put(modelSVMLabels[h], h); 2293 //System.out.println(h + " <-> " + modelSVMLabels[h]); 2294 2295 } 2098 } 2296 2099 double[] scores = new double[modelSVMLabelSize]; 2297 2100 Linear.predictValues(modelSVM, testInstance2, scores); 2298 2101 2299 2102 Map<Double, Integer> scoresValues = new HashMap<>(); 2300 for(int h = 0; h < scores.length; h++){ 2103 for (int h = 0; h < scores.length; h++) { 2301 2104 scoresValues.put(scores[h], h); 2302 //System.out.println(h + " <-> " + scores[h]); 2303 } 2105 } 2304 2106 2305 2107 Arrays.sort(scores); 2306 int predicted1 = modelSVMLabels[scoresValues.get(scores[scores.length-1])]; 2108 int predicted1 = modelSVMLabels[scoresValues.get(scores[scores.length-1])]; 2307 2109 int predicted2 = modelSVMLabels[scoresValues.get(scores[scores.length-2])]; 2308 2110 int predicted3 = modelSVMLabels[scoresValues.get(scores[scores.length-3])]; 2309 int predicted4 = modelSVMLabels[scoresValues.get(scores[scores.length-4])]; 2111 int predicted4 = modelSVMLabels[scoresValues.get(scores[scores.length-4])]; 2310 2112 int predicted5 = modelSVMLabels[scoresValues.get(scores[scores.length-5])]; 2311 2113 int predicted6 = modelSVMLabels[scoresValues.get(scores[scores.length-6])]; 2312 int predicted7 = modelSVMLabels[scoresValues.get(scores[scores.length-7])]; 2114 int predicted7 = modelSVMLabels[scoresValues.get(scores[scores.length-7])]; 2313 2115 int predicted8 = modelSVMLabels[scoresValues.get(scores[scores.length-8])]; 2314 2116 int predicted9 = modelSVMLabels[scoresValues.get(scores[scores.length-9])]; 2315 int predicted10 = modelSVMLabels[scoresValues.get(scores[scores.length-10])]; 2117 int predicted10 = modelSVMLabels[scoresValues.get(scores[scores.length-10])]; 2316 2118 2317 2119 String[] predictedTags = new String[10]; 2318 for( Map.Entry<String, Integer> entry : mapperWithIDs.entrySet()){ 2319 2320 if(entry.getValue().equals(predicted1)){ 2321 //System.out.println("1st predicted class: " +entry.getKey()); 2120 for (Map.Entry<String, Integer> entry : mapperWithIDs.entrySet()) { 2121 2122 if (entry.getValue().equals(predicted1)) { 2322 2123 predictedTags[0] = entry.getKey(); 2323 } 2324 else if(entry.getValue().equals(predicted2)){ 2124 } else if (entry.getValue().equals(predicted2)) { 2325 2125 predictedTags[1] = entry.getKey(); 2326 //System.out.println("2nd predicted class: " +entry.getKey()); 2327 } 2328 else if(entry.getValue().equals(predicted3)){ 2126 } else if (entry.getValue().equals(predicted3)) { 2329 2127 predictedTags[2] = entry.getKey(); 2330 //System.out.println("3rd predicted class: " +entry.getKey()); 2331 } 2332 else if(entry.getValue().equals(predicted4)){ 2128 } else if (entry.getValue().equals(predicted4)) { 2333 2129 predictedTags[3] = entry.getKey(); 2334 //System.out.println("2nd predicted class: " +entry.getKey()); 2335 } 2336 else if(entry.getValue().equals(predicted5)){ 2130 } else if (entry.getValue().equals(predicted5)) { 2337 2131 predictedTags[4] = entry.getKey(); 2338 //System.out.println("3rd predicted class: " +entry.getKey()); 2339 } 2340 else if(entry.getValue().equals(predicted6)){ 2132 } else if (entry.getValue().equals(predicted6)) { 2341 2133 predictedTags[5] = entry.getKey(); 2342 //System.out.println("2nd predicted class: " +entry.getKey()); 2343 } 2344 else if(entry.getValue().equals(predicted7)){ 2134 } else if (entry.getValue().equals(predicted7)) { 2345 2135 predictedTags[6] = entry.getKey(); 2346 //System.out.println("3rd predicted class: " +entry.getKey()); 2347 } 2348 else if(entry.getValue().equals(predicted8)){ 2136 } else if (entry.getValue().equals(predicted8)) { 2349 2137 predictedTags[7] = entry.getKey(); 2350 //System.out.println("2nd predicted class: " +entry.getKey()); 2351 } 2352 else if(entry.getValue().equals(predicted9)){ 2138 } else if (entry.getValue().equals(predicted9)) { 2353 2139 predictedTags[8] = entry.getKey(); 2354 //System.out.println("3rd predicted class: " +entry.getKey()); 2355 } 2356 else if(entry.getValue().equals(predicted10)){ 2140 } else if (entry.getValue().equals(predicted10)) { 2357 2141 predictedTags[9] = entry.getKey(); 2358 //System.out.println("3rd predicted class: " +entry.getKey()); 2359 } 2142 } 2360 2143 } 2361 2144 //clearing model, to add the new computed classes in jlist 2362 2145 model.clear(); 2363 for(Map.Entry<String, String> tag : mappings.entrySet()){ 2364 2365 for (int k=0; k<10; k++){2366 if(tag.getValue().equals(predictedTags[k])){ 2146 for (Map.Entry<String, String> tag : mappings.entrySet()) { 2147 2148 for (int k = 0; k < 10; k++) { 2149 if (tag.getValue().equals(predictedTags[k])) { 2367 2150 predictedTags[k] = tag.getKey(); 2368 2151 model.addElement(tag.getKey()); … … 2371 2154 } 2372 2155 System.out.println("combined, predicted classes: " + Arrays.toString(predictedTags)); 2373 2374 for(int r = 0; r <ranks.length; r++){2156 2157 for (int r = 0; r < ranks.length; r++) { 2375 2158 String predictedTag = predictedTags[r]; 2376 2159 Double currentWeight = alignedFilesAndWeights.get(modelFile); 2377 2160 double finalRank = ranks[r]*currentWeight; 2378 //String roundedWeight = new DecimalFormat("##.###").format(finalRank); 2379 //double finalRounded = Double.parseDouble(roundedWeight); 2380 //System.out.println("rank: " + ranks[r] + ", weight: " + currentWeight 2381 // + ", final rank: " + finalRounded + " " + predictedTags[r]); 2382 2383 if(scoreMap.containsKey(predictedTag)){ 2161 2162 if (scoreMap.containsKey(predictedTag)) { 2384 2163 Double scoreToAdd = scoreMap.get(predictedTag); 2385 2164 scoreMap.put(predictedTag, finalRank+scoreToAdd); 2386 } 2387 else{ 2165 } else { 2388 2166 scoreMap.put(predictedTag, finalRank); 2389 } 2167 } 2390 2168 //add final weight - predicted tag 2391 } 2392 } //files iter 2169 } 2170 } //files iter 2393 2171 model.clear(); 2394 2172 List<Double> scoresList = new ArrayList<>(scoreMap.values()); 2395 Collections.sort(scoresList ,Collections.reverseOrder());2396 2397 for(Double sco : scoresList){ 2398 if(model.size() >9){2173 Collections.sort(scoresList, Collections.reverseOrder()); 2174 2175 for (Double sco : scoresList) { 2176 if (model.size() > 9) { 2399 2177 break; 2400 2178 } 2401 for(Map.Entry<String, Double> scoreEntry : scoreMap.entrySet()){ 2402 2403 if(scoreEntry.getValue().equals(sco)){ 2404 //model.addElement(scoreEntry.toString()); //displaying score 2179 for (Map.Entry<String, Double> scoreEntry : scoreMap.entrySet()) { 2180 if (scoreEntry.getValue().equals(sco)) { 2405 2181 model.addElement(scoreEntry.getKey()); 2406 2182 } 2407 2183 } 2408 2184 } 2409 //System.out.println("final list:" + scoreMap); 2410 } 2185 } 2411 2186 } 2412 2187 2413 2188 private void createOSMObject(Collection<OsmPrimitive> sel) { 2414 2189 2415 2190 MathTransform transform = null; 2416 2191 GeometryFactory geometryFactory = new GeometryFactory(); … … 2419 2194 try { 2420 2195 transform = CRS.findMathTransform(sourceCRS, targetCRS, true); 2421 2196 2422 2197 } catch (FactoryException ex) { 2423 2198 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); 2424 2199 } 2425 2200 2426 2201 //fire an error to the user if he has multiple selection from map 2427 2202 2428 2203 //we consider simple (one instance) selection, so we get the first of the sel list 2429 //int singleSelectionCount = 0; 2430 //Collection<OsmPrimitive> paa = Main.main.getInProgressSelection(); 2431 2204 2432 2205 OSMWay selectedInstance; 2433 2206 List<OsmPrimitive> osmPrimitiveSelection = new ArrayList<>(sel); 2434 2207 OsmPrimitive s; 2435 2208 2436 2209 //get a simple selection 2437 if(!osmPrimitiveSelection.isEmpty()){ 2210 if (!osmPrimitiveSelection.isEmpty()) { 2438 2211 s = osmPrimitiveSelection.get(0); 2439 } 2440 else { 2212 } else { 2441 2213 return; 2442 2214 } 2443 2215 2444 2216 selectedInstance = new OSMWay(); 2445 for(Way selectedWay : s.getDataSet().getSelectedWays()){ 2446 // System.out.println(nod.getNodes()); 2217 for (Way selectedWay : s.getDataSet().getSelectedWays()) { 2447 2218 List<Node> selectedWayNodes = selectedWay.getNodes(); 2448 for(Node node : selectedWayNodes){ 2219 for (Node node : selectedWayNodes) { 2449 2220 node.getCoor(); 2450 //System.out.println(node.getCoor()); 2451 if(node.isLatLonKnown()){ 2452 //LatLon coord = nod.getCoor(); 2221 if (node.isLatLonKnown()) { 2453 2222 double lat = node.getCoor().lat(); 2454 2223 double lon = node.getCoor().lon(); … … 2456 2225 Coordinate sourceCoordinate = new Coordinate(lon, lat); 2457 2226 Coordinate targetGeometry = null; 2458 try { 2227 try { 2459 2228 targetGeometry = JTS.transform(sourceCoordinate, null, transform); 2460 2229 } catch (MismatchedDimensionException | TransformException ex) { 2461 2230 Logger.getLogger(OSMParser.class.getName()).log(Level.SEVERE, null, ex); 2462 } 2463 2464 Geometry geom = geometryFactory.createPoint(new Coordinate(targetGeometry)); 2231 } 2232 2233 Geometry geom = geometryFactory.createPoint(new Coordinate(targetGeometry)); 2465 2234 selectedInstance.addNodeGeometry(geom); 2466 2235 } … … 2468 2237 } 2469 2238 Geometry fullGeom = geometryFactory.buildGeometry(selectedInstance.getNodeGeometries()); 2470 //System.out.println("full geom: " + fullGeom.toText());2471 2239 2472 2240 System.out.println("number of nodes: " + selectedInstance.getNodeGeometries().size()); 2473 2241 2474 if((selectedInstance.getNodeGeometries().size() > 3) && 2475 selectedInstance.getNodeGeometries().get(0).equals(selectedInstance.getNodeGeometries() 2476 .get(selectedInstance.getNodeGeometries().size()-1))) 2477 { 2478 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 2479 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 2242 if ((selectedInstance.getNodeGeometries().size() > 3) && 2243 selectedInstance.getNodeGeometries().get(0).equals(selectedInstance.getNodeGeometries() 2244 .get(selectedInstance.getNodeGeometries().size()-1))) { 2245 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 2246 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 2480 2247 LinearRing linear = geometryFactory.createLinearRing(fullGeom.getCoordinates()); 2481 2248 Polygon poly = new Polygon(linear, null, geometryFactory); … … 2483 2250 2484 2251 System.out.println("\n\npolygon"); 2485 } 2486 else if (selectedInstance.getNodeGeometries().size()>1){ 2487 //it is an open geometry with more than one nodes, make it linestring 2252 } else if (selectedInstance.getNodeGeometries().size() > 1) { 2253 //it is an open geometry with more than one nodes, make it linestring 2488 2254 System.out.println("\n\nlinestring"); 2489 LineString lineString = geometryFactory.createLineString(fullGeom.getCoordinates()); 2490 selectedInstance.setGeometry(lineString); 2491 } 2492 else{ //we assume all the rest geometries are points 2255 LineString lineString = geometryFactory.createLineString(fullGeom.getCoordinates()); 2256 selectedInstance.setGeometry(lineString); 2257 } else { //we assume all the rest geometries are points 2493 2258 System.out.println("\n\npoint"); 2494 2259 Point point = geometryFactory.createPoint(fullGeom.getCoordinate()); … … 2496 2261 } 2497 2262 2498 //System.out.println(tt + " " + s.getDataSet().getNodes());2499 //Collection<Node> nodes = s.getDataSet().getNodes();2500 //nodeTmp.setGeometry(geom);2501 2502 2263 Map<String, String> selectedTags = s.getInterestingTags(); 2503 2264 selectedInstance.setAllTags(selectedTags); 2504 //System.out.println("selected instance interested tags: " + s.getInterestingTags()); 2505 //selectedInstance.setTagKeyValue(selectedTags); 2506 2507 //singleSelectionCount++; 2508 2265 2509 2266 //construct vector here 2510 if(selectedInstance != null){ 2267 if (selectedInstance != null) { 2511 2268 int id; 2512 if(mappings == null){ 2513 System.out.println("null mappings ERROR"); 2514 } 2515 2269 if (mappings == null) { 2270 System.out.println("null mappings ERROR"); 2271 } 2272 2516 2273 OSMClassification classifier = new OSMClassification(); 2517 2274 classifier.calculateClasses(selectedInstance, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 2518 2519 if(modelWithClasses){ 2275 2276 if (modelWithClasses) { 2520 2277 ClassFeatures classFeatures = new ClassFeatures(); 2521 2278 classFeatures.createClassFeatures(selectedInstance, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 2522 2279 id = 1422; 2523 } 2524 else{ 2280 } else { 2525 2281 id = 1; 2526 2282 } 2527 2283 2528 2284 GeometryFeatures geometryFeatures = new GeometryFeatures(id); 2529 2285 geometryFeatures.createGeometryFeatures(selectedInstance); 2530 id =geometryFeatures.getLastID();2286 id = geometryFeatures.getLastID(); 2531 2287 TextualFeatures textualFeatures = new TextualFeatures(id, textualList, languageDetector); 2532 textualFeatures.createTextualFeatures(selectedInstance); 2533 2288 textualFeatures.createTextualFeatures(selectedInstance); 2289 2534 2290 List<FeatureNode> featureNodeList = selectedInstance.getFeatureNodeList(); 2535 2291 System.out.println(featureNodeList); 2536 2292 2537 2293 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 2538 2294 2539 2295 int i = 0; 2540 for(FeatureNode featureNode : featureNodeList){ 2541 2296 for (FeatureNode featureNode : featureNodeList) { 2542 2297 featureNodeArray[i] = featureNode; 2543 //System.out.println("i: " + i + " array:" + featureNodeArray[i]);2544 2298 i++; 2545 2546 } 2547 FeatureNode[] testInstance2 = featureNodeArray; 2548 2299 } 2300 FeatureNode[] testInstance2 = featureNodeArray; 2301 2549 2302 Map<Integer, Integer> mapLabelsToIDs = new HashMap<>(); 2550 for(int h =0; h < modelSVMLabelSize; h++){ 2551 2303 for (int h = 0; h < modelSVMLabelSize; h++) { 2552 2304 mapLabelsToIDs.put(modelSVMLabels[h], h); 2553 //System.out.println(h + " <-> " + modelSVMLabels[h]); 2554 2555 } 2556 2305 } 2306 2557 2307 double[] scores = new double[modelSVMLabelSize]; 2558 2308 Linear.predictValues(modelSVM, testInstance2, scores); 2559 2309 2560 2310 Map<Double, Integer> scoresValues = new HashMap<>(); 2561 for(int h = 0; h < scores.length; h++){ 2311 for (int h = 0; h < scores.length; h++) { 2562 2312 scoresValues.put(scores[h], h); 2563 //System.out.println(h + " <-> " + scores[h]);2564 2313 } 2565 2314 2566 2315 Arrays.sort(scores); 2567 2316 2568 2317 int[] preds = new int[RECOMMENDATIONS_SIZE]; 2569 for (int p=0; p < RECOMMENDATIONS_SIZE; p++){2318 for (int p = 0; p < RECOMMENDATIONS_SIZE; p++) { 2570 2319 preds[p] = modelSVMLabels[scoresValues.get(scores[scores.length-(p+1)])]; 2571 2320 } 2572 2321 String[] predictedTags2 = new String[RECOMMENDATIONS_SIZE]; 2573 2574 for (int p=0; p<RECOMMENDATIONS_SIZE; p++){2575 if(idsWithMappings.containsKey(preds[p])){ 2322 2323 for (int p = 0; p < RECOMMENDATIONS_SIZE; p++) { 2324 if (idsWithMappings.containsKey(preds[p])) { 2576 2325 predictedTags2[p] = idsWithMappings.get(preds[p]); 2577 2326 } 2578 2579 } 2580 2327 } 2328 2581 2329 //clearing model, to add the new computed classes in jlist 2582 2330 model.clear(); 2583 for(Map.Entry<String, String> tag : mappings.entrySet()){ 2584 2585 for(int k=0; k<10; k++){ 2586 if(tag.getValue().equals(predictedTags2[k])){ 2331 for (Map.Entry<String, String> tag : mappings.entrySet()) { 2332 for (int k = 0; k < 10; k++) { 2333 if (tag.getValue().equals(predictedTags2[k])) { 2587 2334 predictedTags2[k] = tag.getKey(); 2588 2335 model.addElement(tag.getKey()); … … 2590 2337 } 2591 2338 } 2592 //System.out.println("createdOSM object, predicted classes: " + Arrays.toString(predictedTags));2593 2339 System.out.println("Optimized - create OSMObject, predicted classes: " + Arrays.toString(predictedTags2)); 2594 2340 } … … 2599 2345 InputStream tagsToClassesMapping = TrainWorker.class.getResourceAsStream("/resources/files/Map"); 2600 2346 Mapper mapper = new Mapper(); 2601 try { 2347 try { 2602 2348 mapper.parseFile(tagsToClassesMapping); 2603 2349 … … 2606 2352 } 2607 2353 mappings = mapper.getMappings(); 2608 mapperWithIDs = mapper.getMappingsWithIDs(); 2354 mapperWithIDs = mapper.getMappingsWithIDs(); 2609 2355 idsWithMappings = mapper.getIDsWithMappings(); 2610 //System.out.println("mappings " + mappings);2611 //System.out.println("mapperWithIDs " + mapperWithIDs);2612 2356 } 2613 2357 2614 2358 private void loadTextualList(File textualListFile) { 2615 2359 2616 2360 Scanner input = null; 2617 2361 2618 2362 try { 2619 2363 input = new Scanner(textualListFile); 2620 } 2621 catch (FileNotFoundException ex) { 2622 //Logger.getLogger(Statistics.class.getName()).log(Level.SEVERE, null, ex); 2623 } 2624 while(input.hasNext()) { 2364 } catch (FileNotFoundException ex) { 2365 Main.warn(ex); 2366 } 2367 while (input.hasNext()) { 2625 2368 String nextLine = input.nextLine(); 2626 2369 textualList.add(nextLine); 2627 2370 } 2628 System.out.println("Textual List parsed from file successfully." + textualList); 2371 System.out.println("Textual List parsed from file successfully." + textualList); 2629 2372 } 2630 2373 … … 2647 2390 private Map<File, Double> getAlignedModels(Map<File, Double> filesAndWeights) { 2648 2391 Map<File, Double> alignedFilesAndWeights = new HashMap<>(); 2649 if(modelWithClasses){ 2650 for(Entry<File, Double> entry : filesAndWeights.entrySet()){ 2392 if (modelWithClasses) { 2393 for (Entry<File, Double> entry : filesAndWeights.entrySet()) { 2651 2394 String absolutePath = entry.getKey().getAbsolutePath(); 2652 if(absolutePath.endsWith(".0")){ 2395 if (absolutePath.endsWith(".0")) { 2653 2396 String newPath = absolutePath.substring(0, absolutePath.length()-2) + ".1"; 2654 2397 File alignedFile = new File(newPath); 2655 if(alignedFile.exists()){ 2398 if (alignedFile.exists()) { 2656 2399 alignedFilesAndWeights.put(alignedFile, entry.getValue()); 2657 } 2658 } 2659 else{ 2400 } 2401 } else { 2660 2402 alignedFilesAndWeights.put(entry.getKey(), entry.getValue()); 2661 } 2662 } 2663 } 2664 else{ 2665 for(Entry<File, Double> entry : filesAndWeights.entrySet()){ 2403 } 2404 } 2405 } else { 2406 for (Entry<File, Double> entry : filesAndWeights.entrySet()) { 2666 2407 String absolutePath = entry.getKey().getAbsolutePath(); 2667 if(absolutePath.endsWith(".1")){ 2408 if (absolutePath.endsWith(".1")) { 2668 2409 String newPath = absolutePath.substring(0, absolutePath.length()-2) + ".0"; 2669 2410 File alignedFile = new File(newPath); 2670 if(alignedFile.exists()){ 2411 if (alignedFile.exists()) { 2671 2412 alignedFilesAndWeights.put(alignedFile, entry.getValue()); 2672 } 2673 } 2674 else{ 2413 } 2414 } else { 2675 2415 alignedFilesAndWeights.put(entry.getKey(), entry.getValue()); 2676 } 2677 } 2416 } 2417 } 2678 2418 } 2679 2419 return alignedFilesAndWeights; -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecToggleDialog.java
r31697 r32404 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.plugins.osmrec; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.GridBagLayout; … … 19 21 import java.util.Set; 20 22 import java.util.TreeMap; 23 21 24 import javax.swing.JLabel; 22 25 import javax.swing.JPanel; … … 41 44 import org.openstreetmap.josm.gui.SideButton; 42 45 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 43 import static org.openstreetmap.josm.tools.I18n.tr; 44 import org.openstreetmap.josm.tools.Predicates; 46 import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor; 45 47 import org.openstreetmap.josm.tools.Shortcut; 46 import org.openstreetmap.josm.tools.Utils;47 48 48 49 /** 49 50 * This class is a modification of the PropertiesDialog for the OSMRec. 50 * 51 * 51 * 52 * 52 53 * This dialog displays the tags of the current selected primitives. 53 54 * … … 101 102 private final AddAction addAction = new AddAction(); 102 103 private final EditActionTrain editAction = new EditActionTrain(); 103 // private final DeleteAction deleteAction = new DeleteAction(); 104 // private final JosmAction[] josmActions = new JosmAction[]{addAction, editAction, deleteAction}; 104 // private final DeleteAction deleteAction = new DeleteAction(); 105 // private final JosmAction[] josmActions = new JosmAction[]{addAction, editAction, deleteAction}; 105 106 106 107 /** … … 143 144 membershipTable.addMouseListener(mouseClickWatch); 144 145 scrollPane.addMouseListener(mouseClickWatch); 145 editHelper.loadTagsIfNeeded(); 146 147 } 148 149 146 editHelper.loadTagsIfNeeded(); 147 148 } 149 150 /** 150 151 * This simply fires up an {@link RelationEditor} for the relation shown; everything else 151 152 * is the editor's business. … … 154 155 */ 155 156 private void editMembership(int row) { 156 Relation relation = (Relation)membershipData.getValueAt(row, 0); 157 Relation relation = (Relation) membershipData.getValueAt(row, 0); 157 158 Main.map.relationListDialog.selectRelation(relation); 158 159 } 159 160 160 161 private int findRow(TableModel model, Object value) { 161 for (int i =0; i<model.getRowCount(); i++) {162 for (int i = 0; i < model.getRowCount(); i++) { 162 163 if (model.getValueAt(i, 0).equals(value)) 163 164 return i; … … 174 175 } 175 176 176 // </editor-fold> 177 // </editor-fold> 177 178 178 179 // <editor-fold defaultstate="collapsed" desc="Event listeners methods"> … … 196 197 selectedTag = editHelper.getChangedKey(); // select last added or last edited key by default 197 198 if (selectedTag == null && tagTable.getSelectedRowCount() == 1) { 198 selectedTag = (String)tagData.getValueAt(tagTable.getSelectedRow(), 0); 199 selectedTag = (String) tagData.getValueAt(tagTable.getSelectedRow(), 0); 199 200 } 200 201 if (membershipTable.getSelectedRowCount() == 1) { 201 selectedRelation = (Relation)membershipData.getValueAt(membershipTable.getSelectedRow(), 0); 202 selectedRelation = (Relation) membershipData.getValueAt(membershipTable.getSelectedRow(), 0); 202 203 } 203 204 … … 228 229 Relation r = (Relation) ref; 229 230 MemberInfo mi = roles.get(r); 230 if(mi == null) { 231 if (mi == null) { 231 232 mi = new MemberInfo(newSel); 232 233 } … … 248 249 int comp = Boolean.valueOf(o1.isDisabledAndHidden()).compareTo(o2.isDisabledAndHidden()); 249 250 return comp != 0 ? comp : DefaultNameFormatter.getInstance().getRelationComparator().compare(o1, o2); 250 }} 251 ); 251 } }); 252 252 253 253 for (Relation r: sortedRelations) { … … 261 261 boolean hasTags = hasSelection && tagData.getRowCount() > 0; 262 262 boolean hasMemberships = hasSelection && membershipData.getRowCount() > 0; 263 263 264 264 addAction.setEnabled(hasSelection); 265 265 //editAction.setEnabled(hasTags || hasMemberships); … … 268 268 tagTable.getTableHeader().setVisible(hasTags); 269 269 selectSth.setVisible(!hasSelection); 270 270 271 271 int selectedIndex; 272 272 if (selectedTag != null && (selectedIndex = findRow(tagData, selectedTag)) != -1) { … … 274 274 } else if (selectedRelation != null && (selectedIndex = findRow(membershipData, selectedRelation)) != -1) { 275 275 membershipTable.changeSelection(selectedIndex, 0, false, false); 276 } else if(hasTags) { 276 } else if (hasTags) { 277 277 tagTable.changeSelection(0, 0, false, false); 278 } else if(hasMemberships) { 278 } else if (hasMemberships) { 279 279 membershipTable.changeSelection(0, 0, false, false); 280 280 } … … 316 316 // </editor-fold> 317 317 318 318 /** 319 319 * Class that watches for mouse clicks 320 320 * @author imi … … 330 330 tagTable.clearSelection(); 331 331 } 332 } else if (e.getSource() == tagTable 332 } else if (e.getSource() == tagTable) { 333 333 // double click, edit or add tag 334 334 int row = tagTable.rowAtPoint(e.getPoint()); … … 338 338 } else { 339 339 editHelper.addTag(); 340 //btnAdd.requestFocusInWindow();341 340 } 342 341 } else if (e.getSource() == membershipTable) { … … 347 346 } else { 348 347 editHelper.addTag(); 349 //btnAdd.requestFocusInWindow();350 348 } 351 349 } … … 379 377 } 380 378 381 String getPositionString() {382 if (positionString == null) {383 positionString = Utils.getPositionListString(position);384 // if not all objects from the selection are member of this relation385 if (Utils.exists(selection, Predicates.not(Predicates.inCollection(members)))) {386 positionString += ",\u2717";387 }388 members = null;389 position = null;390 selection = null;391 }392 return Utils.shortenString(positionString, 20);393 }394 395 String getRoleString() {396 if (roleString == null) {397 for (RelationMember r : role) {398 if (roleString == null) {399 roleString = r.getRole();400 } else if (!roleString.equals(r.getRole())) {401 roleString = tr("<different>");402 break;403 }404 }405 }406 return roleString;407 }408 409 379 @Override 410 380 public String toString() { … … 424 394 return false; 425 395 } 396 426 397 @Override 427 398 public Class<?> getColumnClass(int columnIndex) { … … 443 414 public void actionPerformed(ActionEvent e) { 444 415 editHelper.addTag(); 445 btnAdd.requestFocusInWindow(); 416 btnAdd.requestFocusInWindow(); 446 417 } 447 418 } … … 452 423 */ 453 424 class EditActionTrain extends JosmAction implements ListSelectionListener { 454 EditActionTrain() { 425 EditActionTrain() { 455 426 super(tr("Train a Model"), /* ICON() */ "dialogs/fix", tr("Start the training engine!"), 456 427 Shortcut.registerShortcut("properties:edit", tr("Edit Tags"), KeyEvent.VK_S, 457 428 Shortcut.ALT), false); 458 429 setEnabled(true); 459 updateEnabledState(); 430 updateEnabledState(); 460 431 } 461 432 … … 470 441 int row = membershipTable.getSelectedRow(); 471 442 editHelper.editTag(row, false); 472 //editMembership(row); 473 } 474 else{ 443 } else { 475 444 editHelper.editTag(1, false); 476 445 } … … 480 449 protected void updateEnabledState() { 481 450 setEnabled(true); 482 // setEnabled(483 // (tagTable != null && tagTable.getSelectedRowCount() == 1)484 // ^ (membershipTable != null && membershipTable.getSelectedRowCount() == 1)485 // );486 451 } 487 452 -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/container/OSMNode.java
r31106 r32404 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.container; 1 3 2 package org.openstreetmap.josm.plugins.container; 4 import java.util.HashMap; 5 import java.util.Map; 3 6 4 7 import com.vividsolutions.jts.geom.Geometry; 5 import java.util.HashMap;6 import java.util.Map;7 8 8 9 /** … … 10 11 * @author imis-nkarag 11 12 */ 12 13 public class OSMNode { 13 public class OSMNode { 14 14 15 15 private String id; 16 private String lang;17 16 private String action; //e.g modify 18 private String visible; 17 private String visible; 19 18 private Geometry geometry; 20 19 private String timestamp; … … 23 22 private String version; 24 23 private String changeset; 25 private final Map<String, String> tags = new HashMap<>(); 26 24 private final Map<String, String> tags = new HashMap<>(); 25 27 26 //attribute getters 28 public String getID(){ 27 public String getID() { 29 28 return id; 30 29 } 31 32 public String get lang(){33 return lang;30 31 public String getAction() { 32 return action; 34 33 } 35 34 36 public String get Action(){37 return action;35 public String getVisible() { 36 return visible; 38 37 } 39 40 public String getVisible(){41 return visible;42 }43 38 44 public Geometry getGeometry(){ 39 public Geometry getGeometry() { 45 40 return this.geometry; 46 41 } 47 48 public String getTimestamp(){ 42 43 public String getTimestamp() { 49 44 return timestamp; 50 45 } 51 52 public String getUid(){ 46 47 public String getUid() { 53 48 return uid; 54 } 55 56 public String getUser(){ 49 } 50 51 public String getUser() { 57 52 return user; 58 53 } 59 60 public String getVersion(){ 54 55 public String getVersion() { 61 56 return version; 62 } 57 } 63 58 64 public String getChangeset(){ 59 public String getChangeset() { 65 60 return changeset; 66 } 67 68 public Map<String,String> getTagKeyValue(){ 61 } 62 63 public Map<String, String> getTagKeyValue() { 69 64 return tags; 70 } 71 65 } 66 72 67 //attributes setters 73 public void setID(String id){ 68 public void setID(String id) { 74 69 this.id = id; 75 70 } 76 77 public void setLang(String lang){ 78 this.lang = lang; 79 } 80 81 public void setAction(String action){ 71 72 public void setAction(String action) { 82 73 this.action = action; 83 74 } 84 85 public void setVisible(String visible){ 75 76 public void setVisible(String visible) { 86 77 this.visible = visible; 87 78 } 88 89 public void setGeometry(Geometry geometry){ 79 80 public void setGeometry(Geometry geometry) { 90 81 this.geometry = geometry; 91 82 } 92 93 public void setTimestamp(String timestamp){ 83 84 public void setTimestamp(String timestamp) { 94 85 this.timestamp = timestamp; 95 86 } 96 97 public void setUid(String uid){ 87 88 public void setUid(String uid) { 98 89 this.uid = uid; 99 } 100 101 public void setUser(String user){ 90 } 91 92 public void setUser(String user) { 102 93 this.user = user; 103 94 } 104 95 105 public void setVersion(String version){ 96 public void setVersion(String version) { 106 97 this.version = version; 107 } 108 109 public void setChangeset(String changeset){ 98 } 99 100 public void setChangeset(String changeset) { 110 101 this.changeset = changeset; 111 } 112 113 public void setTagKeyValue(String tagKey, String tagValue){ 102 } 103 104 public void setTagKeyValue(String tagKey, String tagValue) { 114 105 this.tags.put(tagKey, tagValue); 115 106 } 116 107 117 108 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/container/OSMRelation.java
r31106 r32404 1 package org.openstreetmap.josm.plugins.container; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.container; 2 3 3 4 import java.io.Serializable; … … 10 11 /** 11 12 * Class containing information about the OSM relations. 12 * 13 * 13 14 * @author imis-nkarag 14 15 */ 15 16 16 public class OSMRelation implements Serializable{ 17 17 public class OSMRelation implements Serializable { 18 18 19 private String id; 19 20 private Set<Integer> classIDs; 20 21 private final List<String> memberReferences = new ArrayList<>(); 21 22 private final Map<String, String> tags = new HashMap<>(); 22 23 public String getID(){ 23 24 public String getID() { 24 25 return id; 25 26 } 26 27 public List<String> getMemberReferences(){ 27 28 public List<String> getMemberReferences() { 28 29 return memberReferences; 29 30 } 30 31 public Set<Integer> getClassIDs(){ 31 32 public Set<Integer> getClassIDs() { 32 33 return this.classIDs; 33 34 } 34 35 public Map<String, String> getTagKeyValue(){ 35 36 public Map<String, String> getTagKeyValue() { 36 37 return tags; 37 38 } 38 39 public void setID(String id){ 39 40 public void setID(String id) { 40 41 this.id = id; 41 42 } 42 43 public void setClassIDs(Set<Integer> classIDs){ 43 44 public void setClassIDs(Set<Integer> classIDs) { 44 45 this.classIDs = classIDs; 45 } 46 47 public void addMemberReference(String memberReference){ 46 } 47 48 public void addMemberReference(String memberReference) { 48 49 memberReferences.add(memberReference); 49 } 50 51 public void setTagKeyValue(String tagKey, String tagValue){ 50 } 51 52 public void setTagKeyValue(String tagKey, String tagValue) { 52 53 this.tags.put(tagKey, tagValue); 53 54 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/container/OSMWay.java
r32358 r32404 1 package org.openstreetmap.josm.plugins.container; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.container; 2 3 3 4 import java.io.Serializable; … … 19 20 * @author imis-nkarag 20 21 */ 21 public class OSMWay implements Serializable{ 22 public class OSMWay implements Serializable { 22 23 23 24 private String id; … … 30 31 private Map<String, String> tags = new HashMap<>(); 31 32 private Geometry geometry; 32 private TreeMap<Integer,Double> indexVector = new TreeMap<>(); 33 private TreeMap<Integer, Double> indexVector = new TreeMap<>(); 33 34 private ArrayList<FeatureNode> featureNodeList = new ArrayList<>(); 34 //private Map<String, String> tags;35 35 36 36 //way attributes getters 37 public String getID(){ 37 public String getID() { 38 38 return id; 39 39 } 40 40 41 public String getUser(){ 41 public String getUser() { 42 42 return user; 43 43 } 44 44 45 public List<Geometry> getNodeGeometries(){ 45 public List<Geometry> getNodeGeometries() { 46 46 return nodeGeometries; 47 47 } 48 48 49 public Coordinate[] getCoordinateList(){ 49 public Coordinate[] getCoordinateList() { 50 50 coordinateList = nodeGeometries.toArray(new Coordinate[0]); 51 51 return coordinateList; 52 52 } 53 53 54 public Geometry getGeometry(){ 54 public Geometry getGeometry() { 55 55 return geometry; 56 56 } 57 57 58 public List<String> getNodeReferences(){ 58 public List<String> getNodeReferences() { 59 59 return nodeReferences; 60 60 } 61 61 62 public int getNumberOfNodes(){ 62 public int getNumberOfNodes() { 63 63 return nodeReferences.size(); 64 64 } 65 65 66 public Map<String, String> getTagKeyValue(){ 66 public Map<String, String> getTagKeyValue() { 67 67 return tags; 68 68 } 69 69 70 public int getClassID(){ 70 public int getClassID() { 71 71 return classID; 72 72 } 73 73 74 public Set<Integer> getClassIDs(){ 74 public Set<Integer> getClassIDs() { 75 75 return classIDs; 76 76 } 77 77 78 public TreeMap<Integer, Double> getIndexVector(){ 78 public TreeMap<Integer, Double> getIndexVector() { 79 79 return indexVector; 80 80 } 81 81 82 public void setIndexVector(TreeMap<Integer, Double> indexVector){ 82 public void setIndexVector(TreeMap<Integer, Double> indexVector) { 83 83 this.indexVector = indexVector; 84 84 } 85 85 86 86 //way attributes setters 87 public void setID(String id){ 87 public void setID(String id) { 88 88 this.id = id; 89 89 } 90 90 91 public void setUser(String user){ 91 public void setUser(String user) { 92 92 this.user = user; 93 93 } 94 94 95 public void setTagKeyValue(String tagKey, String tagValue){ 95 public void setTagKeyValue(String tagKey, String tagValue) { 96 96 this.tags.put(tagKey, tagValue); 97 97 } 98 98 99 public void addNodeReference(String nodeReference){ 99 public void addNodeReference(String nodeReference) { 100 100 nodeReferences.add(nodeReference); 101 101 } 102 102 103 public void addNodeGeometry(Geometry geometry){ 103 public void addNodeGeometry(Geometry geometry) { 104 104 nodeGeometries.add(geometry); 105 105 } 106 106 107 public void setGeometry(Geometry geometry){ 107 public void setGeometry(Geometry geometry) { 108 108 this.geometry = geometry; 109 109 } 110 110 111 public void setClassID(int classID){ 111 public void setClassID(int classID) { 112 112 this.classID = classID; 113 113 } 114 114 115 public void setClassIDs(Set<Integer> classIDs){ 115 public void setClassIDs(Set<Integer> classIDs) { 116 116 this.classIDs = classIDs; 117 117 } 118 118 119 public void setFeature(FeatureNode featureNode){ 119 public void setFeature(FeatureNode featureNode) { 120 120 this.featureNodeList.add(featureNode); 121 121 } 122 122 123 public List<FeatureNode> getFeatureNodeList(){ 123 public List<FeatureNode> getFeatureNodeList() { 124 124 return featureNodeList; 125 125 } 126 126 127 public void setAllTags(Map<String, String> selectedTags){ 127 public void setAllTags(Map<String, String> selectedTags) { 128 128 tags.putAll(selectedTags); 129 129 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/core/TrainWorker.java
r31564 r32404 1 package org.openstreetmap.josm.plugins.core; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.core; 2 3 3 4 import static java.util.concurrent.TimeUnit.NANOSECONDS; 4 import de.bwaldvogel.liblinear.FeatureNode; 5 import de.bwaldvogel.liblinear.Linear; 6 import de.bwaldvogel.liblinear.Model; 7 import de.bwaldvogel.liblinear.Parameter; 8 import de.bwaldvogel.liblinear.Problem; 9 import de.bwaldvogel.liblinear.SolverType; 5 10 6 import java.awt.event.ActionEvent; 11 7 import java.awt.event.ActionListener; … … 29 25 import java.util.logging.Level; 30 26 import java.util.logging.Logger; 27 31 28 import javax.swing.SwingWorker; 32 import org.openstreetmap.josm.plugins.container.OSMRelation; 33 import org.openstreetmap.josm.plugins.container.OSMWay; 34 import org.openstreetmap.josm.plugins.extractor.Analyzer; 35 import org.openstreetmap.josm.plugins.extractor.LanguageDetector; 36 import org.openstreetmap.josm.plugins.features.ClassFeatures; 37 import org.openstreetmap.josm.plugins.features.GeometryFeatures; 38 import org.openstreetmap.josm.plugins.features.OSMClassification; 39 import org.openstreetmap.josm.plugins.features.RelationFeatures; 40 import org.openstreetmap.josm.plugins.features.TextualFeatures; 41 import org.openstreetmap.josm.plugins.parsers.Mapper; 42 import org.openstreetmap.josm.plugins.parsers.OSMParser; 43 import org.openstreetmap.josm.plugins.parsers.Ontology; 44 import org.openstreetmap.josm.plugins.parsers.TextualStatistics; 29 30 import org.openstreetmap.josm.plugins.osmrec.container.OSMRelation; 31 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 32 import org.openstreetmap.josm.plugins.osmrec.extractor.Analyzer; 33 import org.openstreetmap.josm.plugins.osmrec.extractor.LanguageDetector; 34 import org.openstreetmap.josm.plugins.osmrec.features.ClassFeatures; 35 import org.openstreetmap.josm.plugins.osmrec.features.GeometryFeatures; 36 import org.openstreetmap.josm.plugins.osmrec.features.OSMClassification; 37 import org.openstreetmap.josm.plugins.osmrec.features.RelationFeatures; 38 import org.openstreetmap.josm.plugins.osmrec.features.TextualFeatures; 39 import org.openstreetmap.josm.plugins.osmrec.parsers.Mapper; 40 import org.openstreetmap.josm.plugins.osmrec.parsers.OSMParser; 41 import org.openstreetmap.josm.plugins.osmrec.parsers.Ontology; 42 import org.openstreetmap.josm.plugins.osmrec.parsers.TextualStatistics; 43 44 import de.bwaldvogel.liblinear.FeatureNode; 45 import de.bwaldvogel.liblinear.Linear; 46 import de.bwaldvogel.liblinear.Model; 47 import de.bwaldvogel.liblinear.Parameter; 48 import de.bwaldvogel.liblinear.Problem; 49 import de.bwaldvogel.liblinear.SolverType; 45 50 46 51 /** 47 52 * Provides the necessary functionality for cross validating and training of SVM models. 48 * 53 * 49 54 * @author imis-nkarag 50 55 */ 51 52 56 public class TrainWorker extends SwingWorker<Void, Void> implements ActionListener { 53 57 54 58 private final String inputFilePath; 55 //private final String tagsToClassesFilePath; 56 private static Map<String,String> mappings; 57 private static Map<String,Integer> mapperWithIDs; 58 //private static final String PATH = Paths.get("").toAbsolutePath().toString(); 59 59 private static Map<String, String> mappings; 60 private static Map<String, Integer> mapperWithIDs; 61 60 62 private static List<OSMWay> wayList; 61 63 private static List<OSMRelation> relationList; 62 64 63 65 private static Map<String, List<String>> indirectClasses; 64 66 private static Map<String, Integer> indirectClassesWithIDs; 65 //private static List<OntClass> listHierarchy; 66 private static List<String> namesList; 67 //private static Properties osmRecProperties = new Properties(); 67 private static List<String> namesList; 68 68 private int numberOfTrainingInstances; 69 69 private final String modelDirectoryPath; 70 //private final String textualDirectoryPath;71 70 private final File modelDirectory; 72 71 private static double score1 = 0; … … 85 84 private final boolean topKIsSelected; 86 85 private String textualListFilePath; 87 86 88 87 private static final boolean USE_CLASS_FEATURES = false; 89 88 private static final boolean USE_RELATION_FEATURES = false; … … 92 91 private static LanguageDetector languageDetector; 93 92 private final String inputFileName; 94 95 96 97 public TrainWorker(String inputFilePath, boolean validateFlag, double cParameterFromUser, 93 94 public TrainWorker(String inputFilePath, boolean validateFlag, double cParameterFromUser, 98 95 int topK, int frequency, boolean topKIsSelected, LanguageDetector languageDetector) { 99 super();100 96 TrainWorker.languageDetector = languageDetector; 101 this.inputFilePath = inputFilePath; 97 this.inputFilePath = inputFilePath; 102 98 this.validateFlag = validateFlag; 103 99 this.cParameterFromUser = cParameterFromUser; … … 105 101 this.frequency = frequency; 106 102 this.topKIsSelected = topKIsSelected; 107 if(System.getProperty("os.name").contains("ux")){ 103 if (System.getProperty("os.name").contains("ux")) { 108 104 inputFileName = inputFilePath.substring(inputFilePath.lastIndexOf("/")); 109 } 110 else{ 105 } else { 111 106 inputFileName = inputFilePath.substring(inputFilePath.lastIndexOf("\\")); 112 107 } 113 108 System.out.println("find parent directory, create osmrec dir for models: " + new File(inputFilePath).getParentFile()); 114 109 modelDirectoryPath = new File(inputFilePath).getParentFile() + "/OSMRec_models"; 115 //textualDirectoryPath = new File(inputFilePath).getParentFile() + "/OSMRec_models/textualList.txt"; 116 110 117 111 modelDirectory = new File(modelDirectoryPath); 118 112 119 113 if (!modelDirectory.exists()) { 120 114 modelDirectory.mkdir(); 121 115 System.out.println("model directory created!"); 122 } 123 else { 116 } else { 124 117 System.out.println("directory already exists!"); 125 } 126 } 127 118 } 119 } 120 128 121 @Override 129 public Void doInBackground() throws Exception { 122 public Void doInBackground() throws Exception { 130 123 extractTextualList(); 131 124 parseFiles(); 132 if(validateFlag) {125 if (validateFlag) { 133 126 validateLoop(); 134 127 System.out.println("Training model with the best c: " + bestConfParam); … … 137 130 clearDataset(); 138 131 trainModelWithClasses(bestConfParam); 139 140 } 141 else{ 132 } else { 142 133 clearDataset(); 143 134 trainModel(cParameterFromUser); … … 148 139 return null; 149 140 } 150 151 private void extractTextualList(){ 141 142 private void extractTextualList() { 152 143 System.out.println("Running analysis.."); 153 144 //provide top-K … … 155 146 //Keep terms with frequency higher than N 156 147 //Use the remaining terms as training features 157 148 158 149 Analyzer anal = new Analyzer(inputFilePath, languageDetector); 159 150 anal.runAnalysis(); 160 //System.out.println(anal.getFrequencies());161 //System.out.println(anal.getTopKMostFrequent(15));162 //System.out.println(anal.getWithFrequency(100));163 151 164 152 textualListFilePath = modelDirectory.getAbsolutePath()+"/textualList.txt"; 165 153 File textualFile = new File(textualListFilePath); //decide path of models 166 if(textualFile.exists()){ 154 if (textualFile.exists()) { 167 155 textualFile.delete(); 168 } 156 } 169 157 try { 170 158 textualFile.createNewFile(); 171 159 } catch (IOException ex) { 172 160 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 173 } 174 175 //writeTextualListToFile(textualFilePath, anal.getTopKMostFrequent(15)); 161 } 162 176 163 List<Map.Entry<String, Integer>> textualList; 177 if(topKIsSelected){ 164 if (topKIsSelected) { 178 165 textualList = anal.getTopKMostFrequent(topK); 179 //System.out.println(textualList); 180 } 181 else{ 166 } else { 182 167 textualList = anal.getWithFrequency(frequency); 183 //System.out.println(textualList);184 168 } 185 169 … … 187 171 System.out.println("textual list saved at location:\n" + textualListFilePath); 188 172 //write list to file and let parser do the loading from the names file 189 173 190 174 //method read default list 191 175 //method extract textual list - > the list will be already in memory, so the names parser doesn t have to be called 192 if(USE_CLASS_FEATURES){ 176 if (USE_CLASS_FEATURES) { 193 177 numberOfFeatures = 1422 + 105 + textualList.size(); //105 is number of geometry features 194 } 195 else{ 178 } else { 196 179 numberOfFeatures = 105 + textualList.size(); 197 180 } 198 181 } 199 200 182 201 183 private void parseFiles() { 202 // File tagsToClassesFile = new File(tagsToClassesFilePath);203 204 //osmRecProperties.load(Train.class.getResourceAsStream("/resources/properties/osmRec.properties"));205 184 InputStream tagsToClassesMapping = TrainWorker.class.getResourceAsStream("/resources/files/Map"); 206 185 207 186 Mapper mapper = new Mapper(); 208 try { 187 try { 209 188 mapper.parseFile(tagsToClassesMapping); 210 189 } catch (FileNotFoundException ex) { … … 212 191 } 213 192 mappings = mapper.getMappings(); 214 mapperWithIDs = mapper.getMappingsWithIDs(); 215 //System.out.println("success. mappings empty? " + mappings.isEmpty()); 216 193 mapperWithIDs = mapper.getMappingsWithIDs(); 194 217 195 InputStream ontologyStream = TrainWorker.class.getResourceAsStream("/resources/files/owl.xml"); 218 Ontology ontology = new Ontology(ontologyStream); 196 Ontology ontology = new Ontology(ontologyStream); 219 197 220 198 ontology.parseOntology(); … … 222 200 indirectClasses = ontology.getIndirectClasses(); 223 201 indirectClassesWithIDs = ontology.getIndirectClassesIDs(); 224 //listHierarchy = ontology.getListHierarchy(); 225 226 //InputStream textualFileStream = TrainWorker.class.getResourceAsStream("/resources/files/names"); 227 InputStream textualFileStream = null; 202 203 InputStream textualFileStream = null; 228 204 try { 229 205 textualFileStream = new FileInputStream(new File(textualListFilePath)); … … 231 207 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 232 208 } 233 209 234 210 readTextualFromDefaultList(textualFileStream); 235 236 OSMParser osmParser = new OSMParser(inputFilePath); 237 211 212 OSMParser osmParser = new OSMParser(inputFilePath); 213 238 214 osmParser.parseDocument(); 239 215 relationList = osmParser.getRelationList(); 240 216 wayList = osmParser.getWayList(); 241 217 numberOfTrainingInstances = osmParser.getWayList().size(); 242 System.out.println("number of instances: " + numberOfTrainingInstances); 243 System.out.println("end of parsing files."); 244 245 } 246 247 public void validateLoop(){ 248 // Double[] confParams = new Double[] {Math.pow(2, -10), Math.pow(2, -5),Math.pow(2, -3),Math.pow(2, -1), 249 // Math.pow(2, 0),Math.pow(2, 1),Math.pow(2, 2),Math.pow(2, 3),Math.pow(2, 4),Math.pow(2, 5),Math.pow(2, 6), 250 // Math.pow(2, 8),Math.pow(2, 10),Math.pow(2, 12),Math.pow(2, 14),Math.pow(2, 15),Math.pow(2, 16),Math.pow(2, 17)}; 251 252 Double[] confParams = new Double[] {Math.pow(2, -3),Math.pow(2, 1),Math.pow(2, -10),Math.pow(2, -10), Math.pow(2, -5),Math.pow(2, -3)}; 253 218 System.out.println("number of instances: " + numberOfTrainingInstances); 219 System.out.println("end of parsing files."); 220 221 } 222 223 public void validateLoop() { 224 Double[] confParams = new Double[] {Math.pow(2, -3), Math.pow(2, 1), 225 Math.pow(2, -10), Math.pow(2, -10), Math.pow(2, -5), Math.pow(2, -3)}; 226 254 227 double bestC = Math.pow(2, -10); 255 256 for(Double param : confParams){ 257 258 // if(this.isCancelled()){ 259 // break; 260 // } 228 229 for (Double param : confParams) { 230 261 231 foldScore1 = 0; 262 232 foldScore5 = 0; … … 265 235 clearDataset(); 266 236 System.out.println("fold1"); 267 //crossValidateFold(0, 3, 3, 4, false, param);268 237 crossValidateFold(0, 4, 4, 5, false, param); //4-1 269 setProgress(4*((5*(trainProgress++))/confParams.length)); 270 //System.out.println((5*trainProgress)/confParams.length); 271 238 setProgress(4*((5*(trainProgress++))/confParams.length)); 239 272 240 foldScore1 = foldScore1 + score1; 273 241 foldScore5 = foldScore5 + score5; … … 275 243 clearDataset(); 276 244 System.out.println("fold2"); 277 //crossValidateFold(1, 4, 4, 5, false, param);278 245 crossValidateFold(1, 5, 0, 1, false, param); 279 246 setProgress(4*((5*(trainProgress++))/confParams.length)); 280 //System.out.println((5*trainProgress)/confParams.length); 281 247 282 248 foldScore1 = foldScore1 + score1; 283 249 foldScore5 = foldScore5 + score5; … … 285 251 clearDataset(); 286 252 System.out.println("fold3"); 287 //crossValidateFold(2, 5, 0, 1, false, param);288 253 crossValidateFold(0, 5, 1, 2, true, param); 289 254 setProgress(4*((5*(trainProgress++))/confParams.length)); 290 //System.out.println((5*trainProgress)/confParams.length); 291 255 292 256 foldScore1 = foldScore1 + score1; 293 257 foldScore5 = foldScore5 + score5; … … 295 259 clearDataset(); 296 260 System.out.println("fold4"); 297 //crossValidateFold(0, 5, 1, 2, true, param); 261 //crossValidateFold(0, 5, 1, 2, true, param); 298 262 crossValidateFold(0, 5, 2, 3, true, param); 299 263 setProgress(4*((5*(trainProgress++))/confParams.length)); 300 264 //System.out.println((5*trainProgress)/confParams.length); 301 265 302 266 foldScore1 = foldScore1 + score1; 303 267 foldScore5 = foldScore5 + score5; … … 305 269 clearDataset(); 306 270 System.out.println("fold5"); 307 //crossValidateFold(0, 5, 2, 3, true, param);308 271 crossValidateFold(0, 5, 3, 4, true, param); 309 272 setProgress(4*((5*(trainProgress++))/confParams.length)); 310 //System.out.println((5*trainProgress)/confParams.length); 311 273 312 274 foldScore1 = foldScore1 + score1; 313 275 foldScore5 = foldScore5 + score5; 314 276 foldScore10 = foldScore10 + score10; 315 277 System.out.println("\n\nC=" + param + ", average score 1-5-10: " + foldScore1/5 +" "+ foldScore5/5 + " "+ foldScore10/5); 316 if(bestScore < foldScore1 ){278 if (bestScore < foldScore1) { 317 279 bestScore = foldScore1; 318 280 bestC = param; 319 281 } 320 282 321 283 } 322 284 System.out.println(4*((5*(trainProgress++))/confParams.length)); 323 //setProgress(100);324 285 bestConfParam = bestC; 325 System.out.println("best c param= " + bestC + ", score: " + bestScore/5 326 } 327 328 public void crossValidateFold(int a, int b, int c, int d, boolean skip, double param){ 286 System.out.println("best c param= " + bestC + ", score: " + bestScore/5); 287 } 288 289 public void crossValidateFold(int a, int b, int c, int d, boolean skip, double param) { 329 290 System.out.println("Starting cross validation"); 330 291 int testSize = wayList.size()/5; 331 292 332 List<OSMWay> trainList= new ArrayList<>(); 333 for(int g = a*testSize; g<b*testSize; g++){ // 0~~1~~2~~3~~4~~5 334 if(skip){ 335 if(g == (c)*testSize){g=(c+1)*testSize;} 336 337 } 338 trainList.add(wayList.get(g)); 339 } 293 List<OSMWay> trainList = new ArrayList<>(); 294 for (int g = a*testSize; g < b*testSize; g++) { // 0~~1~~2~~3~~4~~5 295 if (skip) { 296 if (g == (c)*testSize) { 297 g = (c+1)*testSize; 298 } 299 } 300 trainList.add(wayList.get(g)); 301 } 340 302 341 303 int wayListSizeWithoutUnclassified = trainList.size(); 342 int u = 0;343 304 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 344 305 345 306 //set classes for each osm instance 346 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 347 int lalala = 0; 348 for(OSMWay way : trainList){ 307 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 308 for (OSMWay way : trainList) { 349 309 350 310 OSMClassification classifyInstances = new OSMClassification(); 351 311 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 352 312 353 if(way.getClassIDs().isEmpty()){ 313 if (way.getClassIDs().isEmpty()) { 354 314 wayListSizeWithoutUnclassified -= 1; 355 u++; 356 } 357 else { 315 } else { 358 316 sizeToBeAddedToArray = sizeToBeAddedToArray + way.getClassIDs().size()-1; 359 } 360 } 361 double C = param; 317 } 318 } 319 double C = param; 362 320 double eps = 0.001; 363 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; //new double[117558];//364 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; //working[3812];365 int k = 0;366 367 for(OSMWay way : trainList){ 368 //adding multiple vectors 369 int id; 370 if(USE_CLASS_FEATURES) {321 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; 322 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; 323 int k = 0; 324 325 for (OSMWay way : trainList) { 326 //adding multiple vectors 327 int id; 328 if (USE_CLASS_FEATURES) { 371 329 ClassFeatures class_vector = new ClassFeatures(); 372 330 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 373 331 id = 1422; 374 } 375 else{ 332 } else { 376 333 id = 1; 377 334 } … … 382 339 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 383 340 //id = 1526 384 if(USE_RELATION_FEATURES){ 341 if (USE_RELATION_FEATURES) { 385 342 RelationFeatures relationFeatures = new RelationFeatures(id); 386 relationFeatures.createRelationFeatures(way, relationList); 343 relationFeatures.createRelationFeatures(way, relationList); 387 344 id = relationFeatures.getLastID(); 388 } 389 else { 345 } else { 390 346 id = geometryFeatures.getLastID(); 391 347 } 392 348 //id 1531 393 349 394 350 TextualFeatures textualFeatures; 395 if(USE_TEXTUAL_FEATURES){ 351 if (USE_TEXTUAL_FEATURES) { 396 352 textualFeatures = new TextualFeatures(id, namesList, languageDetector); 397 353 textualFeatures.createTextualFeatures(way); 398 354 } 399 355 400 356 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); 401 357 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 402 403 if(!way.getClassIDs().isEmpty()){ 404 int i =0; 405 for(FeatureNode featureNode : featureNodeList){ 358 359 if (!way.getClassIDs().isEmpty()) { 360 int i = 0; 361 for (FeatureNode featureNode : featureNodeList) { 406 362 featureNodeArray[i] = featureNode; 407 363 i++; 408 } 409 for(int classID : way.getClassIDs()){ 410 lalala++; 364 } 365 for (int classID : way.getClassIDs()) { 411 366 trainingSetWithUnknown2[k] = featureNodeArray; 412 367 GROUPS_ARRAY2[k] = classID; 413 k++; 414 } 415 } 416 } 417 418 //Linear.enableDebugOutput();419 Problemproblem= new Problem();420 problem. l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray;//wayListSizeWithoutUnclassified;//wayList.size();421 problem.n = numberOfFeatures;//(geometry 105 + textual //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes368 k++; 369 } 370 } 371 } 372 373 Problem problem = new Problem(); 374 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray; 375 problem.n = numberOfFeatures; 376 //(geometry 105 + textual //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 422 377 problem.x = trainingSetWithUnknown2; // feature nodes 423 378 problem.y = GROUPS_ARRAY2; // target values 424 379 //SolverType solver = SolverType.MCSVM_CS; //Cramer and Singer for multiclass classification - equivalent of SVMlight 425 380 SolverType solver2 = SolverType.getById(2); //2 -- L2-regularized L2-loss support vector classification (primal) 426 381 427 382 Parameter parameter = new Parameter(solver2, C, eps); 428 //System.out.println("param set ok"); 429 //System.out.println("number of features: " + vc.getNumOfFeatures()); 430 383 431 384 long start = System.nanoTime(); 432 385 System.out.println("training..."); 433 386 PrintStream original = System.out; 434 387 System.setOut(new PrintStream(new OutputStream() { 435 @Override 436 public void write(int arg0) throws IOException { 437 438 } 388 @Override 389 public void write(int arg0) throws IOException { 390 391 } 439 392 })); 440 393 441 394 Model model = Linear.train(problem, parameter); 442 395 long end = System.nanoTime(); … … 444 397 System.setOut(original); 445 398 System.out.println("training process completed in: " + NANOSECONDS.toSeconds(elapsedTime) + " seconds."); 446 //System.out.println("trained"); 447 448 //decide model path and naming and/or way of deleting/creating 1 or more models. 449 //File inFile = new File(inputFilePath).getParentFile(); 450 399 400 //decide model path and naming and/or way of deleting/creating 1 or more models. 401 451 402 File modelFile; 452 if(USE_CLASS_FEATURES){ 403 if (USE_CLASS_FEATURES) { 453 404 modelFile = new File(modelDirectory.getAbsolutePath()+"/model_with_classes_c=" + param); 454 } 455 else{ 405 } else { 456 406 modelFile = new File(modelDirectory.getAbsolutePath()+"/model_geometries_textual_c=" + param); 457 407 } 458 459 if(modelFile.exists()){ 408 409 if (modelFile.exists()) { 460 410 modelFile.delete(); 461 } 411 } 462 412 try { 463 //System.out.println("file created");464 413 model.save(modelFile); 465 System.out.println("model saved at: " + modelFile); 414 System.out.println("model saved at: " + modelFile); 466 415 } catch (IOException ex) { 467 416 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 468 417 } 469 470 //end of evaluation training 471 472 473 //test set 474 List<OSMWay> testList= new ArrayList<>(); 475 for(int g = c*testSize; g<d*testSize; g++){ 418 419 //end of evaluation training 420 421 //test set 422 List<OSMWay> testList = new ArrayList<>(); 423 for (int g = c*testSize; g < d*testSize; g++) { 476 424 testList.add(wayList.get(g)); 477 //liblinear test 425 //liblinear test 478 426 } 479 427 System.out.println("testList size: " + testList.size()); 480 int succededInstances =0;481 int succededInstances5 =0;482 int succededInstances10 =0;428 int succededInstances = 0; 429 int succededInstances5 = 0; 430 int succededInstances10 = 0; 483 431 try { 484 432 model = Model.load(modelFile); … … 489 437 int[] labels = model.getLabels(); 490 438 Map<Integer, Integer> mapLabelsToIDs = new HashMap<>(); 491 for (int h =0; h < model.getLabels().length; h++){439 for (int h = 0; h < model.getLabels().length; h++) { 492 440 mapLabelsToIDs.put(labels[h], h); 493 494 //System.out.println(h + " <-> " + labels[h]); 495 } 496 441 } 442 497 443 int wayListSizeWithoutUnclassified2 = testList.size(); 498 for(OSMWay way : testList){ 444 for (OSMWay way : testList) { 499 445 500 446 OSMClassification classifyInstances = new OSMClassification(); 501 447 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 502 if(way.getClassIDs().isEmpty()){ 503 //System.out.println("found unclassified" + way.getClassIDs() + "class: " +way.getClassID()); 448 if (way.getClassIDs().isEmpty()) { 504 449 wayListSizeWithoutUnclassified2 -= 1; 505 //u++; 506 } 507 } 508 450 } 451 } 452 509 453 FeatureNode[] testInstance2; 510 for(OSMWay way : testList){ 511 454 for (OSMWay way : testList) { 455 512 456 int id; 513 514 if(USE_CLASS_FEATURES) {457 458 if (USE_CLASS_FEATURES) { 515 459 ClassFeatures class_vector = new ClassFeatures(); 516 460 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 517 461 id = 1422; 518 } 519 else{ 462 } else { 520 463 id = 1; 521 464 } 522 465 523 466 //pass id also: 1422 if using classes, 1 if not 524 467 GeometryFeatures geometryFeatures = new GeometryFeatures(id); … … 527 470 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 528 471 //id = 1526 529 //System.out.println("id 1526 -> " + geometryFeatures.getLastID()); 530 if(USE_RELATION_FEATURES){ 472 if (USE_RELATION_FEATURES) { 531 473 RelationFeatures relationFeatures = new RelationFeatures(id); 532 relationFeatures.createRelationFeatures(way, relationList); 474 relationFeatures.createRelationFeatures(way, relationList); 533 475 id = relationFeatures.getLastID(); 534 } 535 else { 476 } else { 536 477 id = geometryFeatures.getLastID(); 537 //System.out.println("geom feat " + id);538 478 } 539 479 //id 1531 540 //System.out.println("id 1532 -> " + relationFeatures.getLastID()); 541 if(USE_TEXTUAL_FEATURES){ 480 if (USE_TEXTUAL_FEATURES) { 542 481 TextualFeatures textualFeatures = new TextualFeatures(id, namesList, languageDetector); 543 482 textualFeatures.createTextualFeatures(way); 544 //System.out.println("last textual id: " + textualFeatures.getLastID());545 //System.out.println("full: " + way.getFeatureNodeList());546 }547 else{548 549 483 } 550 484 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); … … 552 486 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 553 487 554 int i =0; 555 for(FeatureNode featureNode : featureNodeList){ 488 int i = 0; 489 for (FeatureNode featureNode : featureNodeList) { 556 490 featureNodeArray[i] = featureNode; 557 491 i++; … … 559 493 560 494 testInstance2 = featureNodeArray; 561 //double prediction = Linear.predict(model, testInstance2);562 //System.out.println("test prediction: " + prediction);563 495 double[] scores = new double[modelLabelSize]; 564 496 Linear.predictValues(model, testInstance2, scores); … … 567 499 //iter scores and find 10 max values with their indexes first. then ask those indexes from model.getlabels 568 500 Map<Double, Integer> scoresValues = new HashMap<>(); 569 for(int h = 0; h < scores.length; h++){ 570 scoresValues.put(scores[h], h); 501 for (int h = 0; h < scores.length; h++) { 502 scoresValues.put(scores[h], h); 571 503 //System.out.println(h + " <-> " + scores[h]); 572 } 504 } 573 505 574 506 Arrays.sort(scores); 575 //System.out.println("max value: " + scores[scores.length-1] + " second max: " + scores[scores.length-2]); 576 //System.out.println("ask this index from labels: " + scoresValues.get(scores[scores.length-1])); 577 //System.out.println("got from labels: " + labels[scoresValues.get(scores[scores.length-1])]); 578 //System.out.println("next prediction: " + labels[scoresValues.get(scores[scores.length-2])]); 579 //System.out.println("way labels: " + way.getClassIDs()); 580 //System.out.println("test prediction: " + prediction); 581 if(way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])])){ 582 succededInstances++; 583 } 584 if( 585 586 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 587 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 588 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 589 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 590 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) 591 ){ 507 if (way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])])) { 508 succededInstances++; 509 } 510 if ( 511 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 512 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 513 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 514 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 515 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) 516 ) { 592 517 succededInstances5++; 593 518 } 594 if( 595 596 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 597 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 598 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 599 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 600 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) || 601 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-6])]) || 602 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-7])]) || 603 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-8])]) || 604 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-9])]) || 605 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-10])]) 606 ){519 if ( 520 521 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 522 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 523 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 524 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 525 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) || 526 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-6])]) || 527 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-7])]) || 528 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-8])]) || 529 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-9])]) || 530 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-10])]) 531 ) { 607 532 succededInstances10++; 608 } 609 //System.out.println("labels: " + Arrays.toString(model.getLabels())); 610 //System.out.println("label[0]: " + model.getLabels()[0]); 611 } 612 533 } 534 } 535 613 536 System.out.println("Succeeded " + succededInstances + " of " + testList.size() + " total (1 class prediction)"); 614 double precision1 = succededInstances/(double)wayListSizeWithoutUnclassified2; 537 double precision1 = succededInstances/(double) wayListSizeWithoutUnclassified2; 615 538 score1 = precision1; 616 539 System.out.println(precision1); 617 540 618 541 System.out.println("Succeeded " + succededInstances5 + " of " + testList.size()+ " total (5 class prediction)"); 619 double precision5 = succededInstances5/(double)wayListSizeWithoutUnclassified2; 542 double precision5 = succededInstances5/(double) wayListSizeWithoutUnclassified2; 620 543 score5 = precision5; 621 544 System.out.println(precision5); 622 545 623 546 System.out.println("Succeeded " + succededInstances10 + " of " + testList.size()+ " total (10 class prediction)"); 624 double precision10 = succededInstances10/(double)wayListSizeWithoutUnclassified2; 547 double precision10 = succededInstances10/(double) wayListSizeWithoutUnclassified2; 625 548 score10 = precision10; 626 System.out.println(precision10); 627 } 628 629 private void trainModel(double param){ 630 549 System.out.println(precision10); 550 } 551 552 private void trainModel(double param) { 553 631 554 int wayListSizeWithoutUnclassified = wayList.size(); 632 int u = 0;633 555 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 634 556 635 557 //set classes for each osm instance 636 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 637 int lalala = 0; 638 if(trainProgress>11){ 558 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 559 if (trainProgress > 11) { 639 560 setProgress(trainProgress-10); 640 } 641 else{ 561 } else { 642 562 setProgress(trainProgress+10); 643 563 } 644 //System.out.println("starting classify instances"); 645 for(OSMWay way : wayList){ 646 //setProgress(trainProgress++); 564 for (OSMWay way : wayList) { 647 565 OSMClassification classifyInstances = new OSMClassification(); 648 566 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 649 567 650 if(way.getClassIDs().isEmpty()){ 568 if (way.getClassIDs().isEmpty()) { 651 569 wayListSizeWithoutUnclassified -= 1; 652 u++; 653 } 654 else { 570 } else { 655 571 sizeToBeAddedToArray = sizeToBeAddedToArray + way.getClassIDs().size()-1; 656 } 657 } 658 //System.out.println("end classify instances"); 659 double C = param; 572 } 573 } 574 double C = param; 660 575 double eps = 0.001; 661 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray];//new double[117558];// 662 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; //working[3812]; 663 int k =0; 664 665 //setProgress(trainProgress+5); 666 for(OSMWay way : wayList){ 667 //adding multiple vectors 668 int id; 669 if(USE_CLASS_FEATURES){ 576 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; 577 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; 578 int k = 0; 579 580 for (OSMWay way : wayList) { 581 //adding multiple vectors 582 int id; 583 if (USE_CLASS_FEATURES) { 670 584 ClassFeatures class_vector = new ClassFeatures(); 671 585 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 672 586 id = 1422; 673 } 674 else{ 587 } else { 675 588 id = 1; 676 589 } … … 681 594 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 682 595 //id = 1526 683 if(USE_RELATION_FEATURES){ 596 if (USE_RELATION_FEATURES) { 684 597 RelationFeatures relationFeatures = new RelationFeatures(id); 685 relationFeatures.createRelationFeatures(way, relationList); 598 relationFeatures.createRelationFeatures(way, relationList); 686 599 id = relationFeatures.getLastID(); 687 } 688 else { 600 } else { 689 601 id = geometryFeatures.getLastID(); 690 602 } 691 603 //id 1531 692 604 693 605 TextualFeatures textualFeatures; 694 if(USE_TEXTUAL_FEATURES){ 695 textualFeatures = new TextualFeatures(id, namesList,languageDetector); 606 if (USE_TEXTUAL_FEATURES) { 607 textualFeatures = new TextualFeatures(id, namesList, languageDetector); 696 608 textualFeatures.createTextualFeatures(way); 697 609 } 698 610 699 611 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); 700 612 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 701 702 if(!way.getClassIDs().isEmpty()){ 703 int i =0; 704 for(FeatureNode featureNode : featureNodeList){ 613 614 if (!way.getClassIDs().isEmpty()) { 615 int i = 0; 616 for (FeatureNode featureNode : featureNodeList) { 705 617 featureNodeArray[i] = featureNode; 706 618 i++; 707 } 708 for(int classID : way.getClassIDs()){ 709 lalala++; 619 } 620 for (int classID : way.getClassIDs()) { 710 621 trainingSetWithUnknown2[k] = featureNodeArray; 711 622 GROUPS_ARRAY2[k] = classID; 712 k++; 713 } 714 } 715 } 716 //System.out.println("feature construction completed."); 717 //Linear.enableDebugOutput(); 718 Problem problem = new Problem(); 719 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray;//wayListSizeWithoutUnclassified;//wayList.size(); 720 problem.n = numberOfFeatures;//3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 623 k++; 624 } 625 } 626 } 627 Problem problem = new Problem(); 628 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray; 629 problem.n = numberOfFeatures; 630 //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 721 631 problem.x = trainingSetWithUnknown2; // feature nodes 722 632 problem.y = GROUPS_ARRAY2; // target values 723 //SolverType solver = SolverType.MCSVM_CS; //Cramer and Singer for multiclass classification - equivalent of SVMlight724 633 SolverType solver2 = SolverType.getById(2); //2 -- L2-regularized L2-loss support vector classification (primal) 725 634 726 635 Parameter parameter = new Parameter(solver2, C, eps); 727 //System.out.println("param set ok"); 728 //System.out.println("number of features: " + vc.getNumOfFeatures()); 729 636 730 637 long start = System.nanoTime(); 731 638 System.out.println("training..."); 732 639 PrintStream original = System.out; 733 640 System.setOut(new PrintStream(new OutputStream() { 734 735 @Override 736 public void write(int arg0) throws IOException { 737 738 } 739 })); 740 741 641 @Override 642 public void write(int arg0) throws IOException { 643 644 } 645 })); 646 742 647 Model model = Linear.train(problem, parameter); 743 648 long end = System.nanoTime(); … … 745 650 System.setOut(original); 746 651 System.out.println("training process completed in: " + NANOSECONDS.toSeconds(elapsedTime) + " seconds."); 747 //System.out.println("trained"); 748 749 //decide model path and naming and/or way of deleting/creating 1 or more models. 750 //File inFile = new File(inputFilePath).getParentFile(); 751 652 653 //decide model path and naming and/or way of deleting/creating 1 or more models. 654 752 655 File modelFile = new File(modelDirectory.getAbsolutePath()+"/best_model"); //decide path of models 753 656 754 657 File customModelFile; 755 if(topKIsSelected){ 658 if (topKIsSelected) { 756 659 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_topK" + topK + ".0"); 757 } 758 else{ 660 } else { 759 661 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + ".0"); 760 662 } 761 762 763 if(modelFile.exists()){ 663 664 665 if (modelFile.exists()) { 764 666 modelFile.delete(); 765 } 766 if(customModelFile.exists()){ 667 } 668 if (customModelFile.exists()) { 767 669 customModelFile.delete(); 768 } 769 670 } 671 770 672 try { 771 673 //System.out.println("file created"); 772 674 model.save(modelFile); 773 675 model.save(customModelFile); 774 System.out.println("best model saved at: " + modelFile); 676 System.out.println("best model saved at: " + modelFile); 775 677 System.out.println("custom model saved at: " + customModelFile); 776 678 } catch (IOException ex) { 777 679 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 778 } 779 } 780 781 private void trainModelWithClasses(double param){ 782 680 } 681 } 682 683 private void trainModelWithClasses(double param) { 684 783 685 int wayListSizeWithoutUnclassified = wayList.size(); 784 int u = 0;785 686 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 786 687 787 688 //set classes for each osm instance 788 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 789 //int lalala = 0; 790 //setProgress(trainProgress-10); 791 for(OSMWay way : wayList){ 792 //setProgress(trainProgress++); 689 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 690 for (OSMWay way : wayList) { 793 691 OSMClassification classifyInstances = new OSMClassification(); 794 692 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 795 693 796 if(way.getClassIDs().isEmpty()){ 694 if (way.getClassIDs().isEmpty()) { 797 695 wayListSizeWithoutUnclassified -= 1; 798 u++; 799 } 800 else { 696 } else { 801 697 sizeToBeAddedToArray = sizeToBeAddedToArray + way.getClassIDs().size()-1; 802 } 803 } 804 double C = param; 698 } 699 } 700 double C = param; 805 701 double eps = 0.001; 806 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray];//new double[117558];// 807 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures+1422]; //working[3812]; 808 int k =0; 809 810 //setProgress(trainProgress+5); 811 for(OSMWay way : wayList){ 812 //adding multiple vectors 813 int id; 814 //if(USE_CLASS_FEATURES){ 815 ClassFeatures class_vector = new ClassFeatures(); 816 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 817 id = 1422; 818 // } 819 // else{ 820 // id = 1; 821 // } 702 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; 703 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures+1422]; 704 int k = 0; 705 706 for (OSMWay way : wayList) { 707 //adding multiple vectors 708 int id; 709 ClassFeatures class_vector = new ClassFeatures(); 710 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 711 id = 1422; 822 712 //pass id also: 1422 if using classes, 1 if not 823 713 GeometryFeatures geometryFeatures = new GeometryFeatures(id); … … 826 716 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 827 717 //id = 1526 828 if(USE_RELATION_FEATURES){ 718 if (USE_RELATION_FEATURES) { 829 719 RelationFeatures relationFeatures = new RelationFeatures(id); 830 relationFeatures.createRelationFeatures(way, relationList); 720 relationFeatures.createRelationFeatures(way, relationList); 831 721 id = relationFeatures.getLastID(); 832 } 833 else { 722 } else { 834 723 id = geometryFeatures.getLastID(); 835 724 } 836 725 //id 1531 837 726 838 727 TextualFeatures textualFeatures; 839 if(USE_TEXTUAL_FEATURES){ 840 textualFeatures = new TextualFeatures(id, namesList,languageDetector); 728 if (USE_TEXTUAL_FEATURES) { 729 textualFeatures = new TextualFeatures(id, namesList, languageDetector); 841 730 textualFeatures.createTextualFeatures(way); 842 731 } 843 732 844 733 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); 845 734 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 846 847 if(!way.getClassIDs().isEmpty()){ 848 int i =0; 849 for(FeatureNode featureNode : featureNodeList){ 735 736 if (!way.getClassIDs().isEmpty()) { 737 int i = 0; 738 for (FeatureNode featureNode : featureNodeList) { 850 739 featureNodeArray[i] = featureNode; 851 740 i++; 852 } 853 for(int classID : way.getClassIDs()){ 854 //lalala++; 741 } 742 for (int classID : way.getClassIDs()) { 855 743 trainingSetWithUnknown2[k] = featureNodeArray; 856 744 GROUPS_ARRAY2[k] = classID; 857 k++; 858 } 859 } 860 } 861 862 //Linear.enableDebugOutput();863 Problemproblem= new Problem();864 problem. l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray;//wayListSizeWithoutUnclassified;//wayList.size();865 problem.n = numberOfFeatures+1422;//3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes745 k++; 746 } 747 } 748 } 749 750 Problem problem = new Problem(); 751 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray; 752 problem.n = numberOfFeatures+1422; 753 //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 866 754 problem.x = trainingSetWithUnknown2; // feature nodes 867 755 problem.y = GROUPS_ARRAY2; // target values 868 //SolverType solver = SolverType.MCSVM_CS; //Cramer and Singer for multiclass classification - equivalent of SVMlight869 756 SolverType solver2 = SolverType.getById(2); //2 -- L2-regularized L2-loss support vector classification (primal) 870 757 871 758 Parameter parameter = new Parameter(solver2, C, eps); 872 //System.out.println("param set ok"); 873 //System.out.println("number of features: " + vc.getNumOfFeatures()); 874 759 875 760 long start = System.nanoTime(); 876 761 System.out.println("training..."); 877 762 PrintStream original = System.out; 878 763 System.setOut(new PrintStream(new OutputStream() { 879 880 @Override 881 public void write(int arg0) throws IOException { 882 883 } 884 })); 885 886 764 @Override 765 public void write(int arg0) throws IOException { 766 767 } 768 })); 769 770 887 771 Model model = Linear.train(problem, parameter); 888 772 long end = System.nanoTime(); … … 890 774 System.setOut(original); 891 775 System.out.println("training process completed in: " + NANOSECONDS.toSeconds(elapsedTime) + " seconds."); 892 //System.out.println("trained"); 893 894 //decide model path and naming and/or way of deleting/creating 1 or more models. 895 //File inFile = new File(inputFilePath).getParentFile(); 896 897 File modelFile = new File(modelDirectory.getAbsolutePath()+"/model_with_classes"); 898 899 776 777 //decide model path and naming and/or way of deleting/creating 1 or more models. 778 779 File modelFile = new File(modelDirectory.getAbsolutePath()+"/model_with_classes"); 780 781 900 782 File customModelFile; 901 if(topKIsSelected){ 783 if (topKIsSelected) { 902 784 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model" + "_c" + param + "_topK" + topK + ".1"); 903 } 904 else{ 785 } else { 905 786 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + ".1"); 906 } 907 908 if(customModelFile.exists()){ 787 } 788 789 if (customModelFile.exists()) { 909 790 customModelFile.delete(); 910 } 911 912 if(modelFile.exists()){ 791 } 792 793 if (modelFile.exists()) { 913 794 modelFile.delete(); 914 } 915 795 } 796 916 797 try { 917 //System.out.println("file created");918 798 model.save(modelFile); 919 799 model.save(customModelFile); 920 System.out.println("model with classes saved at: " + modelFile); 800 System.out.println("model with classes saved at: " + modelFile); 921 801 System.out.println("custom model with classes saved at: " + modelFile); 922 802 } catch (IOException ex) { 923 803 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 924 } 925 } 926 927 private static void clearDataset(){ 928 for(OSMWay way : wayList){ 804 } 805 } 806 807 private static void clearDataset() { 808 for (OSMWay way : wayList) { 929 809 way.getFeatureNodeList().clear(); 930 810 } 931 } 932 811 } 812 933 813 private void readTextualFromDefaultList(InputStream textualFileStream) { 934 814 935 815 TextualStatistics textualStatistics = new TextualStatistics(); 936 816 textualStatistics.parseTextualList(textualFileStream); 937 817 namesList = textualStatistics.getTextualList(); 938 939 } 940 818 } 819 941 820 @Override 942 821 protected void done() { … … 944 823 System.out.println("Training process complete! - > " + get()); 945 824 setProgress(100); 946 } 947 catch (InterruptedException | ExecutionException ignore) { 825 } catch (InterruptedException | ExecutionException ignore) { 948 826 System.out.println("Exception: " + ignore); 949 827 } 950 } 828 } 951 829 952 830 @Override … … 954 832 //cancel button, end process after clearing Dataset 955 833 } 956 957 958 private static void writeTextualListToFile(String filePath, List<Map.Entry<String, Integer>> textualList) { 959 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"))) { 960 for(Map.Entry<String, Integer> entry : textualList){ 834 835 private static void writeTextualListToFile(String filePath, List<Map.Entry<String, Integer>> textualList) { 836 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"))) { 837 for (Map.Entry<String, Integer> entry : textualList) { 961 838 writer.write(entry.getKey()); 962 839 writer.newLine(); 963 //System.out.println(entry.getKey());964 840 } 965 841 } catch (UnsupportedEncodingException ex) { … … 969 845 } catch (IOException ex) { 970 846 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 971 } 847 } 972 848 } 973 849 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/extractor/Analyzer.java
r31461 r32404 1 package org.openstreetmap.josm.plugins.extractor; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.extractor; 2 3 3 4 import java.io.BufferedReader; … … 18 19 import java.util.logging.Level; 19 20 import java.util.logging.Logger; 21 20 22 import org.apache.lucene.analysis.de.GermanAnalyzer; 21 23 import org.apache.lucene.analysis.el.GreekAnalyzer; … … 38 40 39 41 public class Analyzer { 40 42 41 43 private final String osmFilePath; 42 44 private static final HashSet<String> stopWordsList = new HashSet<>(); //add greek list to same file 43 45 private ArrayList<Entry<String, Integer>> frequencies; 44 46 private final LanguageDetector languageDetector; 45 46 public Analyzer(String osmFilePath, LanguageDetector languageDetector){ 47 48 public Analyzer(String osmFilePath, LanguageDetector languageDetector) { 47 49 this.osmFilePath = osmFilePath; 48 50 this.languageDetector = languageDetector; 49 51 } 50 52 51 53 public void runAnalysis() { 52 54 //textual list … … 54 56 frequenceExtractor.parseDocument(); 55 57 Set<Map.Entry<String, Integer>> frequencyEntries = frequenceExtractor.getFrequency().entrySet(); 56 58 57 59 //parse stop words 58 60 loadStopWords(); 59 61 60 62 //send some samples 61 63 ArrayList<Map.Entry<String, Integer>> normalizedList = new ArrayList<>(); 62 64 ArrayList<String> sampleList = new ArrayList<>(); 63 int iters = 0; 64 for(Map.Entry<String, Integer> frequencyEntry : frequencyEntries){ 65 if(iters < 10){65 int iters = 0; 66 for (Map.Entry<String, Integer> frequencyEntry : frequencyEntries) { 67 if (iters < 10) { 66 68 sampleList.add(frequencyEntry.getKey()); 67 69 iters++; 68 70 } 69 71 //remove parenthesis etc here 70 if(!stopWordsList.contains(frequencyEntry.getKey())) {//stopwords72 if (!stopWordsList.contains(frequencyEntry.getKey())) { 71 73 String normalizedName = frequencyEntry.getKey().toLowerCase(); 72 74 normalizedName = normalizedName.replaceAll("[-+.^:,?;'{}\"!()\\[\\]]", ""); 73 75 74 AbstractMap.SimpleEntry<String, Integer> normalizedEntry = new AbstractMap.SimpleEntry<>(normalizedName,frequencyEntry.getValue()); 75 normalizedList.add(normalizedEntry); 76 normalizedList.add(new AbstractMap.SimpleEntry<>(normalizedName, frequencyEntry.getValue())); 76 77 } 77 78 } … … 88 89 langs.put("fr", 0); 89 90 90 for(String word : sampleList){ 91 for (String word : sampleList) { 91 92 //System.out.println("to be detected: " + word); 92 if(!word.isEmpty()){ 93 if (!word.isEmpty()) { 93 94 String lang; 94 95 lang = languageDetector.detect(word); 95 96 switch (lang) { 96 97 98 99 100 101 102 103 break;104 105 106 107 108 109 //es++;110 111 112 113 114 115 break;116 117 118 119 break;120 121 122 123 break;124 125 126 127 break;128 129 130 131 132 133 134 135 136 } 137 } 138 } 139 97 case "en": 98 //en++; 99 langs.put("en", langs.get("en")+1); 100 break; 101 case "el": 102 //el++; 103 langs.put("el", langs.get("el")+1); 104 break; 105 case "de": 106 //de++; 107 langs.put("de", langs.get("de")+1); 108 break; 109 case "es": 110 //es++; 111 langs.put("es", langs.get("es")+1); 112 break; 113 case "ru": 114 //ru++; 115 langs.put("ru", langs.get("ru")+1); 116 break; 117 case "fr": 118 //fr++; 119 langs.put("fr", langs.get("fr")+1); 120 break; 121 case "zh": 122 //zh++; 123 langs.put("zh", langs.get("zh")+1); 124 break; 125 case "tr": 126 //tr++; 127 langs.put("tr", langs.get("tr")+1); 128 break; 129 case "hi": 130 //hi++; 131 langs.put("hi", langs.get("hi")+1); 132 break; 133 //other lang, no support yet 134 //System.out.println("found other language, no support yet :("); 135 default: 136 break; 137 } 138 } 139 } 140 140 141 int maxLangFreq = langs.get("en"); 141 142 String dominantLanguage = "en"; 142 for(Entry<String,Integer> lang : langs.entrySet()){ 143 if(lang.getValue()> maxLangFreq){ 143 for (Entry<String, Integer> lang : langs.entrySet()) { 144 if (lang.getValue() > maxLangFreq) { 144 145 maxLangFreq = lang.getValue(); 145 146 dominantLanguage = lang.getKey(); 146 } 147 } 148 147 } 148 } 149 149 150 switch (dominantLanguage) { 150 case "en": 151 normalizedList = stemEnglish(normalizedList); 152 break; 153 case "el": 154 normalizedList = stemGreek(normalizedList); 155 break; 156 case "de": 157 normalizedList = stemGerman(normalizedList); 158 break; 159 case "es": 160 normalizedList = stemSpanish(normalizedList); 161 break; 162 case "ru": 163 normalizedList = stemRussian(normalizedList); 164 break; 165 case "fr": 166 normalizedList = stemFrench(normalizedList); 167 break; 168 case "zh": 169 normalizedList = stemChinese(normalizedList); 170 break; 171 case "tr": 172 normalizedList = stemTurkish(normalizedList); 173 break; 174 case "hi": 175 normalizedList = stemHindi(normalizedList); 176 break; 177 default: 178 normalizedList = stemEnglish(normalizedList); 179 break; 180 } 181 182 Collections.sort(normalizedList, new Comparator<Map.Entry<String, Integer>>() 183 { 151 case "en": 152 normalizedList = stemEnglish(normalizedList); 153 break; 154 case "el": 155 normalizedList = stemGreek(normalizedList); 156 break; 157 case "de": 158 normalizedList = stemGerman(normalizedList); 159 break; 160 case "es": 161 normalizedList = stemSpanish(normalizedList); 162 break; 163 case "ru": 164 normalizedList = stemRussian(normalizedList); 165 break; 166 case "fr": 167 normalizedList = stemFrench(normalizedList); 168 break; 169 case "zh": 170 normalizedList = stemChinese(normalizedList); 171 break; 172 case "tr": 173 normalizedList = stemTurkish(normalizedList); 174 break; 175 case "hi": 176 normalizedList = stemHindi(normalizedList); 177 break; 178 default: 179 normalizedList = stemEnglish(normalizedList); 180 break; 181 } 182 183 Collections.sort(normalizedList, new Comparator<Map.Entry<String, Integer>>() { 184 184 @Override 185 public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 ) 186 { 187 return (o2.getValue()).compareTo( o1.getValue() ); 188 } 189 } ); 185 public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { 186 return (o2.getValue()).compareTo(o1.getValue()); 187 } 188 }); 190 189 setFrequencies(normalizedList); 191 190 } 192 191 193 192 private static ArrayList<Map.Entry<String, Integer>> stemGreek(List<Map.Entry<String, Integer>> normalizedList) { 194 193 org.apache.lucene.analysis.Analyzer greekAnalyzer = new GreekAnalyzer(Version.LUCENE_36); 195 194 QueryParser greekParser = new QueryParser(Version.LUCENE_36, "", greekAnalyzer); 196 195 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 197 for(Map.Entry<String, Integer> entry : normalizedList){ 198 if(!entry.getKey().isEmpty()){ 199 try { 200 //System.out.println("result: " + greekParser.parse(entry.getKey())); 196 for (Map.Entry<String, Integer> entry : normalizedList) { 197 if (!entry.getKey().isEmpty()) { 198 try { 199 //System.out.println("result: " + greekParser.parse(entry.getKey())); 201 200 String stemmedWord = greekParser.parse(entry.getKey()).toString(); 202 201 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 203 if(!stemmedWord.equals("")){ 204 stemmedList.add(stemmed); 205 } 206 } catch (ParseException ex) { 207 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 208 } 209 } 210 } 211 return stemmedList; 212 } 213 214 private static ArrayList<Map.Entry<String, Integer>> stemEnglish(List<Map.Entry<String, Integer>> normalizedList){ 202 if (!stemmedWord.equals("")) { 203 stemmedList.add(stemmed); 204 } 205 } catch (ParseException ex) { 206 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 207 } 208 } 209 } 210 return stemmedList; 211 } 212 213 private static ArrayList<Map.Entry<String, Integer>> stemEnglish(List<Map.Entry<String, Integer>> normalizedList) { 215 214 org.apache.lucene.analysis.Analyzer englishAnalyzer = new EnglishAnalyzer(Version.LUCENE_36); 216 215 QueryParser englishParser = new QueryParser(Version.LUCENE_36, "", englishAnalyzer); 217 216 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 218 for(Map.Entry<String, Integer> entry : normalizedList) {219 if(!entry.getKey().isEmpty()){ 220 try { 221 //System.out.println("result: " + englishParser.parse(entry.getKey())); 217 for (Map.Entry<String, Integer> entry : normalizedList) { 218 if (!entry.getKey().isEmpty()) { 219 try { 220 //System.out.println("result: " + englishParser.parse(entry.getKey())); 222 221 String stemmedWord = englishParser.parse(entry.getKey()).toString(); 223 222 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 224 if(!stemmedWord.equals("")){ 225 stemmedList.add(stemmed); 226 } 227 } catch (ParseException ex) { 228 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 229 } 230 } 231 } 232 return stemmedList; 233 } 234 235 private static ArrayList<Map.Entry<String, Integer>> stemGerman(List<Map.Entry<String, Integer>> normalizedList){ 223 if (!stemmedWord.equals("")) { 224 stemmedList.add(stemmed); 225 } 226 } catch (ParseException ex) { 227 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 228 } 229 } 230 } 231 return stemmedList; 232 } 233 234 private static ArrayList<Map.Entry<String, Integer>> stemGerman(List<Map.Entry<String, Integer>> normalizedList) { 236 235 org.apache.lucene.analysis.Analyzer germanAnalyzer = new GermanAnalyzer(Version.LUCENE_36); 237 236 QueryParser germanParser = new QueryParser(Version.LUCENE_36, "", germanAnalyzer); 238 237 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 239 for(Map.Entry<String, Integer> entry : normalizedList){ 240 if(!entry.getKey().isEmpty()){ 241 try { 242 //System.out.println("result: " + englishParser.parse(entry.getKey())); 238 for (Map.Entry<String, Integer> entry : normalizedList) { 239 if (!entry.getKey().isEmpty()) { 240 try { 241 //System.out.println("result: " + englishParser.parse(entry.getKey())); 243 242 String stemmedWord = germanParser.parse(entry.getKey()).toString(); 244 243 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 245 if(!stemmedWord.equals("")){ 246 stemmedList.add(stemmed); 247 } 248 } catch (ParseException ex) { 249 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 250 } 251 } 252 } 253 return stemmedList; 254 } 255 256 private static ArrayList<Map.Entry<String, Integer>> stemSpanish(List<Map.Entry<String, Integer>> normalizedList){ 244 if (!stemmedWord.equals("")) { 245 stemmedList.add(stemmed); 246 } 247 } catch (ParseException ex) { 248 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 249 } 250 } 251 } 252 return stemmedList; 253 } 254 255 private static ArrayList<Map.Entry<String, Integer>> stemSpanish(List<Map.Entry<String, Integer>> normalizedList) { 257 256 org.apache.lucene.analysis.Analyzer spanishAnalyzer = new SpanishAnalyzer(Version.LUCENE_36); 258 257 QueryParser spanishParser = new QueryParser(Version.LUCENE_36, "", spanishAnalyzer); 259 258 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 260 for(Map.Entry<String, Integer> entry : normalizedList){ 261 if(!entry.getKey().isEmpty()){ 262 try { 263 //System.out.println("result: " + englishParser.parse(entry.getKey())); 259 for (Map.Entry<String, Integer> entry : normalizedList) { 260 if (!entry.getKey().isEmpty()) { 261 try { 262 //System.out.println("result: " + englishParser.parse(entry.getKey())); 264 263 String stemmedWord = spanishParser.parse(entry.getKey()).toString(); 265 264 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 266 if(!stemmedWord.equals("")){ 267 stemmedList.add(stemmed); 268 } 269 } catch (ParseException ex) { 270 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 271 } 272 } 273 } 274 return stemmedList; 275 } 276 277 private static ArrayList<Map.Entry<String, Integer>> stemRussian(List<Map.Entry<String, Integer>> normalizedList){ 265 if (!stemmedWord.equals("")) { 266 stemmedList.add(stemmed); 267 } 268 } catch (ParseException ex) { 269 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 270 } 271 } 272 } 273 return stemmedList; 274 } 275 276 private static ArrayList<Map.Entry<String, Integer>> stemRussian(List<Map.Entry<String, Integer>> normalizedList) { 278 277 org.apache.lucene.analysis.Analyzer russianAnalyzer = new RussianAnalyzer(Version.LUCENE_36); 279 278 QueryParser russianParser = new QueryParser(Version.LUCENE_36, "", russianAnalyzer); 280 279 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 281 for(Map.Entry<String, Integer> entry : normalizedList){ 282 if(!entry.getKey().isEmpty()){ 283 try { 284 //System.out.println("result: " + englishParser.parse(entry.getKey())); 280 for (Map.Entry<String, Integer> entry : normalizedList) { 281 if (!entry.getKey().isEmpty()) { 282 try { 283 //System.out.println("result: " + englishParser.parse(entry.getKey())); 285 284 String stemmedWord = russianParser.parse(entry.getKey()).toString(); 286 285 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 287 if(!stemmedWord.equals("")){ 288 stemmedList.add(stemmed); 289 } 290 } catch (ParseException ex) { 291 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 292 } 293 } 294 } 295 return stemmedList; 296 } 297 298 private static ArrayList<Map.Entry<String, Integer>> stemFrench(List<Map.Entry<String, Integer>> normalizedList){ 286 if (!stemmedWord.equals("")) { 287 stemmedList.add(stemmed); 288 } 289 } catch (ParseException ex) { 290 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 291 } 292 } 293 } 294 return stemmedList; 295 } 296 297 private static ArrayList<Map.Entry<String, Integer>> stemFrench(List<Map.Entry<String, Integer>> normalizedList) { 299 298 org.apache.lucene.analysis.Analyzer frenchAnalyzer = new FrenchAnalyzer(Version.LUCENE_36); 300 299 QueryParser frenchParser = new QueryParser(Version.LUCENE_36, "", frenchAnalyzer); 301 300 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 302 for(Map.Entry<String, Integer> entry : normalizedList){ 303 if(!entry.getKey().isEmpty()){ 304 try { 305 //System.out.println("result: " + englishParser.parse(entry.getKey())); 301 for (Map.Entry<String, Integer> entry : normalizedList) { 302 if (!entry.getKey().isEmpty()) { 303 try { 304 //System.out.println("result: " + englishParser.parse(entry.getKey())); 306 305 String stemmedWord = frenchParser.parse(entry.getKey()).toString(); 307 306 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 308 if(!stemmedWord.equals("")){ 309 stemmedList.add(stemmed); 310 } 311 } catch (ParseException ex) { 312 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 313 } 314 } 315 } 316 return stemmedList; 317 } 318 319 private static ArrayList<Map.Entry<String, Integer>> stemChinese(List<Map.Entry<String, Integer>> normalizedList){ 320 org.apache.lucene.analysis.Analyzer chineseAnalyzer = new StandardAnalyzer(Version.LUCENE_36); 307 if (!stemmedWord.equals("")) { 308 stemmedList.add(stemmed); 309 } 310 } catch (ParseException ex) { 311 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 312 } 313 } 314 } 315 return stemmedList; 316 } 317 318 private static ArrayList<Map.Entry<String, Integer>> stemChinese(List<Map.Entry<String, Integer>> normalizedList) { 319 org.apache.lucene.analysis.Analyzer chineseAnalyzer = new StandardAnalyzer(Version.LUCENE_36); 321 320 QueryParser chineseParser = new QueryParser(Version.LUCENE_36, "", chineseAnalyzer); 322 321 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 323 for(Map.Entry<String, Integer> entry : normalizedList){ 324 if(!entry.getKey().isEmpty()){ 325 try { 326 //System.out.println("result: " + englishParser.parse(entry.getKey())); 322 for (Map.Entry<String, Integer> entry : normalizedList) { 323 if (!entry.getKey().isEmpty()) { 324 try { 325 //System.out.println("result: " + englishParser.parse(entry.getKey())); 327 326 String stemmedWord = chineseParser.parse(entry.getKey()).toString(); 328 327 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 329 if(!stemmedWord.equals("")){ 330 stemmedList.add(stemmed); 331 } 332 } catch (ParseException ex) { 333 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 334 } 335 } 336 } 337 return stemmedList; 338 } 339 340 private static ArrayList<Map.Entry<String, Integer>> stemTurkish(List<Map.Entry<String, Integer>> normalizedList){ 341 org.apache.lucene.analysis.Analyzer turkishAnalyzer = new TurkishAnalyzer(Version.LUCENE_36); 328 if (!stemmedWord.equals("")) { 329 stemmedList.add(stemmed); 330 } 331 } catch (ParseException ex) { 332 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 333 } 334 } 335 } 336 return stemmedList; 337 } 338 339 private static ArrayList<Map.Entry<String, Integer>> stemTurkish(List<Map.Entry<String, Integer>> normalizedList) { 340 org.apache.lucene.analysis.Analyzer turkishAnalyzer = new TurkishAnalyzer(Version.LUCENE_36); 342 341 QueryParser turkishParser = new QueryParser(Version.LUCENE_36, "", turkishAnalyzer); 343 342 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 344 for(Map.Entry<String, Integer> entry : normalizedList){ 345 if(!entry.getKey().isEmpty()){ 346 try { 347 //System.out.println("result: " + englishParser.parse(entry.getKey())); 343 for (Map.Entry<String, Integer> entry : normalizedList) { 344 if (!entry.getKey().isEmpty()) { 345 try { 346 //System.out.println("result: " + englishParser.parse(entry.getKey())); 348 347 String stemmedWord = turkishParser.parse(entry.getKey()).toString(); 349 348 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 350 if(!stemmedWord.equals("")){ 351 stemmedList.add(stemmed); 352 } 353 } catch (ParseException ex) { 354 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 355 } 356 } 357 } 358 return stemmedList; 359 } 360 361 private static ArrayList<Map.Entry<String, Integer>> stemHindi(List<Map.Entry<String, Integer>> normalizedList){ 362 org.apache.lucene.analysis.Analyzer hindiAnalyzer = new HindiAnalyzer(Version.LUCENE_36); 349 if (!stemmedWord.equals("")) { 350 stemmedList.add(stemmed); 351 } 352 } catch (ParseException ex) { 353 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 354 } 355 } 356 } 357 return stemmedList; 358 } 359 360 private static ArrayList<Map.Entry<String, Integer>> stemHindi(List<Map.Entry<String, Integer>> normalizedList) { 361 org.apache.lucene.analysis.Analyzer hindiAnalyzer = new HindiAnalyzer(Version.LUCENE_36); 363 362 QueryParser hindiParser = new QueryParser(Version.LUCENE_36, "", hindiAnalyzer); 364 363 ArrayList<Map.Entry<String, Integer>> stemmedList = new ArrayList<>(); 365 for(Map.Entry<String, Integer> entry : normalizedList){ 366 if(!entry.getKey().isEmpty()){ 367 try { 368 //System.out.println("result: " + englishParser.parse(entry.getKey())); 364 for (Map.Entry<String, Integer> entry : normalizedList) { 365 if (!entry.getKey().isEmpty()) { 366 try { 367 //System.out.println("result: " + englishParser.parse(entry.getKey())); 369 368 String stemmedWord = hindiParser.parse(entry.getKey()).toString(); 370 369 SimpleEntry<String, Integer> stemmed = new SimpleEntry<>(stemmedWord, entry.getValue()); 371 if(!stemmedWord.equals("")){ 372 stemmedList.add(stemmed); 373 } 374 } catch (ParseException ex) { 375 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 376 } 377 } 378 } 379 return stemmedList; 380 } 381 382 383 384 private void loadStopWords(){ 370 if (!stemmedWord.equals("")) { 371 stemmedList.add(stemmed); 372 } 373 } catch (ParseException ex) { 374 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 375 } 376 } 377 } 378 return stemmedList; 379 } 380 381 private void loadStopWords() { 385 382 //parse stopwordsList 386 383 InputStream fstream = Analyzer.class.getResourceAsStream("/resources/files/stopWords.txt"); 387 384 388 385 try (BufferedReader br = new BufferedReader(new InputStreamReader(fstream))) { 389 386 String strLine; 390 387 391 while ((strLine = br.readLine()) != null) { 392 //System.out.println (strLine); 388 while ((strLine = br.readLine()) != null) { 393 389 stopWordsList.add(strLine); 394 390 } 395 //System.out.println(stopWordsList.size());396 391 397 392 } catch (IOException ex) { 398 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 399 } 400 //return stopWordsList; 401 } 402 403 private void setFrequencies(ArrayList<Map.Entry<String, Integer>> frequencies){ 404 this.frequencies = frequencies; 405 } 406 407 public List<Map.Entry<String, Integer>> getFrequencies(){ 393 Logger.getLogger(Analyzer.class.getName()).log(Level.SEVERE, null, ex); 394 } 395 } 396 397 private void setFrequencies(ArrayList<Map.Entry<String, Integer>> frequencies) { 398 this.frequencies = frequencies; 399 } 400 401 public List<Map.Entry<String, Integer>> getFrequencies() { 408 402 return Collections.unmodifiableList(frequencies); 409 403 } 410 411 public List<Map.Entry<String, Integer>> getTopKMostFrequent(int topK){ 404 405 public List<Map.Entry<String, Integer>> getTopKMostFrequent(int topK) { 412 406 //todo recheck 413 if(topK > frequencies.size()){ 407 if (topK > frequencies.size()) { 414 408 return Collections.unmodifiableList(frequencies); 415 } 416 else{ 409 } else { 417 410 return frequencies.subList(0, topK); 418 411 } 419 420 } 421 422 public List<Map.Entry<String, Integer>> getWithFrequency(int minFrequency){ 412 } 413 414 public List<Map.Entry<String, Integer>> getWithFrequency(int minFrequency) { 423 415 ArrayList<Map.Entry<String, Integer>> withFrequency = new ArrayList<>(); 424 for(Map.Entry<String, Integer> entry : frequencies){ 425 if(entry.getValue()> minFrequency){ 416 for (Map.Entry<String, Integer> entry : frequencies) { 417 if (entry.getValue() > minFrequency) { 426 418 withFrequency.add(entry); 427 } 428 else{ 419 } else { 429 420 return withFrequency; 430 421 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/extractor/FrequenceExtractor.java
r32320 r32404 1 package org.openstreetmap.josm.plugins.extractor; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.extractor; 2 3 3 4 import java.io.IOException; … … 10 11 11 12 import org.openstreetmap.josm.Main; 12 import org.openstreetmap.josm.plugins.container.OSMNode; 13 import org.openstreetmap.josm.plugins.container.OSMRelation; 14 import org.openstreetmap.josm.plugins.container.OSMWay; 13 import org.openstreetmap.josm.plugins.osmrec.container.OSMNode; 14 import org.openstreetmap.josm.plugins.osmrec.container.OSMRelation; 15 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 15 16 import org.openstreetmap.josm.tools.Utils; 16 17 import org.xml.sax.Attributes; … … 25 26 26 27 public class FrequenceExtractor extends DefaultHandler { 27 28 private final List<OSMNode> nodeList; //will be populated with nodes 28 29 private final List<OSMNode> nodeList; //will be populated with nodes 29 30 private final List<OSMRelation> relationList; 30 private final Map<String, OSMNode> nodesWithIDs; //map containing IDs as Strings and the corresponding OSMNode objects 31 private final Map<String, OSMNode> nodesWithIDs; //map containing IDs as Strings and the corresponding OSMNode objects 31 32 private final List<OSMWay> wayList; //populated with ways of the OSM file 32 33 private final String osmXmlFileName; 33 private OSMNode nodeTmp; 34 private OSMWay wayTmp; 35 private OSMRelation relationTmp; 36 private boolean inWay = false; //when parser is in a way node becomes true in order to track the parser position 37 private boolean inNode = false; //becomes true when the parser is in a simple node 34 private OSMNode nodeTmp; 35 private OSMWay wayTmp; 36 private OSMRelation relationTmp; 37 private boolean inWay = false; //when parser is in a way node becomes true in order to track the parser position 38 private boolean inNode = false; //becomes true when the parser is in a simple node 38 39 private boolean inRelation = false; //becomes true when the parser is in a relarion node 39 private Map<String,Integer> frequency; 40 40 private Map<String, Integer> frequency; 41 41 42 public FrequenceExtractor(String osmXmlFileName) { 42 this.osmXmlFileName = osmXmlFileName; 43 this.osmXmlFileName = osmXmlFileName; 43 44 nodeList = new ArrayList<>(); 44 45 wayList = new ArrayList<>(); 45 46 relationList = new ArrayList<>(); 46 nodesWithIDs = new HashMap<>(); 47 nodesWithIDs = new HashMap<>(); 47 48 frequency = new HashMap<>(); 48 for (int i = 0; i<100; i++){49 for (int i = 0; i < 100; i++) { 49 50 frequency.put("", 0); 50 51 } … … 62 63 @Override 63 64 public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException { 64 65 65 66 // if current element is an OSMNode , create new node and populate with the appropriate values 66 67 if (elementName.equalsIgnoreCase("node")) { … … 88 89 89 90 if (inNode) { 90 //if the path is in an OSMNode set tagKey and value to the corresponding node 91 //if the path is in an OSMNode set tagKey and value to the corresponding node 91 92 nodeTmp.setTagKeyValue(attributes.getValue("k"), attributes.getValue("v")); 92 93 } else if (inWay) { 93 94 //else if the path is in an OSM way set tagKey and value to the corresponding way 94 95 wayTmp.setTagKeyValue(attributes.getValue("k"), attributes.getValue("v")); 95 } else if(inRelation){ 96 } else if (inRelation) { 96 97 //set the key-value pairs of relation tags 97 98 relationTmp.setTagKeyValue(attributes.getValue("k"), attributes.getValue("v")); 98 } 99 99 } 100 100 101 } else if (elementName.equalsIgnoreCase("member")) { 101 102 //relationTmp.addMemberReference(attributes.getValue("ref")); 102 } 103 } 103 104 } 104 105 … … 107 108 // if end of node element, add to appropriate list 108 109 if (element.equalsIgnoreCase("node")) { 109 //nodeList.add(nodeTmp); 110 //nodesWithIDs.put(nodeTmp.getID(), nodeTmp); 111 112 Map<String,String> tags = nodeTmp.getTagKeyValue(); 113 //System.out.println("tag: " + tags); 114 if (tags.keySet().contains("name")){ 115 for (Map.Entry<String, String> tag : tags.entrySet()){ 116 if (tag.getKey().equals("name")){ 117 //split name value in each white space and store the values separetely. Count each occurance 110 Map<String, String> tags = nodeTmp.getTagKeyValue(); 111 if (tags.keySet().contains("name")) { 112 for (Map.Entry<String, String> tag : tags.entrySet()) { 113 if (tag.getKey().equals("name")) { 114 //split name value in each white space and store the values separetely. Count each occurance 118 115 String name = tag.getValue(); 119 116 String[] SplitName = name.split("\\s+"); 120 117 121 for (String split : SplitName){ 122 //System.out.println("split: " + split + " 0"); 123 124 //frequency.put(split, k); 125 //put all splits with zero, at the constructor. put here the incremented values. for tomoro 126 if (frequency.get(split) != null){ 127 int k = frequency.get(split) +1; 128 frequency.put(split, k); 129 //System.out.println("get split exists, k= " + k); 130 } 131 else{ 118 for (String split : SplitName) { 119 //put all splits with zero, at the constructor. put here the incremented values. for tomoro 120 if (frequency.get(split) != null) { 121 int k = frequency.get(split) +1; 122 frequency.put(split, k); 123 } else { 132 124 frequency.put(split, 1); 133 125 } 134 135 //System.out.println("frequency getValue +1 " + frequency.get(tag.getValue()) +1);136 126 } 137 //System.out.println("node name: " + tag.getValue());138 127 } 139 } 140 } 141 } 142 143 if (element.equalsIgnoreCase("way")) { 128 } 129 } 130 } 131 132 if (element.equalsIgnoreCase("way")) { 144 133 //name frequency 145 Map<String,String> tags = wayTmp.getTagKeyValue(); 146 147 148 //System.out.println("tag: " + tags); 149 if (tags.keySet().contains("name")){ 150 for (Map.Entry<String, String> tag : tags.entrySet()){ 151 if (tag.getKey().equals("name")){ 152 //split name value in each white space and store the values separetely. Count each occurance 134 Map<String, String> tags = wayTmp.getTagKeyValue(); 135 136 if (tags.keySet().contains("name")) { 137 for (Map.Entry<String, String> tag : tags.entrySet()) { 138 if (tag.getKey().equals("name")) { 139 //split name value in each white space and store the values separetely. Count each occurance 153 140 String name = tag.getValue(); 154 141 String[] SplitName = name.split("\\s+"); 155 //String[] SplitName = name.split("[\\W]"); 156 for (String split : SplitName){ 157 //System.out.println("split: " + split + " 0"); 158 159 //frequency.put(split, k); 160 //put all splits with zero, at the constructor. put here the incremented values. for tomoro 161 if (frequency.get(split) != null){ 162 int k = frequency.get(split) +1; 163 frequency.put(split, k); 164 //System.out.println("get split exists, k= " + k); 165 } 166 else{ 142 for (String split : SplitName) { 143 //put all splits with zero, at the constructor. put here the incremented values. for tomoro 144 if (frequency.get(split) != null) { 145 int k = frequency.get(split) +1; 146 frequency.put(split, k); 147 } else { 167 148 frequency.put(split, 1); 168 149 } 169 170 //System.out.println("frequency getValue +1 " + frequency.get(tag.getValue()) +1);171 150 } 172 //System.out.println("way name: " + tag.getValue());173 151 } 174 } 175 } 176 } 177 178 if(element.equalsIgnoreCase("relation")) { 179 152 } 153 } 154 } 155 156 if (element.equalsIgnoreCase("relation")) { 180 157 //name frequency 181 Map<String,String> tags = relationTmp.getTagKeyValue(); 182 183 //System.out.println("tag: " + tags); 184 if (tags.keySet().contains("name")){ 185 for (Map.Entry<String, String> tag : tags.entrySet()){ 186 if (tag.getKey().equals("name")){ 187 //split name value in each white space and store the values separetely. Count each occurance 158 Map<String, String> tags = relationTmp.getTagKeyValue(); 159 160 if (tags.keySet().contains("name")) { 161 for (Map.Entry<String, String> tag : tags.entrySet()) { 162 if (tag.getKey().equals("name")) { 163 //split name value in each white space and store the values separetely. Count each occurance 188 164 String name = tag.getValue(); 189 165 String[] SplitName = name.split("\\s+"); 190 166 191 for (String split : SplitName){ 192 //System.out.println("split: " + split + " 0"); 193 194 //frequency.put(split, k); 195 //put all splits with zero, at the constructor. put here the incremented values. for tomoro 196 if (frequency.get(split) != null){ 197 int k = frequency.get(split) +1; 198 frequency.put(split, k); 199 //System.out.println("get split exists, k= " + k); 200 } 201 else{ 167 for (String split : SplitName) { 168 //put all splits with zero, at the constructor. put here the incremented values. for tomoro 169 if (frequency.get(split) != null) { 170 int k = frequency.get(split) +1; 171 frequency.put(split, k); 172 } else { 202 173 frequency.put(split, 1); 203 174 } 204 205 //System.out.println("frequency getValue +1 " + frequency.get(tag.getValue()) +1);206 175 } 207 //System.out.println("relation name: " + tag.getValue());208 176 } 209 } 210 } 211 //relationList.add(relationTmp); 177 } 178 } 212 179 } 213 180 } … … 220 187 return wayList; 221 188 } 222 223 public List<OSMRelation> getRelationList(){ 189 190 public List<OSMRelation> getRelationList() { 224 191 return relationList; 225 192 } … … 227 194 public Map<String, OSMNode> getNodesWithIDs() { 228 195 return nodesWithIDs; 229 } 230 196 } 197 231 198 //frequency temp 232 public Map<String, Integer> getFrequency(){ 199 public Map<String, Integer> getFrequency() { 233 200 return frequency; 234 201 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/extractor/LanguageDetector.java
r32320 r32404 1 package org.openstreetmap.josm.plugins.extractor; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.extractor; 2 3 3 4 import java.io.File; … … 20 21 * @author imis-nkarag 21 22 */ 22 public class LanguageDetector { 23 public final class LanguageDetector { 23 24 24 25 private static LanguageDetector languageDetector = null; … … 39 40 public static void loadProfilesFromStream(String languageProfilesPath) { //create profiles directory in system from stream and load them 40 41 41 /* supported languages 42 el:greek, en:english, de:german, fr:french, es:spanish, ru:russian, tr:turkish, zh-cn:chinese, hi:hindi 43 */ 42 /* supported languages 43 el:greek, en:english, de:german, fr:french, es:spanish, ru:russian, tr:turkish, zh-cn:chinese, hi:hindi 44 */ 44 45 InputStream languageProfilesInputStreamEl = LanguageDetector.class.getResourceAsStream("/profiles/el"); 45 46 InputStream languageProfilesInputStreamEn = LanguageDetector.class.getResourceAsStream("/profiles/en"); … … 50 51 InputStream languageProfilesInputStreamTr = LanguageDetector.class.getResourceAsStream("/profiles/tr"); 51 52 InputStream languageProfilesInputStreamZh = LanguageDetector.class.getResourceAsStream("/profiles/zh-cn"); 52 InputStream languageProfilesInputStreamHi = LanguageDetector.class.getResourceAsStream("/profiles/hi"); 53 InputStream languageProfilesInputStreamHi = LanguageDetector.class.getResourceAsStream("/profiles/hi"); 53 54 //InputStream languageProfilesInputStream2 = LanguageDetector.class.getResourceAsStream("/resources/profiles/en"); 54 55 … … 56 57 Utils.mkDirs(new File(languageProfilesPath)); 57 58 } 58 59 59 60 File languageProfilesOutputFileEl = new File(languageProfilesPath + "/el"); 60 61 File languageProfilesOutputFileEn = new File(languageProfilesPath + "/en"); … … 83 84 84 85 try { 85 86 87 88 89 90 91 92 93 86 Files.copy(languageProfilesInputStreamEl, languageProfilesOutputFileEl.toPath()); 87 Files.copy(languageProfilesInputStreamEn, languageProfilesOutputFileEn.toPath()); 88 Files.copy(languageProfilesInputStreamDe, languageProfilesOutputFileDe.toPath()); 89 Files.copy(languageProfilesInputStreamFr, languageProfilesOutputFileFr.toPath()); 90 Files.copy(languageProfilesInputStreamEs, languageProfilesOutputFileEs.toPath()); 91 Files.copy(languageProfilesInputStreamRu, languageProfilesOutputFileRu.toPath()); 92 Files.copy(languageProfilesInputStreamTr, languageProfilesOutputFileTr.toPath()); 93 Files.copy(languageProfilesInputStreamZh, languageProfilesOutputFileZh.toPath()); 94 Files.copy(languageProfilesInputStreamHi, languageProfilesOutputFileHi.toPath()); 94 95 } catch (IOException ex) { 95 96 Logger.getLogger(LanguageDetector.class.getName()).log(Level.SEVERE, null, ex); -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/extractor/SampleModelsExtractor.java
r31554 r32404 1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package org.openstreetmap.josm.plugins.extractor; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.extractor; 7 3 8 4 import java.io.File; … … 10 6 import java.io.IOException; 11 7 import java.io.InputStream; 12 //import java.nio.charset.Charset;13 //import java.nio.charset.Charset;14 import java.util.Scanner;15 8 import java.util.logging.Level; 16 9 import java.util.logging.Logger; 17 //import org.openstreetmap.josm.plugins.osmrec.OSMRecPluginHelper;18 10 19 11 /** … … 27 19 FileOutputStream outputStream = null; 28 20 29 //File modelFile = new File(bestModelPath);30 21 File targetFile = new File(modelPath); 31 32 if(targetFile.exists()){ 22 23 if (targetFile.exists()) { 33 24 return; 34 25 } … … 38 29 Logger.getLogger(SampleModelsExtractor.class.getName()).log(Level.SEVERE, null, ex); 39 30 } 40 41 //svmModelStream = SampleModelsExtractor.class.getResourceAsStream("/resources/profiles/el"); 31 42 32 System.out.println("trying to get stream.. for " + "/resources/files/" + modelName); 43 33 svmModelStream = SampleModelsExtractor.class.getResourceAsStream("/resources/files/" + modelName); 44 34 45 35 try { 46 36 outputStream = new FileOutputStream(targetFile); 47 //Scanner input = new Scanner(svmModelStream); 48 49 37 50 38 int read = 0; 51 39 byte[] bytes = new byte[1024]; -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/features/ClassFeatures.java
r31106 r32404 1 package org.openstreetmap.josm.plugins.features; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.features; 2 3 3 import de.bwaldvogel.liblinear.FeatureNode;4 4 import java.util.List; 5 5 import java.util.Map; 6 6 import java.util.Set; 7 7 import java.util.TreeSet; 8 import org.openstreetmap.josm.plugins.container.OSMWay; 8 9 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 10 11 import de.bwaldvogel.liblinear.FeatureNode; 9 12 10 13 /** … … 13 16 */ 14 17 public class ClassFeatures { 15 18 16 19 private int directClassID = 0; 17 //private static Map<String,String> mappings; 18 //private final Map<String, Integer> mappingsWithIDs; 19 //private final Map<String, List<String>> indirectClasses; 20 //private final Map<String, Integer> indirectClassesIDs; 21 22 public void createClassFeatures(OSMWay wayNode, Map<String,String> mappings, Map<String, Integer> mappingsWithIDs, 23 Map<String, List<String>> indirectClasses, Map<String, Integer> indirectClassesIDs) { 24 20 21 public void createClassFeatures(OSMWay wayNode, Map<String, String> mappings, Map<String, Integer> mappingsWithIDs, 22 Map<String, List<String>> indirectClasses, Map<String, Integer> indirectClassesIDs) { 23 25 24 //iteration for each way node in the wayList 26 //FeatureNode[] shape1 = {new FeatureNode(1, 2), new FeatureNode(2, 2)};27 //ArrayList<FeatureNode> test = new ArrayList<>();28 //test.add(new FeatureNode(1, 2));29 //System.out.println(shape1.length);30 25 Set<Integer> sortedIndirectIDs = new TreeSet<>(); 31 Set<Integer> sortedDirectIDs = new TreeSet<>(); 32 for (Map.Entry<String, String> wayTagKeyValue : wayNode.getTagKeyValue().entrySet()){ 26 Set<Integer> sortedDirectIDs = new TreeSet<>(); 27 for (Map.Entry<String, String> wayTagKeyValue : wayNode.getTagKeyValue().entrySet()) { 33 28 //iteration for each tag (key-value) in the current way node 34 29 35 30 //concat key and value to use it later for checking 36 31 String key = wayTagKeyValue.getKey() + " " + wayTagKeyValue.getValue(); 37 38 for (Map.Entry<String,String> tagMappedToClass : mappings.entrySet()){ 32 33 for (Map.Entry<String, String> tagMappedToClass : mappings.entrySet()) { 39 34 //entry of mappings is e.g "highway residential <-> ResidentialHighway" 40 35 //iteration to discover the wayNode class. This class's ID will be the start of the vector 41 42 if (key.equals(tagMappedToClass.getKey())){ 36 37 if (key.equals(tagMappedToClass.getKey())) { 43 38 String className = tagMappedToClass.getValue(); 44 39 directClassID = mappingsWithIDs.get(className); 45 46 sortedDirectIDs.add(directClassID); 47 48 //the direct class id is the last direct class that the instance is found to belong 40 41 sortedDirectIDs.add(directClassID); 42 43 //the direct class id is the last direct class that the instance is found to belong 49 44 50 45 List<String> superClassesList = indirectClasses.get(className); 51 46 52 if (superClassesList != null) {//check if the class has no superclasses53 47 if (superClassesList != null) { //check if the class has no superclasses 48 54 49 for (String superClass: superClassesList) { 55 50 56 Integer indirectID = indirectClassesIDs.get(superClass); 57 if(indirectID != null) {// there is a chance here that the ID is null,51 Integer indirectID = indirectClassesIDs.get(superClass); //to save time here 52 if (indirectID != null) { // there is a chance here that the ID is null, 58 53 //cause the list of super Classes might contain extra classes with no ID 59 54 //in the indirectClassesIDs map which is constructed from listHierarchyRootClasses method 60 55 //at the OntologyParser. 61 56 //so this condition check will remain for now 62 63 if(!(sortedIndirectIDs.contains(indirectID))){ 57 58 if (!(sortedIndirectIDs.contains(indirectID))) { 64 59 sortedIndirectIDs.add(indirectID); 65 60 //wayNode.getIndexVector().put(indirectID, 1.0); … … 71 66 } 72 67 } 73 } 74 } 68 } 69 } 75 70 wayNode.setClassIDs(sortedDirectIDs); 76 for (Integer dirID : sortedDirectIDs){ 77 wayNode.getFeatureNodeList().add(new FeatureNode(dirID, 1)); 71 for (Integer dirID : sortedDirectIDs) { 72 wayNode.getFeatureNodeList().add(new FeatureNode(dirID, 1)); 78 73 } 79 for (Integer indID : sortedIndirectIDs){ 74 for (Integer indID : sortedIndirectIDs) { 80 75 wayNode.getFeatureNodeList().add(new FeatureNode(indID, 1)); 81 } 76 } 82 77 wayNode.setClassID(directClassID); 83 78 //System.out.println("class: " + wayNode.getFeatureNodeList()); 84 } 79 } 85 80 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/features/GeometryFeatures.java
r31106 r32404 1 package org.openstreetmap.josm.plugins.features; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.features; 3 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 2 8 3 9 import com.vividsolutions.jts.geom.Coordinate; … … 5 11 import com.vividsolutions.jts.geom.GeometryFactory; 6 12 import com.vividsolutions.jts.geom.LineString; 13 7 14 import de.bwaldvogel.liblinear.FeatureNode; 8 import java.util.ArrayList;9 import java.util.List;10 import org.openstreetmap.josm.plugins.container.OSMWay;11 15 12 16 /** 13 17 * Constructs the geometry feature nodes for liblinear. 14 * 18 * 15 19 * @author imis-nkarag 16 20 */ 17 18 21 public class GeometryFeatures { 19 22 20 23 private int id; //= 1422; //pass this as a param from main 21 24 private final GeometryFactory geometryFactory = new GeometryFactory(); … … 24 27 private static final int NUMBER_OF_MEAN = 23; //for boolean intervals 25 28 private static final int NUMBER_OF_VARIANCE = 37; //for boolean intervals 26 27 public GeometryFeatures(int id){ 29 30 public GeometryFeatures(int id) { 28 31 this.id = id; 29 32 } 30 31 public void createGeometryFeatures(OSMWay wayNode){ 32 33 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 34 /////////////////// geometry Features /////////////////// 35 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 33 34 public void createGeometryFeatures(OSMWay wayNode) { 35 36 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 37 /////////////////// geometry Features /////////////////// 38 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 36 39 // geometry type feature // 37 String 40 String geometryType = wayNode.getGeometry().getGeometryType(); 38 41 switch (geometryType) { 39 //the IDs are unique for each geometry type 40 case "LineString": 41 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1)); 42 id += 4; 43 break; 44 case "Polygon": 45 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1)); 46 id += 4; 47 break; 48 case "LinearRing": 49 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1)); 50 id += 4; 51 break; 52 case "Point": 53 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1)); 54 id += 4; 55 break; 56 } 57 //LOG.info("after type " + id + " and further increase " + (id+1)); 42 //the IDs are unique for each geometry type 43 case "LineString": 44 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1)); 45 id += 4; 46 break; 47 case "Polygon": 48 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1)); 49 id += 4; 50 break; 51 case "LinearRing": 52 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1)); 53 id += 4; 54 break; 55 case "Point": 56 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1)); 57 id += 4; 58 break; 59 } 58 60 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 59 61 // rectangle geometry shape feature // 60 62 //id 1426 61 if (wayNode.getGeometry().isRectangle()) {63 if (wayNode.getGeometry().isRectangle()) { 62 64 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 63 65 } … … 65 67 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 66 68 // number of points of geometry feature // 67 id++; //1427 68 //System.out.println("should be 1427 -> " + id); 69 id++; //1427 69 70 int numberOfPoints = wayNode.getGeometry().getNumPoints(); 70 71 numberOfPointsFeature(numberOfPoints, wayNode); … … 74 75 double area = wayNode.getGeometry().getArea(); 75 76 76 if(geometryType.equals("Polygon")) {77 78 areaFeature(area,wayNode); 77 if (geometryType.equals("Polygon")) { 78 79 areaFeature(area, wayNode); 79 80 //the id increases by 25 in the areaFeature method 80 } 81 else{ 81 } else { 82 82 id += 25; 83 83 } 84 84 85 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 86 // resembles to a circle feature // 85 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 86 // resembles to a circle feature // 87 87 //id 1465 88 //id++; 89 if(geometryResemblesCircle(wayNode)){ //this method checks if the shape of the geometry resembles to a circle 90 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 91 } 92 93 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 94 // mean edge feature // 95 88 if (geometryResemblesCircle(wayNode)) { //this method checks if the shape of the geometry resembles to a circle 89 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 90 } 91 92 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 93 // mean edge feature // 94 96 95 id++; 97 96 //TOGGLE COMMENT !! commenting out mean and variance to run the best case 98 99 //System.out.println("mean start" + id); 97 100 98 Coordinate[] nodeGeometries = wayNode.getGeometry().getCoordinates(); 101 99 List<Double> edgeLengths = new ArrayList<>(); 102 103 if(!wayNode.getGeometry().getGeometryType().toUpperCase().equals("POINT")){ 100 101 if (!wayNode.getGeometry().getGeometryType().toUpperCase().equals("POINT")) { 104 102 for (int i = 0; i < nodeGeometries.length-1; i++) { 105 103 Coordinate[] nodePair = new Coordinate[2]; … … 107 105 nodePair[1] = nodeGeometries[i+1]; 108 106 LineString tempGeom = geometryFactory.createLineString(nodePair); 109 edgeLengths.add(tempGeom.getLength()); 107 edgeLengths.add(tempGeom.getLength()); 110 108 } 111 } 112 else{ 109 } else { 113 110 edgeLengths.add(0.0); 114 111 } 115 112 double edgeSum = 0; 116 for(Double edge : edgeLengths){ 113 for (Double edge : edgeLengths) { 117 114 edgeSum = edgeSum + edge; 118 115 } 119 116 double mean = edgeSum/edgeLengths.size(); 120 //double normalizedMean = sqrt(mean); 121 //geometriesPortion = geometriesPortion + id + ":" + normalizedMean + " "; 122 //wayNode.getIndexVector().put(id, normalizedMean); 123 124 //intervals with boolean values for mean feature 125 126 if(mean<2){ 127 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 128 id = id + NUMBER_OF_MEAN; 129 } 130 else if(mean<4){ 117 118 //intervals with boolean values for mean feature 119 120 handleMean(wayNode, mean); 121 122 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 123 // variance feature// 124 double sum = 0; 125 for (Double edge : edgeLengths) { 126 sum = sum + (edge-mean)*(edge-mean); 127 } 128 129 double normalizedVariance = (sum/edgeLengths.size())/(mean*mean); //normalized with square of mean value 130 131 handleVariance(wayNode, normalizedVariance); 132 setLastID(id); 133 } 134 135 private void handleMean(OSMWay wayNode, double mean) { 136 if (mean < 2) { 137 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 138 id = id + NUMBER_OF_MEAN; 139 } else if (mean < 4) { 131 140 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1.0)); 132 141 id = id + NUMBER_OF_MEAN; 133 } 134 else if(mean<6){ 142 } else if (mean < 6) { 135 143 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1.0)); 136 144 id = id + NUMBER_OF_MEAN; 137 } 138 else if(mean<8){ 145 } else if (mean < 8) { 139 146 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1.0)); 140 147 id = id + NUMBER_OF_MEAN; 141 } 142 else if(mean<10){ 148 } else if (mean < 10) { 143 149 wayNode.getFeatureNodeList().add(new FeatureNode(id+4, 1.0)); 144 150 id = id + NUMBER_OF_MEAN; 145 } 146 else if(mean<12){ 151 } else if (mean < 12) { 147 152 wayNode.getFeatureNodeList().add(new FeatureNode(id+5, 1.0)); 148 153 id = id + NUMBER_OF_MEAN; 149 } 150 else if(mean<14){ 154 } else if (mean < 14) { 151 155 wayNode.getFeatureNodeList().add(new FeatureNode(id+6, 1.0)); 152 156 id = id + NUMBER_OF_MEAN; 153 } 154 else if(mean<16){ 157 } else if (mean < 16) { 155 158 wayNode.getFeatureNodeList().add(new FeatureNode(id+7, 1.0)); 156 159 id = id + NUMBER_OF_MEAN; 157 } 158 else if(mean<18){ 160 } else if (mean < 18) { 159 161 wayNode.getFeatureNodeList().add(new FeatureNode(id+8, 1.0)); 160 162 id = id + NUMBER_OF_MEAN; 161 } 162 else if(mean<20){ 163 } else if (mean < 20) { 163 164 wayNode.getFeatureNodeList().add(new FeatureNode(id+9, 1.0)); 164 165 id = id + NUMBER_OF_MEAN; 165 } 166 else if(mean<25){ 166 } else if (mean < 25) { 167 167 wayNode.getFeatureNodeList().add(new FeatureNode(id+10, 1.0)); 168 168 id = id + NUMBER_OF_MEAN; 169 } 170 else if(mean<30){ 169 } else if (mean < 30) { 171 170 wayNode.getFeatureNodeList().add(new FeatureNode(id+11, 1.0)); 172 171 id = id + NUMBER_OF_MEAN; 173 } 174 else if(mean<35){ 172 } else if (mean < 35) { 175 173 wayNode.getFeatureNodeList().add(new FeatureNode(id+12, 1.0)); 176 174 id = id + NUMBER_OF_MEAN; 177 } 178 else if(mean<40){ 175 } else if (mean < 40) { 179 176 wayNode.getFeatureNodeList().add(new FeatureNode(id+13, 1.0)); 180 177 id = id + NUMBER_OF_MEAN; 181 } 182 else if(mean<45){ 178 } else if (mean < 45) { 183 179 wayNode.getFeatureNodeList().add(new FeatureNode(id+14, 1.0)); 184 180 id = id + NUMBER_OF_MEAN; 185 } 186 else if(mean<50){ 181 } else if (mean < 50) { 187 182 wayNode.getFeatureNodeList().add(new FeatureNode(id+15, 1.0)); 188 183 id = id + NUMBER_OF_MEAN; 189 } 190 else if(mean<60){ 184 } else if (mean < 60) { 191 185 wayNode.getFeatureNodeList().add(new FeatureNode(id+16, 1.0)); 192 186 id = id + NUMBER_OF_MEAN; 193 } 194 else if(mean<70){ 187 } else if (mean < 70) { 195 188 wayNode.getFeatureNodeList().add(new FeatureNode(id+17, 1.0)); 196 189 id = id + NUMBER_OF_MEAN; 197 } 198 else if(mean<80){ 190 } else if (mean < 80) { 199 191 wayNode.getFeatureNodeList().add(new FeatureNode(id+18, 1.0)); 200 192 id = id + NUMBER_OF_MEAN; 201 } 202 else if(mean<90){ 193 } else if (mean < 90) { 203 194 wayNode.getFeatureNodeList().add(new FeatureNode(id+19, 1.0)); 204 195 id = id + NUMBER_OF_MEAN; 205 } 206 else if(mean<100){ 196 } else if (mean < 100) { 207 197 wayNode.getFeatureNodeList().add(new FeatureNode(id+20, 1.0)); 208 198 id = id + NUMBER_OF_MEAN; 209 } 210 else if(mean<200){ 199 } else if (mean < 200) { 211 200 wayNode.getFeatureNodeList().add(new FeatureNode(id+21, 1.0)); 212 201 id = id + NUMBER_OF_MEAN; 213 } 214 else { 202 } else { 215 203 wayNode.getFeatureNodeList().add(new FeatureNode(id+22, 1.0)); 216 204 id = id + NUMBER_OF_MEAN; 217 205 } 218 219 //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 220 // variance feature// 221 //id++; //this should be removed if using boolean features with intervals 222 //System.out.println("must be 1467" + id); 223 double sum = 0; 224 for(Double edge : edgeLengths){ 225 sum = sum + (edge-mean)*(edge-mean); 226 } 227 228 //double variance = sum/edgeLengths.size(); 229 double normalizedVariance = (sum/edgeLengths.size())/(mean*mean); //normalized with square of mean value 230 //geometriesPortion = geometriesPortion + id + ":" + normalizedVariance + " "; 231 //wayNode.getIndexVector().put(id, normalizedVariance); 232 //intervals with boolean values for variance feature 233 234 if(normalizedVariance == 0){ 235 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 236 id = id + NUMBER_OF_VARIANCE; 237 } 238 else if(normalizedVariance < 0.005){ 206 } 207 208 private void handleVariance(OSMWay wayNode, double normalizedVariance) { 209 if (normalizedVariance == 0) { 210 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 211 id = id + NUMBER_OF_VARIANCE; 212 } else if (normalizedVariance < 0.005) { 239 213 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1.0)); 240 214 id = id + NUMBER_OF_VARIANCE; 241 } 242 else if(normalizedVariance < 0.01){ 215 } else if (normalizedVariance < 0.01) { 243 216 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1.0)); 244 217 id = id + NUMBER_OF_VARIANCE; 245 } 246 else if(normalizedVariance < 0.02){ 218 } else if (normalizedVariance < 0.02) { 247 219 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1.0)); 248 220 id = id + NUMBER_OF_VARIANCE; 249 } 250 else if(normalizedVariance < 0.03){ 221 } else if (normalizedVariance < 0.03) { 251 222 wayNode.getFeatureNodeList().add(new FeatureNode(id+4, 1.0)); 252 223 id = id + NUMBER_OF_VARIANCE; 253 } 254 else if(normalizedVariance < 0.04){ 224 } else if (normalizedVariance < 0.04) { 255 225 wayNode.getFeatureNodeList().add(new FeatureNode(id+5, 1.0)); 256 226 id = id + NUMBER_OF_VARIANCE; 257 } 258 else if(normalizedVariance < 0.05){ 227 } else if (normalizedVariance < 0.05) { 259 228 wayNode.getFeatureNodeList().add(new FeatureNode(id+6, 1.0)); 260 229 id = id + NUMBER_OF_VARIANCE; 261 } 262 else if(normalizedVariance < 0.06){ 230 } else if (normalizedVariance < 0.06) { 263 231 wayNode.getFeatureNodeList().add(new FeatureNode(id+7, 1.0)); 264 232 id = id + NUMBER_OF_VARIANCE; 265 } 266 else if(normalizedVariance < 0.07){ 233 } else if (normalizedVariance < 0.07) { 267 234 wayNode.getFeatureNodeList().add(new FeatureNode(id+8, 1.0)); 268 235 id = id + NUMBER_OF_VARIANCE; 269 } 270 else if(normalizedVariance < 0.08){ 236 } else if (normalizedVariance < 0.08) { 271 237 wayNode.getFeatureNodeList().add(new FeatureNode(id+9, 1.0)); 272 238 id = id + NUMBER_OF_VARIANCE; 273 } 274 else if(normalizedVariance < 0.09){ 239 } else if (normalizedVariance < 0.09) { 275 240 wayNode.getFeatureNodeList().add(new FeatureNode(id+10, 1.0)); 276 241 id = id + NUMBER_OF_VARIANCE; 277 } 278 else if(normalizedVariance < 0.1){ 242 } else if (normalizedVariance < 0.1) { 279 243 wayNode.getFeatureNodeList().add(new FeatureNode(id+11, 1.0)); 280 244 id = id + NUMBER_OF_VARIANCE; 281 } 282 else if(normalizedVariance < 0.12){ 245 } else if (normalizedVariance < 0.12) { 283 246 wayNode.getFeatureNodeList().add(new FeatureNode(id+12, 1.0)); 284 247 id = id + NUMBER_OF_VARIANCE; 285 } 286 else if(normalizedVariance < 0.14){ 248 } else if (normalizedVariance < 0.14) { 287 249 wayNode.getFeatureNodeList().add(new FeatureNode(id+13, 1.0)); 288 250 id = id + NUMBER_OF_VARIANCE; 289 } 290 else if(normalizedVariance < 0.16){ 251 } else if (normalizedVariance < 0.16) { 291 252 wayNode.getFeatureNodeList().add(new FeatureNode(id+14, 1.0)); 292 253 id = id + NUMBER_OF_VARIANCE; 293 } 294 else if(normalizedVariance < 0.18){ 254 } else if (normalizedVariance < 0.18) { 295 255 wayNode.getFeatureNodeList().add(new FeatureNode(id+15, 1.0)); 296 256 id = id + NUMBER_OF_VARIANCE; 297 } 298 else if(normalizedVariance < 0.20){ 257 } else if (normalizedVariance < 0.20) { 299 258 wayNode.getFeatureNodeList().add(new FeatureNode(id+16, 1.0)); 300 259 id = id + NUMBER_OF_VARIANCE; 301 } 302 else if(normalizedVariance < 0.22){ 260 } else if (normalizedVariance < 0.22) { 303 261 wayNode.getFeatureNodeList().add(new FeatureNode(id+17, 1.0)); 304 262 id = id + NUMBER_OF_VARIANCE; 305 } 306 else if(normalizedVariance < 0.24){ 263 } else if (normalizedVariance < 0.24) { 307 264 wayNode.getFeatureNodeList().add(new FeatureNode(id+18, 1.0)); 308 265 id = id + NUMBER_OF_VARIANCE; 309 } 310 else if(normalizedVariance < 0.26){ 266 } else if (normalizedVariance < 0.26) { 311 267 wayNode.getFeatureNodeList().add(new FeatureNode(id+19, 1.0)); 312 268 id = id + NUMBER_OF_VARIANCE; 313 } 314 else if(normalizedVariance < 0.28){ 269 } else if (normalizedVariance < 0.28) { 315 270 wayNode.getFeatureNodeList().add(new FeatureNode(id+20, 1.0)); 316 271 id = id + NUMBER_OF_VARIANCE; 317 } 318 else if(normalizedVariance < 0.30){ 272 } else if (normalizedVariance < 0.30) { 319 273 wayNode.getFeatureNodeList().add(new FeatureNode(id+21, 1.0)); 320 274 id = id + NUMBER_OF_VARIANCE; 321 } 322 else if(normalizedVariance < 0.32){ 275 } else if (normalizedVariance < 0.32) { 323 276 wayNode.getFeatureNodeList().add(new FeatureNode(id+22, 1.0)); 324 277 id = id + NUMBER_OF_VARIANCE; 325 } 326 else if(normalizedVariance < 0.34){ 278 } else if (normalizedVariance < 0.34) { 327 279 wayNode.getFeatureNodeList().add(new FeatureNode(id+23, 1.0)); 328 280 id = id + NUMBER_OF_VARIANCE; 329 } 330 else if(normalizedVariance < 0.36){ 281 } else if (normalizedVariance < 0.36) { 331 282 wayNode.getFeatureNodeList().add(new FeatureNode(id+24, 1.0)); 332 283 id = id + NUMBER_OF_VARIANCE; 333 } 334 else if(normalizedVariance < 0.38){ 284 } else if (normalizedVariance < 0.38) { 335 285 wayNode.getFeatureNodeList().add(new FeatureNode(id+25, 1.0)); 336 286 id = id + NUMBER_OF_VARIANCE; 337 } 338 else if(normalizedVariance < 0.40){ 287 } else if (normalizedVariance < 0.40) { 339 288 wayNode.getFeatureNodeList().add(new FeatureNode(id+26, 1.0)); 340 289 id = id + NUMBER_OF_VARIANCE; 341 } 342 else if(normalizedVariance < 0.42){ 290 } else if (normalizedVariance < 0.42) { 343 291 wayNode.getFeatureNodeList().add(new FeatureNode(id+27, 1.0)); 344 292 id = id + NUMBER_OF_VARIANCE; 345 } 346 else if(normalizedVariance < 0.44){ 293 } else if (normalizedVariance < 0.44) { 347 294 wayNode.getFeatureNodeList().add(new FeatureNode(id+28, 1.0)); 348 295 id = id + NUMBER_OF_VARIANCE; 349 } 350 else if(normalizedVariance < 0.46){ 296 } else if (normalizedVariance < 0.46) { 351 297 wayNode.getFeatureNodeList().add(new FeatureNode(id+29, 1.0)); 352 298 id = id + NUMBER_OF_VARIANCE; 353 } 354 else if(normalizedVariance < 0.48){ 299 } else if (normalizedVariance < 0.48) { 355 300 wayNode.getFeatureNodeList().add(new FeatureNode(id+30, 1.0)); 356 301 id = id + NUMBER_OF_VARIANCE; 357 } 358 else if(normalizedVariance < 0.5){ 302 } else if (normalizedVariance < 0.5) { 359 303 wayNode.getFeatureNodeList().add(new FeatureNode(id+31, 1.0)); 360 304 id = id + NUMBER_OF_VARIANCE; 361 } 362 else if(normalizedVariance < 0.6){ 305 } else if (normalizedVariance < 0.6) { 363 306 wayNode.getFeatureNodeList().add(new FeatureNode(id+32, 1.0)); 364 307 id = id + NUMBER_OF_VARIANCE; 365 } 366 else if(normalizedVariance < 0.7){ 308 } else if (normalizedVariance < 0.7) { 367 309 wayNode.getFeatureNodeList().add(new FeatureNode(id+33, 1.0)); 368 310 id = id + NUMBER_OF_VARIANCE; 369 } 370 else if(normalizedVariance < 0.8){ 311 } else if (normalizedVariance < 0.8) { 371 312 wayNode.getFeatureNodeList().add(new FeatureNode(id+34, 1.0)); 372 313 id = id + NUMBER_OF_VARIANCE; 373 } 374 else if(normalizedVariance < 0.9){ 314 } else if (normalizedVariance < 0.9) { 375 315 wayNode.getFeatureNodeList().add(new FeatureNode(id+35, 1.0)); 376 316 id = id + NUMBER_OF_VARIANCE; 377 } 378 else if(normalizedVariance < 1){ 317 } else if (normalizedVariance < 1) { 379 318 wayNode.getFeatureNodeList().add(new FeatureNode(id+36, 1.0)); 380 319 id = id + NUMBER_OF_VARIANCE; 381 } 382 else { 320 } else { 383 321 wayNode.getFeatureNodeList().add(new FeatureNode(id+37, 1.0)); 384 322 id = id + NUMBER_OF_VARIANCE; 385 } 386 //System.out.println("mean end from instanceVectors " + id); 387 //System.out.println("geom: " + wayNode.getFeatureNodeList()); 388 //System.out.println("last geometry id: " + id); 389 setLastID(id); 390 391 392 } 393 394 395 396 397 private void numberOfPointsFeature(int numberOfPoints, OSMWay wayNode) { 398 //int NUMBER_OF_POINTS = 13; //increase the id after the feature is found for the next portion of the vector. 399 400 if(numberOfPoints<10){ 401 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 402 id += NUMBER_OF_POINTS; 403 } 404 else if(numberOfPoints<20){ 323 } 324 } 325 326 private void numberOfPointsFeature(int numberOfPoints, OSMWay wayNode) { 327 //increase the id after the feature is found for the next portion of the vector. 328 329 if (numberOfPoints < 10) { 330 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 331 id += NUMBER_OF_POINTS; 332 } else if (numberOfPoints < 20) { 405 333 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1.0)); 406 334 id += NUMBER_OF_POINTS; 407 } 408 else if(numberOfPoints<30){ 335 } else if (numberOfPoints < 30) { 409 336 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1.0)); 410 337 id += NUMBER_OF_POINTS; 411 } 412 else if(numberOfPoints<40){ 338 } else if (numberOfPoints < 40) { 413 339 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1.0)); 414 340 id += NUMBER_OF_POINTS; 415 } 416 else if(numberOfPoints<50){ 341 } else if (numberOfPoints < 50) { 417 342 wayNode.getFeatureNodeList().add(new FeatureNode(id+4, 1.0)); 418 343 id += NUMBER_OF_POINTS; 419 } 420 else if(numberOfPoints<75){ 344 } else if (numberOfPoints < 75) { 421 345 wayNode.getFeatureNodeList().add(new FeatureNode(id+5, 1.0)); 422 346 id += NUMBER_OF_POINTS; 423 } 424 else if(numberOfPoints<100){ 347 } else if (numberOfPoints < 100) { 425 348 wayNode.getFeatureNodeList().add(new FeatureNode(id+6, 1.0)); 426 349 id += NUMBER_OF_POINTS; 427 } 428 else if(numberOfPoints<150){ 350 } else if (numberOfPoints < 150) { 429 351 wayNode.getFeatureNodeList().add(new FeatureNode(id+7, 1.0)); 430 352 id += NUMBER_OF_POINTS; 431 } 432 else if(numberOfPoints<200){ 353 } else if (numberOfPoints < 200) { 433 354 wayNode.getFeatureNodeList().add(new FeatureNode(id+8, 1.0)); 434 355 id += NUMBER_OF_POINTS; 435 } 436 else if(numberOfPoints<300){ 356 } else if (numberOfPoints < 300) { 437 357 wayNode.getFeatureNodeList().add(new FeatureNode(id+9, 1.0)); 438 358 id += NUMBER_OF_POINTS; 439 } 440 else if(numberOfPoints<500){ 359 } else if (numberOfPoints < 500) { 441 360 wayNode.getFeatureNodeList().add(new FeatureNode(id+10, 1.0)); 442 361 id += NUMBER_OF_POINTS; 443 } 444 else if(numberOfPoints<1000){ 362 } else if (numberOfPoints < 1000) { 445 363 wayNode.getFeatureNodeList().add(new FeatureNode(id+11, 1.0)); 446 364 id += NUMBER_OF_POINTS; 447 } 448 else{ 365 } else { 449 366 wayNode.getFeatureNodeList().add(new FeatureNode(id+12, 1.0)); 450 367 id += NUMBER_OF_POINTS; 451 368 } 452 369 } 453 454 private void areaFeature(double area, OSMWay wayNode) { 455 456 if(area<50){ 457 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 458 id += NUMBER_OF_AREA_FEATURES; 459 } 460 else if(area<100){ 370 371 private void areaFeature(double area, OSMWay wayNode) { 372 373 if (area < 50) { 374 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 375 id += NUMBER_OF_AREA_FEATURES; 376 } else if (area < 100) { 461 377 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1.0)); 462 378 id += NUMBER_OF_AREA_FEATURES; 463 } 464 else if(area<150){ 379 } else if (area < 150) { 465 380 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1.0)); 466 381 id += NUMBER_OF_AREA_FEATURES; 467 } 468 else if(area<200){ 382 } else if (area < 200) { 469 383 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1.0)); 470 384 id += NUMBER_OF_AREA_FEATURES; 471 } 472 else if(area<250){ 385 } else if (area < 250) { 473 386 wayNode.getFeatureNodeList().add(new FeatureNode(id+4, 1.0)); 474 387 id += NUMBER_OF_AREA_FEATURES; 475 } 476 else if(area<300){ 388 } else if (area < 300) { 477 389 wayNode.getFeatureNodeList().add(new FeatureNode(id+5, 1.0)); 478 390 id += NUMBER_OF_AREA_FEATURES; 479 } 480 else if(area<350){ 391 } else if (area < 350) { 481 392 wayNode.getFeatureNodeList().add(new FeatureNode(id+6, 1.0)); 482 393 id += NUMBER_OF_AREA_FEATURES; 483 } 484 else if(area<400){ 394 } else if (area < 400) { 485 395 wayNode.getFeatureNodeList().add(new FeatureNode(id+7, 1.0)); 486 396 id += NUMBER_OF_AREA_FEATURES; 487 } 488 else if(area<450){ 397 } else if (area < 450) { 489 398 wayNode.getFeatureNodeList().add(new FeatureNode(id+8, 1.0)); 490 399 id += NUMBER_OF_AREA_FEATURES; 491 } 492 else if(area<500){ 400 } else if (area < 500) { 493 401 wayNode.getFeatureNodeList().add(new FeatureNode(id+9, 1.0)); 494 402 id += NUMBER_OF_AREA_FEATURES; 495 } 496 else if(area<750){ 403 } else if (area < 750) { 497 404 wayNode.getFeatureNodeList().add(new FeatureNode(id+10, 1.0)); 498 405 id += NUMBER_OF_AREA_FEATURES; 499 } 500 else if(area<1000){ 406 } else if (area < 1000) { 501 407 wayNode.getFeatureNodeList().add(new FeatureNode(id+11, 1.0)); 502 408 id += NUMBER_OF_AREA_FEATURES; 503 } 504 else if(area<1250){ 409 } else if (area < 1250) { 505 410 wayNode.getFeatureNodeList().add(new FeatureNode(id+12, 1.0)); 506 411 id += NUMBER_OF_AREA_FEATURES; 507 } 508 else if(area<1500){ 412 } else if (area < 1500) { 509 413 wayNode.getFeatureNodeList().add(new FeatureNode(id+13, 1.0)); 510 414 id += NUMBER_OF_AREA_FEATURES; 511 } 512 else if(area<1750){ 415 } else if (area < 1750) { 513 416 wayNode.getFeatureNodeList().add(new FeatureNode(id+14, 1.0)); 514 417 id += NUMBER_OF_AREA_FEATURES; 515 } 516 else if(area<2000){ 418 } else if (area < 2000) { 517 419 wayNode.getFeatureNodeList().add(new FeatureNode(id+15, 1.0)); 518 420 id += NUMBER_OF_AREA_FEATURES; 519 } 520 else if(area<2250){ 421 } else if (area < 2250) { 521 422 wayNode.getFeatureNodeList().add(new FeatureNode(id+16, 1.0)); 522 423 id += NUMBER_OF_AREA_FEATURES; 523 } 524 else if(area<2500){ 424 } else if (area < 2500) { 525 425 wayNode.getFeatureNodeList().add(new FeatureNode(id+17, 1.0)); 526 426 id += NUMBER_OF_AREA_FEATURES; 527 } 528 else if(area<2750){ 427 } else if (area < 2750) { 529 428 wayNode.getFeatureNodeList().add(new FeatureNode(id+18, 1.0)); 530 429 id += NUMBER_OF_AREA_FEATURES; 531 } 532 else if(area<3000){ 430 } else if (area < 3000) { 533 431 wayNode.getFeatureNodeList().add(new FeatureNode(id+19, 1.0)); 534 432 id += NUMBER_OF_AREA_FEATURES; 535 } 536 else if(area<3500){ 433 } else if (area < 3500) { 537 434 wayNode.getFeatureNodeList().add(new FeatureNode(id+20, 1.0)); 538 435 id += NUMBER_OF_AREA_FEATURES; 539 } 540 else if(area<4000){ 436 } else if (area < 4000) { 541 437 wayNode.getFeatureNodeList().add(new FeatureNode(id+21, 1.0)); 542 438 id += NUMBER_OF_AREA_FEATURES; 543 } 544 else if(area<5000){ 439 } else if (area < 5000) { 545 440 wayNode.getFeatureNodeList().add(new FeatureNode(id+22, 1.0)); 546 441 id += NUMBER_OF_AREA_FEATURES; 547 } 548 else if(area<10000){ 442 } else if (area < 10000) { 549 443 wayNode.getFeatureNodeList().add(new FeatureNode(id+23, 1.0)); 550 444 id += NUMBER_OF_AREA_FEATURES; 551 } 552 else{ 445 } else { 553 446 wayNode.getFeatureNodeList().add(new FeatureNode(id+24, 1.0)); 554 447 id += NUMBER_OF_AREA_FEATURES; 555 } 556 } 557 558 559 private boolean geometryResemblesCircle(OSMWay way){ 448 } 449 } 450 451 private boolean geometryResemblesCircle(OSMWay way) { 560 452 Geometry wayGeometry = way.getGeometry(); 561 453 boolean isCircle = false; 562 if(wayGeometry.getGeometryType().equals("Polygon") && wayGeometry.getNumPoints() >=16){563 564 List<Geometry> points = way.getNodeGeometries(); 565 Geometry firstPoint = points.get(0); 454 if (wayGeometry.getGeometryType().equals("Polygon") && wayGeometry.getNumPoints() >= 16) { 455 456 List<Geometry> points = way.getNodeGeometries(); 457 Geometry firstPoint = points.get(0); 566 458 double radius = firstPoint.distance(wayGeometry.getCentroid()); 567 459 568 460 // buffer around the distance of the first point to centroid 569 double radiusBufferSmaller = radius*0.6; 570 //the rest of the point-to-centroid distances will be compared with these 571 double radiusBufferGreater = radius*1.4; 461 double radiusBufferSmaller = radius*0.6; 462 //the rest of the point-to-centroid distances will be compared with these 463 double radiusBufferGreater = radius*1.4; 572 464 isCircle = true; 573 574 for (Geometry point : points) {465 466 for (Geometry point : points) { 575 467 double tempRadius = point.distance(wayGeometry.getCentroid()); 576 468 boolean tempIsCircle = (radiusBufferSmaller <= tempRadius) && (tempRadius <= radiusBufferGreater); 577 469 isCircle = isCircle && tempIsCircle; //if any of the points give a false, the method will return false 578 //if (!isCircle){break;} 579 } 580 581 double ratio = wayGeometry.getLength() / wayGeometry.getArea(); 582 boolean tempIsCircle = ratio < 0.06; //arbitary value based on statistic measure of osm instances. 583 470 //if (!isCircle) {break;} 471 } 472 473 double ratio = wayGeometry.getLength() / wayGeometry.getArea(); 474 boolean tempIsCircle = ratio < 0.06; //arbitary value based on statistic measure of osm instances. 475 //The smaller this value, the closer this polygon resembles to a circle 584 476 isCircle = isCircle && tempIsCircle; 585 477 } 586 478 return isCircle; 587 } 588 589 private void setLastID(int lastID){ 479 } 480 481 private void setLastID(int lastID) { 590 482 this.id = lastID; 591 483 } 592 593 public int getLastID(){ 484 485 public int getLastID() { 594 486 return id + 1; 595 487 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/features/OSMClassification.java
r31106 r32404 1 package org.openstreetmap.josm.plugins.features; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.features; 2 3 3 4 import java.util.List; … … 5 6 import java.util.Set; 6 7 import java.util.TreeSet; 7 import org.openstreetmap.josm.plugins.container.OSMWay; 8 9 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 8 10 9 11 /** … … 13 15 14 16 public class OSMClassification { 15 17 16 18 private int directClassID = 0; 17 18 19 20 19 20 public void calculateClasses(OSMWay wayNode, Map<String, String> mappings, Map<String, Integer> mappingsWithIDs, 21 Map<String, List<String>> indirectClasses, Map<String, Integer> indirectClassesIDs) { 22 21 23 Set<Integer> sortedIndirectIDs = new TreeSet<>(); 22 Set<Integer> sortedDirectIDs = new TreeSet<>(); 23 for (Map.Entry<String, String> wayTagKeyValue : wayNode.getTagKeyValue().entrySet()){ 24 Set<Integer> sortedDirectIDs = new TreeSet<>(); 25 for (Map.Entry<String, String> wayTagKeyValue : wayNode.getTagKeyValue().entrySet()) { 24 26 //iteration for each tag (key-value) in the current way node 25 27 26 28 //concat key and value to use it later for checking 27 29 String key = wayTagKeyValue.getKey() + " " + wayTagKeyValue.getValue(); 28 29 for (Map.Entry<String,String> tagMappedToClass : mappings.entrySet()){ 30 31 for (Map.Entry<String, String> tagMappedToClass : mappings.entrySet()) { 30 32 //entry of mappings is e.g "highway residential <-> ResidentialHighway" 31 33 //iteration to discover the wayNode class. This class's ID will be the start of the vector 32 33 if (key.equals(tagMappedToClass.getKey())){ 34 35 if (key.equals(tagMappedToClass.getKey())) { 34 36 String className = tagMappedToClass.getValue(); 35 37 directClassID = mappingsWithIDs.get(className); 36 37 sortedDirectIDs.add(directClassID); 38 39 //the direct class id is the last direct class that the instance is found to belong 38 39 sortedDirectIDs.add(directClassID); 40 41 //the direct class id is the last direct class that the instance is found to belong 40 42 41 43 List<String> superClassesList = indirectClasses.get(className); 42 44 43 if (superClassesList != null) {//check if the class has no superclasses44 45 if (superClassesList != null) { //check if the class has no superclasses 46 45 47 for (String superClass: superClassesList) { 46 48 47 Integer indirectID = indirectClassesIDs.get(superClass); 48 if(indirectID != null) {// there is a chance here that the ID is null,49 Integer indirectID = indirectClassesIDs.get(superClass); //to save time here 50 if (indirectID != null) { // there is a chance here that the ID is null, 49 51 //cause the list of super Classes might contain extra classes with no ID 50 52 //in the indirectClassesIDs map which is constructed from listHierarchyRootClasses method 51 53 //at the OntologyParser. 52 54 //so this condition check will remain for now 53 54 if(!(sortedIndirectIDs.contains(indirectID))){ 55 56 if (!(sortedIndirectIDs.contains(indirectID))) { 55 57 sortedIndirectIDs.add(indirectID); 56 58 //wayNode.getIndexVector().put(indirectID, 1.0); … … 62 64 } 63 65 } 64 } 65 } 66 } 67 } 66 68 wayNode.setClassIDs(sortedDirectIDs); 67 69 //System.out.println("OSMClassification, selected instance classes: " + sortedDirectIDs); 68 70 wayNode.setClassID(directClassID); 69 } 71 } 70 72 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/features/RelationFeatures.java
r31106 r32404 1 package org.openstreetmap.josm.plugins.features; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.features; 3 4 import java.util.List; 5 import java.util.Map; 6 7 import org.openstreetmap.josm.plugins.osmrec.container.OSMRelation; 8 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 2 9 3 10 import de.bwaldvogel.liblinear.FeatureNode; 4 import java.util.List;5 import java.util.Map;6 import org.openstreetmap.josm.plugins.container.OSMRelation;7 import org.openstreetmap.josm.plugins.container.OSMWay;8 11 9 12 /** 10 13 * Constructs the relation features for liblinear. 11 * 14 * 12 15 * @author imis-nkarag 13 16 */ 14 17 15 18 public class RelationFeatures { 16 17 18 19 20 }21 22 19 int id; 20 21 public RelationFeatures(int id) { 22 this.id = id; 23 } 24 25 public void createRelationFeatures(OSMWay wayNode, List<OSMRelation> relationList) { 23 26 id++; //this should be removed when using boolean intervals for mean and variance 24 //System.out.println("must be 1468" + id);25 //System.out.println("InstanceVectors rel start" + id);26 27 boolean hasRelation = false; 27 for(OSMRelation relation : relationList){ 28 if(hasRelation){ 28 for (OSMRelation relation : relationList) { 29 if (hasRelation) { 29 30 break; 30 } 31 32 if(relation.getMemberReferences().contains(wayNode.getID())){ 31 } 32 33 if (relation.getMemberReferences().contains(wayNode.getID())) { 33 34 hasRelation = true; 34 35 Map<String, String> tags = relation.getTagKeyValue(); 35 36 if(tags.containsKey("route")){ 37 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 38 } 39 else if(tags.containsKey("multipolygon")){ 40 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1.0)); 41 } 42 else if(tags.containsKey("boundary")){ 43 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1.0)); 44 } 45 else if(tags.containsKey("restriction")){ 46 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1.0)); 47 } 48 else{ 36 37 if (tags.containsKey("route")) { 38 wayNode.getFeatureNodeList().add(new FeatureNode(id, 1.0)); 39 } else if (tags.containsKey("multipolygon")) { 40 wayNode.getFeatureNodeList().add(new FeatureNode(id+1, 1.0)); 41 } else if (tags.containsKey("boundary")) { 42 wayNode.getFeatureNodeList().add(new FeatureNode(id+2, 1.0)); 43 } else if (tags.containsKey("restriction")) { 44 wayNode.getFeatureNodeList().add(new FeatureNode(id+3, 1.0)); 45 } else { 49 46 //the instance may be a member of a relation, but the relation has no type or is incomplete. 50 wayNode.getFeatureNodeList().add(new FeatureNode(id+4, 1.0)); 47 wayNode.getFeatureNodeList().add(new FeatureNode(id+4, 1.0)); 51 48 } 52 49 id = id + 5; 53 50 } 54 51 } 55 56 57 if(!hasRelation){ 52 53 if (!hasRelation) { 58 54 id = id + 5; 59 } 55 } 60 56 } 61 62 public int getLastID(){ 57 58 public int getLastID() { 63 59 return id; 64 } 60 } 65 61 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/features/TextualFeatures.java
r31106 r32404 1 package org.openstreetmap.josm.plugins.features; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.features; 2 3 3 import com.cybozu.labs.langdetect.LangDetectException;4 import de.bwaldvogel.liblinear.FeatureNode;5 4 import java.util.List; 6 5 import java.util.Map; 7 6 import java.util.TreeMap; 8 import org.openstreetmap.josm.plugins.container.OSMWay;9 7 import java.util.logging.Level; 10 8 import java.util.logging.Logger; 9 11 10 import org.apache.lucene.analysis.el.GreekAnalyzer; 12 11 import org.apache.lucene.analysis.en.EnglishAnalyzer; … … 14 13 import org.apache.lucene.queryParser.QueryParser; 15 14 import org.apache.lucene.util.Version; 16 import org.openstreetmap.josm.plugins.extractor.LanguageDetector; 15 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 16 import org.openstreetmap.josm.plugins.osmrec.extractor.LanguageDetector; 17 18 import com.cybozu.labs.langdetect.LangDetectException; 19 20 import de.bwaldvogel.liblinear.FeatureNode; 17 21 18 22 /** 19 23 * Constructs the textual features from the given textual list. 20 * 24 * 21 25 * @author imis-nkarag 22 26 */ 23 27 24 28 public class TextualFeatures { 25 29 26 30 private int id; 27 31 private int numberOfFeatures; … … 31 35 private final QueryParser englishParser; 32 36 private final LanguageDetector languageDetector; 33 37 34 38 public TextualFeatures(int id, List<String> textualList, LanguageDetector languageDetector) { 35 39 36 40 this.id = id; 37 41 this.textualList = textualList; … … 42 46 EnglishAnalyzer englishAnalyzer = new EnglishAnalyzer(Version.LUCENE_36); 43 47 englishParser = new QueryParser(Version.LUCENE_36, "", englishAnalyzer); 44 48 45 49 } 46 47 public void createTextualFeatures(OSMWay wayNode) { 50 51 public void createTextualFeatures(OSMWay wayNode) { 48 52 //namesList.indexOf(name) this index can be zero. 49 //In that case it conflicts the previous geometry id, so we increment id. 50 //System.out.println("begin textual id from InstanceVectors = " + id); 51 53 //In that case it conflicts the previous geometry id, so we increment id. 54 52 55 //idWords: populated with the ID that will be ginen as a feature, mapped with the word found. 53 56 //Chose to store the name for future use. 54 Map<Integer,String> idWords= new TreeMap<>(); 57 Map<Integer, String> idWords = new TreeMap<>(); 55 58 Map<String, String> tags = wayNode.getTagKeyValue(); 56 if (tags.keySet().contains("name")) {59 if (tags.keySet().contains("name")) { 57 60 String nameTag = tags.get("name"); //get the value of the name tag of the current node 58 String[] nameTagSplitList = nameTag.split("\\s"); //split the value to compare individually 59 61 String[] nameTagSplitList = nameTag.split("\\s"); //split the value to compare individually 62 //with the namesList 60 63 String lang = ""; 61 64 try { … … 65 68 } 66 69 67 for(String split : nameTagSplitList){ 70 for (String split : nameTagSplitList) { 68 71 try { 69 //System.out.println("split before: " + split);70 //detectLanguage(split);71 72 72 //TOGGLE 73 73 split = split.replaceAll("[-+.^:,?;'{}\"!()\\[\\]]", ""); 74 if(lang.equals("el")){ 75 76 //if(false){ 74 if (lang.equals("el")) { 77 75 split = stemGreek(split); 78 //System.out.println("en: split after stem: " + split); 79 } 80 else{ 76 } else { 81 77 split = stemEnglish(split); 82 //System.out.println("el: split after stem: " + split);83 78 } 84 79 85 if(textualList.contains(split)){ 80 if (textualList.contains(split)) { 86 81 int currentID = textualList.indexOf(split) + id; 87 82 idWords.put(currentID, split); 88 //System.out.println(currentID + " " + split);89 83 } 90 84 } catch (ParseException ex) { 91 85 Logger.getLogger(TextualFeatures.class.getName()).log(Level.SEVERE, null, ex); 92 86 } 93 } 94 95 for(Integer wordID : idWords.keySet()){ 87 } 88 89 for (Integer wordID : idWords.keySet()) { 96 90 wayNode.getFeatureNodeList().add(new FeatureNode(wordID, 1.0)); 97 91 //System.out.println(wordID); 98 } 99 //System.out.println("until textual " + wayNode.getFeatureNodeList()); 100 } 101 //System.out.println("textual id (number of features): " + (namesList.size()+id)); 102 //System.out.println("textual id (number of features): " + numberOfFeatures); 92 } 93 //System.out.println("until textual " + wayNode.getFeatureNodeList()); 94 } 103 95 } 104 105 public int getLastID(){ 96 97 public int getLastID() { 106 98 return numberOfFeatures; 107 99 } 108 109 private String detectLanguage(String nameTag) throws LangDetectException{ 110 //detect language 111 112 if(!nameTag.isEmpty()){ 100 101 private String detectLanguage(String nameTag) throws LangDetectException { 102 //detect language 103 104 if (!nameTag.isEmpty()) { 113 105 language = languageDetector.detect(nameTag); 114 106 return language; 115 } 116 else{ 107 } else { 117 108 return "no_lang"; 118 109 } … … 122 113 String stemmedWord; 123 114 124 if(!word.isEmpty()){ 115 if (!word.isEmpty()) { 125 116 stemmedWord = greekParser.parse(word).toString(); 126 } 127 else{ 117 } else { 128 118 return word; 129 119 } … … 131 121 return stemmedWord; 132 122 } 133 134 private String stemEnglish(String word) throws ParseException{ 123 124 private String stemEnglish(String word) throws ParseException { 135 125 136 126 String stemmedWord; 137 127 138 if(!word.isEmpty()){ 128 if (!word.isEmpty()) { 139 129 stemmedWord = englishParser.parse(word).toString(); 140 } 141 else{ 130 } else { 142 131 return word; 143 132 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/parsers/Mapper.java
r31554 r32404 1 2 package org.openstreetmap.josm.plugins.parsers; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.parsers; 3 3 4 4 import java.io.FileNotFoundException; … … 14 14 * about the OSM tags and the ontology classes 15 15 * This info is used in the vector construction. 16 * 16 * 17 17 * @author imis-nkarag 18 18 */ … … 23 23 private final HashMap<String, Integer> mappingsWithIDs; 24 24 private final Map<Integer, String> idsWithMappings; 25 26 public Mapper() {27 28 mappings = new LinkedHashMap<>(); 29 mappingsWithIDs = new HashMap<>(); 30 idsWithMappings = new HashMap<>(); 31 25 26 public Mapper() { 27 28 mappings = new LinkedHashMap<>(); 29 mappingsWithIDs = new HashMap<>(); 30 idsWithMappings = new HashMap<>(); 31 32 32 } 33 33 34 34 public void parseFile(InputStream inps) throws FileNotFoundException { 35 35 36 36 Scanner input = new Scanner(inps); //the Map file contains lines of the mappings separated with the symbol "|" 37 38 39 while(input.hasNext()) { 40 41 42 43 44 45 46 47 48 37 //e.g. highway motorway | Motorway 38 //the key will be the string "highway motorway" and the value "Motorway" 39 while (input.hasNext()) { 40 41 String nextLine = input.nextLine(); 42 String[] splitContent = nextLine.split("\\|", 2); //split current line in two parts, 43 //separated by the "|" symbol 44 String key = splitContent[0]; //this key will be mapped to a class 45 String value = splitContent[1]; //this value is the mapped class 46 key = key.trim(); 47 value = value.trim(); 48 mappings.put(key, value); 49 49 } 50 50 constructMappingsWithIDs(); 51 51 constructIDsWithMappings(); 52 52 System.out.println("Mappings file parsed successfully!"); 53 //LOG.info("Mappings file parsed successfully!"); 54 } 55 56 private void constructMappingsWithIDs(){ 53 //LOG.info("Mappings file parsed successfully!"); 54 } 55 56 private void constructMappingsWithIDs() { 57 57 Integer i = 1; //starting ID is 1: SVM multiclass does not accept 0 as a class ID 58 for (String ontologyClass : mappings.values()) {59 mappingsWithIDs.put(ontologyClass, i); 58 for (String ontologyClass : mappings.values()) { 59 mappingsWithIDs.put(ontologyClass, i); 60 60 i++; 61 } 61 } 62 62 } 63 64 public Map<String,Integer> getMappingsWithIDs(){ 63 64 public Map<String, Integer> getMappingsWithIDs() { 65 65 return Collections.unmodifiableMap(mappingsWithIDs); 66 66 } 67 68 public Map<String,String> getMappings(){ 67 68 public Map<String, String> getMappings() { 69 69 return Collections.unmodifiableMap(mappings); 70 } 71 72 private void constructIDsWithMappings(){ 70 } 71 72 private void constructIDsWithMappings() { 73 73 int i = 1; //starting ID is 1: SVM multiclass does not accept 0 as a class ID 74 75 for (String ontologyClass : mappings.values()) {74 75 for (String ontologyClass : mappings.values()) { 76 76 idsWithMappings.put(i, ontologyClass); 77 77 i++; 78 78 } 79 } 80 81 public Map<Integer, String> getIDsWithMappings(){ 79 } 80 81 public Map<Integer, String> getIDsWithMappings() { 82 82 return Collections.unmodifiableMap(idsWithMappings); 83 } 83 } 84 84 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/parsers/OSMParser.java
r32320 r32404 1 package org.openstreetmap.josm.plugins.parsers; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.parsers; 2 3 3 4 import java.io.IOException; … … 21 22 import org.opengis.referencing.operation.TransformException; 22 23 import org.openstreetmap.josm.Main; 23 import org.openstreetmap.josm.plugins.container.OSMNode; 24 import org.openstreetmap.josm.plugins.container.OSMRelation; 25 import org.openstreetmap.josm.plugins.container.OSMWay; 24 import org.openstreetmap.josm.plugins.osmrec.container.OSMNode; 25 import org.openstreetmap.josm.plugins.osmrec.container.OSMRelation; 26 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 26 27 import org.openstreetmap.josm.tools.Utils; 27 28 import org.xml.sax.Attributes; … … 43 44 */ 44 45 public class OSMParser extends DefaultHandler { 45 46 46 47 //private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OSMParser.class); 47 48 48 49 //change from wgs84 to cartesian for later processing of the geometry 49 50 private static final CoordinateReferenceSystem sourceCRS = DefaultGeographicCRS.WGS84; … … 51 52 private final GeometryFactory geometryFactory = new GeometryFactory(); 52 53 private static MathTransform transform = null; 53 private final List<OSMNode> nodeList; //will be populated with nodes 54 private final List<OSMNode> nodeList; //will be populated with nodes 54 55 private final List<OSMRelation> relationList; 55 private final Map<String, OSMNode> nodesWithIDs; //map containing IDs as Strings and the corresponding OSMNode objects 56 private final Map<String, OSMNode> nodesWithIDs; //map containing IDs as Strings and the corresponding OSMNode objects 56 57 private final List<OSMWay> wayList; //populated with ways of the OSM file 57 58 private final String osmXmlFileName; 58 59 private OSMNode nodeTmp; //variable to hold the node object 59 60 private OSMWay wayTmp; //variable to hold the way object 60 private OSMRelation relationTmp; 61 private boolean inWay = false; //when parser is in a way node becomes true in order to track the parser position 62 private boolean inNode = false; //becomes true when the parser is in a simple node 61 private OSMRelation relationTmp; 62 private boolean inWay = false; //when parser is in a way node becomes true in order to track the parser position 63 private boolean inNode = false; //becomes true when the parser is in a simple node 63 64 private boolean inRelation = false; //becomes true when the parser is in a relarion node 64 65 public OSMParser(String osmXmlFileName) 66 this.osmXmlFileName = osmXmlFileName; 65 66 public OSMParser(String osmXmlFileName) { 67 this.osmXmlFileName = osmXmlFileName; 67 68 nodeList = new ArrayList<>(); 68 69 wayList = new ArrayList<>(); 69 70 relationList = new ArrayList<>(); 70 nodesWithIDs = new HashMap<>(); 71 nodesWithIDs = new HashMap<>(); 71 72 try { 72 73 transform = CRS.findMathTransform(sourceCRS, targetCRS, true); … … 86 87 @Override 87 88 public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException { 88 89 89 90 // if current element is an OSMNode , create new node and populate with the appropriate values 90 91 if (elementName.equalsIgnoreCase("node")) { … … 98 99 Coordinate targetGeometry = null; 99 100 Coordinate sourceCoordinate = new Coordinate(longitude, latitude); 100 try { 101 try { 101 102 targetGeometry = JTS.transform(sourceCoordinate, null, transform); 102 103 } catch (MismatchedDimensionException | TransformException ex) { 103 104 Logger.getLogger(OSMParser.class.getName()).log(Level.SEVERE, null, ex); 104 105 } 105 106 106 107 //create geometry object 107 108 Geometry geom = geometryFactory.createPoint(new Coordinate(targetGeometry)); … … 114 115 wayTmp = new OSMWay(); 115 116 wayTmp.setID(attributes.getValue("id")); 116 117 if(attributes.getValue("user") != null){ 117 118 if (attributes.getValue("user") != null) { 118 119 wayTmp.setUser(attributes.getValue("user")); 119 } 120 else{ 120 } else { 121 121 wayTmp.setUser("undefined"); 122 122 } 123 123 124 124 inWay = true; 125 125 inNode = false; … … 137 137 138 138 if (inNode) { 139 //if the path is in an OSMNode set tagKey and value to the corresponding node 139 //if the path is in an OSMNode set tagKey and value to the corresponding node 140 140 nodeTmp.setTagKeyValue(attributes.getValue("k"), attributes.getValue("v")); 141 141 } else if (inWay) { 142 142 //else if the path is in an OSM way set tagKey and value to the corresponding way 143 143 wayTmp.setTagKeyValue(attributes.getValue("k"), attributes.getValue("v")); 144 } else if(inRelation){ 144 } else if (inRelation) { 145 145 //set the key-value pairs of relation tags 146 146 relationTmp.setTagKeyValue(attributes.getValue("k"), attributes.getValue("v")); 147 } 147 } 148 148 } else if (elementName.equalsIgnoreCase("member")) { 149 149 relationTmp.addMemberReference(attributes.getValue("ref")); 150 } 150 } 151 151 } 152 152 … … 158 158 nodesWithIDs.put(nodeTmp.getID(), nodeTmp); 159 159 } 160 161 if (element.equalsIgnoreCase("way")) { 162 160 161 if (element.equalsIgnoreCase("way")) { 162 163 163 //construct the Way geometry from each node of the node references 164 164 List<String> references = wayTmp.getNodeReferences(); 165 165 166 166 for (String entry: references) { 167 Geometry geometry = nodesWithIDs.get(entry).getGeometry(); //get the geometry of the node with ID=entry 168 wayTmp.addNodeGeometry(geometry); //add the node geometry in this way 169 167 Geometry geometry = nodesWithIDs.get(entry).getGeometry(); //get the geometry of the node with ID=entry 168 wayTmp.addNodeGeometry(geometry); //add the node geometry in this way 169 170 170 } 171 171 Geometry geom = geometryFactory.buildGeometry(wayTmp.getNodeGeometries()); 172 173 if((wayTmp.getNumberOfNodes() >3) &&172 173 if ((wayTmp.getNumberOfNodes() > 3) && 174 174 wayTmp.getNodeGeometries().get(0).equals(wayTmp.getNodeGeometries() 175 .get(wayTmp.getNodeGeometries().size()-1))){ 176 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 177 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 178 179 if (!((wayTmp.getTagKeyValue().containsKey("barrier")) || wayTmp.getTagKeyValue().containsKey("highway"))){ 180 //this is not a barrier nor a road, so construct a polygon geometry 181 182 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 183 Polygon poly = new Polygon(linear, null, geometryFactory); 184 wayTmp.setGeometry(poly); 185 } 186 else { 187 //it is either a barrier or a road, so construct a linear ring geometry 188 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 189 wayTmp.setGeometry(linear); 190 } 191 } 192 else if (wayTmp.getNumberOfNodes()>1){ 193 //it is an open geometry with more than one nodes, make it linestring 194 195 LineString lineString = geometryFactory.createLineString(geom.getCoordinates()); 196 wayTmp.setGeometry(lineString); 197 } 198 else{ //we assume all the rest geometries are points 199 //some ways happen to have only one point. Construct a Point. 175 .get(wayTmp.getNodeGeometries().size()-1))) { 176 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 177 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 178 179 if (!((wayTmp.getTagKeyValue().containsKey("barrier")) || wayTmp.getTagKeyValue().containsKey("highway"))) { 180 //this is not a barrier nor a road, so construct a polygon geometry 181 182 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 183 Polygon poly = new Polygon(linear, null, geometryFactory); 184 wayTmp.setGeometry(poly); 185 } else { 186 //it is either a barrier or a road, so construct a linear ring geometry 187 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 188 wayTmp.setGeometry(linear); 189 } 190 } else if (wayTmp.getNumberOfNodes() > 1) { 191 //it is an open geometry with more than one nodes, make it linestring 192 193 LineString lineString = geometryFactory.createLineString(geom.getCoordinates()); 194 wayTmp.setGeometry(lineString); 195 } else { //we assume all the rest geometries are points 196 //some ways happen to have only one point. Construct a Point. 200 197 Point point = geometryFactory.createPoint(geom.getCoordinate()); 201 198 wayTmp.setGeometry(point); 202 199 } 203 200 wayList.add(wayTmp); 204 } 205 206 if(element.equalsIgnoreCase("relation")) { 201 } 202 203 if (element.equalsIgnoreCase("relation")) { 207 204 relationList.add(relationTmp); 208 205 } … … 216 213 return wayList; 217 214 } 218 219 public List<OSMRelation> getRelationList(){ 215 216 public List<OSMRelation> getRelationList() { 220 217 return relationList; 221 218 } … … 223 220 public Map<String, OSMNode> getNodesWithIDs() { 224 221 return nodesWithIDs; 225 } 222 } 226 223 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/parsers/Ontology.java
r31441 r32404 1 package org.openstreetmap.josm.plugins.parsers; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.parsers; 3 4 import java.io.InputStream; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Map; 2 10 3 11 /** … … 10 18 import com.hp.hpl.jena.rdf.model.ModelFactory; 11 19 import com.hp.hpl.jena.util.iterator.ExtendedIterator; 12 import java.io.InputStream;13 import java.util.ArrayList;14 import java.util.HashMap;15 import java.util.Iterator;16 import java.util.List;17 import java.util.Map;18 20 19 21 /** … … 21 23 * The owl file contains information about the ontology and hierarchy of the classes 22 24 * Provides methods for retrieving information about the ontology. 23 * 24 * 25 * 26 * 25 27 * @author imis-nkarag 26 28 */ 27 29 28 public class Ontology { 30 public class Ontology { 29 31 //private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(Ontology.class); 30 32 31 33 private OntModel ontologyModel; 32 34 private List<OntClass> listHierarchy; 33 private static final int additiveID = 1373; 34 private Map<String, List<String>> indirectClasses; 35 private static final int additiveID = 1373; 36 private Map<String, List<String>> indirectClasses; 35 37 private final Map<String, Integer> indirectClassesIDs; 36 38 private final InputStream owlFile; 37 38 public Ontology(InputStream owlFile){ 39 39 40 public Ontology(InputStream owlFile) { 41 40 42 this.owlFile = owlFile; 41 43 indirectClassesIDs = new HashMap<>(); … … 43 45 listHierarchy = new ArrayList<>(); 44 46 } 45 47 46 48 public void parseOntology() { 47 49 try { 48 50 49 //create the ontology model using the base 50 ontologyModel = ModelFactory.createOntologyModel(); 51 52 org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.ERROR); 53 ontologyModel.read(owlFile, null); //Hide RDFDefaultErrorHandler from terminal to keep clear output. 54 org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO); 55 56 listHierarchy = ontologyModel.listHierarchyRootClasses().toList(); 57 setListHierarchy(listHierarchy); 58 59 ExtendedIterator<OntClass> classes = ontologyModel.listClasses(); 60 while (classes.hasNext()) { 61 String className; 62 OntClass obj = classes.next(); 63 64 //compare localname with class name from map and call getSuperclass 65 if (obj.hasSubClass()) { 51 //create the ontology model using the base 52 ontologyModel = ModelFactory.createOntologyModel(); 66 53 67 for (Iterator<OntClass> i = obj.listSubClasses(true); i.hasNext();) { 68 OntClass currentClass = i.next(); 69 70 List<OntClass> superClasses = currentClass.listSuperClasses().toList(); 71 List<String> superClassesStrings = new ArrayList<>(); 72 73 for (OntClass superClass : superClasses){ 74 className = superClass.toString().replace("http://linkedgeodata.org/ontology/", ""); 75 superClassesStrings.add(className); 54 org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.ERROR); 55 ontologyModel.read(owlFile, null); //Hide RDFDefaultErrorHandler from terminal to keep clear output. 56 org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO); 57 58 listHierarchy = ontologyModel.listHierarchyRootClasses().toList(); 59 setListHierarchy(listHierarchy); 60 61 ExtendedIterator<OntClass> classes = ontologyModel.listClasses(); 62 while (classes.hasNext()) { 63 String className; 64 OntClass obj = classes.next(); 65 66 //compare localname with class name from map and call getSuperclass 67 if (obj.hasSubClass()) { 68 69 for (Iterator<OntClass> i = obj.listSubClasses(true); i.hasNext();) { 70 OntClass currentClass = i.next(); 71 72 List<OntClass> superClasses = currentClass.listSuperClasses().toList(); 73 List<String> superClassesStrings = new ArrayList<>(); 74 75 for (OntClass superClass : superClasses) { 76 className = superClass.toString().replace("http://linkedgeodata.org/ontology/", ""); 77 superClassesStrings.add(className); 78 } 79 indirectClasses.put(currentClass.getLocalName(), superClassesStrings); 76 80 } 77 indirectClasses.put(currentClass.getLocalName(), superClassesStrings);78 81 } 79 82 } 83 createIndirectClassesWithIDs(); 84 setIndirectClasses(indirectClasses); 85 setOntologyModel(ontologyModel); 86 } catch (IllegalArgumentException e) { 87 System.out.println(e.getMessage()); 80 88 } 81 createIndirectClassesWithIDs();82 setIndirectClasses(indirectClasses);83 setOntologyModel(ontologyModel);84 }85 catch (IllegalArgumentException e) {86 System.out.println(e.getMessage());87 }88 //LOG.info("Ontology from XML loaded!");89 89 } 90 90 91 91 private void createIndirectClassesWithIDs() { 92 for (int i =0; i<(listHierarchy.size()); i++){93 94 String key = listHierarchy.get(i).toString().replace("http://linkedgeodata.org/ontology/", ""); 92 for (int i = 0; i < listHierarchy.size(); i++) { 93 94 String key = listHierarchy.get(i).toString().replace("http://linkedgeodata.org/ontology/", ""); 95 95 //we add 1 to the ID because we want IDs beginning from 1. listHierarchy has index beginning from 0 96 96 indirectClassesIDs.put(key, i + additiveID); //the IDs start from 1373 to avoid duplicate IDs at the vectorConstructor 97 } 97 } 98 98 } 99 100 private void setOntologyModel(OntModel ontologyModel){ 101 this.ontologyModel = ontologyModel; 99 100 private void setOntologyModel(OntModel ontologyModel) { 101 this.ontologyModel = ontologyModel; 102 102 } 103 104 private void setIndirectClasses(Map<String, List<String>> indirectClasses){ 103 104 private void setIndirectClasses(Map<String, List<String>> indirectClasses) { 105 105 this.indirectClasses = indirectClasses; 106 106 107 107 } 108 109 private void setListHierarchy(List<OntClass> listHierarchy){ 108 109 private void setListHierarchy(List<OntClass> listHierarchy) { 110 110 this.listHierarchy = listHierarchy; 111 111 } 112 113 public OntModel getOntologyModel() {112 113 public OntModel getOntologyModel() { 114 114 return this.ontologyModel; 115 115 } 116 116 117 public Map<String, List<String>> getIndirectClasses(){ 117 public Map<String, List<String>> getIndirectClasses() { 118 118 return this.indirectClasses; 119 119 } 120 121 public List<OntClass> getListHierarchy(){ 120 121 public List<OntClass> getListHierarchy() { 122 122 return this.listHierarchy; 123 123 } 124 125 public Map<String, Integer> getIndirectClassesIDs(){ 124 125 public Map<String, Integer> getIndirectClassesIDs() { 126 126 return this.indirectClassesIDs; 127 127 } 128 128 129 129 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/parsers/TextualStatistics.java
r31402 r32404 1 package org.openstreetmap.josm.plugins.parsers; 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.plugins.osmrec.parsers; 2 3 3 4 import java.io.InputStream; … … 10 11 * Provides functionality for parsing the names file 11 12 * This file contains name occurrences obtained from statistical measures on OSM data. 12 * 13 * 13 14 * @author imis-nkarag 14 15 */ 15 16 16 17 public class TextualStatistics { 17 18 18 19 //private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(TextualStatistics.class); 19 20 private List<String> textualList; 20 21 public void parseTextualList(InputStream textualFileStream){ 22 textualList = new ArrayList<>(); 23 24 Scanner input = new Scanner(textualFileStream); 25 while(input.hasNext()) { 26 String nextLine = input.nextLine(); 27 textualList.add(nextLine); 28 } 29 //LOG.info("Name occurences parsed successfully!"); 21 22 public void parseTextualList(InputStream textualFileStream) { 23 textualList = new ArrayList<>(); 24 25 Scanner input = new Scanner(textualFileStream); 26 while (input.hasNext()) { 27 String nextLine = input.nextLine(); 28 textualList.add(nextLine); 30 29 } 31 32 public List<String> getTextualList(){ 33 return Collections.unmodifiableList(textualList); 34 } 30 //LOG.info("Name occurences parsed successfully!"); 31 } 32 33 public List<String> getTextualList() { 34 return Collections.unmodifiableList(textualList); 35 } 35 36 } 36 37 -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/personalization/HistoryParser.java
r32320 r32404 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.osmrec.personalization; 2 3 … … 25 26 import org.openstreetmap.josm.Main; 26 27 import org.openstreetmap.josm.io.OsmApi; 27 import org.openstreetmap.josm.plugins.container.OSMNode; 28 import org.openstreetmap.josm.plugins.container.OSMWay; 28 import org.openstreetmap.josm.plugins.osmrec.container.OSMNode; 29 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 29 30 import org.openstreetmap.josm.tools.HttpClient; 30 31 import org.openstreetmap.josm.tools.Utils; … … 43 44 /** 44 45 * Parses the history of an OSM user's changesets using OSM API. 45 * 46 * 46 47 * @author imis-nkarag 47 48 */ … … 52 53 private static final GeometryFactory geometryFactory = new GeometryFactory(); 53 54 private MathTransform transform; 54 private OSMNode nodeTmp; 55 private OSMNode nodeTmp; 55 56 56 57 private final List<OSMNode> nodeList; … … 64 65 * @param username user name 65 66 */ 66 public HistoryParser(String username) { 67 public HistoryParser(String username) { 67 68 this.username = username; 68 69 transform = null; … … 78 79 79 80 public void historyParse(String timeInterval) { 80 81 81 82 HashSet<String> changesetIDsList = new HashSet<>(); 82 83 83 84 try { 84 85 String osmUrl = OSM_API + "changesets?display_name=" + username + "&time=" + timeInterval; … … 88 89 Main.debug("changeset size "+ nodes.getLength()); 89 90 for (int i = 0; i < nodes.getLength(); i++) { 90 91 Main.debug("attributes of " + i + "th changeset"); 91 92 String id = nodes.item(i).getAttributes().item(3).toString(); 92 93 Main.debug("id:" + nodes.item(i).getAttributes().item(3)); 93 94 id = stripQuotes(id); 94 changesetIDsList.add(id); 95 } 96 97 for(String id : changesetIDsList){ 95 changesetIDsList.add(id); 96 } 97 98 for (String id : changesetIDsList) { 98 99 getChangesetByID(id); 99 100 } … … 108 109 InputStream xml = HttpClient.create(new URL(changesetByIDURL)).connect().getContent(); 109 110 Node osmChange = Utils.parseSafeDOM(xml).getFirstChild(); 110 111 111 112 //get all nodes first, in order to be able to call all nodes references and create the geometries 112 for(int i = 0; i < osmChange.getChildNodes().getLength(); i++){ 113 for (int i = 0; i < osmChange.getChildNodes().getLength(); i++) { 113 114 String changeType = osmChange.getChildNodes().item(i).getNodeName(); 114 if(!(changeType.equals("#text") || changeType.equals("delete"))){ 115 if (!(changeType.equals("#text") || changeType.equals("delete"))) { 115 116 116 117 NodeList changeChilds = osmChange.getChildNodes().item(i).getChildNodes(); … … 118 119 Node osmObject = changeChilds.item(1); 119 120 120 if(osmObject.getNodeName().equals("node")){ 121 //node data 121 if (osmObject.getNodeName().equals("node")) { 122 //node data 122 123 nodeTmp = new OSMNode(); 123 124 nodeTmp.setID(osmObject.getAttributes().getNamedItem("id").getNodeValue()); … … 128 129 Coordinate targetGeometry = null; 129 130 Coordinate sourceCoordinate = new Coordinate(longitude, latitude); 130 try { 131 try { 131 132 targetGeometry = JTS.transform(sourceCoordinate, null, transform); 132 133 } catch (MismatchedDimensionException | TransformException ex) { … … 136 137 //create geometry object 137 138 Geometry geom = geometryFactory.createPoint(new Coordinate(targetGeometry)); 138 nodeTmp.setGeometry(geom); 139 nodeTmp.setGeometry(geom); 139 140 140 141 nodeList.add(nodeTmp); 141 142 nodesWithIDs.put(nodeTmp.getID(), nodeTmp); 142 } 143 } 143 144 } 144 145 } 145 146 for(int i = 0; i < osmChange.getChildNodes().getLength(); i++){ 146 147 for (int i = 0; i < osmChange.getChildNodes().getLength(); i++) { 147 148 String changeType = osmChange.getChildNodes().item(i).getNodeName(); 148 if(!(changeType.equals("#text") || changeType.equals("delete"))){ 149 if (!(changeType.equals("#text") || changeType.equals("delete"))) { 149 150 NodeList changeChilds = osmChange.getChildNodes().item(i).getChildNodes(); 150 151 151 152 Node osmObject = changeChilds.item(1); 152 if(osmObject.getNodeName().equals("way")){ 153 if (osmObject.getNodeName().equals("way")) { 153 154 154 155 //get way data 155 156 wayTmp = new OSMWay(); 156 wayTmp.setID(osmObject.getAttributes().getNamedItem("id").getNodeValue()); 157 // osmObject.getChildNodes() <-extract tags, then set tags to osm object157 wayTmp.setID(osmObject.getAttributes().getNamedItem("id").getNodeValue()); 158 // extract tags, then set tags to osm object 158 159 Main.debug("\n\nWAY: " + wayTmp.getID()); 159 for (int l=0; l<osmObject.getChildNodes().getLength(); l++){160 for (int l = 0; l < osmObject.getChildNodes().getLength(); l++) { 160 161 String wayChild = osmObject.getChildNodes().item(l).getNodeName(); 161 162 162 if(wayChild.equals("tag")){ 163 if (wayChild.equals("tag")) { 163 164 String key = osmObject.getChildNodes().item(l).getAttributes().getNamedItem("k").getNodeValue(); 164 165 String value = osmObject.getChildNodes().item(l).getAttributes().getNamedItem("v").getNodeValue(); 165 166 System.out.println("key: " + key + " value: " + value); 166 wayTmp.setTagKeyValue(key,value); 167 } 168 else if(wayChild.equals("nd")){ 167 wayTmp.setTagKeyValue(key, value); 168 } else if (wayChild.equals("nd")) { 169 169 wayTmp.addNodeReference(osmObject.getChildNodes().item(l).getAttributes().getNamedItem("ref").getNodeValue()); 170 170 } 171 171 } 172 172 173 173 //construct the Way geometry from each node of the node references 174 174 List<String> references = wayTmp.getNodeReferences(); 175 175 176 176 for (String entry: references) { 177 if(nodesWithIDs.containsKey(entry)){177 if (nodesWithIDs.containsKey(entry)) { 178 178 Geometry geometry = nodesWithIDs.get(entry).getGeometry(); //get the geometry of the node with ID=entry 179 179 wayTmp.addNodeGeometry(geometry); //add the node geometry in this way 180 } 181 else{ 182 Main.debug("nodes with ids, no entry " + entry); 183 getNodeFromAPI(entry); 184 } 180 } else { 181 Main.debug("nodes with ids, no entry " + entry); 182 getNodeFromAPI(entry); 183 } 185 184 } 186 185 187 186 Geometry geom = geometryFactory.buildGeometry(wayTmp.getNodeGeometries()); 188 if((wayTmp.getNodeGeometries().size() >3) &&187 if ((wayTmp.getNodeGeometries().size() > 3) && 189 188 wayTmp.getNodeGeometries().get(0).equals(wayTmp.getNodeGeometries() 190 .get(wayTmp.getNodeGeometries().size()-1))){ 191 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 192 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 193 194 if (!((wayTmp.getTagKeyValue().containsKey("barrier")) || wayTmp.getTagKeyValue().containsKey("highway"))){ 195 //this is not a barrier nor a road, so construct a polygon geometry 196 197 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 198 Polygon poly = new Polygon(linear, null, geometryFactory); 199 wayTmp.setGeometry(poly); 189 .get(wayTmp.getNodeGeometries().size()-1))) { 190 //checks if the beginning and ending node are the same and the number of nodes are more than 3. 191 //the nodes must be more than 3, because jts does not allow a construction of a linear ring with less points. 192 193 if (!((wayTmp.getTagKeyValue().containsKey("barrier")) || wayTmp.getTagKeyValue().containsKey("highway"))) { 194 //this is not a barrier nor a road, so construct a polygon geometry 195 196 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 197 Polygon poly = new Polygon(linear, null, geometryFactory); 198 wayTmp.setGeometry(poly); 199 } else { 200 //it is either a barrier or a road, so construct a linear ring geometry 201 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 202 wayTmp.setGeometry(linear); 200 203 } 201 else { 202 //it is either a barrier or a road, so construct a linear ring geometry 203 LinearRing linear = geometryFactory.createLinearRing(geom.getCoordinates()); 204 wayTmp.setGeometry(linear); 205 } 206 } 207 else if (wayTmp.getNodeGeometries().size() > 1){ 208 //it is an open geometry with more than one nodes, make it linestring 204 } else if (wayTmp.getNodeGeometries().size() > 1) { 205 //it is an open geometry with more than one nodes, make it linestring 209 206 210 207 LineString lineString = geometryFactory.createLineString(geom.getCoordinates()); 211 wayTmp.setGeometry(lineString); 212 } 213 else{ //we assume all the rest geometries are points 214 //some ways happen to have only one point. Construct a Point. 208 wayTmp.setGeometry(lineString); 209 } else { //we assume all the rest geometries are points 210 //some ways happen to have only one point. Construct a Point. 215 211 Point point = geometryFactory.createPoint(geom.getCoordinate()); 216 212 wayTmp.setGeometry(point); 217 213 } 218 wayList.add(wayTmp); 219 } 214 wayList.add(wayTmp); 215 } 220 216 } 221 } 217 } 222 218 } catch (IOException | ParserConfigurationException | SAXException ex) { 223 219 Logger.getLogger(HistoryParser.class.getName()).log(Level.SEVERE, null, ex); 224 } 220 } 225 221 } 226 222 … … 236 232 String lat = nodes.item(0).getAttributes().getNamedItem("lat").getNodeValue(); 237 233 String lon = nodes.item(0).getAttributes().getNamedItem("lon").getNodeValue(); 238 234 239 235 nodeTmp = new OSMNode(); 240 236 nodeTmp.setID(nodeID); … … 245 241 Coordinate targetGeometry = null; 246 242 Coordinate sourceCoordinate = new Coordinate(longitude, latitude); 247 try { 243 try { 248 244 targetGeometry = JTS.transform(sourceCoordinate, null, transform); 249 245 } catch (MismatchedDimensionException | TransformException ex) { … … 253 249 //create geometry object 254 250 Geometry geom = geometryFactory.createPoint(new Coordinate(targetGeometry)); 255 nodeTmp.setGeometry(geom); 251 nodeTmp.setGeometry(geom); 256 252 257 253 nodeList.add(nodeTmp); 258 254 nodesWithIDs.put(nodeTmp.getID(), nodeTmp); 259 255 260 256 } catch (IOException | ParserConfigurationException | SAXException ex) { 261 257 Logger.getLogger(HistoryParser.class.getName()).log(Level.SEVERE, null, ex); 262 258 } 263 259 } 264 265 public List<OSMWay> getWayList(){ 260 261 public List<OSMWay> getWayList() { 266 262 return wayList; 267 263 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/personalization/TrainByUser.java
r31564 r32404 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.osmrec.personalization; 2 3 3 4 import static java.util.concurrent.TimeUnit.NANOSECONDS; 4 import de.bwaldvogel.liblinear.FeatureNode; 5 import de.bwaldvogel.liblinear.Linear; 6 import de.bwaldvogel.liblinear.Model; 7 import de.bwaldvogel.liblinear.Parameter; 8 import de.bwaldvogel.liblinear.Problem; 9 import de.bwaldvogel.liblinear.SolverType; 5 10 6 import java.awt.event.ActionEvent; 11 7 import java.awt.event.ActionListener; … … 29 25 import java.util.logging.Level; 30 26 import java.util.logging.Logger; 27 31 28 import javax.swing.SwingWorker; 32 import org.openstreetmap.josm.plugins.container.OSMRelation; 33 import org.openstreetmap.josm.plugins.container.OSMWay; 34 import org.openstreetmap.josm.plugins.extractor.Analyzer; 35 import org.openstreetmap.josm.plugins.extractor.LanguageDetector; 36 import org.openstreetmap.josm.plugins.features.ClassFeatures; 37 import org.openstreetmap.josm.plugins.features.GeometryFeatures; 38 import org.openstreetmap.josm.plugins.features.OSMClassification; 39 import org.openstreetmap.josm.plugins.features.RelationFeatures; 40 import org.openstreetmap.josm.plugins.features.TextualFeatures; 41 import org.openstreetmap.josm.plugins.parsers.Mapper; 42 import org.openstreetmap.josm.plugins.parsers.Ontology; 43 import org.openstreetmap.josm.plugins.parsers.TextualStatistics; 29 30 import org.openstreetmap.josm.plugins.osmrec.container.OSMRelation; 31 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 32 import org.openstreetmap.josm.plugins.osmrec.extractor.Analyzer; 33 import org.openstreetmap.josm.plugins.osmrec.extractor.LanguageDetector; 34 import org.openstreetmap.josm.plugins.osmrec.features.ClassFeatures; 35 import org.openstreetmap.josm.plugins.osmrec.features.GeometryFeatures; 36 import org.openstreetmap.josm.plugins.osmrec.features.OSMClassification; 37 import org.openstreetmap.josm.plugins.osmrec.features.RelationFeatures; 38 import org.openstreetmap.josm.plugins.osmrec.features.TextualFeatures; 39 import org.openstreetmap.josm.plugins.osmrec.parsers.Mapper; 40 import org.openstreetmap.josm.plugins.osmrec.parsers.Ontology; 41 import org.openstreetmap.josm.plugins.osmrec.parsers.TextualStatistics; 42 43 import de.bwaldvogel.liblinear.FeatureNode; 44 import de.bwaldvogel.liblinear.Linear; 45 import de.bwaldvogel.liblinear.Model; 46 import de.bwaldvogel.liblinear.Parameter; 47 import de.bwaldvogel.liblinear.Problem; 48 import de.bwaldvogel.liblinear.SolverType; 44 49 45 50 /** 46 51 * Trains a model by the history of given user. 47 * 52 * 48 53 * @author imis-nkarag 49 54 */ 50 51 public class TrainByUser extends SwingWorker<Void, Void> implements ActionListener{ 52 55 public class TrainByUser extends SwingWorker<Void, Void> implements ActionListener { 56 53 57 private final String inputFilePath; 54 private static Map<String,String> mappings; 55 private static Map<String,Integer> mapperWithIDs; 58 private static Map<String, String> mappings; 59 private static Map<String, Integer> mapperWithIDs; 56 60 private static List<OSMWay> wayList; 57 61 private static List<OSMRelation> relationList; 58 62 59 63 private static Map<String, List<String>> indirectClasses; 60 64 private static Map<String, Integer> indirectClassesWithIDs; 61 //private static List<OntClass> listHierarchy; 62 private static List<String> namesList; 65 private static List<String> namesList; 63 66 private int numberOfTrainingInstances; 64 67 private final String modelDirectoryPath; … … 71 74 private static double foldScore10 = 0; 72 75 private static double bestScore = 0; 73 //int trainProgress = 0;74 76 private final boolean validateFlag; 75 77 private final double cParameterFromUser; … … 78 80 private final int frequency; 79 81 private final boolean topKIsSelected; 80 private String textualListFilePath; 82 private String textualListFilePath; 81 83 private static final boolean USE_CLASS_FEATURES = false; 82 84 private static final boolean USE_RELATION_FEATURES = false; … … 86 88 private final String username; 87 89 private final String inputFileName; 88 89 public TrainByUser(String inputFilePath, String username, boolean validateFlag, double cParameterFromUser, 90 91 public TrainByUser(String inputFilePath, String username, boolean validateFlag, double cParameterFromUser, 90 92 int topK, int frequency, boolean topKIsSelected, LanguageDetector languageDetector, List<OSMWay> wayList) { 91 super(); 92 TrainByUser.languageDetector = languageDetector; 93 this.inputFilePath = inputFilePath; 93 TrainByUser.languageDetector = languageDetector; 94 this.inputFilePath = inputFilePath; 94 95 this.username = username; 95 96 this.validateFlag = validateFlag; … … 99 100 this.topKIsSelected = topKIsSelected; 100 101 System.out.println("find parent directory, create osmrec dir for models: " + new File(inputFilePath).getParentFile()); 101 102 if(System.getProperty("os.name").contains("ux")){ 102 103 if (System.getProperty("os.name").contains("ux")) { 103 104 inputFileName = inputFilePath.substring(inputFilePath.lastIndexOf("/")); 104 } 105 else{ 105 } else { 106 106 inputFileName = inputFilePath.substring(inputFilePath.lastIndexOf("\\")); 107 107 } 108 108 109 109 modelDirectoryPath = new File(inputFilePath).getParentFile() + "/OSMRec_models"; 110 //textualDirectoryPath = new File(inputFilePath).getParentFile() + "/OSMRec_models/textualList.txt"; 111 110 112 111 modelDirectory = new File(modelDirectoryPath); 113 112 114 113 if (!modelDirectory.exists()) { 115 114 modelDirectory.mkdir(); 116 115 System.out.println("model directory created!"); 117 } 118 else { 116 } else { 119 117 System.out.println("directory already exists!"); 120 } 118 } 121 119 TrainByUser.wayList = wayList; 122 120 } 123 121 124 122 @Override 125 public Void doInBackground() throws Exception { 123 public Void doInBackground() throws Exception { 126 124 extractTextualList(); 127 125 parseFiles(); 128 if(validateFlag) {126 if (validateFlag) { 129 127 firePropertyChange("progress", getProgress(), 5); 130 //setProgress(5);131 128 validateLoop(); 132 129 firePropertyChange("progress", getProgress(), 40); 133 //setProgress(40);134 130 System.out.println("Training model with the best c: " + bestConfParam); 135 131 clearDataset(); 136 132 trainModel(bestConfParam); 137 133 firePropertyChange("progress", getProgress(), 60); 138 //setProgress(80);139 134 clearDataset(); 140 135 trainModelWithClasses(bestConfParam); 141 136 firePropertyChange("progress", getProgress(), 100); 142 137 setProgress(100); 143 } 144 else{ 145 146 //firePropertyChange("progress", getProgress(), 5); 147 //setProgress(5); 138 } else { 148 139 clearDataset(); 149 140 firePropertyChange("progress", getProgress(), 10); 150 //setProgress(10);151 141 trainModel(cParameterFromUser); 152 //setProgress(60);153 142 firePropertyChange("progress", getProgress(), 60); 154 143 clearDataset(); 155 144 firePropertyChange("progress", getProgress(), 65); 156 //setProgress(65); 157 trainModelWithClasses(cParameterFromUser); 158 145 trainModelWithClasses(cParameterFromUser); 146 159 147 firePropertyChange("progress", getProgress(), 100); 160 148 setProgress(100); 161 149 System.out.println("done."); 162 150 } 163 System.out.println("Train by user process complete."); 151 System.out.println("Train by user process complete."); 164 152 return null; 165 153 } 166 167 private void extractTextualList(){ 154 155 private void extractTextualList() { 168 156 System.out.println("Running analysis.."); 169 157 //provide top-K … … 171 159 //Keep terms with frequency higher than N 172 160 //Use the remaining terms as training features 173 161 174 162 Analyzer anal = new Analyzer(inputFilePath, languageDetector); 175 163 anal.runAnalysis(); … … 177 165 textualListFilePath = modelDirectory.getAbsolutePath()+"/textualList.txt"; 178 166 File textualFile = new File(textualListFilePath); //decide path of models 179 if(textualFile.exists()){ 167 if (textualFile.exists()) { 180 168 textualFile.delete(); 181 } 169 } 182 170 try { 183 171 textualFile.createNewFile(); 184 172 } catch (IOException ex) { 185 173 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); 186 } 187 174 } 175 188 176 //writeTextualListToFile(textualFilePath, anal.getTopKMostFrequent(15)); 189 177 List<Map.Entry<String, Integer>> textualList; 190 if(topKIsSelected){ 178 if (topKIsSelected) { 191 179 textualList = anal.getTopKMostFrequent(topK); 192 180 System.out.println(textualList); 193 } 194 else{ 181 } else { 195 182 textualList = anal.getWithFrequency(frequency); 196 183 System.out.println(textualList); … … 200 187 System.out.println("textual list saved at location:\n" + textualListFilePath); 201 188 //write list to file and let parser do the loading from the names file 202 189 203 190 //method read default list 204 191 //method extract textual list - > the list will be already in memory, so the names parser doesn t have to be called 205 if(USE_CLASS_FEATURES){ 192 if (USE_CLASS_FEATURES) { 206 193 numberOfFeatures = 1422 + 105 + textualList.size(); 207 } 208 else{ 194 } else { 209 195 numberOfFeatures = 105 + textualList.size(); 210 196 } 211 } 212 197 } 198 213 199 private void parseFiles() { 214 // File tagsToClassesFile = new File(tagsToClassesFilePath);215 216 //osmRecProperties.load(Train.class.getResourceAsStream("/resources/properties/osmRec.properties"));217 200 InputStream tagsToClassesMapping = TrainByUser.class.getResourceAsStream("/resources/files/Map"); 218 201 219 202 Mapper mapper = new Mapper(); 220 try { 203 try { 221 204 mapper.parseFile(tagsToClassesMapping); 222 205 } catch (FileNotFoundException ex) { … … 224 207 } 225 208 mappings = mapper.getMappings(); 226 mapperWithIDs = mapper.getMappingsWithIDs(); 227 //System.out.println("success. mappings empty? " + mappings.isEmpty()); 228 209 mapperWithIDs = mapper.getMappingsWithIDs(); 210 229 211 InputStream ontologyStream = TrainByUser.class.getResourceAsStream("/resources/files/owl.xml"); 230 Ontology ontology = new Ontology(ontologyStream); 212 Ontology ontology = new Ontology(ontologyStream); 231 213 232 214 ontology.parseOntology(); … … 234 216 indirectClasses = ontology.getIndirectClasses(); 235 217 indirectClassesWithIDs = ontology.getIndirectClassesIDs(); 236 //listHierarchy = ontology.getListHierarchy(); 237 238 //InputStream textualFileStream = TrainWorker.class.getResourceAsStream("/resources/files/names"); 239 InputStream textualFileStream = null; 218 219 //InputStream textualFileStream = TrainWorker.class.getResourceAsStream("/resources/files/names"); 220 InputStream textualFileStream = null; 240 221 try { 241 222 textualFileStream = new FileInputStream(new File(textualListFilePath)); … … 243 224 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); 244 225 } 245 246 readTextualFromDefaultList(textualFileStream); 247 226 227 readTextualFromDefaultList(textualFileStream); 228 248 229 //osmParser.parseDocument(); 249 230 //relationList = osmParser.getRelationList(); 250 231 //wayList = osmParser.getWayList(); 251 232 numberOfTrainingInstances = wayList.size(); 252 System.out.println("number of instances: " + numberOfTrainingInstances); 253 System.out.println("end of parsing files."); 254 if(numberOfTrainingInstances == 0){ 255 System.out.println("This user has not edited the loaded area. Cannot train a model!"); 256 } 257 } 258 259 private void validateLoop(){ 260 // Double[] confParams = new Double[] {Math.pow(2, -10), Math.pow(2, -5),Math.pow(2, -3),Math.pow(2, -1), 261 // Math.pow(2, 0),Math.pow(2, 1),Math.pow(2, 2),Math.pow(2, 3),Math.pow(2, 4),Math.pow(2, 5),Math.pow(2, 6), 262 // Math.pow(2, 8),Math.pow(2, 10),Math.pow(2, 12),Math.pow(2, 14),Math.pow(2, 15),Math.pow(2, 16),Math.pow(2, 17)}; 263 264 Double[] confParams = new Double[] {Math.pow(2, -10),Math.pow(2, -10), Math.pow(2, -5),Math.pow(2, -3),Math.pow(2, -1),Math.pow(2, 0)}; 265 233 System.out.println("number of instances: " + numberOfTrainingInstances); 234 System.out.println("end of parsing files."); 235 if (numberOfTrainingInstances == 0) { 236 System.out.println("This user has not edited the loaded area. Cannot train a model!"); 237 } 238 } 239 240 private void validateLoop() { 241 Double[] confParams = new Double[] { 242 Math.pow(2, -10), Math.pow(2, -10), Math.pow(2, -5), Math.pow(2, -3), Math.pow(2, -1), Math.pow(2, 0)}; 243 266 244 double bestC = Math.pow(2, -10); 267 268 for(Double param : confParams){ 269 //setProgress(4*((5*(trainProgress++))/confParams.length)); 245 246 for (Double param : confParams) { 270 247 foldScore1 = 0; 271 248 foldScore5 = 0; … … 273 250 System.out.println("\n\n\nrunning for C = " + param); 274 251 clearDataset(); 275 System.out.println("fold1"); 276 //crossValidateFold(0, 3, 3, 4, false, param); 252 System.out.println("fold1"); 277 253 crossValidateFold(0, 4, 4, 5, false, param); //4-1 278 //setProgress(4*((5*(trainProgress++))/confParams.length)); 279 //System.out.println((5*trainProgress)/confParams.length); 280 254 281 255 foldScore1 = foldScore1 + score1; 282 256 foldScore5 = foldScore5 + score5; … … 284 258 clearDataset(); 285 259 System.out.println("fold2"); 286 //crossValidateFold(1, 4, 4, 5, false, param);287 260 crossValidateFold(1, 5, 0, 1, false, param); 288 //setProgress(4*((5*(trainProgress++))/confParams.length)); 289 //System.out.println((5*trainProgress)/confParams.length); 290 261 291 262 foldScore1 = foldScore1 + score1; 292 263 foldScore5 = foldScore5 + score5; … … 294 265 clearDataset(); 295 266 System.out.println("fold3"); 296 //crossValidateFold(2, 5, 0, 1, false, param);297 267 crossValidateFold(0, 5, 1, 2, true, param); 298 //setProgress(4*((5*(trainProgress++))/confParams.length)); 299 //System.out.println((5*trainProgress)/confParams.length); 300 268 301 269 foldScore1 = foldScore1 + score1; 302 270 foldScore5 = foldScore5 + score5; … … 304 272 clearDataset(); 305 273 System.out.println("fold4"); 306 //crossValidateFold(0, 5, 1, 2, true, param);307 274 crossValidateFold(0, 5, 2, 3, true, param); 308 //setProgress(4*((5*(trainProgress++))/confParams.length)); 309 //System.out.println((5*trainProgress)/confParams.length); 310 275 311 276 foldScore1 = foldScore1 + score1; 312 277 foldScore5 = foldScore5 + score5; … … 314 279 clearDataset(); 315 280 System.out.println("fold5"); 316 //crossValidateFold(0, 5, 2, 3, true, param);317 281 crossValidateFold(0, 5, 3, 4, true, param); 318 //setProgress(4*((5*(trainProgress++))/confParams.length)); 319 //System.out.println((5*trainProgress)/confParams.length); 320 282 321 283 foldScore1 = foldScore1 + score1; 322 284 foldScore5 = foldScore5 + score5; 323 285 foldScore10 = foldScore10 + score10; 324 286 System.out.println("\n\nC=" + param + ", average score 1-5-10: " + foldScore1/5 +" "+ foldScore5/5 + " "+ foldScore10/5); 325 if(bestScore < foldScore1 ){287 if (bestScore < foldScore1) { 326 288 bestScore = foldScore1; 327 289 bestC = param; 328 290 } 329 330 } 331 //System.out.println(4*((5*(trainProgress++))/confParams.length)); 332 //setProgress(100); 291 292 } 333 293 bestConfParam = bestC; 334 System.out.println("best c param= " + bestC + ", score: " + bestScore/5 335 } 336 337 private void crossValidateFold(int a, int b, int c, int d, boolean skip, double param){ 294 System.out.println("best c param= " + bestC + ", score: " + bestScore/5); 295 } 296 297 private void crossValidateFold(int a, int b, int c, int d, boolean skip, double param) { 338 298 System.out.println("Starting cross validation"); 339 299 int testSize = wayList.size()/5; 340 300 341 List<OSMWay> trainList= new ArrayList<>(); 342 for(int g = a*testSize; g<b*testSize; g++){ // 0~~1~~2~~3~~4~~5 343 if(skip){ 344 if(g == (c)*testSize){g=(c+1)*testSize;} 345 346 } 347 trainList.add(wayList.get(g)); 348 } 301 List<OSMWay> trainList = new ArrayList<>(); 302 for (int g = a*testSize; g < b*testSize; g++) { // 0~~1~~2~~3~~4~~5 303 if (skip) { 304 if (g == (c)*testSize) { 305 g = (c+1)*testSize; 306 } 307 } 308 trainList.add(wayList.get(g)); 309 } 349 310 350 311 int wayListSizeWithoutUnclassified = trainList.size(); 351 int u = 0;352 312 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 353 313 354 314 //set classes for each osm instance 355 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 356 for(OSMWay way : trainList){ 315 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 316 for (OSMWay way : trainList) { 357 317 358 318 OSMClassification classifyInstances = new OSMClassification(); 359 319 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 360 320 361 if(way.getClassIDs().isEmpty()){ 321 if (way.getClassIDs().isEmpty()) { 362 322 wayListSizeWithoutUnclassified -= 1; 363 u++; 364 } 365 else { 323 } else { 366 324 sizeToBeAddedToArray = sizeToBeAddedToArray + way.getClassIDs().size()-1; 367 } 368 } 369 double C = param; 325 } 326 } 327 double C = param; 370 328 double eps = 0.001; 371 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; //new double[117558];//372 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; //working[3812];373 int k = 0;374 375 for(OSMWay way : trainList){ 376 //adding multiple vectors 377 int id; 378 if(USE_CLASS_FEATURES) {329 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; 330 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; 331 int k = 0; 332 333 for (OSMWay way : trainList) { 334 //adding multiple vectors 335 int id; 336 if (USE_CLASS_FEATURES) { 379 337 ClassFeatures class_vector = new ClassFeatures(); 380 338 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 381 339 id = 1422; 382 } 383 else{ 340 } else { 384 341 id = 1; 385 342 } … … 390 347 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 391 348 //id = 1526 392 if(USE_RELATION_FEATURES){ 349 if (USE_RELATION_FEATURES) { 393 350 RelationFeatures relationFeatures = new RelationFeatures(id); 394 relationFeatures.createRelationFeatures(way, relationList); 351 relationFeatures.createRelationFeatures(way, relationList); 395 352 id = relationFeatures.getLastID(); 396 } 397 else { 353 } else { 398 354 id = geometryFeatures.getLastID(); 399 355 } 400 356 //id 1531 401 357 402 358 TextualFeatures textualFeatures; 403 if(USE_TEXTUAL_FEATURES){ 359 if (USE_TEXTUAL_FEATURES) { 404 360 textualFeatures = new TextualFeatures(id, namesList, languageDetector); 405 361 textualFeatures.createTextualFeatures(way); 406 362 } 407 363 408 364 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); 409 365 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 410 411 if(!way.getClassIDs().isEmpty()){ 412 int i =0; 413 for(FeatureNode featureNode : featureNodeList){ 366 367 if (!way.getClassIDs().isEmpty()) { 368 int i = 0; 369 for (FeatureNode featureNode : featureNodeList) { 414 370 featureNodeArray[i] = featureNode; 415 371 i++; 416 } 417 for(int classID : way.getClassIDs()){ 372 } 373 for (int classID : way.getClassIDs()) { 418 374 trainingSetWithUnknown2[k] = featureNodeArray; 419 375 GROUPS_ARRAY2[k] = classID; 420 k++; 421 } 422 } 423 } 424 425 //Linear.enableDebugOutput();426 Problemproblem= new Problem();427 problem. l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray;//wayListSizeWithoutUnclassified;//wayList.size();428 problem.n = numberOfFeatures;//(geometry 105 + textual //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes376 k++; 377 } 378 } 379 } 380 381 Problem problem = new Problem(); 382 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray; 383 problem.n = numberOfFeatures; 384 //(geometry 105 + textual //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 429 385 problem.x = trainingSetWithUnknown2; // feature nodes 430 386 problem.y = GROUPS_ARRAY2; // target values 431 //SolverType solver = SolverType.MCSVM_CS; //Cramer and Singer for multiclass classification - equivalent of SVMlight432 387 SolverType solver2 = SolverType.getById(2); //2 -- L2-regularized L2-loss support vector classification (primal) 433 388 434 389 Parameter parameter = new Parameter(solver2, C, eps); 435 //System.out.println("param set ok"); 436 //System.out.println("number of features: " + vc.getNumOfFeatures()); 437 390 438 391 long start = System.nanoTime(); 439 392 System.out.println("training..."); 440 393 PrintStream original = System.out; 441 394 System.setOut(new PrintStream(new OutputStream() { 442 @Override 443 public void write(int arg0) throws IOException { 444 445 } 395 @Override 396 public void write(int arg0) throws IOException { 397 398 } 446 399 })); 447 400 448 401 Model model = Linear.train(problem, parameter); 449 402 long end = System.nanoTime(); … … 451 404 System.setOut(original); 452 405 System.out.println("training process completed in: " + NANOSECONDS.toSeconds(elapsedTime) + " seconds."); 453 //System.out.println("trained"); 454 455 //decide model path and naming and/or way of deleting/creating 1 or more models. 456 //File inFile = new File(inputFilePath).getParentFile(); 457 406 407 //decide model path and naming and/or way of deleting/creating 1 or more models. 408 458 409 File modelFile; 459 if(USE_CLASS_FEATURES){ 410 if (USE_CLASS_FEATURES) { 460 411 modelFile = new File(modelDirectory.getAbsolutePath()+"/user_" + username + "_model_with_classes_c=" + param); 461 } 462 else{ 412 } else { 463 413 modelFile = new File(modelDirectory.getAbsolutePath()+"/user_" + username + "_model_geometries_textual_c=" + param); 464 414 } 465 466 if(modelFile.exists()){ 415 416 if (modelFile.exists()) { 467 417 modelFile.delete(); 468 } 418 } 469 419 try { 470 420 //System.out.println("file created"); 471 421 model.save(modelFile); 472 //System.out.println("saved"); 422 //System.out.println("saved"); 473 423 } catch (IOException ex) { 474 424 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); 475 425 } 476 426 477 //test set 478 List<OSMWay> testList= new ArrayList<>(); 479 for(int g = c*testSize; g <d*testSize; g++){427 //test set 428 List<OSMWay> testList = new ArrayList<>(); 429 for (int g = c*testSize; g < d*testSize; g++) { 480 430 testList.add(wayList.get(g)); 481 //liblinear test 431 //liblinear test 482 432 } 483 433 System.out.println("testList size: " + testList.size()); 484 int succededInstances =0;485 int succededInstances5 =0;486 int succededInstances10 =0;434 int succededInstances = 0; 435 int succededInstances5 = 0; 436 int succededInstances10 = 0; 487 437 try { 488 438 model = Model.load(modelFile); … … 493 443 int[] labels = model.getLabels(); 494 444 Map<Integer, Integer> mapLabelsToIDs = new HashMap<>(); 495 for (int h =0; h < model.getLabels().length; h++){445 for (int h = 0; h < model.getLabels().length; h++) { 496 446 mapLabelsToIDs.put(labels[h], h); 497 //System.out.println(h + " <-> " + labels[h]); 498 } 499 447 } 448 500 449 int wayListSizeWithoutUnclassified2 = testList.size(); 501 for(OSMWay way : testList){ 450 for (OSMWay way : testList) { 502 451 503 452 OSMClassification classifyInstances = new OSMClassification(); 504 453 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 505 if(way.getClassIDs().isEmpty()){ 506 //System.out.println("found unclassified" + way.getClassIDs() + "class: " +way.getClassID()); 454 if (way.getClassIDs().isEmpty()) { 507 455 wayListSizeWithoutUnclassified2 -= 1; 508 //u++; 509 } 510 } 511 456 } 457 } 458 512 459 FeatureNode[] testInstance2; 513 for(OSMWay way : testList){ 514 460 for (OSMWay way : testList) { 461 515 462 int id; 516 517 if(USE_CLASS_FEATURES) {463 464 if (USE_CLASS_FEATURES) { 518 465 ClassFeatures class_vector = new ClassFeatures(); 519 466 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 520 467 id = 1422; 521 } 522 else{ 468 } else { 523 469 id = 1; 524 470 } 525 471 526 472 //pass id also: 1422 if using classes, 1 if not 527 473 GeometryFeatures geometryFeatures = new GeometryFeatures(id); … … 530 476 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 531 477 //id = 1526 532 //System.out.println("id 1526 -> " + geometryFeatures.getLastID()); 533 if(USE_RELATION_FEATURES){ 478 if (USE_RELATION_FEATURES) { 534 479 RelationFeatures relationFeatures = new RelationFeatures(id); 535 relationFeatures.createRelationFeatures(way, relationList); 480 relationFeatures.createRelationFeatures(way, relationList); 536 481 id = relationFeatures.getLastID(); 537 } 538 else { 482 } else { 539 483 id = geometryFeatures.getLastID(); 540 //System.out.println("geom feat " + id);541 484 } 542 485 //id 1531 543 //System.out.println("id 1532 -> " + relationFeatures.getLastID()); 544 if(USE_TEXTUAL_FEATURES){ 486 if (USE_TEXTUAL_FEATURES) { 545 487 TextualFeatures textualFeatures = new TextualFeatures(id, namesList, languageDetector); 546 488 textualFeatures.createTextualFeatures(way); 547 //System.out.println("last textual id: " + textualFeatures.getLastID());548 //System.out.println("full: " + way.getFeatureNodeList());549 }550 else{551 552 489 } 553 490 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); … … 555 492 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 556 493 557 int i =0; 558 for(FeatureNode featureNode : featureNodeList){ 494 int i = 0; 495 for (FeatureNode featureNode : featureNodeList) { 559 496 featureNodeArray[i] = featureNode; 560 497 i++; … … 562 499 563 500 testInstance2 = featureNodeArray; 564 //double prediction = Linear.predict(model, testInstance2);565 //System.out.println("test prediction: " + prediction);566 501 double[] scores = new double[modelLabelSize]; 567 502 Linear.predictValues(model, testInstance2, scores); … … 570 505 //iter scores and find 10 max values with their indexes first. then ask those indexes from model.getlabels 571 506 Map<Double, Integer> scoresValues = new HashMap<>(); 572 for(int h = 0; h < scores.length; h++){ 573 scoresValues.put(scores[h], h); 507 for (int h = 0; h < scores.length; h++) { 508 scoresValues.put(scores[h], h); 574 509 //System.out.println(h + " <-> " + scores[h]); 575 } 510 } 576 511 577 512 Arrays.sort(scores); … … 582 517 //System.out.println("way labels: " + way.getClassIDs()); 583 518 //System.out.println("test prediction: " + prediction); 584 if(way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])])){ 585 succededInstances++; 586 } 587 if( 588 589 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 590 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 591 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 592 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 593 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) 594 ){519 if (way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])])) { 520 succededInstances++; 521 } 522 if ( 523 524 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 525 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 526 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 527 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 528 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) 529 ) { 595 530 succededInstances5++; 596 531 } 597 if( 598 599 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 600 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 601 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 602 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 603 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) || 604 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-6])]) || 605 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-7])]) || 606 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-8])]) || 607 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-9])]) || 608 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-10])]) 609 ){532 if ( 533 534 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-1])]) || 535 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-2])]) || 536 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-3])]) || 537 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-4])]) || 538 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-5])]) || 539 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-6])]) || 540 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-7])]) || 541 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-8])]) || 542 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-9])]) || 543 way.getClassIDs().contains(labels[scoresValues.get(scores[scores.length-10])]) 544 ) { 610 545 succededInstances10++; 611 } 612 //System.out.println("labels: " + Arrays.toString(model.getLabels())); 613 //System.out.println("label[0]: " + model.getLabels()[0]); 614 } 615 546 } 547 } 548 616 549 System.out.println("Succeeded " + succededInstances + " of " + testList.size() + " total (1 class prediction)"); 617 double precision1 = succededInstances/(double)wayListSizeWithoutUnclassified2; 550 double precision1 = succededInstances/(double) wayListSizeWithoutUnclassified2; 618 551 score1 = precision1; 619 552 System.out.println(precision1); 620 553 621 554 System.out.println("Succeeded " + succededInstances5 + " of " + testList.size()+ " total (5 class prediction)"); 622 double precision5 = succededInstances5/(double)wayListSizeWithoutUnclassified2; 555 double precision5 = succededInstances5/(double) wayListSizeWithoutUnclassified2; 623 556 score5 = precision5; 624 557 System.out.println(precision5); 625 558 626 559 System.out.println("Succeeded " + succededInstances10 + " of " + testList.size()+ " total (10 class prediction)"); 627 double precision10 = succededInstances10/(double)wayListSizeWithoutUnclassified2; 560 double precision10 = succededInstances10/(double) wayListSizeWithoutUnclassified2; 628 561 score10 = precision10; 629 System.out.println(precision10); 630 } 631 632 private void trainModel(double param){ 633 562 System.out.println(precision10); 563 } 564 565 private void trainModel(double param) { 566 634 567 int wayListSizeWithoutUnclassified = wayList.size(); 635 int u = 0;636 568 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 637 if(wayListSizeWithoutUnclassified == 0){ 569 if (wayListSizeWithoutUnclassified == 0) { 638 570 System.out.println("aborting training process.."); 639 571 return; 640 572 } 641 573 //set classes for each osm instance 642 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 643 644 //System.out.println("starting classify instances"); 645 for(OSMWay way : wayList){ 646 //setProgress(trainProgress++); 574 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 575 576 for (OSMWay way : wayList) { 647 577 OSMClassification classifyInstances = new OSMClassification(); 648 578 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 649 579 650 if(way.getClassIDs().isEmpty()){ 580 if (way.getClassIDs().isEmpty()) { 651 581 wayListSizeWithoutUnclassified -= 1; 652 u++; 653 } 654 else { 582 } else { 655 583 sizeToBeAddedToArray = sizeToBeAddedToArray + way.getClassIDs().size()-1; 656 } 657 } 584 } 585 } 658 586 //System.out.println("end classify instances"); 659 double C = param; 587 double C = param; 660 588 double eps = 0.001; 661 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray];//new double[117558];// 662 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; //working[3812]; 663 int k =0; 664 665 //setProgress(trainProgress+5); 666 for(OSMWay way : wayList){ 667 //adding multiple vectors 668 int id; 669 if(USE_CLASS_FEATURES){ 589 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; 590 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures]; 591 int k = 0; 592 593 for (OSMWay way : wayList) { 594 //adding multiple vectors 595 int id; 596 if (USE_CLASS_FEATURES) { 670 597 ClassFeatures class_vector = new ClassFeatures(); 671 598 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 672 599 id = 1422; 673 } 674 else{ 600 } else { 675 601 id = 1; 676 602 } … … 681 607 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 682 608 //id = 1526 683 if(USE_RELATION_FEATURES){ 609 if (USE_RELATION_FEATURES) { 684 610 RelationFeatures relationFeatures = new RelationFeatures(id); 685 relationFeatures.createRelationFeatures(way, relationList); 611 relationFeatures.createRelationFeatures(way, relationList); 686 612 id = relationFeatures.getLastID(); 687 } 688 else { 613 } else { 689 614 id = geometryFeatures.getLastID(); 690 615 } 691 616 //id 1531 692 617 693 618 TextualFeatures textualFeatures; 694 if(USE_TEXTUAL_FEATURES){ 695 textualFeatures = new TextualFeatures(id, namesList,languageDetector); 619 if (USE_TEXTUAL_FEATURES) { 620 textualFeatures = new TextualFeatures(id, namesList, languageDetector); 696 621 textualFeatures.createTextualFeatures(way); 697 622 } 698 623 699 624 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); 700 625 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 701 702 if(!way.getClassIDs().isEmpty()){ 703 int i =0; 704 for(FeatureNode featureNode : featureNodeList){ 626 627 if (!way.getClassIDs().isEmpty()) { 628 int i = 0; 629 for (FeatureNode featureNode : featureNodeList) { 705 630 featureNodeArray[i] = featureNode; 706 631 i++; 707 } 708 for(int classID : way.getClassIDs()){ 632 } 633 for (int classID : way.getClassIDs()) { 709 634 trainingSetWithUnknown2[k] = featureNodeArray; 710 635 GROUPS_ARRAY2[k] = classID; 711 k++; 712 } 713 } 714 } 715 //System.out.println("feature construction completed."); 716 //Linear.enableDebugOutput(); 717 Problem problem = new Problem(); 718 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray;//wayListSizeWithoutUnclassified;//wayList.size(); 719 problem.n = numberOfFeatures;//3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 636 k++; 637 } 638 } 639 } 640 Problem problem = new Problem(); 641 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray; 642 problem.n = numberOfFeatures; 643 //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 720 644 problem.x = trainingSetWithUnknown2; // feature nodes 721 645 problem.y = GROUPS_ARRAY2; // target values 722 //SolverType solver = SolverType.MCSVM_CS; //Cramer and Singer for multiclass classification - equivalent of SVMlight723 646 SolverType solver2 = SolverType.getById(2); //2 -- L2-regularized L2-loss support vector classification (primal) 724 647 725 648 Parameter parameter = new Parameter(solver2, C, eps); 726 //System.out.println("param set ok"); 727 //System.out.println("number of features: " + vc.getNumOfFeatures()); 728 649 729 650 long start = System.nanoTime(); 730 651 System.out.println("training..."); 731 652 PrintStream original = System.out; 732 653 System.setOut(new PrintStream(new OutputStream() { 733 734 @Override 735 public void write(int arg0) throws IOException { 736 737 } 738 })); 739 740 654 @Override 655 public void write(int arg0) throws IOException { 656 657 } 658 })); 659 660 741 661 Model model = Linear.train(problem, parameter); 742 662 long end = System.nanoTime(); … … 744 664 System.setOut(original); 745 665 System.out.println("training process completed in: " + NANOSECONDS.toSeconds(elapsedTime) + " seconds."); 746 //System.out.println("trained"); 747 748 //decide model path and naming and/or way of deleting/creating 1 or more models. 749 //File inFile = new File(inputFilePath).getParentFile(); 750 666 667 //decide model path and naming and/or way of deleting/creating 1 or more models. 668 751 669 File modelFile = new File(modelDirectory.getAbsolutePath()+"/best_model"); //decide path of models 752 670 753 671 File customModelFile; 754 if(topKIsSelected){ 755 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_topK" + topK + "user" + username + ".0"); 756 } 757 else{ 758 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + "user" + username + ".0"); 759 } 760 761 if(modelFile.exists()){ 672 if (topKIsSelected) { 673 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + 674 inputFileName + "_model_c" + param + "_topK" + topK + "user" + username + ".0"); 675 } else { 676 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + 677 inputFileName + "_model_c" + param + "_maxF" + frequency + "user" + username + ".0"); 678 } 679 680 if (modelFile.exists()) { 762 681 modelFile.delete(); 763 } 764 if(customModelFile.exists()){ 682 } 683 if (customModelFile.exists()) { 765 684 customModelFile.delete(); 766 } 685 } 767 686 try { 768 //System.out.println("file created");769 687 model.save(modelFile); 770 688 model.save(customModelFile); 771 System.out.println("model saved at: " + modelFile); 689 System.out.println("model saved at: " + modelFile); 772 690 System.out.println("custom model saved at: " + customModelFile); 773 691 } catch (IOException ex) { 774 692 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); 775 } 776 } 777 778 private void trainModelWithClasses(double param){ 779 693 } 694 } 695 696 private void trainModelWithClasses(double param) { 697 780 698 int wayListSizeWithoutUnclassified = wayList.size(); 781 int u = 0;782 699 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 783 784 if(wayListSizeWithoutUnclassified == 0){ 700 701 if (wayListSizeWithoutUnclassified == 0) { 785 702 System.out.println("aborting training process with classes.."); 786 703 return; 787 704 } 788 705 //set classes for each osm instance 789 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 790 //int lalala = 0; 791 //setProgress(trainProgress-10); 792 for(OSMWay way : wayList){ 793 //setProgress(trainProgress++); 706 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size 707 for (OSMWay way : wayList) { 794 708 OSMClassification classifyInstances = new OSMClassification(); 795 709 classifyInstances.calculateClasses(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 796 710 797 if(way.getClassIDs().isEmpty()){ 711 if (way.getClassIDs().isEmpty()) { 798 712 wayListSizeWithoutUnclassified -= 1; 799 u++; 800 } 801 else { 713 } else { 802 714 sizeToBeAddedToArray = sizeToBeAddedToArray + way.getClassIDs().size()-1; 803 } 804 } 805 double C = param; 715 } 716 } 717 double C = param; 806 718 double eps = 0.001; 807 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray];//new double[117558];// 808 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures+1422]; //working[3812]; 809 int k =0; 810 811 //setProgress(trainProgress+5); 812 for(OSMWay way : wayList){ 813 //adding multiple vectors 814 int id; 815 //if(USE_CLASS_FEATURES){ 816 ClassFeatures class_vector = new ClassFeatures(); 817 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 818 id = 1422; 819 // } 820 // else{ 821 // id = 1; 822 // } 719 double[] GROUPS_ARRAY2 = new double[wayListSizeWithoutUnclassified+sizeToBeAddedToArray]; 720 FeatureNode[][] trainingSetWithUnknown2 = new FeatureNode[wayListSizeWithoutUnclassified+sizeToBeAddedToArray][numberOfFeatures+1422]; 721 int k = 0; 722 723 for (OSMWay way : wayList) { 724 //adding multiple vectors 725 int id; 726 ClassFeatures class_vector = new ClassFeatures(); 727 class_vector.createClassFeatures(way, mappings, mapperWithIDs, indirectClasses, indirectClassesWithIDs); 728 id = 1422; 823 729 //pass id also: 1422 if using classes, 1 if not 824 730 GeometryFeatures geometryFeatures = new GeometryFeatures(id); … … 827 733 //id after geometry, cases: all geometry features with mean-variance boolean intervals: 828 734 //id = 1526 829 if(USE_RELATION_FEATURES){ 735 if (USE_RELATION_FEATURES) { 830 736 RelationFeatures relationFeatures = new RelationFeatures(id); 831 relationFeatures.createRelationFeatures(way, relationList); 737 relationFeatures.createRelationFeatures(way, relationList); 832 738 id = relationFeatures.getLastID(); 833 } 834 else { 739 } else { 835 740 id = geometryFeatures.getLastID(); 836 741 } 837 742 //id 1531 838 743 839 744 TextualFeatures textualFeatures; 840 if(USE_TEXTUAL_FEATURES){ 841 textualFeatures = new TextualFeatures(id, namesList,languageDetector); 745 if (USE_TEXTUAL_FEATURES) { 746 textualFeatures = new TextualFeatures(id, namesList, languageDetector); 842 747 textualFeatures.createTextualFeatures(way); 843 748 } 844 749 845 750 List<FeatureNode> featureNodeList = way.getFeatureNodeList(); 846 751 FeatureNode[] featureNodeArray = new FeatureNode[featureNodeList.size()]; 847 848 if(!way.getClassIDs().isEmpty()){ 849 int i =0; 850 for(FeatureNode featureNode : featureNodeList){ 752 753 if (!way.getClassIDs().isEmpty()) { 754 int i = 0; 755 for (FeatureNode featureNode : featureNodeList) { 851 756 featureNodeArray[i] = featureNode; 852 757 i++; 853 } 854 for(int classID : way.getClassIDs()){ 855 //lalala++; 758 } 759 for (int classID : way.getClassIDs()) { 856 760 trainingSetWithUnknown2[k] = featureNodeArray; 857 761 GROUPS_ARRAY2[k] = classID; 858 k++; 859 } 860 } 861 } 862 863 //Linear.enableDebugOutput();864 Problemproblem= new Problem();865 problem. l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray;//wayListSizeWithoutUnclassified;//wayList.size();866 problem.n = numberOfFeatures+1422;//3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes762 k++; 763 } 764 } 765 } 766 767 Problem problem = new Problem(); 768 problem.l = wayListSizeWithoutUnclassified+sizeToBeAddedToArray; 769 problem.n = numberOfFeatures+1422; 770 //3797; // number of features //the largest index of all features //3811;//3812 //1812 with classes 867 771 problem.x = trainingSetWithUnknown2; // feature nodes 868 772 problem.y = GROUPS_ARRAY2; // target values 869 //SolverType solver = SolverType.MCSVM_CS; //Cramer and Singer for multiclass classification - equivalent of SVMlight870 773 SolverType solver2 = SolverType.getById(2); //2 -- L2-regularized L2-loss support vector classification (primal) 871 774 872 775 Parameter parameter = new Parameter(solver2, C, eps); 873 //System.out.println("param set ok"); 874 //System.out.println("number of features: " + vc.getNumOfFeatures()); 875 776 876 777 long start = System.nanoTime(); 877 778 System.out.println("training..."); 878 779 PrintStream original = System.out; 879 780 System.setOut(new PrintStream(new OutputStream() { 880 881 @Override 882 public void write(int arg0) throws IOException { 883 884 } 885 })); 886 887 781 @Override 782 public void write(int arg0) throws IOException { 783 784 } 785 })); 786 888 787 Model model = Linear.train(problem, parameter); 889 788 long end = System.nanoTime(); … … 891 790 System.setOut(original); 892 791 System.out.println("training process completed in: " + NANOSECONDS.toSeconds(elapsedTime) + " seconds."); 893 //System.out.println("trained"); 894 895 //decide model path and naming and/or way of deleting/creating 1 or more models. 896 //File inFile = new File(inputFilePath).getParentFile(); 897 898 File modelFile = new File(modelDirectory.getAbsolutePath()+"/model_with_classes"); 792 793 //decide model path and naming and/or way of deleting/creating 1 or more models. 794 795 File modelFile = new File(modelDirectory.getAbsolutePath()+"/model_with_classes"); 899 796 File customModelFile; 900 if(topKIsSelected){ 901 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_topK" + topK + "user" + username + ".1"); 902 } 903 else{ 904 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + "user" + username + ".1"); 905 } 906 907 if(customModelFile.exists()){ 797 if (topKIsSelected) { 798 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + 799 inputFileName + "_model_c" + param + "_topK" + topK + "user" + username + ".1"); 800 } else { 801 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + 802 inputFileName + "_model_c" + param + "_maxF" + frequency + "user" + username + ".1"); 803 } 804 805 if (customModelFile.exists()) { 908 806 customModelFile.delete(); 909 } 910 if(modelFile.exists()){ 807 } 808 if (modelFile.exists()) { 911 809 modelFile.delete(); 912 } 810 } 913 811 try { 914 //System.out.println("file created");915 812 model.save(modelFile); 916 813 model.save(customModelFile); 917 System.out.println("model with classes saved at: " + modelFile); 814 System.out.println("model with classes saved at: " + modelFile); 918 815 System.out.println("custom model with classes saved at: " + customModelFile); 919 816 } catch (IOException ex) { 920 817 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); 921 } 922 } 923 924 private static void clearDataset(){ 925 for(OSMWay way : wayList){ 818 } 819 } 820 821 private static void clearDataset() { 822 for (OSMWay way : wayList) { 926 823 way.getFeatureNodeList().clear(); 927 824 } 928 } 929 825 } 826 930 827 private void readTextualFromDefaultList(InputStream textualFileStream) { 931 828 932 829 TextualStatistics textualStatistics = new TextualStatistics(); 933 830 textualStatistics.parseTextualList(textualFileStream); 934 831 namesList = textualStatistics.getTextualList(); 935 936 } 937 938 private static void writeTextualListToFile(String filePath, List<Map.Entry<String, Integer>> textualList) 939 { 940 941 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"))) { 942 for(Map.Entry<String, Integer> entry : textualList){ 832 833 } 834 835 private static void writeTextualListToFile(String filePath, List<Map.Entry<String, Integer>> textualList) { 836 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"))) { 837 for (Map.Entry<String, Integer> entry : textualList) { 943 838 writer.write(entry.getKey()); 944 839 writer.newLine(); … … 951 846 } catch (IOException ex) { 952 847 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); 953 } 954 } 955 848 } 849 } 850 956 851 @Override 957 852 protected void done() { … … 960 855 firePropertyChange("progress", getProgress(), 100); 961 856 setProgress(100); 962 } 963 catch (InterruptedException | ExecutionException ignore) { 857 } catch (InterruptedException | ExecutionException ignore) { 964 858 System.out.println("Exception: " + ignore); 965 859 } 966 } 967 860 } 861 968 862 @Override 969 863 public void actionPerformed(ActionEvent ae) { 970 864 //cancel button, end process after clearing Dataset 971 } 865 } 972 866 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/personalization/UserDataExtractAndTrainWorker.java
r31441 r32404 1 // License: GPL. For details, see LICENSE file. 1 2 package org.openstreetmap.josm.plugins.osmrec.personalization; 2 3 … … 11 12 import java.util.List; 12 13 import java.util.TimeZone; 14 13 15 import javax.swing.SwingWorker; 14 import org.openstreetmap.josm.plugins.container.OSMWay; 15 import org.openstreetmap.josm.plugins.extractor.LanguageDetector; 16 import org.openstreetmap.josm.plugins.parsers.OSMParser; 16 17 import org.openstreetmap.josm.plugins.osmrec.container.OSMWay; 18 import org.openstreetmap.josm.plugins.osmrec.extractor.LanguageDetector; 19 import org.openstreetmap.josm.plugins.osmrec.parsers.OSMParser; 17 20 18 21 /** 19 22 * Extracts user information about history edits or by area and initiates the training process based on that user. 20 * 23 * 21 24 * @author imis-nkarag 22 25 */ 23 26 24 public class UserDataExtractAndTrainWorker extends SwingWorker<Void, Void> implements ActionListener{ 25 27 public class UserDataExtractAndTrainWorker extends SwingWorker<Void, Void> implements ActionListener { 28 26 29 private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; 27 30 private static final ArrayList<String> timeIntervals = new ArrayList<>(); … … 38 41 private final LanguageDetector languageDetector; 39 42 private int userTrainProgress = 0; 40 41 public UserDataExtractAndTrainWorker(String inputFilePath, String username, Integer days, boolean byArea, boolean validateFlag, 42 double cParameterFromUser, int topK, int frequency, boolean topKIsSelected, LanguageDetector languageDetector){ 43 43 44 public UserDataExtractAndTrainWorker(String inputFilePath, String username, Integer days, boolean byArea, boolean validateFlag, 45 double cParameterFromUser, int topK, int frequency, boolean topKIsSelected, LanguageDetector languageDetector) { 46 44 47 this.inputFilePath = inputFilePath; 45 48 this.username = username; … … 50 53 this.topK = topK; 51 54 this.frequency = frequency; 52 this.topKIsSelected = topKIsSelected; 55 this.topKIsSelected = topKIsSelected; 53 56 this.languageDetector = languageDetector; 54 57 55 58 } 56 59 57 60 @Override 58 61 protected Void doInBackground() throws Exception { 59 62 60 63 System.out.println("UserDataExtractAndTrainWorker doInBackground initiating.."); 61 if(byArea){ 62 extractByArea(); 64 if (byArea) { 65 extractByArea(); 66 } else { 67 extractHistory(); 63 68 } 64 else{ 65 extractHistory(); 66 } 67 68 TrainByUser trainByUser = new TrainByUser(inputFilePath, username, validateFlag, 69 70 TrainByUser trainByUser = new TrainByUser(inputFilePath, username, validateFlag, 69 71 cParameterFromUser, topK, frequency, topKIsSelected, languageDetector, wayList); 70 72 71 73 System.out.println("trainByUser executing.."); 72 74 trainByUser.addPropertyChangeListener(new PropertyChangeListener() { 73 74 @Override 75 76 @Override 75 77 public void propertyChange(PropertyChangeEvent evt) { 76 if("progress".equals(evt.getPropertyName())) { 78 if ("progress".equals(evt.getPropertyName())) { 77 79 int progress = (Integer) evt.getNewValue(); 78 80 System.out.println("progress++ from property change listener, progress: " + progress); 79 81 setProgress(progress); 80 82 81 83 } 82 } 83 }); 84 85 86 87 88 84 } 85 }); 86 89 87 trainByUser.doInBackground(); 90 88 setProgress(100); 91 89 return null; 92 90 } 93 91 94 92 private void extractHistory() { 95 if(wayList != null){ 93 if (wayList != null) { 96 94 wayList.clear(); 97 95 } 98 96 99 97 produceTimeIntervals(days); 100 98 HistoryParser historyParser = new HistoryParser(username); 101 102 for(String time : timeIntervals){ 99 100 for (String time : timeIntervals) { 103 101 System.out.println("interval\n " + time); 104 102 historyParser.historyParse(time); … … 106 104 wayList = historyParser.getWayList(); 107 105 } 108 106 109 107 private void extractByArea() { 110 108 System.out.println("Extracting by Area.."); 111 109 112 if(wayList != null){ 110 if (wayList != null) { 113 111 wayList.clear(); 114 112 } 115 //System.out.println("wayList cleared."); 116 113 117 114 OSMParser osmParser = new OSMParser(inputFilePath); 118 115 osmParser.parseDocument(); 119 116 120 //System.out.println("OSMParser done.");121 122 117 List<OSMWay> completeWayList = osmParser.getWayList(); 123 118 System.out.println("completeWayList size: " + completeWayList.size()); 124 119 System.out.println("populating wayList with edits from username: " + username); 125 for(OSMWay way : completeWayList){ 120 for (OSMWay way : completeWayList) { 126 121 //System.out.println("current way user: " + way.getUser()); 127 if(way.getUser().equals(username)){ 122 if (way.getUser().equals(username)) { 128 123 System.out.println("found user edit!"); 129 124 wayList.add(way); 130 } 131 } 125 } 126 } 132 127 System.out.println("weeding wayList by user done."); 133 if(wayList.isEmpty()){ 128 if (wayList.isEmpty()) { 134 129 System.out.println("User has not edited this Area. Try \"By time\" option."); 135 } 136 else{ 130 } else { 137 131 System.out.println("User has edited " + wayList.size() + " OSM entities in this area."); 138 132 } 139 } 140 133 } 134 141 135 private void produceTimeIntervals(Integer days) { 142 136 … … 144 138 sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 145 139 146 Date currentDate = new Date(); 140 Date currentDate = new Date(); 147 141 Calendar cal = Calendar.getInstance(); 148 142 149 143 cal.setTime(currentDate); 150 144 String currentTimeString = sdf.format(currentDate); 151 145 152 146 cal.add(Calendar.DATE, -days); //starting date 153 147 Date startingTime = cal.getTime(); 154 148 155 149 String startingTimeString = sdf.format(startingTime); 156 //System.out.println("starting date: " + startingTimeString); 157 //add to list 158 150 //add to list 151 159 152 String nextIntervalTime = startingTimeString; 160 161 //do, while date is before current date,after the addition of the 10 days 153 154 //do, while date is before current date,after the addition of the 10 days 162 155 do { 163 156 cal.add(Calendar.DATE, 10); 164 157 165 158 Date intervalTime = cal.getTime(); 166 159 String intervalTimeString = sdf.format(intervalTime); 167 String timeOsmApiArgument = nextIntervalTime + "," + intervalTimeString; 168 169 if(cal.getTime().after(currentDate)){ 160 String timeOsmApiArgument = nextIntervalTime + "," + intervalTimeString; 161 162 if (cal.getTime().after(currentDate)) { 170 163 //add the offset of remaining days as last interval 171 164 timeOsmApiArgument = nextIntervalTime + "," + currentTimeString; … … 173 166 System.out.println(timeOsmApiArgument); 174 167 break; 175 } 176 else{ 168 } else { 177 169 timeIntervals.add(timeOsmApiArgument); 178 170 } 179 171 nextIntervalTime = intervalTimeString; 180 172 System.out.println(" ti: " + timeOsmApiArgument); 181 } while(cal.getTime().before(currentDate)); 173 } while (cal.getTime().before(currentDate)); 182 174 } 183 175 … … 185 177 public void actionPerformed(ActionEvent ae) { 186 178 //this.firePropertyChange("progress", userTrainProgress, userTrainProgress+10); 187 // if(userTrainProgress <100){ 188 // setProgress(userTrainProgress+10); 189 // } 190 179 // if (userTrainProgress <100) { 180 // setProgress(userTrainProgress+10); 181 // } 182 191 183 } 192 184 }
Note:
See TracChangeset
for help on using the changeset viewer.