Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
116
Task/Inverted-index/C++/inverted-index.cpp
Normal file
116
Task/Inverted-index/C++/inverted-index.cpp
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
const std::string _CHARS = "abcdefghijklmnopqrstuvwxyz0123456789.:-_/";
|
||||
const size_t MAX_NODES = 41;
|
||||
|
||||
class node
|
||||
{
|
||||
public:
|
||||
node() { clear(); }
|
||||
node( char z ) { clear(); }
|
||||
~node() { for( int x = 0; x < MAX_NODES; x++ ) if( next[x] ) delete next[x]; }
|
||||
void clear() { for( int x = 0; x < MAX_NODES; x++ ) next[x] = 0; isWord = false; }
|
||||
bool isWord;
|
||||
std::vector<std::string> files;
|
||||
node* next[MAX_NODES];
|
||||
};
|
||||
|
||||
class index {
|
||||
public:
|
||||
void add( std::string s, std::string fileName ) {
|
||||
std::transform( s.begin(), s.end(), s.begin(), tolower );
|
||||
std::string h;
|
||||
for( std::string::iterator i = s.begin(); i != s.end(); i++ ) {
|
||||
if( *i == 32 ) {
|
||||
pushFileName( addWord( h ), fileName );
|
||||
h.clear();
|
||||
continue;
|
||||
}
|
||||
h.append( 1, *i );
|
||||
}
|
||||
if( h.length() )
|
||||
pushFileName( addWord( h ), fileName );
|
||||
}
|
||||
void findWord( std::string s ) {
|
||||
std::vector<std::string> v = find( s );
|
||||
if( !v.size() ) {
|
||||
std::cout << s + " was not found!\n";
|
||||
return;
|
||||
}
|
||||
std::cout << s << " found in:\n";
|
||||
for( std::vector<std::string>::iterator i = v.begin(); i != v.end(); i++ ) {
|
||||
std::cout << *i << "\n";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
private:
|
||||
void pushFileName( node* n, std::string fn ) {
|
||||
std::vector<std::string>::iterator i = std::find( n->files.begin(), n->files.end(), fn );
|
||||
if( i == n->files.end() ) n->files.push_back( fn );
|
||||
}
|
||||
const std::vector<std::string>& find( std::string s ) {
|
||||
size_t idx;
|
||||
std::transform( s.begin(), s.end(), s.begin(), tolower );
|
||||
node* rt = &root;
|
||||
for( std::string::iterator i = s.begin(); i != s.end(); i++ ) {
|
||||
idx = _CHARS.find( *i );
|
||||
if( idx < MAX_NODES ) {
|
||||
if( !rt->next[idx] ) return std::vector<std::string>();
|
||||
rt = rt->next[idx];
|
||||
}
|
||||
}
|
||||
if( rt->isWord ) return rt->files;
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
node* addWord( std::string s ) {
|
||||
size_t idx;
|
||||
node* rt = &root, *n;
|
||||
for( std::string::iterator i = s.begin(); i != s.end(); i++ ) {
|
||||
idx = _CHARS.find( *i );
|
||||
if( idx < MAX_NODES ) {
|
||||
n = rt->next[idx];
|
||||
if( n ){
|
||||
rt = n;
|
||||
continue;
|
||||
}
|
||||
n = new node( *i );
|
||||
rt->next[idx] = n;
|
||||
rt = n;
|
||||
}
|
||||
}
|
||||
rt->isWord = true;
|
||||
return rt;
|
||||
}
|
||||
node root;
|
||||
};
|
||||
int main( int argc, char* argv[] ) {
|
||||
index t;
|
||||
std::string s;
|
||||
std::string files[] = { "file1.txt", "f_text.txt", "text_1b.txt" };
|
||||
|
||||
for( int x = 0; x < 3; x++ ) {
|
||||
std::ifstream f;
|
||||
f.open( files[x].c_str(), std::ios::in );
|
||||
if( f.good() ) {
|
||||
while( !f.eof() ) {
|
||||
f >> s;
|
||||
t.add( s, files[x] );
|
||||
s.clear();
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
while( true ) {
|
||||
std::cout << "Enter one word to search for, return to exit: ";
|
||||
std::getline( std::cin, s );
|
||||
if( !s.length() ) break;
|
||||
t.findWord( s );
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -2,11 +2,11 @@ sub MAIN (*@files) {
|
|||
(my %norm).push: do for @files -> $file {
|
||||
$file X=> slurp($file).lc.words;
|
||||
}
|
||||
(my %inv).push: %norm.invert.uniq;
|
||||
(my %inv).push: %norm.invert.unique;
|
||||
|
||||
while prompt("Search terms: ").words -> @words {
|
||||
for @words -> $word {
|
||||
say "$word => %inv.{$word.lc}";
|
||||
say "$word => {%inv.{$word.lc}//'(not found)'}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,64 +1,58 @@
|
|||
/*REXX program illustrates building a simple inverted index & word find.*/
|
||||
@.='' /*dictionary of words (so far).*/
|
||||
!='' /*a list of found words (so far).*/
|
||||
|
||||
call invertI 0, 'BURMA0.TXT' /*read file 0 ... */
|
||||
call invertI 1, 'BURMA1.TXT' /* " " 1 ... */
|
||||
call invertI 2, 'BURMA2.TXT' /* " " 2 ... */
|
||||
call invertI 3, 'BURMA3.TXT' /* " " 3 ... */
|
||||
call invertI 4, 'BURMA4.TXT' /* " " 4 ... */
|
||||
call invertI 5, 'BURMA5.TXT' /* " " 5 ... */
|
||||
call invertI 6, 'BURMA6.TXT' /* " " 6 ... */
|
||||
call invertI 7, 'BURMA7.TXT' /* " " 7 ... */
|
||||
call invertI 8, 'BURMA8.TXT' /* " " 8 ... */
|
||||
call invertI 9, 'BURMA9.TXT' /* " " 9 ... */
|
||||
|
||||
call invertI 0, 'BURMA0.TXT' /*read the file: BURMA0.TXT ...*/
|
||||
call invertI 1, 'BURMA1.TXT' /* " " ~ BURMA1.TXT ...*/
|
||||
call invertI 2, 'BURMA2.TXT' /* " " ~ BURMA2.TXT ...*/
|
||||
call invertI 3, 'BURMA3.TXT' /* " " ~ BURMA3.TXT ...*/
|
||||
call invertI 4, 'BURMA4.TXT' /* " " ~ BURMA4.TXT ...*/
|
||||
call invertI 5, 'BURMA5.TXT' /* " " ~ BURMA5.TXT ...*/
|
||||
call invertI 6, 'BURMA6.TXT' /* " " ~ BURMA6.TXT ...*/
|
||||
call invertI 7, 'BURMA7.TXT' /* " " ~ BURMA7.TXT ...*/
|
||||
call invertI 8, 'BURMA8.TXT' /* " " ~ BURMA8.TXT ...*/
|
||||
call invertI 9, 'BURMA9.TXT' /* " " ~ BURMA9.TXT ...*/
|
||||
call findAword 'does' /*find a word. */
|
||||
call findAword '60' /*find another word. */
|
||||
call findAword "don't" /*and find another word. */
|
||||
call findAword "burma-shave" /*and find yet another word. */
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────FINDAWORD subroutine────────────────*/
|
||||
findAword: procedure expose @. /*get A word, and uppercase it. */
|
||||
parse arg ox; arg x /*OX= word; X= uppercase version*/
|
||||
_=@.x
|
||||
oxo='───'ox"───"
|
||||
findAword: procedure expose @.; arg x /*get an uppercase version of X. */
|
||||
parse arg ox /*get original (as-is) value of X*/
|
||||
_=@.x; oxo='───'ox"───"
|
||||
if _=='' then do
|
||||
say 'word' oxo "not found."
|
||||
say 'word' oxo "not found."
|
||||
return 0
|
||||
end
|
||||
_@=_ /*save _, pass it back to invoker*/
|
||||
say 'word' oxo "found in:"
|
||||
do until _==''; parse var _ f w _; say
|
||||
say ' file='f ' word='w
|
||||
end /*until ... */
|
||||
say 'word' oxo "found in:"
|
||||
do until _==''; parse var _ f w _
|
||||
say ' file='f ' word='w
|
||||
end /*until ··· */
|
||||
return _@
|
||||
/*─────────────────────────────────────INVERTI subroutine───────────────*/
|
||||
invertI: procedure expose @. !; parse arg #,fn /*file#, filename*/
|
||||
call lineout fn /*close the file, just in case. */
|
||||
w=0 /*number of words so far. */
|
||||
w=0 /*number of words found (so far).*/
|
||||
do while lines(fn)\==0 /* [↓] process the entire file.*/
|
||||
_=space(linein(fn)) /*read a line, elide extra blanks*/
|
||||
if _=='' then iterate /*if blank record, then ignore it*/
|
||||
say 'file' #", record:" _ /*echo a record (to be verbose).*/
|
||||
|
||||
do while lines(fn)\==0 /*process the entire file (below)*/
|
||||
_=space(linein(fn)) /*read 1 line, elide extra blanks*/
|
||||
if _=='' then iterate /*if blank record, then ignore it*/
|
||||
say 'file' #",record="_ /*echo a record, just to be verbose.*/
|
||||
|
||||
do until _=='' /*pick off words until done. */
|
||||
parse upper var _ xxx _ /*pick off a word (uppercased). */
|
||||
xxx=stripper(xxx) /*strip any ending punctuation. */
|
||||
if xxx='' then iterate /*is the word now blank (null) ? */
|
||||
w=w+1 /*bump the word counter. */
|
||||
@.xxx=@.xxx # w
|
||||
if wordpos(xxx,!)==0 then !=! xxx /*add to THE list of words found.*/
|
||||
end /*until ... */
|
||||
end /*while lines(fn)¬==0*/
|
||||
|
||||
say; call lineout fn /*close the file, just to be neat*/
|
||||
do until _=='' /*pick off words until done. */
|
||||
parse upper var _ ? _ /*pick off a word (uppercased). */
|
||||
?=stripper(?) /*strip any trailing punctuation.*/
|
||||
if ?='' then iterate /*is the word now blank (null) ? */
|
||||
w=w+1 /*bump the word counter (index). */
|
||||
@.?=@.? # w /*append the new word to a list. */
|
||||
if wordpos(?,!)==0 then !=! ? /*add to the list of words found.*/
|
||||
end /*until ··· */
|
||||
end /*while ··· */
|
||||
say; call lineout fn /*close the file, just to be neat*/
|
||||
return w /*return the index of the word. */
|
||||
/*─────────────────────────────────────STRIPPER subroutine──────────────*/
|
||||
stripper: procedure; parse arg q /*remove punctuation at word-end.*/
|
||||
@punctuation='.,:;?¿!¡' /*serveral punctuation marks. */
|
||||
do j=1 for length(@punctuation)
|
||||
q=strip(q,'T',substr(@punctuation,j,1))
|
||||
end /*j*/
|
||||
@punctuation='.,:;?¿!¡∙·'; do j=1 for length(@punctuation)
|
||||
q=strip(q,'T',substr(@punctuation,j,1))
|
||||
end /*j*/
|
||||
return q
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue