You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Mar 19 18:13:56 2020
  5. @author: ljia
  6. """
  7. from gklearn.utils import dummy_node
  8. def construct_node_map_from_solver(solver, node_map, solution_id):
  9. node_map.clear()
  10. num_nodes_g = node_map.num_source_nodes()
  11. num_nodes_h = node_map.num_target_nodes()
  12. # add deletions and substitutions
  13. for row in range(0, num_nodes_g):
  14. col = solver.get_assigned_col(row, solution_id)
  15. if col >= num_nodes_h:
  16. node_map.add_assignment(row, dummy_node())
  17. else:
  18. node_map.add_assignment(row, col)
  19. # insertions.
  20. for col in range(0, num_nodes_h):
  21. if solver.get_assigned_row(col, solution_id) >= num_nodes_g:
  22. node_map.add_assignment(dummy_node(), col)
  23. def options_string_to_options_map(options_string):
  24. """Transforms an options string into an options map.
  25. Parameters
  26. ----------
  27. options_string : string
  28. Options string of the form "[--<option> <arg>] [...]".
  29. Return
  30. ------
  31. options_map : dict{string : string}
  32. Map with one key-value pair (<option>, <arg>) for each option contained in the string.
  33. """
  34. if options_string == '':
  35. return
  36. options_map = {}
  37. words = []
  38. tokenize(options_string, ' ', words)
  39. expect_option_name = True
  40. for word in words:
  41. if expect_option_name:
  42. is_opt_name, word = is_option_name(word)
  43. if is_opt_name:
  44. option_name = word
  45. if option_name in options_map:
  46. raise Exception('Multiple specification of option "' + option_name + '".')
  47. options_map[option_name] = ''
  48. else:
  49. raise Exception('Invalid options "' + options_string + '". Usage: options = "[--<option> <arg>] [...]"')
  50. else:
  51. is_opt_name, word = is_option_name(word)
  52. if is_opt_name:
  53. raise Exception('Invalid options "' + options_string + '". Usage: options = "[--<option> <arg>] [...]"')
  54. else:
  55. options_map[option_name] = word
  56. expect_option_name = not expect_option_name
  57. return options_map
  58. def tokenize(sentence, sep, words):
  59. """Separates a sentence into words separated by sep (unless contained in single quotes).
  60. Parameters
  61. ----------
  62. sentence : string
  63. The sentence that should be tokenized.
  64. sep : string
  65. The separator. Must be different from "'".
  66. words : list[string]
  67. The obtained words.
  68. """
  69. outside_quotes = True
  70. word_length = 0
  71. pos_word_start = 0
  72. for pos in range(0, len(sentence)):
  73. if sentence[pos] == '\'':
  74. if not outside_quotes and pos < len(sentence) - 1:
  75. if sentence[pos + 1] != sep:
  76. raise Exception('Sentence contains closing single quote which is followed by a char different from ' + sep + '.')
  77. word_length += 1
  78. outside_quotes = not outside_quotes
  79. elif outside_quotes and sentence[pos] == sep:
  80. if word_length > 0:
  81. words.append(sentence[pos_word_start:pos_word_start + word_length])
  82. pos_word_start = pos + 1
  83. word_length = 0
  84. else:
  85. word_length += 1
  86. if not outside_quotes:
  87. raise Exception('Sentence contains unbalanced single quotes.')
  88. if word_length > 0:
  89. words.append(sentence[pos_word_start:pos_word_start + word_length])
  90. def is_option_name(word):
  91. """Checks whether a word is an option name and, if so, removes the leading dashes.
  92. Parameters
  93. ----------
  94. word : string
  95. Word.
  96. return
  97. ------
  98. True if word is of the form "--<option>".
  99. word : string
  100. The word without the leading dashes.
  101. """
  102. if word[0] == '\'':
  103. word = word[1:len(word) - 2]
  104. return False, word
  105. if len(word) < 3:
  106. return False, word
  107. if word[0] == '-' and word[1] == '-' and word[2] != '-':
  108. word = word[2:]
  109. return True, word
  110. return False, word

A Python package for graph kernels, graph edit distances and graph pre-image problem.