VALUE rb_digest(int argc, VALUE *argv, VALUE self) { VALUE string, options, method = Qnil; rb_scan_args(argc, argv, "11", &string, &options); if (TYPE(options) == T_HASH) method = rb_hash_aref(options, sMethod); int algo = NIL_P(method) ? 0 : FIX2INT(method); return SIZET2NUM(algo == 1 ? djhash(CSTRING(string)) : sdbm(CSTRING(string))); }
/** * Gets a string from a unique string space. * * @returns Pointer to the string node. * @returns NULL if the string was not found in the string space. * @param pStrSpace The space to insert it into. * @param pszString The string to get. */ RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString) { size_t cchString; KAVLKEY Key = sdbm(pszString, &cchString); PRTSTRSPACECORE pCur = KAVL_FN(Get)(pStrSpace, Key); if (!pCur) return NULL; /* Linear search. */ for (; pCur; pCur = pCur->pList) if ( pCur->cchString == cchString && !memcmp(pCur->pszString, pszString, cchString)) return pCur; return NULL; }
/** * Inserts a string into a unique string space. * * @returns true on success. * @returns false if the string collided with an existing string. * @param pStrSpace The space to insert it into. * @param pStr The string node. */ RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr) { pStr->Key = sdbm(pStr->pszString, &pStr->cchString); PRTSTRSPACECORE pMatch = KAVL_FN(Get)(pStrSpace, pStr->Key); if (!pMatch) return KAVL_FN(Insert)(pStrSpace, pStr); /* Check for clashes. */ for (PRTSTRSPACECORE pCur = pMatch; pCur; pCur = pCur->pList) if ( pCur->cchString == pStr->cchString && !memcmp(pCur->pszString, pStr->pszString, pStr->cchString)) return false; pStr->pList = pMatch->pList; pMatch->pList = pStr; return true; }
/** * Adds a dependency. * * @returns Pointer to the allocated dependency. * @param pszFilename The filename. Does not need to be terminated. * @param cchFilename The length of the filename. */ PDEP depAdd(const char *pszFilename, size_t cchFilename) { unsigned uHash = sdbm(pszFilename, cchFilename); PDEP pDep; PDEP pDepPrev; /* * Check if we've already got this one. */ pDepPrev = NULL; for (pDep = g_pDeps; pDep; pDepPrev = pDep, pDep = pDep->pNext) if ( pDep->uHash == uHash && pDep->cchFilename == cchFilename && !memcmp(pDep->szFilename, pszFilename, cchFilename)) return pDep; /* * Add it. */ pDep = (PDEP)malloc(sizeof(*pDep) + cchFilename); if (!pDep) { fprintf(stderr, "\nOut of memory! (requested %lx bytes)\n\n", (unsigned long)(sizeof(*pDep) + cchFilename)); exit(1); } pDep->cchFilename = cchFilename; memcpy(pDep->szFilename, pszFilename, cchFilename); pDep->szFilename[cchFilename] = '\0'; pDep->uHash = uHash; if (pDepPrev) { pDep->pNext = pDepPrev->pNext; pDepPrev->pNext = pDep; } else { pDep->pNext = g_pDeps; g_pDeps = pDep; } return pDep; }
VALUE rb_base64(int argc, VALUE *argv, VALUE self) { VALUE string, options, method = Qnil; rb_scan_args(argc, argv, "11", &string, &options); if (TYPE(options) == T_HASH) method = rb_hash_aref(options, sMethod); int algo = NIL_P(method) ? 0 : FIX2INT(method); char buffer[12]; uint64_t idx = 0, n, digest = (algo == 1 ? djhash(CSTRING(string)) : sdbm(CSTRING(string))); while (digest > 0) { n = digest >> 6; buffer[idx++] = digestlookup[digest - (n << 6)]; digest = n; } return rb_str_new(buffer, idx); }
/** * Removes a string from a unique string space. * * @returns Pointer to the removed string node. * @returns NULL if the string was not found in the string space. * @param pStrSpace The space to insert it into. * @param pszString The string to remove. */ RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString) { size_t cchString; KAVLKEY Key = sdbm(pszString, &cchString); PRTSTRSPACECORE pCur = KAVL_FN(Get)(pStrSpace, Key); if (!pCur) return NULL; /* find the right one. */ PRTSTRSPACECORE pPrev = NULL; for (; pCur; pPrev = pCur, pCur = pCur->pList) if ( pCur->cchString == cchString && !memcmp(pCur->pszString, pszString, cchString)) break; if (pCur) { if (pPrev) /* simple, it's in the linked list. */ pPrev->pList = pCur->pList; else { /* in the tree. remove and reinsert list head. */ PRTSTRSPACECORE pInsert = pCur->pList; pCur->pList = NULL; pCur = KAVL_FN(Remove)(pStrSpace, Key); Assert(pCur); if (pInsert) { PRTSTRSPACECORE pList = pInsert->pList; bool fRc = KAVL_FN(Insert)(pStrSpace, pInsert); Assert(fRc); NOREF(fRc); pInsert->pList = pList; } } } return pCur; }
/* wrapper */ int get_key(char* str) { unsigned long k; k = sdbm( str ) % 10000; return k; };
int main(int argc, char *argv[]) { FILE *bron; char **lexicon; unsigned long *freqs; unsigned long *hash; char word[1024]; char capword[1024]; char witheword[1024]; char closestword[MAXNRCLOSEST+1][1024]; int closest[MAXNRCLOSEST+1]; unsigned long closestfreq[MAXNRCLOSEST+1]; unsigned long thishash; int i,j,k,l,thislev,nrlex=0,nrclosest=0,readnr,wordlen,lexlen; FILE *context; unsigned long freqthres=MINFREQTHRESHOLD; char inlex,withe,cap,inflection; float current_confidence; float highest_confidence; /* allocate lexicon */ lexicon=malloc(sizeof(char*)); freqs=malloc(sizeof(unsigned long)); hash=malloc(sizeof(unsigned long)); /* read lexicon */ nrlex=0; bron=fopen(argv[1],"r"); while (!feof(bron)) { fscanf(bron,"%d %s ", &readnr,word); lexicon[nrlex]=malloc((strlen(word)+1)*sizeof(char)); strcpy(lexicon[nrlex],word); freqs[nrlex]=readnr; hash[nrlex]=sdbm(word); nrlex++; lexicon=realloc(lexicon,(nrlex+1)*sizeof(char*)); freqs=realloc(freqs,(nrlex+1)*sizeof(unsigned long)); hash=realloc(hash,(nrlex+1)*sizeof(unsigned long)); } fclose(bron); if (DEBUG2) fprintf(stderr,"read %d words from lexicon\n", nrlex); context=fopen(argv[2],"r"); while (!feof(context)) { fscanf(context,"%s ",word); withe=0; if (word[strlen(word)-1]=='e') { strcpy(witheword,""); for (i=0; i<strlen(word)-1; i++) { strcat(witheword," "); witheword[i]=word[i]; } withe=1; } cap=0; strcpy(capword,word); if ((word[0]>='A')&& (word[0]<='Z')) { capword[0]+=32; cap=1; } nrclosest=0; wordlen=strlen(word); if (strcmp(word,"één")==0) wordlen=3; if ((wordlen>=MINLENGTH)&& (!cap)) { inlex=0; thishash=sdbm(word); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if (thishash==hash[k]) { inlex=1; freqthres=FREQFACTOR*freqs[k]; if (DEBUG2) fprintf(stderr,"word [%s] in lexicon, with frequency %ld, so threshold %ld\n", word,freqs[k],freqthres); } } if (!inlex) { // does word without e occur? if (withe) { thishash=sdbm(witheword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if (thishash==hash[k]) { inlex=1; freqthres=FREQFACTOR*freqs[k]; } } } // does lo-cap word occur? /* if ((!inlex)&&(cap)) { thishash=sdbm(capword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if (thishash==hash[k]) { inlex=1; freqthres=FREQFACTOR*freqs[k]; } } } */ if (!inlex) { freqthres=MINFREQTHRESHOLD; if (DEBUG2) fprintf(stderr,"word [%s] not in lexicon, so threshold %d\n", word,MINFREQTHRESHOLD); } } for (k=0; ((k<nrlex)&&(freqs[k]>freqthres)); k++) { lexlen=strlen(lexicon[k]); if ((lexlen>wordlen+MAXLD)|| (wordlen>lexlen+MAXLD)) thislev=MAXLD+1; else thislev=ld1(lexicon[k],word); //thislev=levenshtein(lexicon[k],word); if (((thislev<=MAXLD)&&(wordlen>LDTHRESHOLD))|| ((thislev<=MEDLD)&&(wordlen<=LDTHRESHOLD))) { inflection=0; // check: plural? if ((((lexicon[k][strlen(lexicon[k])-1]=='s')&& (word[strlen(word)-1]!='s')))|| (((lexicon[k][strlen(lexicon[k])-1]!='s')&& (word[strlen(word)-1]=='s')))) inflection=1; if ((((lexicon[k][strlen(lexicon[k])-1]=='e')&& (word[strlen(word)-1]!='e')))|| (((lexicon[k][strlen(lexicon[k])-1]!='e')&& (word[strlen(word)-1]=='e')))) inflection=1; if ((((lexicon[k][strlen(lexicon[k])-1]=='n')&& (word[strlen(word)-1]=='t')))|| (((lexicon[k][strlen(lexicon[k])-1]=='t')&& (word[strlen(word)-1]=='n')))) inflection=1; if (!inflection) { j=0; while ((j<nrclosest)&& (freqs[k]<closestfreq[j])) j++; if (j<nrclosest) { // move up for (l=nrclosest; l>j; l--) { strcpy(closestword[l],closestword[l-1]); closest[l]=closest[l-1]; closestfreq[l]=closestfreq[l-1]; } } // insert strcpy(closestword[j],lexicon[k]); closest[j]=thislev; closestfreq[j]=freqs[k]; if (nrclosest<MAXNRCLOSEST) nrclosest++; } } } if (DEBUG2) { fprintf(stderr,"closest to %s:\n", word); for (i=0; i<nrclosest; i++) fprintf(stderr," %2d %s\n", i,closestword[i]); } } fprintf(stdout,"%s", word); if (nrclosest>0) { for (i=0; i<nrclosest; i++) { fprintf(stdout," %s", closestword[i]); current_confidence = (1 - ((float)freqthres/(float)FREQFACTOR)/((float)closestfreq[i]/(float)FREQFACTOR)); if (current_confidence > highest_confidence) { highest_confidence = current_confidence; } } if (DEBUG) { fprintf(stderr,"correction suggestions for %s: ", word); for (i=0; i<nrclosest; i++) fprintf(stderr," %s", closestword[i]); fprintf(stderr,"\n"); } } if(highest_confidence > 0) { fprintf(stdout,"%6.3f\n",highest_confidence); } else { fprintf(stdout,"\n"); } highest_confidence = 0; } fclose(context); return 0; }
int main(int argc, char *argv[]) { FILE *bron,*aspell; char **lexicon; unsigned long *freqs; unsigned long *hash; char word[1024]; char capword[1024]; char commandline[1024]; char closestword[MAXNRCLOSEST+1][1024]; int closest[MAXNRCLOSEST+1]; unsigned long closestfreq[MAXNRCLOSEST+1]; unsigned long thishash; int i,j,k,l,thislev,nrlex=0,nrclosest=0,nraspell,readnr,wordlen,lexlen; FILE *context; char inlex,cap,hyp,inflection,tokstatus; char aspellline[32768]; char memline[32768]; char *part; char aspellword[1024]; /* allocate lexicon */ lexicon=malloc(sizeof(char*)); freqs=malloc(sizeof(unsigned long)); hash=malloc(sizeof(unsigned long)); /* read lexicon */ nrlex=0; bron=fopen(argv[1],"r"); while (!feof(bron)) { fscanf(bron,"%d %s ", &readnr,word); lexicon[nrlex]=malloc((strlen(word)+1)*sizeof(char)); strcpy(lexicon[nrlex],word); freqs[nrlex]=readnr; hash[nrlex]=sdbm(word); nrlex++; lexicon=realloc(lexicon,(nrlex+1)*sizeof(char*)); freqs=realloc(freqs,(nrlex+1)*sizeof(unsigned long)); hash=realloc(hash,(nrlex+1)*sizeof(unsigned long)); } fclose(bron); if (DEBUG2) fprintf(stderr,"read %d words from lexicon\n", nrlex); /* run aspell on input.tok.txt */ sprintf(commandline,"aspell --lang=en --pipe < %s > input.tok.aspell\n", argv[2]); if (DEBUG) fprintf(stderr,"executing: %s", commandline); system(commandline); context=fopen(argv[2],"r"); aspell=fopen("input.tok.aspell","r"); fgets(aspellline,32768,aspell); while (!feof(context)) { fscanf(context,"%s ",word); wordlen=strlen(word); nrclosest=0; tokstatus=1; if (strstr(",.!?:;\'\"(){}[]\\/|€",word)) tokstatus=0; if (tokstatus) if (isNumber(word)) tokstatus=0; if (strstr(word,"http://")) tokstatus=0; if (strstr(word,"@")) tokstatus=0; if (tokstatus) { fgets(aspellline,32768,aspell); strcpy(memline,aspellline); part=strtok(aspellline," \n"); if (strcmp(part,"&")==0) { // read alternatives from aspellline part=strtok(NULL," \n"); if (strcmp(part,word)==0) { part=strtok(NULL," \n"); sscanf(part,"%d",&nraspell); if (DEBUG2) fprintf(stderr,"[%s] [%d options]\n", word,nraspell); inlex=0; thishash=sdbm(word); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if (thishash==hash[k]) { inlex=1; if (DEBUG2) fprintf(stderr,"word [%s] in lexicon, with frequency %ld\n", word,freqs[k]); } } // read extra string (offset) part=strtok(NULL," \n"); for (k=0; k<nraspell; k++) { part=strtok(NULL," \n"); strcpy(aspellword,""); for (l=0; l<strlen(part)-1; l++) { strcat(aspellword," "); aspellword[l]=part[l]; } if (!strstr(aspellword,"-")) { lexlen=strlen(aspellword); if ((lexlen>wordlen+MAXLD)|| (wordlen>lexlen+MAXLD)) thislev=MAXLD+1; else thislev=levenshtein(aspellword,word); if (thislev<=MAXLD) { inflection=0; // check: plural? if ((((aspellword[strlen(aspellword)-1]=='s')&& (word[strlen(word)-1]!='s')))|| (((aspellword[strlen(aspellword)-1]!='s')&& (word[strlen(word)-1]=='s')))) inflection=1; if ((((aspellword[strlen(aspellword)-1]=='e')&& (word[strlen(word)-1]!='e')))|| (((aspellword[strlen(aspellword)-1]!='e')&& (word[strlen(word)-1]=='e')))) inflection=1; if ((((aspellword[strlen(aspellword)-1]=='n')&& (word[strlen(word)-1]=='t')))|| (((aspellword[strlen(aspellword)-1]=='t')&& (word[strlen(word)-1]=='n')))) inflection=1; if (!inflection) { j=0; while ((j<nrclosest)&& (freqs[k]<closestfreq[j])) j++; if (j<nrclosest) { // move up for (l=nrclosest; l>j; l--) { strcpy(closestword[l],closestword[l-1]); closest[l]=closest[l-1]; closestfreq[l]=closestfreq[l-1]; } } // insert strcpy(closestword[j],aspellword); closest[j]=thislev; closestfreq[j]=freqs[k]; if (nrclosest<MAXNRCLOSEST) nrclosest++; } } } } if (DEBUG2) { fprintf(stderr,"closest to %s:\n", word); for (i=0; i<nrclosest; i++) fprintf(stderr," %2d %s\n", i,closestword[i]); } } } } fprintf(stdout,"%s", word); // post-filter (too bad for all the work done before): // don't correct capitalized words, don't correct words with // hyphens cap=0; if ((word[0]>='A')&& (word[0]<='Z')) cap=1; hyp=0; if (strstr(word,"-")) hyp=1; if ((nrclosest>0)&& (wordlen>=MINLENGTH)&& (!cap)&& (!hyp)) { for (i=0; i<nrclosest; i++) fprintf(stdout," %s", closestword[i]); if (DEBUG) { fprintf(stderr,"correction suggestions for %s: ", word); for (i=0; i<nrclosest; i++) fprintf(stderr," %s", closestword[i]); fprintf(stderr,"\n"); } fprintf(stdout," 0.6"); } fprintf(stdout,"\n"); } fclose(aspell); fclose(context); return 0; }
unsigned int doc_hash (void *ky) { return sdbm((char *)ky); }
int main(int argc, char *argv[]) { FILE *bron; char **lexicon; unsigned long *freqs; unsigned long *hash; char word[4096]; char memword[4096]; char corword[4096]; char errword[4096]; char capword[4096]; char witheword[4096]; char line[32768]; char closestword[MAXNRCLOSEST+1][1024]; int closest[MAXNRCLOSEST+1]; unsigned long closestfreq[MAXNRCLOSEST+1]; unsigned long thishash,corhash,errhash,corfreq,errfreq; int i,j,k,l,thislev,nrlex=0,nrclosest=0,readnr,wordlen,lexlen,counter; FILE *context; char *part; unsigned long freqthres=MINFREQTHRESHOLD; char inlex,withe,cap,inflection; setbuf(stdout,NULL); /* allocate lexicon */ lexicon=malloc(sizeof(char*)); freqs=malloc(sizeof(unsigned long)); hash=malloc(sizeof(unsigned long)); /* read lexicon */ nrlex=0; bron=fopen(argv[1],"r"); readnr=999; fgets(line,32768,bron); while ((!feof(bron))&& (readnr>1)) { sscanf(line,"%d %s ", &readnr,word); lexicon[nrlex]=malloc((strlen(word)+1)*sizeof(char)); strcpy(lexicon[nrlex],word); freqs[nrlex]=readnr; hash[nrlex]=sdbm(word); nrlex++; lexicon=realloc(lexicon,(nrlex+1)*sizeof(char*)); freqs=realloc(freqs,(nrlex+1)*sizeof(unsigned long)); hash=realloc(hash,(nrlex+1)*sizeof(unsigned long)); fgets(line,32768,bron); } fclose(bron); if (DEBUG2) fprintf(stderr,"read %d words from lexicon\n", nrlex); context=fopen(argv[2],"r"); counter=0; while (!feof(context)) { fscanf(context,"%s ",word); strcpy(memword,word); part=strtok(word,"~"); strcpy(corword,part); part=strtok(NULL,"~"); strcpy(errword,part); corhash=sdbm(corword); errhash=sdbm(errword); corfreq=errfreq=0; for (k=0; k<nrlex; k++) { if (corhash==hash[k]) corfreq=freqs[k]; if (errhash==hash[k]) errfreq=freqs[k]; } if (((errfreq*5)>corfreq)&& (corfreq>0)) fprintf(stderr,"corword: [%s]-[%ld], errword: [%s]-[%ld]\n", corword,corfreq,errword,errfreq); else fprintf(stdout,"%s\n", memword); counter++; if (counter%1000==0) fprintf(stderr,"processed %d word-error pairs\n", counter); } fclose(context); return 0; }
int main(int argc, char *argv[]) { FILE *bron,*context; char *part; char **errors; char **corrections; unsigned long *hash; char word[1024]; char line[32768]; unsigned long thishash; int i,nrpairs=0; /* allocate error-correction pair list */ errors=malloc(sizeof(char*)); corrections=malloc(sizeof(char*)); hash=malloc(sizeof(unsigned long)); /* read error-correction pair list */ nrpairs=0; bron=fopen(argv[1],"r"); fgets(line,32768,bron); while (!feof(bron)) { part=strtok(line,"~\n"); strcpy(word,part); corrections[nrpairs]=malloc((strlen(word)+1)*sizeof(char)); strcpy(corrections[nrpairs],word); part=strtok(NULL,"~\n"); strcpy(word,part); errors[nrpairs]=malloc((strlen(word)+1)*sizeof(char)); strcpy(errors[nrpairs],word); hash[nrpairs]=sdbm(word); nrpairs++; corrections=realloc(corrections,(nrpairs+1)*sizeof(char*)); errors=realloc(errors,(nrpairs+1)*sizeof(char*)); hash=realloc(hash,(nrpairs+1)*sizeof(unsigned long)); fgets(line,32768,bron); } fclose(bron); if (DEBUG) fprintf(stderr,"read %d words from lexicon\n", nrpairs); context=fopen(argv[2],"r"); while (!feof(context)) { fscanf(context,"%s ",word); thishash=sdbm(word); fprintf(stdout,"%s", word); for (i=0; i<nrpairs; i++) { if (thishash==hash[i]) { if (DEBUG) fprintf(stderr,"found correction [%s] for presumed error [%s]\n", corrections[i],word); fprintf(stdout," %s", corrections[i]); } } fprintf(stdout,"\n"); } fclose(context); return 0; }
int main(int argc, char * argv[]) { /* * Before starting main application, need to set 'QT_X11_NO_MITSHM=1' * to make the runtime work with IBM PPC machine. */ #if defined (Q_OS_LINUX) QByteArray val("1"); qputenv("QT_X11_NO_MITSHM", val); #endif // Create the QT application QApplication app(argc, argv); app.setQuitOnLastWindowClosed(false); // Setup the settings management QCoreApplication::setOrganizationName("pgadmin"); QCoreApplication::setOrganizationDomain("pgadmin.org"); QCoreApplication::setApplicationName(PGA_APP_NAME.toLower().replace(" ", "")); #if QT_VERSION >= 0x050000 // Set high DPI pixmap to display icons clear on Qt widget. QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); #endif // Create a hash of the executable path so we can run copies side-by-side QString homeDir = QDir::homePath(); unsigned long exeHash = sdbm((unsigned char *)argv[0]); // Create the address file, that will be used to store the appserver URL for this instance addrFileName = homeDir + (QString("/.%1.%2.addr").arg(PGA_APP_NAME).arg(exeHash)).remove(" "); QFile addrFile(addrFileName); // Create a system-wide semaphore keyed by app name, exe hash and the username // to ensure instances are unique to the user and path QString userName = qgetenv("USER"); // *nix if (userName.isEmpty()) userName = qgetenv("USERNAME"); // Windows QString semaName = QString("%1-%2-%3-sema").arg(PGA_APP_NAME).arg(userName).arg(exeHash); QString shmemName = QString("%1-%2-%3-shmem").arg(PGA_APP_NAME).arg(userName).arg(exeHash); QSystemSemaphore sema(semaName, 1); sema.acquire(); #ifndef Q_OS_WIN32 // We may need to clean up stale shmem segments on *nix. Attaching and detaching // should remove the segment if it is orphaned. QSharedMemory stale_shmem(shmemName); if (stale_shmem.attach()) stale_shmem.detach(); #endif QSharedMemory shmem(shmemName); bool is_running; if (shmem.attach()) { is_running = true; } else { shmem.create(1); is_running = false; } sema.release(); QSettings settings; if (is_running){ addrFile.open(QIODevice::ReadOnly | QIODevice::Text); QTextStream in(&addrFile); QString addr = in.readLine(); QString cmd = settings.value("BrowserCommand").toString(); if (!cmd.isEmpty()) { cmd.replace("%URL%", addr); QProcess::startDetached(cmd); } else { if (!QDesktopServices::openUrl(addr)) { QString error(QWidget::tr("Failed to open the system default web browser. Is one installed?.")); QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error); exit(1); } } return 0; } atexit(cleanup); // In windows and linux, it is required to set application level proxy // because socket bind logic to find free port gives socket creation error // when system proxy is configured. We are also setting // "setUseSystemConfiguration"=true to use the system proxy which will // override this application level proxy. As this bug is fixed in Qt 5.9 so // need to set application proxy for Qt version < 5.9. // #if defined (Q_OS_WIN) && QT_VERSION <= 0x050800 // Give dummy URL required to find proxy server configured in windows. QNetworkProxyQuery proxyQuery(QUrl("https://www.pgadmin.org")); QNetworkProxy l_proxy; QList<QNetworkProxy> listOfProxies = QNetworkProxyFactory::systemProxyForQuery(proxyQuery); if (listOfProxies.size()) { l_proxy = listOfProxies[0]; // If host name is not empty means proxy server is configured. if (!l_proxy.hostName().isEmpty()) { QNetworkProxy::setApplicationProxy(QNetworkProxy()); } } #endif #if defined (Q_OS_LINUX) && QT_VERSION <= 0x050800 QByteArray proxy_env; proxy_env = qgetenv("http_proxy"); // If http_proxy environment is defined in linux then proxy server is configured. if (!proxy_env.isEmpty()) { QNetworkProxy::setApplicationProxy(QNetworkProxy()); } #endif // Display the spash screen QSplashScreen *splash = new QSplashScreen(); splash->setPixmap(QPixmap(":/splash.png")); splash->show(); app.processEvents(QEventLoop::AllEvents); quint16 port = 0L; // Find an unused port number. Essentially, we're just reserving one // here that Flask will use when we start up the server. // In order to use the socket, we need to free this socket ASAP. // Hence - putting this code in a code block so the scope of the socket // variable vanishes to make that socket available. { #if QT_VERSION >= 0x050000 QTcpSocket socket; #if QT_VERSION >= 0x050900 socket.setProxy(QNetworkProxy::NoProxy); #endif socket.bind(0, QTcpSocket::ShareAddress); #else QUdpSocket socket; socket.bind(0, QUdpSocket::ShareAddress); #endif port = socket.localPort(); } // Generate a random key to authenticate the client to the server QString key = QUuid::createUuid().toString(); key = key.mid(1, key.length() - 2); // Generate the filename for the log logFileName = homeDir + (QString("/.%1.%2.log").arg(PGA_APP_NAME).arg(exeHash)).remove(" "); // Start the tray service TrayIcon *trayicon = new TrayIcon(logFileName); if (!trayicon->Init()) { QString error = QString(QWidget::tr("An error occurred initialising the tray icon")); QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error); exit(1); } // Fire up the webserver Server *server; bool done = false; while (done != true) { server = new Server(port, key, logFileName); if (!server->Init()) { splash->finish(NULL); qDebug() << server->getError(); QString error = QString(QWidget::tr("An error occurred initialising the application server:\n\n%1")).arg(server->getError()); QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error); exit(1); } server->start(); // This is a hack to give the server a chance to start and potentially fail. As // the Python interpreter is a synchronous call, we can't check for proper startup // easily in a more robust way - we have to rely on a clean startup not returning. // It should always fail pretty quickly, and take longer to start if it succeeds, so // we don't really get a visible delay here. delay(1000); // Any errors? if (server->isFinished() || server->getError().length() > 0) { splash->finish(NULL); qDebug() << server->getError(); QString error = QString(QWidget::tr("An error occurred initialising the application server:\n\n%1")).arg(server->getError()); QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error); // Allow the user to tweak the Python Path if needed bool ok; ConfigWindow *dlg = new ConfigWindow(); dlg->setWindowTitle(QWidget::tr("Configuration")); dlg->setBrowserCommand(settings.value("BrowserCommand").toString()); dlg->setPythonPath(settings.value("PythonPath").toString()); dlg->setApplicationPath(settings.value("ApplicationPath").toString()); dlg->setModal(true); ok = dlg->exec(); QString browsercommand = dlg->getBrowserCommand(); QString pythonpath = dlg->getPythonPath(); QString applicationpath = dlg->getApplicationPath(); if (ok) { settings.setValue("BrowserCommand", browsercommand); settings.setValue("PythonPath", pythonpath); settings.setValue("ApplicationPath", applicationpath); settings.sync(); } else { exit(1); } delete server; } else done = true; } // Ensure the server gets cleaned up later QObject::connect(server, SIGNAL(finished()), server, SLOT(deleteLater())); // Generate the app server URL QString appServerUrl = QString("http://127.0.0.1:%1/?key=%2").arg(port).arg(key); // Read the server connection timeout from the registry or set the default timeout. int timeout = settings.value("ConnectionTimeout", 30).toInt(); // Now the server should be up, we'll attempt to connect and get a response. // We'll retry in a loop a few time before aborting if necessary. QTime endTime = QTime::currentTime().addSecs(timeout); bool alive = false; while(QTime::currentTime() <= endTime) { alive = PingServer(QUrl(appServerUrl)); if (alive) { break; } delay(200); } // Attempt to connect one more time in case of a long network timeout while looping if (!alive && !PingServer(QUrl(appServerUrl))) { splash->finish(NULL); QString error(QWidget::tr("The application server could not be contacted.")); QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error); exit(1); } // Stash the URL for any duplicate processes to open if (addrFile.open(QIODevice::WriteOnly)) { addrFile.setPermissions(QFile::ReadOwner|QFile::WriteOwner); QTextStream out(&addrFile); out << appServerUrl << endl; } // Go! trayicon->setAppServerUrl(appServerUrl); // Enable the shutdown server menu as server started successfully. trayicon->enableShutdownMenu(); QString cmd = settings.value("BrowserCommand").toString(); if (!cmd.isEmpty()) { cmd.replace("%URL%", appServerUrl); QProcess::startDetached(cmd); } else { if (!QDesktopServices::openUrl(appServerUrl)) { QString error(QWidget::tr("Failed to open the system default web browser. Is one installed?.")); QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error); exit(1); } } QObject::connect(trayicon, SIGNAL(shutdownSignal(QUrl)), server, SLOT(shutdown(QUrl))); splash->finish(NULL); return app.exec(); }
int main(int argc, char *argv[]) { FILE *bron; char **lexicon; unsigned long *freqs; unsigned long *hash; char word[1024]; char altword[1024]; char thealtword[1024]; unsigned long thishash; int i,k,nrlex=0,readnr,wordlen; FILE *context; char inlex,altsound; /* allocate lexicon */ lexicon=malloc(sizeof(char*)); freqs=malloc(sizeof(unsigned long)); hash=malloc(sizeof(unsigned long)); /* read lexicon */ nrlex=0; bron=fopen(argv[1],"r"); while (!feof(bron)) { fscanf(bron,"%d %s ", &readnr,word); lexicon[nrlex]=malloc((strlen(word)+1)*sizeof(char)); strcpy(lexicon[nrlex],word); freqs[nrlex]=readnr; hash[nrlex]=sdbm(word); nrlex++; lexicon=realloc(lexicon,(nrlex+1)*sizeof(char*)); freqs=realloc(freqs,(nrlex+1)*sizeof(unsigned long)); hash=realloc(hash,(nrlex+1)*sizeof(unsigned long)); } fclose(bron); if (DEBUG2) fprintf(stderr,"read %d words from lexicon\n", nrlex); context=fopen(argv[2],"r"); while (!feof(context)) { fscanf(context,"%s ",word); wordlen=strlen(word); // is there an obvious soundalike alternative? altsound=0; if (strlen(word)>=MINLENGTH) if ((strstr(word,"ei"))|| (strstr(word,"ij"))|| (strstr(word,"au"))|| (strstr(word,"ou"))|| (strstr(word,"g"))|| (strstr(word,"ch"))|| (strstr(word,"ks"))|| (strstr(word,"x"))) altsound=1; if (altsound) { inlex=0; thishash=sdbm(word); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if (thishash==hash[k]) { inlex=1; if (DEBUG3) fprintf(stderr,"word [%s] in lexicon, with frequency %ld\n", word,freqs[k]); } } // if the word itself does not exist, let's look for a soundalike if (!inlex) { if (strstr(word,"ei")) { strcpy(altword,word); i=0; while ((i<strlen(word))&& !((word[i]=='e')&& (word[i+1]=='i'))) i++; if (i<strlen(word)) { altword[i]='i'; altword[i+1]='j'; } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (strstr(word,"ij")) { strcpy(altword,word); i=0; while ((i<strlen(word))&& !((word[i]=='i')&& (word[i+1]=='j'))) i++; if (i<strlen(word)) { altword[i]='e'; altword[i+1]='i'; } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (strstr(word,"au")) { strcpy(altword,word); i=0; while ((i<strlen(word))&& !((word[i]=='a')&& (word[i+1]=='u'))) i++; if (i<strlen(word)) { altword[i]='o'; } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (strstr(word,"ou")) { strcpy(altword,word); i=0; while ((i<strlen(word))&& !((word[i]=='o')&& (word[i+1]=='u'))) i++; if (i<strlen(word)) { altword[i]='a'; } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (strstr(word,"g")) { strcpy(altword,""); for (i=0; i<strlen(word); i++) { if (word[i]=='g') strcat(altword,"ch"); else { strcat(altword," "); altword[strlen(altword)-1]=word[i]; } } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (strstr(word,"ch")) { strcpy(altword,""); for (i=0; i<strlen(word); i++) { if ((word[i]=='c')&& (word[i+1]=='h')) { strcat(altword,"g"); i++; } else { strcat(altword," "); altword[strlen(altword)-1]=word[i]; } } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (strstr(word,"x")) { strcpy(altword,""); for (i=0; i<strlen(word); i++) { if (word[i]=='x') strcat(altword,"ks"); else { strcat(altword," "); altword[strlen(altword)-1]=word[i]; } } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (strstr(word,"ks")) { strcpy(altword,""); for (i=0; i<strlen(word); i++) { if ((word[i]=='k')&& (word[i+1]=='s')) { strcat(altword,"x"); i++; } else { strcat(altword," "); altword[strlen(altword)-1]=word[i]; } } // does altword occur? if (DEBUG2) fprintf(stderr,"checking if soundalike word %s exists\n", altword); thishash=sdbm(altword); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if ((thishash==hash[k])&& (freqs[k]>MINFREQTHRESHOLD)) { inlex=1; strcpy(thealtword,altword); if (DEBUG2) fprintf(stderr,"found soundalike word in lexicon: %s\n", thealtword); } } } if (!inlex) altsound=0; } else altsound=0; } fprintf(stdout,"%s", word); if ((altsound)&& (strlen(thealtword)>0)) { fprintf(stdout," %s", thealtword); if (DEBUG) { fprintf(stderr,"soundalike for [%s]: [%s]\n", word,thealtword); } } fprintf(stdout,"\n"); } fclose(context); return 0; }
int main(int argc, char *argv[]) { FILE *bron; char **lexicon; unsigned long *freqs; unsigned long *hash; unsigned long thishash; char word[1024]; char left[1024]; char right[1024]; float leftfraction,rightfraction; char checklist[MAXNRCHECK][1024]; int i,j,k,nrlex=0,leftpart,rightpart,readnr,nrcheck; unsigned long curfreq,leftfreq,rightfreq; FILE *context; char inlex,inlist,leftinlex,rightinlex,runon; /* allocate lexicon */ lexicon=malloc(sizeof(char*)); freqs=malloc(sizeof(unsigned long)); hash=malloc(sizeof(unsigned long)); /* read lexicon */ nrlex=0; bron=fopen(argv[1],"r"); while (!feof(bron)) { fscanf(bron,"%d %s ", &readnr,word); lexicon[nrlex]=malloc((strlen(word)+1)*sizeof(char)); strcpy(lexicon[nrlex],word); freqs[nrlex]=readnr; hash[nrlex]=sdbm(word); nrlex++; lexicon=realloc(lexicon,(nrlex+1)*sizeof(char*)); freqs=realloc(freqs,(nrlex+1)*sizeof(unsigned long)); hash=realloc(hash,(nrlex+1)*sizeof(unsigned long)); } fclose(bron); if (DEBUG2) fprintf(stderr,"read %d words from lexicon\n", nrlex); /* reading exception list */ bron=fopen(argv[2],"r"); nrcheck=0; while ((!feof(bron))&& (nrcheck<MAXNRCHECK)) { fscanf(bron,"%s ", checklist[nrcheck]); nrcheck++; } fclose(bron); context=fopen(argv[3],"r"); while (!feof(context)) { fscanf(context,"%s ",word); inlist=0; for (k=0; ((k<nrcheck)&&(!inlist)); k++) { if (strcmp(word,checklist[k])==0) inlist=1; } runon=0; if ((!inlist)&& (strlen(word)>=(2*MINLEN))) { curfreq=0; inlex=0; thishash=sdbm(word); for (k=0; ((k<nrlex)&&(!inlex)); k++) { if (thishash==hash[k]) { curfreq=freqs[k]; inlex=1; } } if (!inlex) { for (i=MINLEN; ((!runon)&&(i<(strlen(word)-MINLEN))); i++) { strcpy(left,""); for (j=0; j<i; j++) { strcat(left," "); left[j]=word[j]; } strcpy(right,""); for (j=i; j<strlen(word); j++) { strcat(right," "); right[strlen(right)-1]=word[j]; } leftfreq=0; leftinlex=0; thishash=sdbm(left); for (k=0; ((k<nrlex)&&(!leftinlex)); k++) { if (thishash==hash[k]) { leftfreq=freqs[k]; leftinlex=1; } if (strstr(left,lexicon[k])) if (strlen(lexicon[k])+MINLEN>=strlen(left)) leftpart++; } if (leftinlex) { rightfreq=0; rightinlex=0; thishash=sdbm(right); for (k=0; ((k<nrlex)&&(!rightinlex)); k++) { if (thishash==hash[k]) { rightfreq=freqs[k]; rightinlex=1; } } if (rightinlex) { leftpart=0; for (k=0; k<nrlex; k++) { if (strstr(lexicon[k],left)) if (strlen(lexicon[k])-MINLEN>=strlen(left)) { leftpart++; } } rightpart=0; for (k=0; k<nrlex; k++) { if (strstr(lexicon[k],right)) if (strlen(lexicon[k])-MINLEN>=strlen(right)) { rightpart++; } } if (leftpart>0) leftfraction=leftfreq/(1.*leftpart); else leftfraction=0.0; if (rightpart>0) rightfraction=rightfreq/(1.*rightpart); else rightfraction=0.0; if (DEBUG2) { fprintf(stderr,"[%s] is %d times part of a larger word, fraction 1 in %.0f\n", left,leftpart,leftfraction); fprintf(stderr,"[%s] is %d times part of a larger word, fraction 1 in %.0f\n", right,rightpart,rightfraction); } if ((leftfraction>THRESHOLD)&&(rightfraction>THRESHOLD)) { if (DEBUG) fprintf(stderr,"split run-on [%s] (freq %ld) into [%s] (freq %ld) [%s] (freq %ld)\n", word,curfreq,left,leftfreq,right,rightfreq); runon=1; } } } } } } fprintf(stdout,"%s", word); if (runon) { fprintf(stdout," %s %s%6.3f", left,right, (1- (1 / (((leftfraction+rightfraction)/2)/THRESHOLD)))); } fprintf(stdout,"\n"); } fclose(context); return 0; }