PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/external/srec/tools/thirdparty/OpenFst/fst/lib/properties.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 340 lines | 260 code | 35 blank | 45 comment | 56 complexity | a99281225534d7e61d0de23fb3cda458 MD5 | raw file
  1. // properties.cc
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. //
  16. // \file
  17. // Functions for updating property bits for various FST operations and
  18. // string names of the properties.
  19. #include <vector>
  20. #include "fst/lib/properties.h"
  21. namespace fst {
  22. // These functions determine the properties associated with the FST
  23. // result of various finite-state operations. The property arguments
  24. // correspond to the operation's FST arguments. The properties
  25. // returned assume the operation modifies its first argument.
  26. // Bitwise-and this result with kCopyProperties for the case when a
  27. // new (possibly delayed) FST is instead constructed.
  28. // Properties for a concatenatively-closed FST.
  29. uint64 ClosureProperties(uint64 inprops, bool star, bool delayed) {
  30. uint64 outprops = (kAcceptor | kUnweighted | kAccessible) & inprops;
  31. if (!delayed)
  32. outprops |= (kExpanded | kMutable | kCoAccessible |
  33. kNotTopSorted | kNotString) & inprops;
  34. if (!delayed || inprops & kAccessible)
  35. outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic |
  36. kNotILabelSorted | kNotOLabelSorted | kWeighted |
  37. kNotAccessible | kNotCoAccessible) & inprops;
  38. return outprops;
  39. }
  40. // Properties for a complemented FST.
  41. uint64 ComplementProperties(uint64 inprops) {
  42. uint64 outprops = kAcceptor | kUnweighted | kNoEpsilons |
  43. kNoIEpsilons | kNoOEpsilons |
  44. kIDeterministic | kODeterministic | kAccessible;
  45. outprops |= (kILabelSorted | kOLabelSorted | kInitialCyclic) & inprops;
  46. if (inprops & kAccessible)
  47. outprops |= kNotILabelSorted | kNotOLabelSorted | kCyclic;
  48. return outprops;
  49. }
  50. // Properties for a composed FST.
  51. uint64 ComposeProperties(uint64 inprops1, uint64 inprops2) {
  52. uint64 outprops = kAccessible;
  53. outprops |= (kAcceptor | kNoIEpsilons | kAcyclic | kInitialAcyclic) &
  54. inprops1 & inprops2;
  55. if ((kNoIEpsilons & inprops1 & inprops2)) {
  56. outprops |= kIDeterministic & inprops1 & inprops2;
  57. }
  58. return outprops;
  59. }
  60. // Properties for a concatenated FST.
  61. uint64 ConcatProperties(uint64 inprops1, uint64 inprops2, bool delayed) {
  62. uint64 outprops =
  63. (kAcceptor | kUnweighted | kAcyclic) & inprops1 & inprops2;
  64. bool empty1 = delayed; // Can fst1 be the empty machine?
  65. bool empty2 = delayed; // Can fst2 be the empty machine?
  66. if (!delayed) {
  67. outprops |= (kExpanded | kMutable | kNotTopSorted | kNotString) & inprops1;
  68. outprops |= (kNotTopSorted | kNotString) & inprops2;
  69. }
  70. if (!empty1)
  71. outprops |= (kInitialAcyclic | kInitialCyclic) & inprops1;
  72. if (!delayed || inprops1 & kAccessible)
  73. outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic |
  74. kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted |
  75. kNotOLabelSorted | kWeighted | kCyclic |
  76. kNotAccessible | kNotCoAccessible) & inprops1;
  77. if ((inprops1 & (kAccessible | kCoAccessible)) ==
  78. (kAccessible | kCoAccessible) && !empty1) {
  79. outprops |= kAccessible & inprops2;
  80. if (!empty2)
  81. outprops |= kCoAccessible & inprops2;
  82. if (!delayed || inprops2 & kAccessible)
  83. outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic |
  84. kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted |
  85. kNotOLabelSorted | kWeighted | kCyclic |
  86. kNotAccessible | kNotCoAccessible) & inprops2;
  87. }
  88. return outprops;
  89. }
  90. // Properties for a determinized FST.
  91. uint64 DeterminizeProperties(uint64 inprops) {
  92. uint64 outprops = kIDeterministic | kAccessible;
  93. outprops |= (kAcceptor | kNoEpsilons | kAcyclic |
  94. kInitialAcyclic | kCoAccessible | kString) & inprops;
  95. if (inprops & kAccessible)
  96. outprops |= (kNotAcceptor | kEpsilons | kIEpsilons | kOEpsilons |
  97. kCyclic) & inprops;
  98. if (inprops & kAcceptor)
  99. outprops |= (kNoIEpsilons | kNoOEpsilons | kAccessible) & inprops;
  100. return outprops;
  101. }
  102. // Properties for a differenced FST.
  103. uint64 DifferenceProperties(uint64 inprops1, uint64 inprops2) {
  104. return IntersectProperties(inprops1, ComplementProperties(inprops2));
  105. }
  106. // Properties for factored weight FST.
  107. uint64 FactorWeightProperties(uint64 inprops) {
  108. uint64 outprops = (kExpanded | kMutable | kAcceptor |
  109. kAcyclic | kAccessible | kCoAccessible) & inprops;
  110. if (inprops & kAccessible)
  111. outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic |
  112. kEpsilons | kIEpsilons | kOEpsilons | kCyclic |
  113. kNotILabelSorted | kNotOLabelSorted)
  114. & inprops;
  115. return outprops;
  116. }
  117. // Properties for an intersected FST.
  118. uint64 IntersectProperties(uint64 inprops1, uint64 inprops2) {
  119. uint64 outprops = kAcceptor | kAccessible;
  120. outprops |= (kNoEpsilons | kNoIEpsilons | kNoOEpsilons | kAcyclic |
  121. kInitialAcyclic) & inprops1 & inprops2;
  122. if ((kNoIEpsilons & inprops1 & inprops2))
  123. outprops |= (kIDeterministic | kODeterministic) & inprops1 & inprops2;
  124. return outprops;
  125. }
  126. // Properties for an inverted FST.
  127. uint64 InvertProperties(uint64 inprops) {
  128. uint64 outprops = (kExpanded | kMutable | kAcceptor | kNotAcceptor |
  129. kEpsilons | kNoEpsilons | kWeighted | kUnweighted |
  130. kCyclic | kAcyclic | kInitialCyclic | kInitialAcyclic |
  131. kTopSorted | kNotTopSorted |
  132. kAccessible | kNotAccessible |
  133. kCoAccessible | kNotCoAccessible |
  134. kString | kNotString) & inprops;
  135. if (kIDeterministic & inprops)
  136. outprops |= kODeterministic;
  137. if (kNonIDeterministic & inprops)
  138. outprops |= kNonODeterministic;
  139. if (kODeterministic & inprops)
  140. outprops |= kIDeterministic;
  141. if (kNonODeterministic & inprops)
  142. outprops |= kNonIDeterministic;
  143. if (kIEpsilons & inprops)
  144. outprops |= kOEpsilons;
  145. if (kNoIEpsilons & inprops)
  146. outprops |= kNoOEpsilons;
  147. if (kOEpsilons & inprops)
  148. outprops |= kIEpsilons;
  149. if (kNoOEpsilons & inprops)
  150. outprops |= kNoIEpsilons;
  151. if (kILabelSorted & inprops)
  152. outprops |= kOLabelSorted;
  153. if (kNotILabelSorted & inprops)
  154. outprops |= kNotOLabelSorted;
  155. if (kOLabelSorted & inprops)
  156. outprops |= kILabelSorted;
  157. if (kNotOLabelSorted & inprops)
  158. outprops |= kNotILabelSorted;
  159. return outprops;
  160. }
  161. // Properties for a projected FST.
  162. uint64 ProjectProperties(uint64 inprops, bool project_input) {
  163. uint64 outprops = kAcceptor;
  164. outprops |= (kExpanded | kMutable | kWeighted | kUnweighted |
  165. kCyclic | kAcyclic | kInitialCyclic | kInitialAcyclic |
  166. kTopSorted | kNotTopSorted | kAccessible | kNotAccessible |
  167. kCoAccessible | kNotCoAccessible |
  168. kString | kNotString) & inprops;
  169. if (project_input) {
  170. outprops |= (kIDeterministic | kNonIDeterministic |
  171. kIEpsilons | kNoIEpsilons |
  172. kILabelSorted | kNotILabelSorted) & inprops;
  173. if (kIDeterministic & inprops)
  174. outprops |= kODeterministic;
  175. if (kNonIDeterministic & inprops)
  176. outprops |= kNonODeterministic;
  177. if (kIEpsilons & inprops)
  178. outprops |= kOEpsilons | kEpsilons;
  179. if (kNoIEpsilons & inprops)
  180. outprops |= kNoOEpsilons | kNoEpsilons;
  181. if (kILabelSorted & inprops)
  182. outprops |= kOLabelSorted;
  183. if (kNotILabelSorted & inprops)
  184. outprops |= kNotOLabelSorted;
  185. } else {
  186. outprops |= (kODeterministic | kNonODeterministic |
  187. kOEpsilons | kNoOEpsilons |
  188. kOLabelSorted | kNotOLabelSorted) & inprops;
  189. if (kODeterministic & inprops)
  190. outprops |= kIDeterministic;
  191. if (kNonODeterministic & inprops)
  192. outprops |= kNonIDeterministic;
  193. if (kOEpsilons & inprops)
  194. outprops |= kIEpsilons | kEpsilons;
  195. if (kNoOEpsilons & inprops)
  196. outprops |= kNoIEpsilons | kNoEpsilons;
  197. if (kOLabelSorted & inprops)
  198. outprops |= kILabelSorted;
  199. if (kNotOLabelSorted & inprops)
  200. outprops |= kNotILabelSorted;
  201. }
  202. return outprops;
  203. }
  204. // Properties for a replace FST.
  205. uint64 ReplaceProperties(const vector<uint64>& inprops) {
  206. return 0;
  207. }
  208. // Properties for a relabeled FST.
  209. uint64 RelabelProperties(uint64 inprops) {
  210. uint64 outprops = (kExpanded | kMutable |
  211. kWeighted | kUnweighted |
  212. kCyclic | kAcyclic |
  213. kInitialCyclic | kInitialAcyclic |
  214. kTopSorted | kNotTopSorted |
  215. kAccessible | kNotAccessible |
  216. kCoAccessible | kNotCoAccessible |
  217. kString | kNotString) & inprops;
  218. return outprops;
  219. }
  220. // Properties for a reversed FST. (the superinitial state limits this set)
  221. uint64 ReverseProperties(uint64 inprops) {
  222. uint64 outprops =
  223. (kExpanded | kMutable | kAcceptor | kNotAcceptor | kEpsilons |
  224. kIEpsilons | kOEpsilons | kWeighted | kUnweighted |
  225. kCyclic | kAcyclic) & inprops;
  226. return outprops;
  227. }
  228. // Properties for re-weighted FST.
  229. uint64 ReweightProperties(uint64 inprops) {
  230. uint64 outprops = inprops & kWeightInvariantProperties;
  231. outprops = outprops & ~kCoAccessible;
  232. return outprops;
  233. }
  234. // Properties for an epsilon-removed FST.
  235. uint64 RmEpsilonProperties(uint64 inprops, bool delayed) {
  236. uint64 outprops = kNoEpsilons;
  237. outprops |= (kAcceptor | kAcyclic | kInitialAcyclic) & inprops;
  238. if (inprops & kAcceptor)
  239. outprops |= kNoIEpsilons | kNoOEpsilons;
  240. if (!delayed) {
  241. outprops |= kExpanded | kMutable;
  242. outprops |= kTopSorted & inprops;
  243. }
  244. if (!delayed || inprops & kAccessible)
  245. outprops |= kNotAcceptor & inprops;
  246. return outprops;
  247. }
  248. // Properties for a synchronized FST.
  249. uint64 SynchronizeProperties(uint64 inprops) {
  250. uint64 outprops = (kAcceptor | kAcyclic | kAccessible | kCoAccessible |
  251. kUnweighted) & inprops;
  252. if (inprops & kAccessible)
  253. outprops |= (kCyclic | kNotCoAccessible | kWeighted) & inprops;
  254. return outprops;
  255. }
  256. // Properties for a unioned FST.
  257. uint64 UnionProperties(uint64 inprops1, uint64 inprops2, bool delayed) {
  258. uint64 outprops = (kAcceptor | kUnweighted | kAcyclic | kAccessible)
  259. & inprops1 & inprops2;
  260. bool empty1 = delayed; // Can fst1 be the empty machine?
  261. bool empty2 = delayed; // Can fst2 be the empty machine?
  262. if (!delayed) {
  263. outprops |= (kExpanded | kMutable | kNotTopSorted | kNotString) & inprops1;
  264. outprops |= (kNotTopSorted | kNotString) & inprops2;
  265. }
  266. if (!empty1 && !empty2) {
  267. outprops |= kEpsilons | kIEpsilons | kOEpsilons;
  268. outprops |= kCoAccessible & inprops1 & inprops2;
  269. }
  270. // Note kNotCoAccessible does not hold because of kInitialAcyclic opt.
  271. if (!delayed || inprops1 & kAccessible)
  272. outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic |
  273. kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted |
  274. kNotOLabelSorted | kWeighted | kCyclic |
  275. kNotAccessible) & inprops1;
  276. if (!delayed || inprops2 & kAccessible)
  277. outprops |= (kNotAcceptor | kNonIDeterministic | kNonODeterministic |
  278. kEpsilons | kIEpsilons | kOEpsilons | kNotILabelSorted |
  279. kNotOLabelSorted | kWeighted | kCyclic |
  280. kNotAccessible | kNotCoAccessible) & inprops2;
  281. return outprops;
  282. }
  283. // Property string names (indexed by bit position).
  284. const char *PropertyNames[] = {
  285. // binary
  286. "expanded", "mutable", "", "", "", "", "", "",
  287. "", "", "", "", "", "", "", "",
  288. // trinary
  289. "acceptor", "not acceptor",
  290. "input deterministic", "non input deterministic",
  291. "output deterministic", "non output deterministic",
  292. "input/output epsilons", "no input/output epsilons",
  293. "input epsilons", "no input epsilons",
  294. "output epsilons", "no output epsilons",
  295. "input label sorted", "not input label sorted",
  296. "output label sorted", "not output label sorted",
  297. "weighted", "unweighted",
  298. "cyclic", "acyclic",
  299. "cyclic at initial state", "acyclic at initial state",
  300. "top sorted", "not top sorted",
  301. "accessible", "not accessible",
  302. "coaccessible", "not coaccessible",
  303. "string", "not string",
  304. };
  305. }