Showing posts with label NLP. Show all posts
Showing posts with label NLP. Show all posts

Tuesday, March 08, 2022

Ruminating on the Turing Test

 The Turing Test was made famous by Alan Turing in the year 1950. The Turing Test essentially tests a computer's ability to communicate indistinguishably from a human. The Turing Test was also called as the 'Imitation Game' by Alan earlier. 

A good introduction to the Turing Test can be found here - https://youtu.be/3wLqsRLvV-c

Though many claim that the turing test was passed by a AI chatbot called Eugene Goostman, but in reality it is not so. No computer has ever passed the Turing Test - https://isturingtestpassed.github.io/

Intelligent chatbots have really come a long way - The Google Duplex Demo e.g. https://youtu.be/0YaAFRirkfk

Maybe when we achieve AGI (Artificial General Intelligence), then the Turing Test would be accurately passed :)

Sunday, March 06, 2022

Ruminating on n-gram models

N-gram is a fundamental concept in NLP and is used in many language models. In simple terms, N-gram is nothing but a sequence on N words - e.g. San Francisco (is a 2-gram) and The Three Musketeers (is a 3-gram). 

N-grams are very useful because they can be used for making next word predictions, correcting spellings or grammar. Ever wondered how Gmail is able to suggest auto-completion of sentences? This is possible because Google has created a language model that can predict next words.

N-grams are also used for correcting spelling errors - e.g. “drink cofee” could be corrected to “drink coffee” because the language model can predict that 'drink' and 'coffee' being together have a high probability. Also the 'edit distance' between 'cofee' and 'coffee' is 1, hence it is a typo.

Thus N-grams are used to create probabilistic language models called n-gram models. N-gram models predict the occurrence of a word based on its N – 1 previous word. 

The 'N' depends on the type of analysis we want to do - e.g. Research has also shown that trigrams and 4-grams work the best for spam filtering. 

Some good info on N-grams is available at the Standford University site - https://web.stanford.edu/~jurafsky/slp3/slides/LM_4.pdf

Google books also has a "N-Gram" viewer displays a graph showing how those phrases have occurred in a corpus of books (e.g., "British English", "English Fiction", "French") over the selected years. I found this to be useful in understanding what topic was popular in which years: https://books.google.com/ngrams

Ruminating on Text Normalization

Text normalization is the process of converting text to a standard form before we use them for training AI NLP models. The following techniques are typically used for normalizing text. 

Tokenization:  Tokenization is the process of breaking down sentences into words. In many Latin-derived languages, "space" is considered to be a word delimeter. But there are special cases such as 'New York', 'Rock-n-Roll' etc. Also Chinese and Japanese languages do not have spaces between words. We may laso wante to tokenize emoticons and hashtags. 

Lemmatization: In this process, we check if words have the same 'root' - e.g. sings, sang. We then normalize the words to the common root word. 

Stemming: Stemming can be considered a form of Lemmatization wherein we just strip the suffixes from the end of the word - e.g. troubled and troubles can be stemmed to 'troubl'.

Lemmatization is more computationally intensive than Stemming because it actually maps the word to a dictionary and finds the root word. Whereas Stemming just uses some crude heuristic process that chops off the ends of words in the hope of getting the root word. Stemming is thus much faster when you are dealing with a large corupus of text. The following examples will make the difference clear. 

  • The word "better" has "good" as its lemma. This link is missed by stemming, as it requires a dictionary look-up.
  • The word "walk" is the base form for word "walking", and hence this is matched in both stemming and lemmatisation.
  • If you lemmatize the word 'Caring', it would return 'Care'. If you stem, it would return 'Car' and this is erroneous.

Sentence Segmentation: This entails breaking up a long sentence into smaller sentences using chars such as '! ; ?'. 

Spelling Correction and UK/US differences: As part of the normalization activity, we may also want to correct some common spelling mistakes and also normalize the different spelling between UK/US like neighbour/neighbor.

Wednesday, February 02, 2022

Fuzzy matching of Strings

Quite often during document processing or email processing tasks, we need to compare strings or search for keywords. While the traditional way of doing this would be using String comparison or RegEx, there are a number of other techniques available. 

Fuzzy matching is an approximate string matching technique that is typically used to identify typos or spelling mistakes. Fuzzy matching algorithms try to measure how close two strings are to one another using a concept called as 'Edit Distance'. In simple words, 'edit distance' can be considered as the number of edits required to make both the sentences same. There are different types of edit distance measurements as described in the Wikipedia article above. 

TheFuzz is a cool Python library that can be used to measure the Levenshtein Distance between sequences. The following articles would help you quickly grasp the basics of using the library. 

https://www.activestate.com/blog/how-to-implement-fuzzy-matching-in-python/

https://www.analyticsvidhya.com/blog/2021/07/fuzzy-string-matching-a-hands-on-guide/

https://towardsdatascience.com/fuzzy-string-matching-in-python-68f240d910fe