yawn. I'm tired. the problem today is to implement autocomplete--
oh? what's this? coltrans already wrote the spec for me. how convenient! ^C ^V...
It's none other than round fifty, a round number, and if I'm not in Err, a quarter quell! 'Course, round twenty-five hadn't any especial attention but I digress. It's time to get with the times. We live in the future, a world of AI and computers. Your challenge, an ye deign to rise to meet it, is to implement machine learning text autocomplete.
Now, allow me to produce this envelope with a flourish, and reveal that extra quarterly sauce: all tributes will be democratically elected for a change— whoops, wrong envelope, let me light that one on fire. No, the true trick is, only languages released over 25 years ago are permitted — as a reminder to would-be rebels that change is bad (and an encore for has-been languages so that we learn from the past).
You may choose one of the following APIs:
function train(corpus: string): void, function complete(text_so_far: string): string
function complete(corpus: string, text_so_far: string): string
Something sensible along these lines.
'Machine learning' includes techniques like Markov chains. The corpus parameter has text available to help train your machine learning autocomplete. You can expect train to be called exactly once. Your complete function may produce any amount of output, although the result should depend on text_so_far given some corpus.
hi, me again. coltrans pretty much covered everything, but I'd like to clarify something about the allowed languages. the age requirement only applies to the language, not the particular version you're using.
that means that you can use python 3.12, even though it's brand new, because python 1 is from 1991. ok?
#include<iostream>#include<random>#include<string>#include<unordered_map>#include<vector>// A markov state contains words with their weightstypedefstd::unordered_map<std::string,std::size_t>MarkovState;typedefstd::unordered_map<std::string,MarkovState>MarkovChain;MarkovChainmarkovChain;std::vector<std::string>split_str(conststd::string&str,conststd::string&delim){std::size_tcurrent_pos=0;std::size_tprev_pos=0;std::vector<std::string>result{};while((current_pos=str.find(delim,prev_pos))!=std::string::npos){result.push_back(str.substr(prev_pos,current_pos-prev_pos));prev_pos=current_pos+delim.size();}result.push_back(str.substr(prev_pos,current_pos));returnresult;}std::stringget_next_word(conststd::string&last_word){if(markovChain.count(last_word)==0){return"";}// Weighted random shitstaticstd::random_devicedev;staticstd::mt19937rng(dev());std::size_ttotal_weight=0;for(auto&[word,weight]:markovChain[last_word]){total_weight+=weight;}std::uniform_int_distribution<std::size_t>dist(0,total_weight-1);std::size_trandom_weight=dist(rng);std::size_tsum=0;for(auto&[word,weight]:markovChain[last_word]){if(sum<=random_weight&&random_weight<sum+weight){returnword;}sum+=weight;}return"";}voidtrain(conststd::string&corpus){std::vector<std::string>splitted_corpus=split_str(corpus," ");// Build Markov chainfor(std::size_ti=0;i<splitted_corpus.size()-1;++i){std::string¤t_word=splitted_corpus[i];std::string&next_word=splitted_corpus[i+1];markovChain[current_word][next_word]++;}}std::stringcomplete(conststd::string&text_so_far,std::size_tlen=1){autov=split_str(text_so_far," ");std::stringlast_word=v[v.size()-1];std::stringresult{};while(len--){std::stringnext_word=get_next_word(last_word);if(next_word.empty()){returnresult;}result+=(next_word+" ");last_word=next_word;}returnresult;}intmain(){std::stringtraining_text{};std::stringtext_so_far{};std::cout<<"Enter training text: ";std::getline(std::cin,training_text);std::cout<<"Enter text so far: ";std::getline(std::cin,text_so_far);train(training_text);std::cout<<complete(text_so_far,99)<<std::endl;return0;}
importjava.util.*;importjava.util.regex.Pattern;importjava.util.stream.*;importjava.io.*;// da spek sed "Your complete function may produce any amount of output, although the result should depend on text_so_far given some corpus."// dis meks a infinte strem o'. do smth liek java thing.java "I like fish" <bible.txt | head -c 1000 o jst ctrl^C idkclassThing{publicstaticvoidmain(String...args){finalvarcorpus=newBufferedReader(newInputStreamReader(System.in)).lines().collect(Collectors.joining("\n"));finalvartext_so_far=args[0];finalvarn=3;varnext=newHashMap<String,ArrayList<String>>();varp=Pattern.compile("\\w+");varm=p.matcher(corpus);varprevs=newArrayList<String>();prevs.add("");while(m.find()){vartok=m.group();varctx=prevs.get(prevs.size()-1);for(vari=1;i<=Math.min(prevs.size()-1,n);i++){varnexts=next.getOrDefault(ctx,newArrayList<>());nexts.add(tok);next.put(ctx,nexts);ctx=" "+prevs.get(prevs.size()-1-i)+" "+ctx;}prevs.add(tok);}varr=newRandom();m=p.matcher(text_so_far);varks=prevs;prevs=newArrayList<String>();for(vari=0;i<n;i++)prevs.add(ks.get(r.nextInt(ks.size())));while(m.find())prevs.add(m.group());while(true){varctx=prevs.get(prevs.size()-1);varchoisez=newArrayList<String>();for(varj=1;j<=Math.min(prevs.size()-1,n);j++){choisez.addAll(next.getOrDefault(ctx,newArrayList<>()));ctx=prevs.get(prevs.size()-1-j)+" "+ctx;}if(choisez.isEmpty()){for(vari=0;i<n;i++)prevs.add(ks.get(r.nextInt(ks.size())));}varw=choisez.get(r.nextInt(choisez.size()));System.out.print(" "+w);prevs.add(w);}}}
post a comment