Package: fast-autocomplete
Fast Autocomplete 0.9.0zepworks.comFast autocomplete using Directed Word Graph (DWG) and Levenshtein Edit Distance.The results are cached via LFU (Least Frequently Used).WhyRead about why fast-autocomplete was built here:http://zepworks.com/posts/you-autocomplete-me/This library was written when we came to the conclusion that Elasticsearch's Autocomplete suggestor is not fast enough and doesn't do everything that we need:Once we switched to Fast Autocomplete, our average latency went from 120ms to 30ms so an improvement of 3-4x in performance and errors went down to zero.Elasticsearch's Autocomplete suggestor does not handle any sort of combination of the words you have put in. For example Fast Autocomplete can handle2018 Toyota Camry in Los Angeleswhen the words2018,Toyota Camry,Los Angelesare seperately fed into it. While Elasticsearch's autocomplete needs that whole sentence to be fed to it to show it in Autocomplete results.You might say:Regarding #1: Yes, but you are using caching. Answer: shhh Yes, keep it quiet. We are also doing Levenshtein Edit distance using a C library so it improves there too.Regarding #2: Cool. Answer: Ok, now we are talking.HowRead about how fast-autocomplete works here:http://zepworks.com/posts/you-autocomplete-me/In a nutshell, what the fast Autocomplete does is:Populate the DWG with your words.Follow the graph nodes letter by letter until it finds nodes that have words in them.Continue after words are found on the graph until it reaches the leaf node.Restart from the root node again until it reaches a letter that doesn't exist on the graph.Depending on how much is left from the rest of the word, return all the descendant words from where it got stuckOr run Levenshtein edit distance to find closes words to what is left and the continue from there.By doing so, it can tokenize a text such as:2018 Toyota Camry in Los Angelesinto [2018,toyota camry,in,los angeles]And return Autocomplete results as you type.Installpip install fast-autocompleteNote: Fast Autocomplete only works with Python 3.6 and newer.Are you still on Python 2? TIME TO UPGRADE.LicenceMITDWGThe data structure we use in this library is called Dawg.DWG stands for Directed Word Graph. Here is an example DWG based on the "makes_models_short.csv" that is provided in the tests:UsageFirst of all lets start from your data. The library leaves it up to you how to prepare your data.
If you want to go straight to the factory function that lets you use the library in its easiest and most common case, skip all these and jump to thesortingexample.Example 1>>>fromfast_autocompleteimportAutoComplete>>>words={'book':{},'burrito':{},'pizza':{},'pasta':{}}>>>autocomplete=AutoComplete(words=words)>>>autocomplete.search(word='b',max_cost=3,size=3)[['book'],['burrito']]>>>autocomplete.search(word='bu',max_cost=3,size=3)[['burrito']]>>>autocomplete.search(word='barrito',max_cost=3,size=3)# mis-spelling[['burrito']]Words is a dictionary and each word can have a context. For example the "count", how to display the word, some other context around the word etc. In this example words didn't have any context.Example 2Imagine that we have a csv with the following content from vehicles' make and models:make,model
acura,zdx
alfa romeo,4c
alfa romeo,4c coupe
alfa romeo,giulia
bmw,1 series
bmw,2 series
2007,2007
2017,2017
2018,2018What we want to do is to convert this to a dictionary of words and their context.importcsvfromfast_autocomplete.miscimportread_csv_gendefget_words(path):csv_gen=read_csv_gen(path,csv_func=csv.DictReader)words={}forlineincsv_gen:make=line['make']model=line['model']ifmake!=model:local_words=[model,'{}{}'.format(make,model)]whilelocal_words:word=local_words.pop()ifwordnotinwords:words[word]={}ifmakenotinwords:words[make]={}returnwordstheread_csv_genis just a helper function. You don't really need it. The whole point is that we are converting that csv to a dictionary that looks like this:>>>words=get_words('path to the csv')>>>words{'acura zdx':{},'zdx':{},'acura':{},'alfa romeo 4c':{},'4c':{},'alfa romeo':{},'alfa romeo 4c coupe':{},'4c coupe':{},'alfa romeo giulia':{},'giulia':{},'bmw 1 series':{},'1 series':{},'bmw':{},'bmw 2 series':{},'2 series':{},'2007':{},'2017':{},'2018':{}}This is a dictionary of words to their context. We have decided that we don't want any context for the words in this example so all the contexts are empty. However generally you will want some context around the words for more complicated logics. The context is used to convert the words "keys" into their context which is the value of the key in the words dictionary.In addition to words, we usually want a dictionary of synonyms. Something like this:synonyms={"alfa romeo":["alfa"],"bmw":["beemer","bimmer"],"mercedes-benz":["mercedes","benz"],"volkswagen":["vw"]}Note that synonyms are optional. Maybe in your use case you don't need synonyms.Now we can use the above to initialize Autocompletefromfast_autocompleteimportAutoCompleteautocomplete=AutoComplete(words=words,synonyms=synonyms)At this point, AutoComplete has created adwgstructure.Now you can search!word: the word to return autocomplete results formax_cost: Maximum Levenshtein edit distance to be considered when calculating resultssize: The max number of results to return>>>autocomplete.search(word='2018 bmw 1',max_cost=3,size=3)[['2018','bmw'],['2018','bmw 1 series']]Now what if we pressed a by mistake then? It still works. No problem.>>>autocomplete.search(word='2018 bmw 1a',max_cost=3,size=3)[['2018','bmw'],['2018','bmw 1 series']]Ok let's search for Alfa now:>>>autocomplete.search(word='alfa',max_cost=3,size=3)[['alfa romeo'],['alfa romeo 4c'],['alfa romeo giulia']]What if we don't know how to pronounce alfa and we typealpha?>>>autocomplete.search(word='alpha',max_cost=3,size=3)[['alfa romeo'],['alfa romeo 4c'],['alfa romeo giulia']]It still works!Fast-Autocomplete makes sure the results make sense!Ok lets add the wordLos Angelesthere to the words:>>>words['los angeles']={}>>>words['in']={}>>>autocomplete.search(word='2007 alfa in los',max_cost=3,size=3)[['2007','alfa romeo','in'],['2007','alfa romeo','in','los angeles']]So far we have not used the context. And this library leaves it up to you how to use the context. But basically if we giving a context to each one of those words, then the above response could easly be translated to the list of those contexts.contextIf our words dictionary was:words={'in':{},'alfa romeo':{'type':'make'},'2007':{'type':'year'},'los angeles':{'type':'location'},}Then theautocomplete.wordscan be used to map the results into their context:[['2007', 'alfa romeo', 'in'], ['2007', 'alfa romeo', 'in', 'los angeles']]

converted to contexts:

[[{'year': '2007'}, {'make': alfa romeo'}], [{'year': '2007'}, {'make': alfa romeo'}, {'location': 'los angeles'}]]SortingMost people who use Fast Autocomplete, want to control how results are sorted. If you don't control that, the results will be sorted based on the order that Autocomplete found the nodes in the graph that matched the criteria.The easiest way to sort is to give each item a count.Fast AutoComplete will use the count to sort items that are partial matches.For example:Make a json file that is a dictionary of words to their context.The format of the file needs to be:{word:[context,displayvalue,count]}An example is included in thesample_words.json{"acura rlx":[{"model":"rlx","make":"acura"},"Acura RLX",3132],"rlx":[{"model":"rlx","make":"acura"},"Acura RLX",3132],"acura":[{"make":"acura"},"Acura",130123],...}You might be wondering why things are in this format. It is to save space when this json can become very big easily and the keys become repetitive. That's why we are using a list with predefined order of keys. For your use case for now you can leave the context and display values as None if you want. We will opensource other factory functions soon that will fully utilize those keys in the context.Launch Autocomplete via the factory function:fromfast_autocompleteimportautocomplete_factorycontent_files={'words':{'filepath':path/to/sample_words.json,'compress':True# means compress the graph data in memory}}autocomplete=autocomplete_factory(content_files=content_files)You can use Autocomplete and the results are ordered by count!>>>autocomplete.search(word='acu')[['acura'],['acura mdx'],['acura rdx']]How do we use the context and display value now?Great question. You need to extend AutoComplete class to use these items. I will write a blog post about it.Here is a simple example without any extending:>>>autocomplete.words['acura']WordValue(context={'make':'acura'},display='Acura',count=130123,original_key=None)>>>autocomplete.words['acura'].displayAcuraChange the sorting by updating countsFast Autocomplete by default uses the "count" of the items to sort the items in the results. Think about these counts as a "guide" to Fast autocomplete so it can polish its results. Depending on whether or not Fast autocomplete finds exact matches to user's query, the counts will be used to refine the results. You can update the counts in an autocomplete object live.For example, in thesample csv of car makes and modelswe have:make,model,count
Toyota,Aurion,6094
Toyota,Avalon,8803
Toyota,Avensis,1630
Toyota,Auris,4025
Toyota,Aygo,2115If we use the autocomplete to search:>>>auto_complete=AutoComplete(words=WIKIPEDIA_WORDS,synonyms=SYNONYMS,full_stop_words=['bmw','alfa romeo'])>>>autocomplete.search(word='toyota a')[['toyota'],['toyota avalon'],['toyota aurion'],['toyota auris']]However as you can noticetoyota aygohad the count of 2115 and thus it didn't make it to the top 3 results.We can set the count fortoyota aygoto a higher number to boost it in the results usingupdate_count_of_word.Theupdate_count_of_wordcan change the count via setting the word's count directly or by offsetting its current value.>>>auto_complete=AutoComplete(words=WIKIPEDIA_WORDS,synonyms=SYNONYMS,full_stop_words=['bmw','alfa romeo'])>>>auto_complete.update_count_of_word(word='toyota aygo',count=10000)10000Now if we search:>>>autocomplete.search(word='toyota a')[['toyota'],['toyota aygo'],['toyota avalon'],['toyota aurion']]We can double check the count of a node:>>>autocomplete.get_count_of_word('toyota aygo')10000Now let's use the offset to offset the current count of a different node:>>>auto_complete.update_count_of_word(word='toyota aurion',offset=-6000)94When we search,toyota aurionis not in the top 3 results anymore!>>>autocomplete.search(word='toyota a')[['toyota'],['toyota aygo'],['toyota avalon'],['toyota auris']]UnicodeBy default this package only accepts ASCII lowercase letters, a-z. However you can pass the characters that you want to be acceptable viavalid_chars_for_stringfor strings, andvalid_chars_for_integerfor numbers. For example here we tell Autocomplete to consider the Farsi alphabet characters for string characters.AutoComplete(words=SHORT_WORDS_UNICODE,valid_chars_for_string='اآبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی')If you want to pass other characters in addition to ASCII letters, such as punctuation marks, you need to set thevalid_chars_for_stringvariable to include all of the characters you need. For example, the following code block sets ASCII letters a-z along with periods and apostrophes:valid_chars=".'"valid_chars+=string.ascii_lowercaseAutoComplete(words=WORDS_WITH_PUNCTUATION,valid_chars_for_string=valid_chars)DrawThis package can actually draw the dwgs as it is populating them or just once the dwg is populated for you!
Here is the animation of populating the dwg with words from "makes_models_short.csv":Draw animation of dwg populatingfromfast_autocompleteimportAutoComplete,DrawGraphMixinclassAutoCompleteDraw(DrawGraphMixin,AutoComplete):DRAW_POPULATION_ANIMATION=TrueDRAW_POPULATION_ANIMATION_PATH='animation/short_.svg'DRAW_POPULATION_ANIMATION_FILENO_PADDING=6autocomplete=AutoCompleteDraw(words=words,synonyms=synonyms)As soon as you initialize the above AutoCompleteDraw class, it will populate the dwg and generate the animation!
For an example of this code properly setup, take a look at the tests. In fact the animation in thedwgsection is generated the same way via unit tests!Note that if you have many words, the graph file will be big. Instead of drawing all frames as the dwg is being populated, you can just draw the final stage:Draw the final graphTo draw just one graph that shows the final stage of the dwg, use the draw mixin and run the draw_graph function:fromfast_autocompleteimportAutoComplete,DrawGraphMixinclassAutoCompleteDraw(DrawGraphMixin,AutoComplete):passautocomplete=AutoCompleteDraw(words=words,synonyms=synonyms)autocomplete.draw_graph('path to file')DemoIf you want to have a real-time interaction with Autocomplete results in your terminal, you can use the demo module:Just pass it an instance of the autocomplete and the search configs:fromfast_autocompleteimportdemodemo(autocomplete,max_cost=3,size=5)DevelopClone the repoMake a virtualenv with Python 3.6 or newerpip install -r requirements-dev.txtRun testspytestWe try to maintain high standard in code coverage. Currently thedwgmodule's coverage is around 99%!ReleasesWe use bump2version to bump and tag releases.gitcheckoutmaster&&gitpull
bump2version{patch|minor|major}gitpush&&gitpush--tagsAuthorsAutocomplete written bySep Dehpour.LFU Cache byShane WangOther ways of doing AutoCompleteElastic search. Yes, Elasticsearch generally is abetterAutocomplete solution than this library. I said generally. In our specific use case, we wanted Autocomplete to be faster than Elasticsearch and handle combination of words. Otherwise Elasticsearch would have been perfect. Behind the scene Elasticsearch uses Finite State Transducer (FST) in Lucene to achive AutoComplete. FST is more complicated than what we have used in fast-autocomplete.If your autocomplete is supposed to return results based on a big blog of text (for example based on some book contents), then a better solution is to go with Markov chains and conditional probability. Yes, there is already a library out there for it!https://github.com/rodricios/autocompleteand it looks great. Disclaimer: we have not actually used it since it doesn't fit our specific use-case.FAQWhy DWGDWG stands for Directed Word Graph. Originally we were using Trie-Tree structure. But soon it was obvious that some branches needed to  merge back to other branches. Such asbeemerandbmwbranches both need to end in the same node since they are synonyms. Thus we used DWG.What are synonyms, clean synonyms and partial synonymsSynonyms are words that should produce the same results.For examplebeemerandbmwshould both give youbmw.alfaandalfa romeoshould both give youalfa romeoThe synonyms get divided into 2 groups:clean synonyms: The 2 words share little or no words. For examplebeemervs.bmw.partial synonyms: One of the 2 words is a substring of the other one. For examplealfaandalfa romeoorgmvs.gmc.Internally these 2 types of synonyms are treated differently but as a user of the library, you don't need to really care about it. You just provide the synonyms dictionary via defining theget_synonymsmethod.Why do you have a whole subtree for partial synonymsQ: Partial synonym means the synonym is a part of the original word. Such asalfais a partial synonym foralfa romeo.
In that case you are inserting bothalfaandalfa romeoin the dwg.alfawill havealfa 4candalpha romeowill havealfa romeo 4cbranches. Why not just havealfabranches to bealfa romeoand from there you will have automatically all the sub branches ofalfa romeo.Answer: We use letters for edges. Soalfacan have only one edge coming out of it that is space (). And that edge is going to a node that has sub-branches toalfa romoe,alfa 4cetc. It can't have agoing to that node and anothergoing toalfa romeo's immediate child. That way when we are traversing the dwg for the input ofalfa 4we get to the correct node.I put Toyota in the Dawg but when I typetoy, it doesn't show up.Answer: If you putToyotawith capital T in the dwg, it expects the search word to start with capital T too. We suggest that you lower case everything before putting them in dwg. Fast-autocomplete does not automatically do that for you since it assumes thewordsdictionary is what you want to be put in the dwg. It is up to you to clean your own data before putting it in the dwg.