featstruct.doctest 37 KB

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