featstruct.doctest 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ==================================
  4. Feature Structures & Unification
  5. ==================================
  6. >>> from __future__ import print_function
  7. >>> from nltk.featstruct import FeatStruct
  8. >>> from nltk.sem.logic import Variable, VariableExpression, Expression
  9. .. note:: For now, featstruct uses the older lambdalogic semantics
  10. module. Eventually, it should be updated to use the new first
  11. order predicate logic module.
  12. Overview
  13. ~~~~~~~~
  14. A feature structure is a mapping from feature identifiers to feature
  15. values, where feature values can be simple values (like strings or
  16. ints), nested feature structures, or variables:
  17. >>> fs1 = FeatStruct(number='singular', person=3)
  18. >>> print(fs1)
  19. [ number = 'singular' ]
  20. [ person = 3 ]
  21. Feature structure may be nested:
  22. >>> fs2 = FeatStruct(type='NP', agr=fs1)
  23. >>> print(fs2)
  24. [ agr = [ number = 'singular' ] ]
  25. [ [ person = 3 ] ]
  26. [ ]
  27. [ type = 'NP' ]
  28. Variables are used to indicate that two features should be assigned
  29. the same value. For example, the following feature structure requires
  30. that the feature fs3['agr']['number'] be bound to the same value as the
  31. feature fs3['subj']['number'].
  32. >>> fs3 = FeatStruct(agr=FeatStruct(number=Variable('?n')),
  33. ... subj=FeatStruct(number=Variable('?n')))
  34. >>> print(fs3)
  35. [ agr = [ number = ?n ] ]
  36. [ ]
  37. [ subj = [ number = ?n ] ]
  38. Feature structures are typically used to represent partial information
  39. about objects. A feature name that is not mapped to a value stands
  40. for a feature whose value is unknown (*not* a feature without a
  41. value). Two feature structures that represent (potentially
  42. overlapping) information about the same object can be combined by
  43. *unification*.
  44. >>> print(fs2.unify(fs3))
  45. [ agr = [ number = 'singular' ] ]
  46. [ [ person = 3 ] ]
  47. [ ]
  48. [ subj = [ number = 'singular' ] ]
  49. [ ]
  50. [ type = 'NP' ]
  51. When two inconsistent feature structures are unified, the unification
  52. fails and returns ``None``.
  53. >>> fs4 = FeatStruct(agr=FeatStruct(person=1))
  54. >>> print(fs4.unify(fs2))
  55. None
  56. >>> print(fs2.unify(fs4))
  57. None
  58. ..
  59. >>> del fs1, fs2, fs3, fs4 # clean-up
  60. Feature Structure Types
  61. -----------------------
  62. There are actually two types of feature structure:
  63. - *feature dictionaries*, implemented by `FeatDict`, act like
  64. Python dictionaries. Feature identifiers may be strings or
  65. instances of the `Feature` class.
  66. - *feature lists*, implemented by `FeatList`, act like Python
  67. lists. Feature identifiers are integers.
  68. When you construct a feature structure using the `FeatStruct`
  69. constructor, it will automatically decide which type is appropriate:
  70. >>> type(FeatStruct(number='singular'))
  71. <class 'nltk.featstruct.FeatDict'>
  72. >>> type(FeatStruct([1,2,3]))
  73. <class 'nltk.featstruct.FeatList'>
  74. Usually, we will just use feature dictionaries; but sometimes feature
  75. lists can be useful too. Two feature lists will unify with each other
  76. only if they have equal lengths, and all of their feature values
  77. match. If you wish to write a feature list that contains 'unknown'
  78. values, you must use variables:
  79. >>> fs1 = FeatStruct([1,2,Variable('?y')])
  80. >>> fs2 = FeatStruct([1,Variable('?x'),3])
  81. >>> fs1.unify(fs2)
  82. [1, 2, 3]
  83. ..
  84. >>> del fs1, fs2 # clean-up
  85. Parsing Feature Structure Strings
  86. ---------------------------------
  87. Feature structures can be constructed directly from strings. Often,
  88. this is more convenient than constructing them directly. NLTK can
  89. parse most feature strings to produce the corresponding feature
  90. structures. (But you must restrict your base feature values to
  91. strings, ints, logic expressions (`nltk.sem.logic.Expression`), and a
  92. few other types discussed below).
  93. Feature dictionaries are written like Python dictionaries, except that
  94. keys are not put in quotes; and square brackets (``[]``) are used
  95. instead of braces (``{}``):
  96. >>> FeatStruct('[tense="past", agr=[number="sing", person=3]]')
  97. [agr=[number='sing', person=3], tense='past']
  98. If a feature value is a single alphanumeric word, then it does not
  99. need to be quoted -- it will be automatically treated as a string:
  100. >>> FeatStruct('[tense=past, agr=[number=sing, person=3]]')
  101. [agr=[number='sing', person=3], tense='past']
  102. Feature lists are written like python lists:
  103. >>> FeatStruct('[1, 2, 3]')
  104. [1, 2, 3]
  105. The expression ``[]`` is treated as an empty feature dictionary, not
  106. an empty feature list:
  107. >>> type(FeatStruct('[]'))
  108. <class 'nltk.featstruct.FeatDict'>
  109. Feature Paths
  110. -------------
  111. Features can be specified using *feature paths*, or tuples of feature
  112. identifiers that specify path through the nested feature structures to
  113. a value.
  114. >>> fs1 = FeatStruct('[x=1, y=[1,2,[z=3]]]')
  115. >>> fs1['y']
  116. [1, 2, [z=3]]
  117. >>> fs1['y', 2]
  118. [z=3]
  119. >>> fs1['y', 2, 'z']
  120. 3
  121. ..
  122. >>> del fs1 # clean-up
  123. Reentrance
  124. ----------
  125. Feature structures may contain reentrant feature values. A *reentrant
  126. feature value* is a single feature structure that can be accessed via
  127. multiple feature paths.
  128. >>> fs1 = FeatStruct(x='val')
  129. >>> fs2 = FeatStruct(a=fs1, b=fs1)
  130. >>> print(fs2)
  131. [ a = (1) [ x = 'val' ] ]
  132. [ ]
  133. [ b -> (1) ]
  134. >>> fs2
  135. [a=(1)[x='val'], b->(1)]
  136. As you can see, reentrane is displayed by marking a feature structure
  137. with a unique identifier, in this case ``(1)``, the first time it is
  138. encountered; and then using the special form ``var -> id`` whenever it
  139. is encountered again. You can use the same notation to directly
  140. create reentrant feature structures from strings.
  141. >>> FeatStruct('[a=(1)[], b->(1), c=[d->(1)]]')
  142. [a=(1)[], b->(1), c=[d->(1)]]
  143. Reentrant feature structures may contain cycles:
  144. >>> fs3 = FeatStruct('(1)[a->(1)]')
  145. >>> fs3['a', 'a', 'a', 'a']
  146. (1)[a->(1)]
  147. >>> fs3['a', 'a', 'a', 'a'] is fs3
  148. True
  149. Unification preserves the reentrance relations imposed by both of the
  150. unified feature structures. In the feature structure resulting from
  151. unification, any modifications to a reentrant feature value will be
  152. visible using any of its feature paths.
  153. >>> fs3.unify(FeatStruct('[a=[b=12], c=33]'))
  154. (1)[a->(1), b=12, c=33]
  155. ..
  156. >>> del fs1, fs2, fs3 # clean-up
  157. Feature Structure Equality
  158. --------------------------
  159. Two feature structures are considered equal if they assign the same
  160. values to all features, *and* they contain the same reentrances.
  161. >>> fs1 = FeatStruct('[a=(1)[x=1], b->(1)]')
  162. >>> fs2 = FeatStruct('[a=(1)[x=1], b->(1)]')
  163. >>> fs3 = FeatStruct('[a=[x=1], b=[x=1]]')
  164. >>> fs1 == fs1, fs1 is fs1
  165. (True, True)
  166. >>> fs1 == fs2, fs1 is fs2
  167. (True, False)
  168. >>> fs1 == fs3, fs1 is fs3
  169. (False, False)
  170. Note that this differs from how Python dictionaries and lists define
  171. equality -- in particular, Python dictionaries and lists ignore
  172. reentrance relations. To test two feature structures for equality
  173. while ignoring reentrance relations, use the `equal_values()` method:
  174. >>> fs1.equal_values(fs1)
  175. True
  176. >>> fs1.equal_values(fs2)
  177. True
  178. >>> fs1.equal_values(fs3)
  179. True
  180. ..
  181. >>> del fs1, fs2, fs3 # clean-up
  182. Feature Value Sets & Feature Value Tuples
  183. -----------------------------------------
  184. `nltk.featstruct` defines two new data types that are intended to be
  185. used as feature values: `FeatureValueTuple` and `FeatureValueSet`.
  186. Both of these types are considered base values -- i.e., unification
  187. does *not* apply to them. However, variable binding *does* apply to
  188. any values that they contain.
  189. Feature value tuples are written with parentheses:
  190. >>> fs1 = FeatStruct('[x=(?x, ?y)]')
  191. >>> fs1
  192. [x=(?x, ?y)]
  193. >>> fs1.substitute_bindings({Variable('?x'): 1, Variable('?y'): 2})
  194. [x=(1, 2)]
  195. Feature sets are written with braces:
  196. >>> fs1 = FeatStruct('[x={?x, ?y}]')
  197. >>> fs1
  198. [x={?x, ?y}]
  199. >>> fs1.substitute_bindings({Variable('?x'): 1, Variable('?y'): 2})
  200. [x={1, 2}]
  201. In addition to the basic feature value tuple & set classes, nltk
  202. defines feature value unions (for sets) and feature value
  203. concatenations (for tuples). These are written using '+', and can be
  204. used to combine sets & tuples:
  205. >>> fs1 = FeatStruct('[x=((1, 2)+?z), z=?z]')
  206. >>> fs1
  207. [x=((1, 2)+?z), z=?z]
  208. >>> fs1.unify(FeatStruct('[z=(3, 4, 5)]'))
  209. [x=(1, 2, 3, 4, 5), z=(3, 4, 5)]
  210. Thus, feature value tuples and sets can be used to build up tuples
  211. and sets of values over the corse of unification. For example, when
  212. parsing sentences using a semantic feature grammar, feature sets or
  213. feature tuples can be used to build a list of semantic predicates as
  214. the sentence is parsed.
  215. As was mentioned above, unification does not apply to feature value
  216. tuples and sets. One reason for this that it's impossible to define a
  217. single correct answer for unification when concatenation is used.
  218. Consider the following example:
  219. >>> fs1 = FeatStruct('[x=(1, 2, 3, 4)]')
  220. >>> fs2 = FeatStruct('[x=(?a+?b), a=?a, b=?b]')
  221. If unification applied to feature tuples, then the unification
  222. algorithm would have to arbitrarily choose how to divide the tuple
  223. (1,2,3,4) into two parts. Instead, the unification algorithm refuses
  224. to make this decision, and simply unifies based on value. Because
  225. (1,2,3,4) is not equal to (?a+?b), fs1 and fs2 will not unify:
  226. >>> print(fs1.unify(fs2))
  227. None
  228. If you need a list-like structure that unification does apply to, use
  229. `FeatList`.
  230. ..
  231. >>> del fs1, fs2 # clean-up
  232. Light-weight Feature Structures
  233. -------------------------------
  234. Many of the functions defined by `nltk.featstruct` can be applied
  235. directly to simple Python dictionaries and lists, rather than to
  236. full-fledged `FeatDict` and `FeatList` objects. In other words,
  237. Python ``dicts`` and ``lists`` can be used as "light-weight" feature
  238. structures.
  239. >>> # Note: pprint prints dicts sorted
  240. >>> from pprint import pprint
  241. >>> from nltk.featstruct import unify
  242. >>> pprint(unify(dict(x=1, y=dict()), dict(a='a', y=dict(b='b'))))
  243. {'a': 'a', 'x': 1, 'y': {'b': 'b'}}
  244. However, you should keep in mind the following caveats:
  245. - Python dictionaries & lists ignore reentrance when checking for
  246. equality between values. But two FeatStructs with different
  247. reentrances are considered nonequal, even if all their base
  248. values are equal.
  249. - FeatStructs can be easily frozen, allowing them to be used as
  250. keys in hash tables. Python dictionaries and lists can not.
  251. - FeatStructs display reentrance in their string representations;
  252. Python dictionaries and lists do not.
  253. - FeatStructs may *not* be mixed with Python dictionaries and lists
  254. (e.g., when performing unification).
  255. - FeatStructs provide a number of useful methods, such as `walk()`
  256. and `cyclic()`, which are not available for Python dicts & lists.
  257. In general, if your feature structures will contain any reentrances,
  258. or if you plan to use them as dictionary keys, it is strongly
  259. recommended that you use full-fledged `FeatStruct` objects.
  260. Custom Feature Values
  261. ---------------------
  262. The abstract base class `CustomFeatureValue` can be used to define new
  263. base value types that have custom unification methods. For example,
  264. the following feature value type encodes a range, and defines
  265. unification as taking the intersection on the ranges:
  266. >>> from functools import total_ordering
  267. >>> from nltk.featstruct import CustomFeatureValue, UnificationFailure
  268. >>> @total_ordering
  269. ... class Range(CustomFeatureValue):
  270. ... def __init__(self, low, high):
  271. ... assert low <= high
  272. ... self.low = low
  273. ... self.high = high
  274. ... def unify(self, other):
  275. ... if not isinstance(other, Range):
  276. ... return UnificationFailure
  277. ... low = max(self.low, other.low)
  278. ... high = min(self.high, other.high)
  279. ... if low <= high: return Range(low, high)
  280. ... else: return UnificationFailure
  281. ... def __repr__(self):
  282. ... return '(%s<x<%s)' % (self.low, self.high)
  283. ... def __eq__(self, other):
  284. ... if not isinstance(other, Range):
  285. ... return False
  286. ... return (self.low == other.low) and (self.high == other.high)
  287. ... def __lt__(self, other):
  288. ... if not isinstance(other, Range):
  289. ... return True
  290. ... return (self.low, self.high) < (other.low, other.high)
  291. >>> fs1 = FeatStruct(x=Range(5,8), y=FeatStruct(z=Range(7,22)))
  292. >>> print(fs1.unify(FeatStruct(x=Range(6, 22))))
  293. [ x = (6<x<8) ]
  294. [ ]
  295. [ y = [ z = (7<x<22) ] ]
  296. >>> print(fs1.unify(FeatStruct(x=Range(9, 12))))
  297. None
  298. >>> print(fs1.unify(FeatStruct(x=12)))
  299. None
  300. >>> print(fs1.unify(FeatStruct('[x=?x, y=[z=?x]]')))
  301. [ x = (7<x<8) ]
  302. [ ]
  303. [ y = [ z = (7<x<8) ] ]
  304. Regression Tests
  305. ~~~~~~~~~~~~~~~~
  306. Dictionary access methods (non-mutating)
  307. ----------------------------------------
  308. >>> fs1 = FeatStruct(a=1, b=2, c=3)
  309. >>> fs2 = FeatStruct(x=fs1, y='x')
  310. Feature structures support all dictionary methods (excluding the class
  311. method `dict.fromkeys()`). Non-mutating methods:
  312. >>> sorted(fs2.keys()) # keys()
  313. ['x', 'y']
  314. >>> sorted(fs2.values()) # values()
  315. [[a=1, b=2, c=3], 'x']
  316. >>> sorted(fs2.items()) # items()
  317. [('x', [a=1, b=2, c=3]), ('y', 'x')]
  318. >>> sorted(fs2) # __iter__()
  319. ['x', 'y']
  320. >>> 'a' in fs2, 'x' in fs2 # __contains__()
  321. (False, True)
  322. >>> fs2.has_key('a'), fs2.has_key('x') # has_key()
  323. (False, True)
  324. >>> fs2['x'], fs2['y'] # __getitem__()
  325. ([a=1, b=2, c=3], 'x')
  326. >>> fs2['a'] # __getitem__()
  327. Traceback (most recent call last):
  328. . . .
  329. KeyError: 'a'
  330. >>> fs2.get('x'), fs2.get('y'), fs2.get('a') # get()
  331. ([a=1, b=2, c=3], 'x', None)
  332. >>> fs2.get('x', 'hello'), fs2.get('a', 'hello') # get()
  333. ([a=1, b=2, c=3], 'hello')
  334. >>> len(fs1), len(fs2) # __len__
  335. (3, 2)
  336. >>> fs2.copy() # copy()
  337. [x=[a=1, b=2, c=3], y='x']
  338. >>> fs2.copy() is fs2 # copy()
  339. False
  340. Note: by default, `FeatStruct.copy()` does a deep copy. Use
  341. `FeatStruct.copy(deep=False)` for a shallow copy.
  342. ..
  343. >>> del fs1, fs2 # clean-up.
  344. Dictionary access methods (mutating)
  345. ------------------------------------
  346. >>> fs1 = FeatStruct(a=1, b=2, c=3)
  347. >>> fs2 = FeatStruct(x=fs1, y='x')
  348. Setting features (`__setitem__()`)
  349. >>> fs1['c'] = 5
  350. >>> fs1
  351. [a=1, b=2, c=5]
  352. >>> fs1['x'] = 12
  353. >>> fs1
  354. [a=1, b=2, c=5, x=12]
  355. >>> fs2['x', 'a'] = 2
  356. >>> fs2
  357. [x=[a=2, b=2, c=5, x=12], y='x']
  358. >>> fs1
  359. [a=2, b=2, c=5, x=12]
  360. Deleting features (`__delitem__()`)
  361. >>> del fs1['x']
  362. >>> fs1
  363. [a=2, b=2, c=5]
  364. >>> del fs2['x', 'a']
  365. >>> fs1
  366. [b=2, c=5]
  367. `setdefault()`:
  368. >>> fs1.setdefault('b', 99)
  369. 2
  370. >>> fs1
  371. [b=2, c=5]
  372. >>> fs1.setdefault('x', 99)
  373. 99
  374. >>> fs1
  375. [b=2, c=5, x=99]
  376. `update()`:
  377. >>> fs2.update({'a':'A', 'b':'B'}, c='C')
  378. >>> fs2
  379. [a='A', b='B', c='C', x=[b=2, c=5, x=99], y='x']
  380. `pop()`:
  381. >>> fs2.pop('a')
  382. 'A'
  383. >>> fs2
  384. [b='B', c='C', x=[b=2, c=5, x=99], y='x']
  385. >>> fs2.pop('a')
  386. Traceback (most recent call last):
  387. . . .
  388. KeyError: 'a'
  389. >>> fs2.pop('a', 'foo')
  390. 'foo'
  391. >>> fs2
  392. [b='B', c='C', x=[b=2, c=5, x=99], y='x']
  393. `clear()`:
  394. >>> fs1.clear()
  395. >>> fs1
  396. []
  397. >>> fs2
  398. [b='B', c='C', x=[], y='x']
  399. `popitem()`:
  400. >>> sorted([fs2.popitem() for i in range(len(fs2))])
  401. [('b', 'B'), ('c', 'C'), ('x', []), ('y', 'x')]
  402. >>> fs2
  403. []
  404. Once a feature structure has been frozen, it may not be mutated.
  405. >>> fs1 = FeatStruct('[x=1, y=2, z=[a=3]]')
  406. >>> fs1.freeze()
  407. >>> fs1.frozen()
  408. True
  409. >>> fs1['z'].frozen()
  410. True
  411. >>> fs1['x'] = 5
  412. Traceback (most recent call last):
  413. . . .
  414. ValueError: Frozen FeatStructs may not be modified.
  415. >>> del fs1['x']
  416. Traceback (most recent call last):
  417. . . .
  418. ValueError: Frozen FeatStructs may not be modified.
  419. >>> fs1.clear()
  420. Traceback (most recent call last):
  421. . . .
  422. ValueError: Frozen FeatStructs may not be modified.
  423. >>> fs1.pop('x')
  424. Traceback (most recent call last):
  425. . . .
  426. ValueError: Frozen FeatStructs may not be modified.
  427. >>> fs1.popitem()
  428. Traceback (most recent call last):
  429. . . .
  430. ValueError: Frozen FeatStructs may not be modified.
  431. >>> fs1.setdefault('x')
  432. Traceback (most recent call last):
  433. . . .
  434. ValueError: Frozen FeatStructs may not be modified.
  435. >>> fs1.update(z=22)
  436. Traceback (most recent call last):
  437. . . .
  438. ValueError: Frozen FeatStructs may not be modified.
  439. ..
  440. >>> del fs1, fs2 # clean-up.
  441. Feature Paths
  442. -------------
  443. Make sure that __getitem__ with feature paths works as intended:
  444. >>> fs1 = FeatStruct(a=1, b=2,
  445. ... c=FeatStruct(
  446. ... d=FeatStruct(e=12),
  447. ... f=FeatStruct(g=55, h='hello')))
  448. >>> fs1[()]
  449. [a=1, b=2, c=[d=[e=12], f=[g=55, h='hello']]]
  450. >>> fs1['a'], fs1[('a',)]
  451. (1, 1)
  452. >>> fs1['c','d','e']
  453. 12
  454. >>> fs1['c','f','g']
  455. 55
  456. Feature paths that select unknown features raise KeyError:
  457. >>> fs1['c', 'f', 'e']
  458. Traceback (most recent call last):
  459. . . .
  460. KeyError: ('c', 'f', 'e')
  461. >>> fs1['q', 'p']
  462. Traceback (most recent call last):
  463. . . .
  464. KeyError: ('q', 'p')
  465. Feature paths that try to go 'through' a feature that's not a feature
  466. structure raise KeyError:
  467. >>> fs1['a', 'b']
  468. Traceback (most recent call last):
  469. . . .
  470. KeyError: ('a', 'b')
  471. Feature paths can go through reentrant structures:
  472. >>> fs2 = FeatStruct('(1)[a=[b=[c->(1), d=5], e=11]]')
  473. >>> fs2['a', 'b', 'c', 'a', 'e']
  474. 11
  475. >>> fs2['a', 'b', 'c', 'a', 'b', 'd']
  476. 5
  477. >>> fs2[tuple('abcabcabcabcabcabcabcabcabcabca')]
  478. (1)[b=[c=[a->(1)], d=5], e=11]
  479. Indexing requires strings, `Feature`\s, or tuples; other types raise a
  480. TypeError:
  481. >>> fs2[12]
  482. Traceback (most recent call last):
  483. . . .
  484. TypeError: Expected feature name or path. Got 12.
  485. >>> fs2[list('abc')]
  486. Traceback (most recent call last):
  487. . . .
  488. TypeError: Expected feature name or path. Got ['a', 'b', 'c'].
  489. Feature paths can also be used with `get()`, `has_key()`, and
  490. `__contains__()`.
  491. >>> fpath1 = tuple('abcabc')
  492. >>> fpath2 = tuple('abcabz')
  493. >>> fs2.get(fpath1), fs2.get(fpath2)
  494. ((1)[a=[b=[c->(1), d=5], e=11]], None)
  495. >>> fpath1 in fs2, fpath2 in fs2
  496. (True, False)
  497. >>> fs2.has_key(fpath1), fs2.has_key(fpath2)
  498. (True, False)
  499. ..
  500. >>> del fs1, fs2 # clean-up
  501. Reading Feature Structures
  502. --------------------------
  503. Empty feature struct:
  504. >>> FeatStruct('[]')
  505. []
  506. Test features with integer values:
  507. >>> FeatStruct('[a=12, b=-33, c=0]')
  508. [a=12, b=-33, c=0]
  509. Test features with string values. Either single or double quotes may
  510. be used. Strings are evaluated just like python strings -- in
  511. particular, you can use escape sequences and 'u' and 'r' prefixes, and
  512. triple-quoted strings.
  513. >>> FeatStruct('[a="", b="hello", c="\'", d=\'\', e=\'"\']')
  514. [a='', b='hello', c="'", d='', e='"']
  515. >>> FeatStruct(r'[a="\\", b="\"", c="\x6f\\y", d="12"]')
  516. [a='\\', b='"', c='o\\y', d='12']
  517. >>> FeatStruct(r'[b=r"a\b\c"]')
  518. [b='a\\b\\c']
  519. >>> FeatStruct('[x="""a"""]')
  520. [x='a']
  521. Test parsing of reentrant feature structures.
  522. >>> FeatStruct('[a=(1)[], b->(1)]')
  523. [a=(1)[], b->(1)]
  524. >>> FeatStruct('[a=(1)[x=1, y=2], b->(1)]')
  525. [a=(1)[x=1, y=2], b->(1)]
  526. Test parsing of cyclic feature structures.
  527. >>> FeatStruct('[a=(1)[b->(1)]]')
  528. [a=(1)[b->(1)]]
  529. >>> FeatStruct('(1)[a=[b=[c->(1)]]]')
  530. (1)[a=[b=[c->(1)]]]
  531. Strings of the form "+name" and "-name" may be used to specify boolean
  532. values.
  533. >>> FeatStruct('[-bar, +baz, +foo]')
  534. [-bar, +baz, +foo]
  535. None, True, and False are recognized as values:
  536. >>> FeatStruct('[bar=True, baz=False, foo=None]')
  537. [+bar, -baz, foo=None]
  538. Special features:
  539. >>> FeatStruct('NP/VP')
  540. NP[]/VP[]
  541. >>> FeatStruct('?x/?x')
  542. ?x[]/?x[]
  543. >>> print(FeatStruct('VP[+fin, agr=?x, tense=past]/NP[+pl, agr=?x]'))
  544. [ *type* = 'VP' ]
  545. [ ]
  546. [ [ *type* = 'NP' ] ]
  547. [ *slash* = [ agr = ?x ] ]
  548. [ [ pl = True ] ]
  549. [ ]
  550. [ agr = ?x ]
  551. [ fin = True ]
  552. [ tense = 'past' ]
  553. Here the slash feature gets coerced:
  554. >>> FeatStruct('[*slash*=a, x=b, *type*="NP"]')
  555. NP[x='b']/a[]
  556. >>> FeatStruct('NP[sem=<bob>]/NP')
  557. NP[sem=<bob>]/NP[]
  558. >>> FeatStruct('S[sem=<walk(bob)>]')
  559. S[sem=<walk(bob)>]
  560. >>> print(FeatStruct('NP[sem=<bob>]/NP'))
  561. [ *type* = 'NP' ]
  562. [ ]
  563. [ *slash* = [ *type* = 'NP' ] ]
  564. [ ]
  565. [ sem = <bob> ]
  566. Playing with ranges:
  567. >>> from nltk.featstruct import RangeFeature, FeatStructReader
  568. >>> width = RangeFeature('width')
  569. >>> reader = FeatStructReader([width])
  570. >>> fs1 = reader.fromstring('[*width*=-5:12]')
  571. >>> fs2 = reader.fromstring('[*width*=2:123]')
  572. >>> fs3 = reader.fromstring('[*width*=-7:-2]')
  573. >>> fs1.unify(fs2)
  574. [*width*=(2, 12)]
  575. >>> fs1.unify(fs3)
  576. [*width*=(-5, -2)]
  577. >>> print(fs2.unify(fs3)) # no overlap in width.
  578. None
  579. The slash feature has a default value of 'False':
  580. >>> print(FeatStruct('NP[]/VP').unify(FeatStruct('NP[]'), trace=1))
  581. <BLANKLINE>
  582. Unification trace:
  583. / NP[]/VP[]
  584. |\ NP[]
  585. |
  586. | Unify feature: *type*
  587. | / 'NP'
  588. | |\ 'NP'
  589. | |
  590. | +-->'NP'
  591. |
  592. | Unify feature: *slash*
  593. | / VP[]
  594. | |\ False
  595. | |
  596. X X <-- FAIL
  597. None
  598. The demo structures from category.py. They all parse, but they don't
  599. do quite the right thing, -- ?x vs x.
  600. >>> FeatStruct(pos='n', agr=FeatStruct(number='pl', gender='f'))
  601. [agr=[gender='f', number='pl'], pos='n']
  602. >>> FeatStruct(r'NP[sem=<bob>]/NP')
  603. NP[sem=<bob>]/NP[]
  604. >>> FeatStruct(r'S[sem=<app(?x, ?y)>]')
  605. S[sem=<?x(?y)>]
  606. >>> FeatStruct('?x/?x')
  607. ?x[]/?x[]
  608. >>> FeatStruct('VP[+fin, agr=?x, tense=past]/NP[+pl, agr=?x]')
  609. VP[agr=?x, +fin, tense='past']/NP[agr=?x, +pl]
  610. >>> FeatStruct('S[sem = <app(?subj, ?vp)>]')
  611. S[sem=<?subj(?vp)>]
  612. >>> FeatStruct('S')
  613. S[]
  614. The parser also includes support for reading sets and tuples.
  615. >>> FeatStruct('[x={1,2,2,2}, y={/}]')
  616. [x={1, 2}, y={/}]
  617. >>> FeatStruct('[x=(1,2,2,2), y=()]')
  618. [x=(1, 2, 2, 2), y=()]
  619. >>> print(FeatStruct('[x=(1,[z=(1,2,?x)],?z,{/})]'))
  620. [ x = (1, [ z = (1, 2, ?x) ], ?z, {/}) ]
  621. Note that we can't put a featstruct inside a tuple, because doing so
  622. would hash it, and it's not frozen yet:
  623. >>> print(FeatStruct('[x={[]}]'))
  624. Traceback (most recent call last):
  625. . . .
  626. TypeError: FeatStructs must be frozen before they can be hashed.
  627. There's a special syntax for taking the union of sets: "{...+...}".
  628. The elements should only be variables or sets.
  629. >>> FeatStruct('[x={?a+?b+{1,2,3}}]')
  630. [x={?a+?b+{1, 2, 3}}]
  631. There's a special syntax for taking the concatenation of tuples:
  632. "(...+...)". The elements should only be variables or tuples.
  633. >>> FeatStruct('[x=(?a+?b+(1,2,3))]')
  634. [x=(?a+?b+(1, 2, 3))]
  635. Parsing gives helpful messages if your string contains an error.
  636. >>> FeatStruct('[a=, b=5]]')
  637. Traceback (most recent call last):
  638. . . .
  639. ValueError: Error parsing feature structure
  640. [a=, b=5]]
  641. ^ Expected value
  642. >>> FeatStruct('[a=12 22, b=33]')
  643. Traceback (most recent call last):
  644. . . .
  645. ValueError: Error parsing feature structure
  646. [a=12 22, b=33]
  647. ^ Expected comma
  648. >>> FeatStruct('[a=5] [b=6]')
  649. Traceback (most recent call last):
  650. . . .
  651. ValueError: Error parsing feature structure
  652. [a=5] [b=6]
  653. ^ Expected end of string
  654. >>> FeatStruct(' *++*')
  655. Traceback (most recent call last):
  656. . . .
  657. ValueError: Error parsing feature structure
  658. *++*
  659. ^ Expected open bracket or identifier
  660. >>> FeatStruct('[x->(1)]')
  661. Traceback (most recent call last):
  662. . . .
  663. ValueError: Error parsing feature structure
  664. [x->(1)]
  665. ^ Expected bound identifier
  666. >>> FeatStruct('[x->y]')
  667. Traceback (most recent call last):
  668. . . .
  669. ValueError: Error parsing feature structure
  670. [x->y]
  671. ^ Expected identifier
  672. >>> FeatStruct('')
  673. Traceback (most recent call last):
  674. . . .
  675. ValueError: Error parsing feature structure
  676. <BLANKLINE>
  677. ^ Expected open bracket or identifier
  678. Unification
  679. -----------
  680. Very simple unifications give the expected results:
  681. >>> FeatStruct().unify(FeatStruct())
  682. []
  683. >>> FeatStruct(number='singular').unify(FeatStruct())
  684. [number='singular']
  685. >>> FeatStruct().unify(FeatStruct(number='singular'))
  686. [number='singular']
  687. >>> FeatStruct(number='singular').unify(FeatStruct(person=3))
  688. [number='singular', person=3]
  689. Merging nested structures:
  690. >>> fs1 = FeatStruct('[A=[B=b]]')
  691. >>> fs2 = FeatStruct('[A=[C=c]]')
  692. >>> fs1.unify(fs2)
  693. [A=[B='b', C='c']]
  694. >>> fs2.unify(fs1)
  695. [A=[B='b', C='c']]
  696. A basic case of reentrant unification
  697. >>> fs4 = FeatStruct('[A=(1)[B=b], E=[F->(1)]]')
  698. >>> fs5 = FeatStruct("[A=[C='c'], E=[F=[D='d']]]")
  699. >>> fs4.unify(fs5)
  700. [A=(1)[B='b', C='c', D='d'], E=[F->(1)]]
  701. >>> fs5.unify(fs4)
  702. [A=(1)[B='b', C='c', D='d'], E=[F->(1)]]
  703. More than 2 paths to a value
  704. >>> fs1 = FeatStruct("[a=[],b=[],c=[],d=[]]")
  705. >>> fs2 = FeatStruct('[a=(1)[], b->(1), c->(1), d->(1)]')
  706. >>> fs1.unify(fs2)
  707. [a=(1)[], b->(1), c->(1), d->(1)]
  708. fs1[a] gets unified with itself
  709. >>> fs1 = FeatStruct('[x=(1)[], y->(1)]')
  710. >>> fs2 = FeatStruct('[x=(1)[], y->(1)]')
  711. >>> fs1.unify(fs2)
  712. [x=(1)[], y->(1)]
  713. Bound variables should get forwarded appropriately
  714. >>> fs1 = FeatStruct('[A=(1)[X=x], B->(1), C=?cvar, D=?dvar]')
  715. >>> fs2 = FeatStruct('[A=(1)[Y=y], B=(2)[Z=z], C->(1), D->(2)]')
  716. >>> fs1.unify(fs2)
  717. [A=(1)[X='x', Y='y', Z='z'], B->(1), C->(1), D->(1)]
  718. >>> fs2.unify(fs1)
  719. [A=(1)[X='x', Y='y', Z='z'], B->(1), C->(1), D->(1)]
  720. Cyclic structure created by unification.
  721. >>> fs1 = FeatStruct('[F=(1)[], G->(1)]')
  722. >>> fs2 = FeatStruct('[F=[H=(2)[]], G->(2)]')
  723. >>> fs3 = fs1.unify(fs2)
  724. >>> fs3
  725. [F=(1)[H->(1)], G->(1)]
  726. >>> fs3['F'] is fs3['G']
  727. True
  728. >>> fs3['F'] is fs3['G']['H']
  729. True
  730. >>> fs3['F'] is fs3['G']['H']['H']
  731. True
  732. >>> fs3['F'] is fs3['F']['H']['H']['H']['H']['H']['H']['H']['H']
  733. True
  734. Cyclic structure created w/ variables.
  735. >>> fs1 = FeatStruct('[F=[H=?x]]')
  736. >>> fs2 = FeatStruct('[F=?x]')
  737. >>> fs3 = fs1.unify(fs2, rename_vars=False)
  738. >>> fs3
  739. [F=(1)[H->(1)]]
  740. >>> fs3['F'] is fs3['F']['H']
  741. True
  742. >>> fs3['F'] is fs3['F']['H']['H']
  743. True
  744. >>> fs3['F'] is fs3['F']['H']['H']['H']['H']['H']['H']['H']['H']
  745. True
  746. Unifying w/ a cyclic feature structure.
  747. >>> fs4 = FeatStruct('[F=[H=[H=[H=(1)[]]]], K->(1)]')
  748. >>> fs3.unify(fs4)
  749. [F=(1)[H->(1)], K->(1)]
  750. >>> fs4.unify(fs3)
  751. [F=(1)[H->(1)], K->(1)]
  752. Variable bindings should preserve reentrance.
  753. >>> bindings = {}
  754. >>> fs1 = FeatStruct("[a=?x]")
  755. >>> fs2 = fs1.unify(FeatStruct("[a=[]]"), bindings)
  756. >>> fs2['a'] is bindings[Variable('?x')]
  757. True
  758. >>> fs2.unify(FeatStruct("[b=?x]"), bindings)
  759. [a=(1)[], b->(1)]
  760. Aliased variable tests
  761. >>> fs1 = FeatStruct("[a=?x, b=?x]")
  762. >>> fs2 = FeatStruct("[b=?y, c=?y]")
  763. >>> bindings = {}
  764. >>> fs3 = fs1.unify(fs2, bindings)
  765. >>> fs3
  766. [a=?x, b=?x, c=?x]
  767. >>> bindings
  768. {Variable('?y'): Variable('?x')}
  769. >>> fs3.unify(FeatStruct("[a=1]"))
  770. [a=1, b=1, c=1]
  771. If we keep track of the bindings, then we can use the same variable
  772. over multiple calls to unify.
  773. >>> bindings = {}
  774. >>> fs1 = FeatStruct('[a=?x]')
  775. >>> fs2 = fs1.unify(FeatStruct('[a=[]]'), bindings)
  776. >>> fs2.unify(FeatStruct('[b=?x]'), bindings)
  777. [a=(1)[], b->(1)]
  778. >>> bindings
  779. {Variable('?x'): []}
  780. ..
  781. >>> del fs1, fs2, fs3, fs4, fs5 # clean-up
  782. Unification Bindings
  783. --------------------
  784. >>> bindings = {}
  785. >>> fs1 = FeatStruct('[a=?x]')
  786. >>> fs2 = FeatStruct('[a=12]')
  787. >>> fs3 = FeatStruct('[b=?x]')
  788. >>> fs1.unify(fs2, bindings)
  789. [a=12]
  790. >>> bindings
  791. {Variable('?x'): 12}
  792. >>> fs3.substitute_bindings(bindings)
  793. [b=12]
  794. >>> fs3 # substitute_bindings didn't mutate fs3.
  795. [b=?x]
  796. >>> fs2.unify(fs3, bindings)
  797. [a=12, b=12]
  798. >>> bindings = {}
  799. >>> fs1 = FeatStruct('[a=?x, b=1]')
  800. >>> fs2 = FeatStruct('[a=5, b=?x]')
  801. >>> fs1.unify(fs2, bindings)
  802. [a=5, b=1]
  803. >>> sorted(bindings.items())
  804. [(Variable('?x'), 5), (Variable('?x2'), 1)]
  805. ..
  806. >>> del fs1, fs2, fs3 # clean-up
  807. Expressions
  808. -----------
  809. >>> e = Expression.fromstring('\\P y.P(z,y)')
  810. >>> fs1 = FeatStruct(x=e, y=Variable('z'))
  811. >>> fs2 = FeatStruct(y=VariableExpression(Variable('John')))
  812. >>> fs1.unify(fs2)
  813. [x=<\P y.P(John,y)>, y=<John>]
  814. Remove Variables
  815. ----------------
  816. >>> FeatStruct('[a=?x, b=12, c=[d=?y]]').remove_variables()
  817. [b=12, c=[]]
  818. >>> FeatStruct('(1)[a=[b=?x,c->(1)]]').remove_variables()
  819. (1)[a=[c->(1)]]
  820. Equality & Hashing
  821. ------------------
  822. The `equal_values` method checks whether two feature structures assign
  823. the same value to every feature. If the optional argument
  824. ``check_reentrances`` is supplied, then it also returns false if there
  825. is any difference in the reentrances.
  826. >>> a = FeatStruct('(1)[x->(1)]')
  827. >>> b = FeatStruct('(1)[x->(1)]')
  828. >>> c = FeatStruct('(1)[x=[x->(1)]]')
  829. >>> d = FeatStruct('[x=(1)[x->(1)]]')
  830. >>> e = FeatStruct('(1)[x=[x->(1), y=1], y=1]')
  831. >>> def compare(x,y):
  832. ... assert x.equal_values(y, True) == y.equal_values(x, True)
  833. ... assert x.equal_values(y, False) == y.equal_values(x, False)
  834. ... if x.equal_values(y, True):
  835. ... assert x.equal_values(y, False)
  836. ... print('equal values, same reentrance')
  837. ... elif x.equal_values(y, False):
  838. ... print('equal values, different reentrance')
  839. ... else:
  840. ... print('different values')
  841. >>> compare(a, a)
  842. equal values, same reentrance
  843. >>> compare(a, b)
  844. equal values, same reentrance
  845. >>> compare(a, c)
  846. equal values, different reentrance
  847. >>> compare(a, d)
  848. equal values, different reentrance
  849. >>> compare(c, d)
  850. equal values, different reentrance
  851. >>> compare(a, e)
  852. different values
  853. >>> compare(c, e)
  854. different values
  855. >>> compare(d, e)
  856. different values
  857. >>> compare(e, e)
  858. equal values, same reentrance
  859. Feature structures may not be hashed until they are frozen:
  860. >>> hash(a)
  861. Traceback (most recent call last):
  862. . . .
  863. TypeError: FeatStructs must be frozen before they can be hashed.
  864. >>> a.freeze()
  865. >>> v = hash(a)
  866. Feature structures define hash consistently. The following example
  867. looks at the hash value for each (fs1,fs2) pair; if their hash values
  868. are not equal, then they must not be equal. If their hash values are
  869. equal, then display a message, and indicate whether their values are
  870. indeed equal. Note that c and d currently have the same hash value,
  871. even though they are not equal. That is not a bug, strictly speaking,
  872. but it wouldn't be a bad thing if it changed.
  873. >>> for fstruct in (a, b, c, d, e):
  874. ... fstruct.freeze()
  875. >>> for fs1_name in 'abcde':
  876. ... for fs2_name in 'abcde':
  877. ... fs1 = locals()[fs1_name]
  878. ... fs2 = locals()[fs2_name]
  879. ... if hash(fs1) != hash(fs2):
  880. ... assert fs1 != fs2
  881. ... else:
  882. ... print('%s and %s have the same hash value,' %
  883. ... (fs1_name, fs2_name))
  884. ... if fs1 == fs2: print('and are equal')
  885. ... else: print('and are not equal')
  886. a and a have the same hash value, and are equal
  887. a and b have the same hash value, and are equal
  888. b and a have the same hash value, and are equal
  889. b and b have the same hash value, and are equal
  890. c and c have the same hash value, and are equal
  891. c and d have the same hash value, and are not equal
  892. d and c have the same hash value, and are not equal
  893. d and d have the same hash value, and are equal
  894. e and e have the same hash value, and are equal
  895. ..
  896. >>> del a, b, c, d, e, v # clean-up
  897. Tracing
  898. -------
  899. >>> fs1 = FeatStruct('[a=[b=(1)[], c=?x], d->(1), e=[f=?x]]')
  900. >>> fs2 = FeatStruct('[a=(1)[c="C"], e=[g->(1)]]')
  901. >>> fs1.unify(fs2, trace=True)
  902. <BLANKLINE>
  903. Unification trace:
  904. / [a=[b=(1)[], c=?x], d->(1), e=[f=?x]]
  905. |\ [a=(1)[c='C'], e=[g->(1)]]
  906. |
  907. | Unify feature: a
  908. | / [b=[], c=?x]
  909. | |\ [c='C']
  910. | |
  911. | | Unify feature: a.c
  912. | | / ?x
  913. | | |\ 'C'
  914. | | |
  915. | | +-->Variable('?x')
  916. | |
  917. | +-->[b=[], c=?x]
  918. | Bindings: {?x: 'C'}
  919. |
  920. | Unify feature: e
  921. | / [f=?x]
  922. | |\ [g=[c='C']]
  923. | |
  924. | +-->[f=?x, g=[b=[], c=?x]]
  925. | Bindings: {?x: 'C'}
  926. |
  927. +-->[a=(1)[b=(2)[], c='C'], d->(2), e=[f='C', g->(1)]]
  928. Bindings: {?x: 'C'}
  929. [a=(1)[b=(2)[], c='C'], d->(2), e=[f='C', g->(1)]]
  930. >>>
  931. >>> fs1 = FeatStruct('[a=?x, b=?z, c=?z]')
  932. >>> fs2 = FeatStruct('[a=?y, b=?y, c=?q]')
  933. >>> #fs1.unify(fs2, trace=True)
  934. >>>
  935. ..
  936. >>> del fs1, fs2 # clean-up
  937. Unification on Dicts & Lists
  938. ----------------------------
  939. It's possible to do unification on dictionaries:
  940. >>> from nltk.featstruct import unify
  941. >>> pprint(unify(dict(x=1, y=dict(z=2)), dict(x=1, q=5)), width=1)
  942. {'q': 5, 'x': 1, 'y': {'z': 2}}
  943. It's possible to do unification on lists as well:
  944. >>> unify([1, 2, 3], [1, Variable('x'), 3])
  945. [1, 2, 3]
  946. Mixing dicts and lists is fine:
  947. >>> pprint(unify([dict(x=1, y=dict(z=2)),3], [dict(x=1, q=5),3]),
  948. ... width=1)
  949. [{'q': 5, 'x': 1, 'y': {'z': 2}}, 3]
  950. Mixing dicts and FeatStructs is discouraged:
  951. >>> unify(dict(x=1), FeatStruct(x=1))
  952. Traceback (most recent call last):
  953. . . .
  954. ValueError: Mixing FeatStruct objects with Python dicts and lists is not supported.
  955. But you can do it if you really want, by explicitly stating that both
  956. dictionaries and FeatStructs should be treated as feature structures:
  957. >>> unify(dict(x=1), FeatStruct(x=1), fs_class=(dict, FeatStruct))
  958. {'x': 1}
  959. Finding Conflicts
  960. -----------------
  961. >>> from nltk.featstruct import conflicts
  962. >>> fs1 = FeatStruct('[a=[b=(1)[c=2], d->(1), e=[f->(1)]]]')
  963. >>> fs2 = FeatStruct('[a=[b=[c=[x=5]], d=[c=2], e=[f=[c=3]]]]')
  964. >>> for path in conflicts(fs1, fs2):
  965. ... print('%-8s: %r vs %r' % ('.'.join(path), fs1[path], fs2[path]))
  966. a.b.c : 2 vs [x=5]
  967. a.e.f.c : 2 vs 3
  968. ..
  969. >>> del fs1, fs2 # clean-up
  970. Retracting Bindings
  971. -------------------
  972. >>> from nltk.featstruct import retract_bindings
  973. >>> bindings = {}
  974. >>> fs1 = FeatStruct('[a=?x, b=[c=?y]]')
  975. >>> fs2 = FeatStruct('[a=(1)[c=[d=1]], b->(1)]')
  976. >>> fs3 = fs1.unify(fs2, bindings)
  977. >>> print(fs3)
  978. [ a = (1) [ c = [ d = 1 ] ] ]
  979. [ ]
  980. [ b -> (1) ]
  981. >>> pprint(bindings)
  982. {Variable('?x'): [c=[d=1]], Variable('?y'): [d=1]}
  983. >>> retract_bindings(fs3, bindings)
  984. [a=?x, b=?x]
  985. >>> pprint(bindings)
  986. {Variable('?x'): [c=?y], Variable('?y'): [d=1]}
  987. Squashed Bugs
  988. ~~~~~~~~~~~~~
  989. In svn rev 5167, unifying two feature structures that used the same
  990. variable would cause those variables to become aliased in the output.
  991. >>> fs1 = FeatStruct('[a=?x]')
  992. >>> fs2 = FeatStruct('[b=?x]')
  993. >>> fs1.unify(fs2)
  994. [a=?x, b=?x2]
  995. There was a bug in svn revision 5172 that caused `rename_variables` to
  996. rename variables to names that are already used.
  997. >>> FeatStruct('[a=?x, b=?x2]').rename_variables(
  998. ... vars=[Variable('?x')])
  999. [a=?x3, b=?x2]
  1000. >>> fs1 = FeatStruct('[a=?x]')
  1001. >>> fs2 = FeatStruct('[a=?x, b=?x2]')
  1002. >>> fs1.unify(fs2)
  1003. [a=?x, b=?x2]
  1004. There was a bug in svn rev 5167 that caused us to get the following
  1005. example wrong. Basically the problem was that we only followed
  1006. 'forward' pointers for other, not self, when unifying two feature
  1007. structures. (nb: this test assumes that features are unified in
  1008. alphabetical order -- if they are not, it might pass even if the bug
  1009. is present.)
  1010. >>> fs1 = FeatStruct('[a=[x=1], b=?x, c=?x]')
  1011. >>> fs2 = FeatStruct('[a=(1)[], b->(1), c=[x=2]]')
  1012. >>> print(fs1.unify(fs2))
  1013. None
  1014. ..
  1015. >>> del fs1, fs2 # clean-up