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

231 lines
6.4KB

  1. /**********************************************************************
  2. ** Copyright (C) 2000 Trolltech AS. All rights reserved.
  3. **
  4. ** numberh.cpp
  5. **
  6. ** This file is part of TQt Linguist.
  7. **
  8. ** See the file LICENSE included in the distribution for the usage
  9. ** and distribution terms.
  10. **
  11. ** The file is provided AS IS with NO WARRANTY OF ANY KIND,
  12. ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
  13. ** A PARTICULAR PURPOSE.
  14. **
  15. **********************************************************************/
  16. #include <qmemarray.h>
  17. #include <qcstring.h>
  18. #include <qmap.h>
  19. #include <qstringlist.h>
  20. #include <ctype.h>
  21. #include <metatranslator.h>
  22. typedef TQMap<TQCString, MetaTranslatorMessage> TMM;
  23. typedef TQValueList<MetaTranslatorMessage> TML;
  24. static bool isDigitFriendly( int c )
  25. {
  26. return ispunct( c ) || isspace( c );
  27. }
  28. static int numberLength( const char *s )
  29. {
  30. int i = 0;
  31. if ( isdigit(s[0]) ) {
  32. do {
  33. i++;
  34. } while ( isdigit(s[i]) ||
  35. (isDigitFriendly(s[i]) &&
  36. (isdigit(s[i + 1]) ||
  37. (isDigitFriendly(s[i + 1]) && isdigit(s[i + 2])))) );
  38. }
  39. return i;
  40. }
  41. /*
  42. Returns a version of 'key' where all numbers have been replaced by zeroes. If
  43. there were none, returns "".
  44. */
  45. static TQCString zeroKey( const char *key )
  46. {
  47. TQCString zeroed( strlen(key) + 1 );
  48. char *z = zeroed.data();
  49. int i = 0, j = 0;
  50. int len;
  51. bool metSomething = FALSE;
  52. while ( key[i] != '\0' ) {
  53. len = numberLength( key + i );
  54. if ( len > 0 ) {
  55. i += len;
  56. z[j++] = '0';
  57. metSomething = TRUE;
  58. } else {
  59. z[j++] = key[i++];
  60. }
  61. }
  62. z[j] = '\0';
  63. if ( metSomething )
  64. return zeroed;
  65. else
  66. return "";
  67. }
  68. static TQString translationAttempt( const TQString& oldTranslation,
  69. const char *oldSource,
  70. const char *newSource )
  71. {
  72. int p = zeroKey( oldSource ).contains( '0' );
  73. int oldSourceLen = qstrlen( oldSource );
  74. TQString attempt;
  75. TQStringList oldNumbers;
  76. TQStringList newNumbers;
  77. TQMemArray<bool> met( p );
  78. TQMemArray<int> matchedYet( p );
  79. int i, j;
  80. int k = 0, ell, best;
  81. int m, n;
  82. int pass;
  83. /*
  84. This algorithm is hard to follow, so we'll consider an example
  85. all along: oldTranslation is "XeT 3.0", oldSource is "TeX 3.0"
  86. and newSource is "XeT 3.1".
  87. First, we set up two tables: oldNumbers and newNumbers. In our
  88. example, oldNumber[0] is "3.0" and newNumber[0] is "3.1".
  89. */
  90. for ( i = 0, j = 0; i < oldSourceLen; i++, j++ ) {
  91. m = numberLength( oldSource + i );
  92. n = numberLength( newSource + j );
  93. if ( m > 0 ) {
  94. oldNumbers.append( TQCString(oldSource + i, m + 1) );
  95. newNumbers.append( TQCString(newSource + j, n + 1) );
  96. i += m;
  97. j += n;
  98. met[k] = FALSE;
  99. matchedYet[k] = 0;
  100. k++;
  101. }
  102. }
  103. /*
  104. We now go over the old translation, "XeT 3.0", one letter at a
  105. time, looking for numbers found in oldNumbers. Whenever such a
  106. number is met, it is replaced with its newNumber equivalent. In
  107. our example, the "3.0" of "XeT 3.0" becomes "3.1".
  108. */
  109. for ( i = 0; i < (int) oldTranslation.length(); i++ ) {
  110. attempt += oldTranslation[i];
  111. for ( k = 0; k < p; k++ ) {
  112. if ( oldTranslation[i] == oldNumbers[k][matchedYet[k]] )
  113. matchedYet[k]++;
  114. else
  115. matchedYet[k] = 0;
  116. }
  117. /*
  118. Let's find out if the last character ended a match. We make
  119. two passes over the data. In the first pass, we try to
  120. match only numbers that weren't matched yet; if that fails,
  121. the second pass does the trick. This is useful in some
  122. suspicious cases, flagged below.
  123. */
  124. for ( pass = 0; pass < 2; pass++ ) {
  125. best = p; // an impossible value
  126. for ( k = 0; k < p; k++ ) {
  127. if ( (!met[k] || pass > 0) &&
  128. matchedYet[k] == (int) oldNumbers[k].length() &&
  129. numberLength(oldTranslation.latin1() + (i + 1) -
  130. matchedYet[k]) == matchedYet[k] ) {
  131. // the longer the better
  132. if ( best == p || matchedYet[k] > matchedYet[best] )
  133. best = k;
  134. }
  135. }
  136. if ( best != p ) {
  137. attempt.truncate( attempt.length() - matchedYet[best] );
  138. attempt += newNumbers[best];
  139. met[best] = TRUE;
  140. for ( k = 0; k < p; k++ )
  141. matchedYet[k] = 0;
  142. break;
  143. }
  144. }
  145. }
  146. /*
  147. We flag two kinds of suspicious cases. They are identified as
  148. such with comments such as "{2000?}" at the end.
  149. Example of the first kind: old source text "TeX 3.0" translated
  150. as "XeT 2.0" is flagged "TeX 2.0 {3.0?}", no matter what the
  151. new text is.
  152. */
  153. for ( k = 0; k < p; k++ ) {
  154. if ( !met[k] )
  155. attempt += TQString( " {" ) + newNumbers[k] + TQString( "?}" );
  156. }
  157. /*
  158. Example of the second kind: "1 of 1" translated as "1 af 1",
  159. with new source text "1 of 2", generates "1 af 2 {1 or 2?}"
  160. because it's not clear which of "1 af 2" and "2 af 1" is right.
  161. */
  162. for ( k = 0; k < p; k++ ) {
  163. for ( ell = 0; ell < p; ell++ ) {
  164. if ( k != ell && oldNumbers[k] == oldNumbers[ell] &&
  165. newNumbers[k] < newNumbers[ell] )
  166. attempt += TQString( " {" ) + newNumbers[k] + TQString( " or " ) +
  167. newNumbers[ell] + TQString( "?}" );
  168. }
  169. }
  170. return attempt;
  171. }
  172. /*
  173. Augments a MetaTranslator with translations easily derived from
  174. similar existing (probably obsolete) translations.
  175. For example, if "TeX 3.0" is translated as "XeT 3.0" and "TeX 3.1"
  176. has no translation, "XeT 3.1" is added to the translator and is
  177. marked Unfinished.
  178. */
  179. void applyNumberHeuristic( MetaTranslator *tor, bool verbose )
  180. {
  181. TMM translated, untranslated;
  182. TMM::Iterator t, u;
  183. TML all = tor->messages();
  184. TML::Iterator it;
  185. int inserted = 0;
  186. for ( it = all.begin(); it != all.end(); ++it ) {
  187. if ( (*it).type() == MetaTranslatorMessage::Unfinished ) {
  188. if ( (*it).translation().isEmpty() )
  189. untranslated.insert( zeroKey((*it).sourceText()), *it );
  190. } else if ( !(*it).translation().isEmpty() ) {
  191. translated.insert( zeroKey((*it).sourceText()), *it );
  192. }
  193. }
  194. for ( u = untranslated.begin(); u != untranslated.end(); ++u ) {
  195. t = translated.find( u.key() );
  196. if ( t != translated.end() && !t.key().isEmpty() &&
  197. qstrcmp((*t).sourceText(), (*u).sourceText()) != 0 ) {
  198. MetaTranslatorMessage m( *u );
  199. m.setTranslation( translationAttempt((*t).translation(),
  200. (*t).sourceText(),
  201. (*u).sourceText()) );
  202. tor->insert( m );
  203. inserted++;
  204. }
  205. }
  206. if ( verbose && inserted != 0 )
  207. qWarning( " number heuristic provided %d translation%s",
  208. inserted, inserted == 1 ? "" : "s" );
  209. }