Changeset 31554 in osm for applications/editors/josm/plugins/OSMRecPlugin
- Timestamp:
- 2015-09-14T17:16:17+02:00 (9 years ago)
- Location:
- applications/editors/josm/plugins/OSMRecPlugin
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/OSMRecPlugin/nbproject/private/private.xml
r31461 r31554 4 4 <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> 5 5 <group> 6 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/ extractor/LanguageDetector.java</file>6 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/parsers/Mapper.java</file> 7 7 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecPlugin.java</file> 8 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecToggleDialog.java</file>9 8 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecPluginHelper.java</file> 10 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/extractor/Analyzer.java</file> 9 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/personalization/HistoryParser.java</file> 10 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/personalization/TrainByUser.java</file> 11 11 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/core/TrainWorker.java</file> 12 <file>file:/home/imis-nkarag/software/josm_svn/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/extractor/SampleModelsExtractor.java</file> 12 13 </group> 13 14 </open-files> -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/core/TrainWorker.java
r31461 r31554 91 91 private static int numberOfFeatures; 92 92 private static LanguageDetector languageDetector; 93 private final String inputFileName; 93 94 94 95 … … 104 105 this.frequency = frequency; 105 106 this.topKIsSelected = topKIsSelected; 107 108 inputFileName = inputFilePath.substring(inputFilePath.lastIndexOf("/")); 106 109 System.out.println("find parent directory, create osmrec dir for models: " + new File(inputFilePath).getParentFile()); 107 110 modelDirectoryPath = new File(inputFilePath).getParentFile() + "/OSMRec_models"; … … 744 747 745 748 File modelFile = new File(modelDirectory.getAbsolutePath()+"/best_model"); //decide path of models 749 750 File customModelFile; 751 if(topKIsSelected){ 752 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_topK" + topK + ".0"); 753 } 754 else{ 755 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + ".0"); 756 } 757 758 746 759 if(modelFile.exists()){ 747 760 modelFile.delete(); 748 } 761 } 762 if(customModelFile.exists()){ 763 customModelFile.delete(); 764 } 765 749 766 try { 750 767 //System.out.println("file created"); 751 768 model.save(modelFile); 769 model.save(customModelFile); 752 770 System.out.println("best model saved at: " + modelFile); 771 System.out.println("custom model saved at: " + customModelFile); 753 772 } catch (IOException ex) { 754 773 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); … … 873 892 874 893 File modelFile = new File(modelDirectory.getAbsolutePath()+"/model_with_classes"); 894 895 896 File customModelFile; 897 if(topKIsSelected){ 898 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model" + "_c" + param + "_topK" + topK + ".1"); 899 } 900 else{ 901 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + ".1"); 902 } 903 904 if(customModelFile.exists()){ 905 customModelFile.delete(); 906 } 907 875 908 if(modelFile.exists()){ 876 909 modelFile.delete(); 877 } 910 } 911 878 912 try { 879 913 //System.out.println("file created"); 880 914 model.save(modelFile); 915 model.save(customModelFile); 881 916 System.out.println("model with classes saved at: " + modelFile); 917 System.out.println("custom model with classes saved at: " + modelFile); 882 918 } catch (IOException ex) { 883 919 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/extractor/SampleModelsExtractor.java
r31402 r31554 29 29 //File modelFile = new File(bestModelPath); 30 30 File targetFile = new File(modelPath); 31 32 if(targetFile.exists()){ 33 return; 34 } 31 35 try { 32 36 targetFile.createNewFile(); … … 38 42 System.out.println("trying to get stream.. for " + "/resources/files/" + modelName); 39 43 svmModelStream = SampleModelsExtractor.class.getResourceAsStream("/resources/files/" + modelName); 40 41 42 //Scanner input = new Scanner(svmModelStream); 43 //svmModelStream. 44 45 46 // while (input.hasNext()) { 47 // String nextLine = input.nextLine(); 48 // 49 // //outputStream.write(nextLine.getBytes(Charset.forName("UTF-8"))); 50 // System.out.println(nextLine); 51 // //outputStream.write(nextLine); 52 // //textualList.add(nextLine); 53 // 54 // } 55 // System.out.println("GER RESOURCE SUCCESS"); 56 44 57 45 try { 58 46 outputStream = new FileOutputStream(targetFile); -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecPlugin.java
r31106 r31554 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;8 //import org.openstreetmap.josm.gui.dialogs.properties.PropertiesDialog; 9 9 10 10 /** -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/OSMRecPluginHelper.java
r31461 r31554 37 37 import java.beans.PropertyChangeEvent; 38 38 import java.beans.PropertyChangeListener; 39 import java.io.EOFException; 39 40 import java.io.File; 41 import java.io.FileInputStream; 40 42 import java.io.FileNotFoundException; 43 import java.io.FileOutputStream; 41 44 import java.io.IOException; 42 45 import java.io.InputStream; 46 import java.io.ObjectInputStream; 47 import java.io.ObjectOutputStream; 43 48 import java.text.DecimalFormat; 44 49 import java.util.ArrayList; … … 53 58 import java.util.List; 54 59 import java.util.Map; 60 import java.util.Map.Entry; 55 61 import java.util.Scanner; 56 62 import java.util.concurrent.CancellationException; … … 74 80 import javax.swing.JDialog; 75 81 import javax.swing.JFileChooser; 82 import javax.swing.JFrame; 76 83 import javax.swing.JLabel; 77 84 import javax.swing.JList; … … 164 171 private boolean useCustomSVMModel = false; 165 172 private String customSVMModelPath; 173 private final String combinedModelClasses; 174 private final String combinedModel; 166 175 167 176 // Selection that we are editing by using both dialogs … … 205 214 MODEL_PATH = new File(MAIN_PATH).getParentFile() + "/OSMRec_models"; 206 215 TEXTUAL_LIST_PATH = MODEL_PATH + "/textualList.txt"; 216 combinedModelClasses = MODEL_PATH + "/combinedModel.1"; 217 combinedModel = MODEL_PATH + "/combinedModel.0"; 207 218 bestModelPath = MODEL_PATH + "/best_model"; 219 customSVMModelPath = bestModelPath; 208 220 modelWithClassesPath = MODEL_PATH + "/model_with_classes"; 209 221 languageDetector = LanguageDetector.getInstance(MODEL_PATH + "/profiles"); 210 222 211 SampleModelsExtractor sa = new SampleModelsExtractor();212 213 sa .extractSampleSVMmodel("best_model", bestModelPath);214 sa .extractSampleSVMmodel("model_with_classes", modelWithClassesPath);223 SampleModelsExtractor sampleModelsExtractor = new SampleModelsExtractor(); 224 225 sampleModelsExtractor.extractSampleSVMmodel("best_model", bestModelPath); 226 sampleModelsExtractor.extractSampleSVMmodel("model_with_classes", modelWithClassesPath); 215 227 216 228 } … … 1199 1211 private final JButton chooseModelButton; 1200 1212 private final JTextField chooseModelTextField; 1201 private final JList<String> modelCombinationList;1213 1202 1214 private final DefaultListModel<String> combinationDefaultListModel = new DefaultListModel<>(); 1215 private final JList<String> modelCombinationList = new javax.swing.JList<>(combinationDefaultListModel); 1203 1216 private final JPanel modelCombinationPanel; 1204 1217 //private final JTextField weightTextField; … … 1210 1223 private final JButton resetWeightsButton; 1211 1224 private final JButton removeSelectedModelButton; 1212 private finalMap<JTextField, String> weightFieldsAndPaths = new HashMap<>();1225 private Map<JTextField, String> weightFieldsAndPaths = new HashMap<>(); 1213 1226 private final Map<String, Double> normalizedPathsAndWeights = new HashMap<>(); 1227 private final JOptionPane pane; 1228 private final JDialog dlg; 1229 private final JPanel mainPanel; 1230 private final JPanel singleSelectionPanel; 1231 private final JPanel setResetWeightsPanel; 1232 private final JScrollPane combinationScrollPane; 1233 private final JScrollPane singleSelectionScrollPane; 1234 private final TitledBorder modelTitle; 1235 private final TitledBorder weightTitle; 1236 private final TitledBorder combineTitle; 1237 //private final BorderLayout mainBorderLayout; 1238 //private final BorderLayout mainBorderLayoutDefault; 1239 //private final Dimension combinationPanelDimension; 1240 private final Dimension singleSelectionDimension; 1241 private final Dimension modelCombinationDimension; 1242 private final Dimension mainPanelDimension; 1214 1243 1215 public ModelSettingsDialog(Collection<OsmPrimitive> sel, AddTagsDialog addDialog){ 1216 //checkbox: use combined models 1217 //button: remove selected model 1244 1245 public ModelSettingsDialog(Collection<OsmPrimitive> sel1, final AddTagsDialog addDialog){ 1246 1247 loadPreviousCombinedSVMModel(); 1248 singleSelectionDimension = new Dimension(470,70); 1249 modelCombinationDimension = new Dimension(450,250); 1250 mainPanelDimension = new Dimension(600,350); 1218 1251 1219 1252 //------- <NORTH of main> ---------// 1220 JPanelmainPanel = new JPanel(new BorderLayout(10,10));1221 JPanelsingleSelectionPanel = new JPanel(new BorderLayout(10,10));1222 JPanelsetResetWeightsPanel = new JPanel();1253 mainPanel = new JPanel(new BorderLayout(10,10)); 1254 singleSelectionPanel = new JPanel(new BorderLayout(10,10)); 1255 setResetWeightsPanel = new JPanel(); 1223 1256 1224 1257 chooseModelLabel = new javax.swing.JLabel("Choose a Model:"); … … 1230 1263 singleSelectionPanel.add(chooseModelTextField, BorderLayout.WEST); 1231 1264 singleSelectionPanel.add(chooseModelButton, BorderLayout.EAST); 1265 1266 singleSelectionScrollPane = new JScrollPane(singleSelectionPanel); 1267 singleSelectionScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 1268 singleSelectionScrollPane.setPreferredSize(singleSelectionDimension); 1269 1232 1270 //------- </NORTH of main> ---------// 1233 1271 1234 1272 //------- <WEST of main> ---------// 1235 modelCombinationList = new javax.swing.JList<>(combinationDefaultListModel);1273 //modelCombinationList = new javax.swing.JList<>(combinationDefaultListModel); 1236 1274 modelCombinationList.setFixedCellHeight(20); 1237 1275 modelCombinationList.setEnabled(false); 1238 modelCombinationPanel = new JPanel(new BorderLayout( ));1276 modelCombinationPanel = new JPanel(new BorderLayout(10,10)); //new BorderLayout() 1239 1277 1240 1278 weightsPanel = new JPanel(); … … 1257 1295 modelCombinationPanel.add(setResetWeightsPanel, BorderLayout.SOUTH); 1258 1296 1259 JScrollPanecombinationScrollPane = new JScrollPane(modelCombinationPanel);1297 combinationScrollPane = new JScrollPane(modelCombinationPanel); 1260 1298 1261 1299 combinationScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 1262 combinationScrollPane.setPreferredSize(new Dimension(450,250)); // w/h 1300 combinationScrollPane.setPreferredSize(modelCombinationDimension); //new Dimension(450,250) // w/h 1301 1263 1302 //------- </WEST of main> ---------// 1264 1303 1265 1266 1304 //------- <SOUTH of main> ---------// 1267 1305 useModelCombinationCheckbox = new JCheckBox("Combine different models?"); … … 1270 1308 1271 1309 //------- <Borders> ---------// 1272 TitledBordermodelTitle = BorderFactory.createTitledBorder("Models");1273 TitledBorderweightTitle = BorderFactory.createTitledBorder("W");1274 TitledBordercombineTitle = BorderFactory.createTitledBorder("Combine Models");1310 modelTitle = BorderFactory.createTitledBorder("Models"); 1311 weightTitle = BorderFactory.createTitledBorder("W"); 1312 combineTitle = BorderFactory.createTitledBorder("Combine Models"); 1275 1313 modelCombinationList.setBorder(modelTitle); 1276 1314 weightsPanel.setBorder(weightTitle); 1315 1316 for(Entry<JTextField, String> entry : weightFieldsAndPaths.entrySet()){ 1317 //modelCombinationList.add(entry.getValue()); 1318 combinationDefaultListModel.addElement(entry.getValue()); 1319 1320 JTextField weightTextField = new javax.swing.JTextField("0.00"); 1321 weightTextField.setMaximumSize(new Dimension(80,20)); 1322 weightsPanel.add(entry.getKey()); 1323 1324 //entry.getKey().setText("0.00"); 1325 } 1326 1277 1327 //modelCombinationPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.GRAY, Color.WHITE)); 1278 1328 modelCombinationPanel.setBorder(combineTitle); … … 1314 1364 } 1315 1365 }); 1316 mainPanel.add(singleSelection Panel, BorderLayout.NORTH);1317 mainPanel.add(combinationScrollPane, BorderLayout.CENTER); 1366 mainPanel.add(singleSelectionScrollPane, BorderLayout.NORTH); 1367 mainPanel.add(combinationScrollPane, BorderLayout.CENTER); 1318 1368 mainPanel.add(useModelCombinationCheckbox,BorderLayout.SOUTH); 1319 1369 1370 mainPanel.setPreferredSize(mainPanelDimension); 1371 1320 1372 this.add(mainPanel); 1321 1322 JOptionPane pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); 1323 JDialog dlg = pane.createDialog(Main.parent, tr("Model Settings")); 1324 dlg.setVisible(true); 1325 //JButton ok = dlg.getRootPane().getDefaultButton(); 1326 1373 //pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); 1374 1375 //final JFrame frame = new JFrame(); 1376 pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION) { 1377 @Override 1378 public void setValue(Object newValue) { 1379 super.setValue(newValue); 1380 if(newValue instanceof Integer && (int) newValue == 0 && useModelCombinationCheckbox.isSelected()){ 1381 System.out.println("model settings button value: " + newValue); 1382 System.out.println("\nUSE COMBINED MODEL\n"); 1383 useCombinedModel = true; 1384 useCustomSVMModel = false; 1385 1386 addDialog.loadSVMmodel(); 1387 addDialog.createOSMObject(sel); 1388 saveCombinedModel(); 1389 dlg.setVisible(false); 1390 //this.setVisible(false); 1391 } 1392 else if(newValue instanceof Integer && (int) newValue == -1 && useModelCombinationCheckbox.isSelected()){ 1393 System.out.println("model settings button value: " + newValue); 1394 useCombinedModel = false; 1395 useCustomSVMModel = false; 1396 System.out.println("Use combined model"); 1397 1398 addDialog.loadSVMmodel(); 1399 addDialog.createOSMObject(sel); 1400 dlg.setVisible(false); 1401 } 1402 else if(newValue instanceof Integer && (int) newValue == 0 && !useModelCombinationCheckbox.isSelected() ){ 1403 System.out.println("model settings button value: " + newValue); 1404 System.out.println("Don t use combined model, use custom model"); 1405 useCombinedModel = false; 1406 useCustomSVMModel = true; 1407 addDialog.loadSVMmodel(); 1408 addDialog.createOSMObject(sel); 1409 dlg.setVisible(false); 1410 } 1411 else if(newValue instanceof Integer && (int) newValue == -1 && !useModelCombinationCheckbox.isSelected() ){ 1412 System.out.println("model settings button value: " + newValue); 1413 System.out.println("Don t use combined model, use custom model"); 1414 useCombinedModel = false; 1415 useCustomSVMModel = false; 1416 addDialog.loadSVMmodel(); 1417 addDialog.createOSMObject(sel); 1418 dlg.setVisible(false); 1419 } 1420 else if(newValue == null || newValue.equals("uninitializedValue")){ 1421 System.out.println("uninitializedValue, do nothing"); 1422 } 1423 //JOptionPane.showMessageDialog(frame.getContentPane(), "You have hit " + newValue); 1424 } 1425 }; 1426 1427 1428 //pane.showConfirmDialog(); 1429 //pane.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY); 1430 1431 1432 //JButton ok = dlg.getRootPane().getDefaultButton(); 1433 dlg = pane.createDialog(Main.parent, tr("Model Settings")); 1434 dlg.setVisible(true); 1435 1436 /* 1327 1437 //returned value of pane.getValue indicates the key pressed by the user. "0" is for the OK button 1328 1438 int buttonValue = -1; 1329 1439 if(pane.getValue() != null){ 1440 //System.out.println("type? " + pane.getValue()); 1330 1441 buttonValue = (int) pane.getValue(); 1331 1442 } 1332 1333 System.out.println("value of model sel button: " + pane.getValue()); 1334 if(buttonValue == 0 && useCombinedModel && useModelCombinationCheckbox.isSelected()){ 1335 System.out.println("\n\nUSE COMBINED MODEL\n\n"); 1443 1444 System.out.println("value of model sel button: " + pane.getValue()); 1445 1446 if(buttonValue == 0 && useModelCombinationCheckbox.isSelected()){ 1447 useCombinedModel = true; 1448 System.out.println("\nold - USE COMBINED MODEL\n"); 1336 1449 //recompute predictions with combination 1337 modelWithClasses = false; 1450 1451 //modelWithClasses = false; 1338 1452 addDialog.loadSVMmodel(); 1339 addDialog.createOSMObject(sel); 1340 } 1341 else { 1342 useCombinedModel = false; 1453 addDialog.createOSMObject(sel); 1454 saveCombinedModel(); 1455 dlg.setVisible(false); 1456 //this.setVisible(false); 1457 } 1458 else if(useModelCombinationCheckbox.isSelected()){ 1459 useCombinedModel = true; 1460 System.out.println("old - use combined model"); 1461 1343 1462 addDialog.loadSVMmodel(); 1344 1463 addDialog.createOSMObject(sel); 1345 } 1464 dlg.setVisible(false); 1465 } 1466 else{ 1467 System.out.println("old - don t use combined model, use custom model"); 1468 useCombinedModel = false; 1469 useCustomSVMModel = true; 1470 addDialog.loadSVMmodel(); 1471 addDialog.createOSMObject(sel); 1472 dlg.setVisible(false); 1473 } 1474 */ 1346 1475 } 1347 1476 1348 private void modelChooserButtonActionPerformed(java.awt.event.ActionEvent evt) { 1477 public void makeVisible(boolean visible){ 1478 dlg.setVisible(true); 1479 } 1480 1481 private void modelChooserButtonActionPerformed(java.awt.event.ActionEvent evt) { 1482 1349 1483 try { 1350 1484 final File file = new File(chooseModelTextField.getText()); … … 1384 1518 1385 1519 private void userCombinationCheckboxActionPerformed(java.awt.event.ActionEvent evt) { 1520 1386 1521 if(useModelCombinationCheckbox.isSelected()){ 1522 useCombinedModel = true; 1523 useCustomSVMModel = false; //reseting the selected custom SVM model only here 1387 1524 removeSelectedModelButton.setEnabled(true); 1388 1525 acceptWeightsButton.setEnabled(true); … … 1396 1533 weightPanelComponent.setEnabled(true); 1397 1534 } 1398 useCustomSVMModel = false; //reseting the selected custom SVM model only here1535 1399 1536 } 1400 1537 else{ 1538 useCombinedModel = false; 1539 useCustomSVMModel = true; 1401 1540 removeSelectedModelButton.setEnabled(false); 1402 1541 acceptWeightsButton.setEnabled(false); … … 1414 1553 1415 1554 private void acceptWeightsButtonActionPerformed(ActionEvent evt) { 1555 int weightsCount = 0; 1416 1556 removeSelectedModelButton.setEnabled(false); 1417 1557 double weightSum = 0; 1418 1558 for(JTextField weightField : weightFieldsAndPaths.keySet()){ 1559 if(weightField.getText().equals("")){ 1560 weightField.setText("0.00"); 1561 } 1562 1419 1563 try{ 1420 1564 Double weightValue = Double.parseDouble(weightField.getText()); … … 1426 1570 Main.warn(ex); 1427 1571 } 1572 weightsCount++; 1428 1573 } 1429 1574 … … 1437 1582 1438 1583 weightValue = Math.abs(weightValue)/weightSum; //normalize 1584 1585 if(weightSum == 0){ 1586 //System.out.println("Zero weights"); 1587 weightValue = 1.0/weightsCount; 1588 } 1589 1439 1590 weightField.setText(new DecimalFormat("#.##").format(weightValue)); 1440 1591 normalizedPathsAndWeights.put(weightFieldsAndPaths.get(weightField), weightValue); … … 1450 1601 1451 1602 useCombinedModel = true; 1603 useCustomSVMModel = false; 1604 1452 1605 //filesAndWeights.putAll(normalizedPathsAndWeights); 1453 1606 } … … 1462 1615 private void removeSelectedModelButtonActionPerformed(ActionEvent evt) { 1463 1616 int index = modelCombinationList.getSelectedIndex(); 1464 combinationDefaultListModel.remove(index); 1617 String modelToBeRemoved = combinationDefaultListModel.get(index); 1618 combinationDefaultListModel.remove(index); 1619 System.out.println("model to be removed: " + modelToBeRemoved); 1620 1621 Iterator<Entry<JTextField, String>> it = weightFieldsAndPaths.entrySet().iterator(); 1622 while (it.hasNext()) { 1623 Entry<JTextField, String> en = it.next(); 1624 if(en.getValue().equals(modelToBeRemoved)){ 1625 it.remove(); 1626 } 1627 } 1628 System.out.println("model to be removed: " + modelToBeRemoved); 1629 1465 1630 weightsPanel.remove(index); 1466 1631 weightsPanel.revalidate(); 1467 1632 weightsPanel.repaint(); 1633 } 1634 1635 @SuppressWarnings("unchecked") 1636 private void loadPreviousCombinedSVMModel() { 1637 File combinedModelClassesFile = new File(combinedModelClasses); 1638 1639 if (combinedModelClassesFile.exists()) { 1640 FileInputStream fileIn = null; 1641 ObjectInputStream in = null; 1642 try { 1643 fileIn = new FileInputStream(combinedModelClassesFile); 1644 in = new ObjectInputStream(fileIn); 1645 weightFieldsAndPaths = (Map<JTextField, String>) in.readObject(); 1646 } catch (FileNotFoundException ex) { 1647 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); 1648 } catch (IOException | ClassNotFoundException ex) { 1649 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); 1650 } finally { 1651 try { 1652 in.close(); 1653 fileIn.close(); 1654 } catch (IOException ex) { 1655 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); 1656 } 1657 } 1658 } else { 1659 try { 1660 combinedModelClassesFile.createNewFile(); 1661 } catch (IOException ex) { 1662 Logger.getLogger(OSMRecPluginHelper.class.getName()).log(Level.SEVERE, null, ex); 1663 } 1664 } 1665 } 1666 1667 private void saveCombinedModel() { 1668 try (FileOutputStream fileOut = new FileOutputStream(combinedModelClasses); 1669 ObjectOutputStream out = new ObjectOutputStream(fileOut)) { 1670 out.writeObject(weightFieldsAndPaths); 1671 } catch (IOException e) { 1672 System.out.println("serialize error" + e); 1673 } 1468 1674 } 1469 1675 } … … 1484 1690 private Map<String, String> mappings; 1485 1691 private Map<String, Integer> mapperWithIDs; 1692 private Map<Integer, String> idsWithMappings; 1486 1693 private List<String> textualList = new ArrayList<>(); 1487 1694 //private boolean useClassFeatures = false; 1488 private final JCheckBox useTagsCheckBox; 1695 private final JCheckBox useTagsCheckBox; 1696 private ModelSettingsDialog modelSettingsDialog; 1697 private static final int RECOMMENDATIONS_SIZE = 10; 1489 1698 1490 1699 public AddTagsDialog() { … … 1493 1702 setCancelButton(2); 1494 1703 configureContextsensitiveHelp("/Dialog/AddValue", true /* show help button */); 1495 final AddTagsDialog lala= this;1704 final AddTagsDialog addTagsDialog = this; 1496 1705 1497 1706 loadOntology(); … … 1567 1776 @Override 1568 1777 public void actionPerformed(java.awt.event.ActionEvent evt) { 1569 1570 ModelSettingsDialog modelSettingsDialog = new ModelSettingsDialog(sel, lala); 1571 } 1572 }); 1778 if(modelSettingsDialog == null){ 1779 System.out.println("new modelSettingsDialog"); 1780 modelSettingsDialog = new ModelSettingsDialog(sel, addTagsDialog); 1781 1782 //modelSettingsDialog.makeVisible(true); 1783 //modelSettingsDialog.setVisible(true); 1784 } 1785 else{ 1786 System.out.println("set modelSettingsDialog visible"); 1787 modelSettingsDialog.makeVisible(true); 1788 //modelSettingsDialog.setVisible(true); 1789 } 1790 } 1791 }); 1573 1792 1574 1793 useTagsCheckBox.addActionListener(new java.awt.event.ActionListener() { … … 1938 2157 1939 2158 private void loadSVMmodel() { 2159 File modelDirectory = new File(MODEL_PATH); 2160 File modelFile = null; 2161 if(useCombinedModel){ 2162 if(filesAndWeights.isEmpty()){ 2163 System.out.println("No models selected! Loading defaults.."); 2164 if(modelWithClasses){ 2165 System.out.println("Using default/last model with classes: " + modelDirectory.getAbsolutePath() + "/model_with_classes"); 2166 modelFile = new File(modelDirectory.getAbsolutePath() + "/model_with_classes"); 2167 } 2168 else{ 2169 System.out.println("Using default/last model without classes: " + modelDirectory.getAbsolutePath() + "/best_model"); 2170 modelFile = new File(modelDirectory.getAbsolutePath() + "/best_model"); 2171 } 2172 } 2173 if(modelWithClasses){ //check filenames to define if model with classes is selected 2174 System.out.println("Using combined model with classes"); 2175 useCombinedSVMmodels(sel, true); 2176 } 2177 else{ 2178 System.out.println("Using combined model without classes"); 2179 useCombinedSVMmodels(sel, false); 2180 } 2181 2182 } 2183 else if(useCustomSVMModel){ 2184 System.out.println("custom path: " + customSVMModelPath); 2185 File checkExistance = new File(customSVMModelPath); 2186 if(checkExistance.exists() && checkExistance.isFile()){ 2187 if (modelWithClasses) { 2188 System.out.println("Using custom model with classes: "); 2189 if (customSVMModelPath.endsWith(".0")) { 2190 //String customSVMModelPathWithClasses = customSVMModelPath.replace(".0", ".1"); 2191 String customSVMModelPathWithClasses = customSVMModelPath.substring(0, customSVMModelPath.length() - 2) + ".1"; 2192 2193 modelFile = new File(customSVMModelPathWithClasses); 2194 System.out.println(customSVMModelPathWithClasses); 2195 } 2196 else { 2197 modelFile = new File(customSVMModelPath); 2198 } 2199 } else { 2200 System.out.println("Using custom model without classes"); 2201 if (customSVMModelPath.endsWith(".1")) { 2202 String customSVMModelPathWithoutClasses = customSVMModelPath.substring(0, customSVMModelPath.length() - 2) + ".0"; 2203 modelFile = new File(customSVMModelPathWithoutClasses); 2204 System.out.println(customSVMModelPathWithoutClasses); 2205 } else { 2206 modelFile = new File(customSVMModelPath); 2207 } 2208 } 2209 try { 2210 System.out.println("try to load model: " + modelFile.getAbsolutePath()); 2211 modelSVM = Model.load(modelFile); 2212 System.out.println("model loaded!"); 2213 2214 } catch (IOException ex) { 2215 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 2216 } 2217 modelSVMLabelSize = modelSVM.getLabels().length; 2218 modelSVMLabels = modelSVM.getLabels(); 2219 2220 } 2221 else { 2222 //user chose to use a custom model, but did not provide a path to a model: 2223 if(modelWithClasses){ 2224 System.out.println("Using default/last model with classes"); 2225 modelFile = new File(modelDirectory.getAbsolutePath() + "/model_with_classes"); 2226 } 2227 else{ 2228 System.out.println("Using default/last model without classes"); 2229 modelFile = new File(modelDirectory.getAbsolutePath() + "/best_model"); 2230 } 2231 2232 try { 2233 System.out.println("try to load model: " + modelFile.getAbsolutePath()); 2234 modelSVM = Model.load(modelFile); 2235 System.out.println("model loaded!"); 2236 2237 } catch (IOException ex) { 2238 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 2239 } 2240 modelSVMLabelSize = modelSVM.getLabels().length; 2241 modelSVMLabels = modelSVM.getLabels(); 2242 2243 } 2244 } 2245 else { 2246 if(modelWithClasses){ 2247 System.out.println("Using default/last model with classes"); 2248 modelFile = new File(modelDirectory.getAbsolutePath() + "/model_with_classes"); 2249 } 2250 else{ 2251 System.out.println("Using default/last model without classes"); 2252 modelFile = new File(modelDirectory.getAbsolutePath() + "/best_model"); 2253 } 2254 2255 try { 2256 System.out.println("try to load model: " + modelFile.getAbsolutePath()); 2257 modelSVM = Model.load(modelFile); 2258 System.out.println("model loaded!"); 2259 2260 } catch (IOException ex) { 2261 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); 2262 } 2263 modelSVMLabelSize = modelSVM.getLabels().length; 2264 modelSVMLabels = modelSVM.getLabels(); 2265 } 2266 2267 2268 2269 //try to use last SVM model 2270 /* 1940 2271 if(useCombinedModel){ 1941 2272 System.out.println("Using combined model."); 1942 useCombinedSVMmodels(sel, false); 2273 if(modelWithClasses){ //check filenames to define if model with classes is selected 2274 useCombinedSVMmodels(sel, true); 2275 } 2276 else{ 2277 useCombinedSVMmodels(sel, false); 2278 } 2279 1943 2280 return; 1944 2281 } 1945 2282 //String modelDirectoryPath = new File(MAIN_PATH).getParentFile() + "/OSMRec_models"; 1946 File modelDirectory = new File(MODEL_PATH);1947 1948 File modelFile;2283 //File modelDirectory = new File(MODEL_PATH); 2284 2285 //File modelFile; 1949 2286 if(useCustomSVMModel){ 1950 2287 System.out.println("using custom model: " + customSVMModelPath); … … 1971 2308 modelSVMLabels = modelSVM.getLabels(); 1972 2309 //System.out.println("is model path same as this? " + MODEL_PATH + "\n" + modelDirectoryPath); 2310 */ 1973 2311 } 1974 2312 … … 2054 2392 if(selectedInstance != null){ 2055 2393 int id; 2056 if(mappings == null){2057 System.out.println("null mappings ERROR");2058 }2059 2394 2060 2395 OSMClassification classifier = new OSMClassification(); … … 2099 2434 2100 2435 Map<String, Double> scoreMap = new HashMap<>(); 2101 2102 for(File modelFile : filesAndWeights.keySet()){ 2103 //filessAndweights 2436 2437 Map<File, Double> alignedFilesAndWeights = getAlignedModels(filesAndWeights); 2438 2439 for(File modelFile : alignedFilesAndWeights.keySet()){ 2440 2104 2441 2105 2442 try { 2106 2443 modelSVM = Model.load(modelFile); 2107 System.out.println("model loaded: " + modelFile.getAbsolutePath());2444 //System.out.println("model loaded: " + modelFile.getAbsolutePath()); 2108 2445 } catch (IOException ex) { 2109 2446 Logger.getLogger(TrainWorker.class.getName()).log(Level.SEVERE, null, ex); … … 2196 2533 } 2197 2534 } 2198 System.out.println(" predicted classes: " + Arrays.toString(predictedTags));2535 System.out.println("combined, predicted classes: " + Arrays.toString(predictedTags)); 2199 2536 2200 2537 for(int r = 0; r<ranks.length; r++){ 2201 2538 String predictedTag = predictedTags[r]; 2202 Double currentWeight = filesAndWeights.get(modelFile);2539 Double currentWeight = alignedFilesAndWeights.get(modelFile); 2203 2540 double finalRank = ranks[r]*currentWeight; 2204 2541 //String roundedWeight = new DecimalFormat("##.###").format(finalRank); … … 2391 2728 2392 2729 Arrays.sort(scores); 2393 2730 2731 int[] preds = new int[RECOMMENDATIONS_SIZE]; 2732 for(int p=0; p < RECOMMENDATIONS_SIZE; p++){ 2733 preds[p] = modelSVMLabels[scoresValues.get(scores[scores.length-(p+1)])]; 2734 } 2735 String[] predictedTags2 = new String[RECOMMENDATIONS_SIZE]; 2736 2737 for(int p=0; p<RECOMMENDATIONS_SIZE; p++){ 2738 if(idsWithMappings.containsKey(preds[p])){ 2739 predictedTags2[p] = idsWithMappings.get(preds[p]); 2740 } 2741 2742 } 2743 2744 /* 2394 2745 int predicted1 = modelSVMLabels[scoresValues.get(scores[scores.length-1])]; 2395 2746 int predicted2 = modelSVMLabels[scoresValues.get(scores[scores.length-2])]; … … 2448 2799 } 2449 2800 } 2801 */ 2450 2802 //clearing model, to add the new computed classes in jlist 2451 2803 model.clear(); … … 2453 2805 2454 2806 for(int k=0; k<10; k++){ 2455 if(tag.getValue().equals(predictedTags [k])){2456 predictedTags [k] = tag.getKey();2807 if(tag.getValue().equals(predictedTags2[k])){ 2808 predictedTags2[k] = tag.getKey(); 2457 2809 model.addElement(tag.getKey()); 2458 2810 } 2459 2811 } 2460 2812 } 2461 System.out.println("predicted classes: " + Arrays.toString(predictedTags)); 2813 //System.out.println("createdOSM object, predicted classes: " + Arrays.toString(predictedTags)); 2814 System.out.println("Optimized - create OSMObject, predicted classes: " + Arrays.toString(predictedTags2)); 2462 2815 } 2463 2816 } … … 2475 2828 mappings = mapper.getMappings(); 2476 2829 mapperWithIDs = mapper.getMappingsWithIDs(); 2830 idsWithMappings = mapper.getIDsWithMappings(); 2477 2831 //System.out.println("mappings " + mappings); 2478 2832 //System.out.println("mapperWithIDs " + mapperWithIDs); … … 2511 2865 indirectClassesWithIDs = ontology.getIndirectClassesIDs(); 2512 2866 } 2867 2868 private Map<File, Double> getAlignedModels(Map<File, Double> filesAndWeights) { 2869 Map<File, Double> alignedFilesAndWeights = new HashMap<>(); 2870 if(modelWithClasses){ 2871 for(Entry<File, Double> entry : filesAndWeights.entrySet()){ 2872 String absolutePath = entry.getKey().getAbsolutePath(); 2873 if(absolutePath.endsWith(".0")){ 2874 String newPath = absolutePath.substring(0, absolutePath.length()-2) + ".1"; 2875 File alignedFile = new File(newPath); 2876 if(alignedFile.exists()){ 2877 alignedFilesAndWeights.put(alignedFile, entry.getValue()); 2878 } 2879 } 2880 else{ 2881 alignedFilesAndWeights.put(entry.getKey(), entry.getValue()); 2882 } 2883 } 2884 } 2885 else{ 2886 for(Entry<File, Double> entry : filesAndWeights.entrySet()){ 2887 String absolutePath = entry.getKey().getAbsolutePath(); 2888 if(absolutePath.endsWith(".1")){ 2889 String newPath = absolutePath.substring(0, absolutePath.length()-2) + ".0"; 2890 File alignedFile = new File(newPath); 2891 if(alignedFile.exists()){ 2892 alignedFilesAndWeights.put(alignedFile, entry.getValue()); 2893 } 2894 } 2895 else{ 2896 alignedFilesAndWeights.put(entry.getKey(), entry.getValue()); 2897 } 2898 } 2899 } 2900 return alignedFilesAndWeights; 2901 } 2513 2902 } 2514 2903 } -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/osmrec/personalization/TrainByUser.java
r31441 r31554 85 85 private static LanguageDetector languageDetector; 86 86 private final String username; 87 private final String inputFileName; 87 88 88 89 public TrainByUser(String inputFilePath, String username, boolean validateFlag, double cParameterFromUser, … … 98 99 this.topKIsSelected = topKIsSelected; 99 100 System.out.println("find parent directory, create osmrec dir for models: " + new File(inputFilePath).getParentFile()); 101 102 inputFileName = inputFilePath.substring(inputFilePath.lastIndexOf("/")); 100 103 modelDirectoryPath = new File(inputFilePath).getParentFile() + "/OSMRec_models"; 101 104 //textualDirectoryPath = new File(inputFilePath).getParentFile() + "/OSMRec_models/textualList.txt"; … … 243 246 System.out.println("number of instances: " + numberOfTrainingInstances); 244 247 System.out.println("end of parsing files."); 245 248 if(numberOfTrainingInstances == 0){ 249 System.out.println("This user has not edited the loaded area. Cannot train a model!"); 250 } 246 251 } 247 252 … … 450 455 } 451 456 else{ 452 modelFile = new File(modelDirectory.getAbsolutePath()+"/user_ _" + username + "_model_geometries_textual_c=" + param);457 modelFile = new File(modelDirectory.getAbsolutePath()+"/user_" + username + "_model_geometries_textual_c=" + param); 453 458 } 454 459 … … 624 629 int u = 0; 625 630 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 631 if(wayListSizeWithoutUnclassified == 0){ 632 System.out.println("aborting training process.."); 633 return; 634 } 626 635 //set classes for each osm instance 627 636 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size … … 735 744 736 745 File modelFile = new File(modelDirectory.getAbsolutePath()+"/best_model"); //decide path of models 746 747 File customModelFile; 748 if(topKIsSelected){ 749 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_topK" + topK + "user" + username + ".0"); 750 } 751 else{ 752 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + "user" + username + ".0"); 753 } 754 737 755 if(modelFile.exists()){ 738 756 modelFile.delete(); 739 } 757 } 758 if(customModelFile.exists()){ 759 customModelFile.delete(); 760 } 740 761 try { 741 762 //System.out.println("file created"); 742 763 model.save(modelFile); 743 //System.out.println("saved"); 764 model.save(customModelFile); 765 System.out.println("model saved at: " + modelFile); 766 System.out.println("custom model saved at: " + customModelFile); 744 767 } catch (IOException ex) { 745 768 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); … … 753 776 System.out.println("trainList size: " + wayListSizeWithoutUnclassified); 754 777 778 if(wayListSizeWithoutUnclassified == 0){ 779 System.out.println("aborting training process with classes.."); 780 return; 781 } 755 782 //set classes for each osm instance 756 783 int sizeToBeAddedToArray = 0; //this will be used to proper init the features array, adding the multiple vectors size … … 864 891 865 892 File modelFile = new File(modelDirectory.getAbsolutePath()+"/model_with_classes"); 893 File customModelFile; 894 if(topKIsSelected){ 895 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_topK" + topK + "user" + username + ".1"); 896 } 897 else{ 898 customModelFile = new File(modelDirectory.getAbsolutePath()+"/" + inputFileName + "_model_c" + param + "_maxF" + frequency + "user" + username + ".1"); 899 } 900 901 if(customModelFile.exists()){ 902 customModelFile.delete(); 903 } 866 904 if(modelFile.exists()){ 867 905 modelFile.delete(); … … 870 908 //System.out.println("file created"); 871 909 model.save(modelFile); 872 //System.out.println("saved"); 910 model.save(customModelFile); 911 System.out.println("model with classes saved at: " + modelFile); 912 System.out.println("custom model with classes saved at: " + customModelFile); 873 913 } catch (IOException ex) { 874 914 Logger.getLogger(TrainByUser.class.getName()).log(Level.SEVERE, null, ex); -
applications/editors/josm/plugins/OSMRecPlugin/src/org/openstreetmap/josm/plugins/parsers/Mapper.java
r31402 r31554 22 22 private final HashMap<String, String> mappings; 23 23 private final HashMap<String, Integer> mappingsWithIDs; 24 private final Map<Integer, String> idsWithMappings; 24 25 25 26 public Mapper(){ … … 27 28 mappings = new LinkedHashMap<>(); 28 29 mappingsWithIDs = new HashMap<>(); 30 idsWithMappings = new HashMap<>(); 29 31 30 32 } … … 47 49 } 48 50 constructMappingsWithIDs(); 51 constructIDsWithMappings(); 49 52 System.out.println("Mappings file parsed successfully!"); 50 53 //LOG.info("Mappings file parsed successfully!"); … … 65 68 public Map<String,String> getMappings(){ 66 69 return Collections.unmodifiableMap(mappings); 67 } 70 } 71 72 private void constructIDsWithMappings(){ 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()){ 76 idsWithMappings.put(i, ontologyClass); 77 i++; 78 } 79 } 80 81 public Map<Integer, String> getIDsWithMappings(){ 82 return Collections.unmodifiableMap(idsWithMappings); 83 } 68 84 }
Note:
See TracChangeset
for help on using the changeset viewer.