Site icon THE FOREFRONT OF TECHNOLOGY

How to train a Topic Tagging Model to assign high-quality topic to articles

How to train a Topic Tagging Model to assign high-quality topic to articles

Author(s: Fabio Chiusano Original publication on , the World’s Most Reputable AI and Technology News and Media Company. We invite you to become an AI sponsor if you’re working on an AI product or service. helps technology and AI startups scale. We can help you bring your technology to mass markets. Gabriel Sollmann, Unsplash Natural Language Processing. From the training set creation to a trained model. Topic Tagging refers to assigning topics for various content forms. Text is most popular. Authors often tag articles in newspapers to organize knowledge and make it easier for the readers to find the information they are looking for. It can also be used to analyse a large amount of text data such as a stream or articles from blogs, where each blog may use different tags, or none at all, or social media posts. Two types of topic tagging can be distinguished: Predictive and extractive. Consider the following example: You can buy and sell goods or services with cryptocurrencies. Learn more about cryptocurrency, including how you can buy it. The extraction topic tag works by looking for keywords in text, and then using the normalized form to create topics. These topics can often be enriched with categories from open-source knowledge bases such as WikiData. For the examples above, topics would include Money, Business and Give. Predictive topic tagging is a method that uses a set of predefined topics to train a classification model using several examples of text for each topic. You can easily find the training set on the internet, as each article that has tags is an example of a possible training sample. Topics that could be used to predict the outcome of this example are Cryptocurrency and Cryptography Technology, Cryptography Technology, Cryptography Technology, Cryptography Technology, Cryptography Technology, Cryptography Technology, Cryptography Technology, Cryptography Technology, Cryptographic, Trade, Security, Security. Safety. Privacy. Blockchain. Economics. Below is an overview of extractive topic tagging and predictive topic tag, including pros and cons. Comparison of extractive and predictive topic-tagging. Photo by the author. This article aims to help you predict topics of high quality. We will focus our attention on Predictive Topic Tagging. Training data is necessary to train topic tagging models. Articles published online may include tags that identify their authors. This ensures data is of high quality. Medium provides a great source for such articles. Let’s use Medium to create a training set. This Colab contains the full code. To scrape Medium for training data, you’ll need newspaper3k in order to extract HTML article data and langdetect so that English articles are kept. https://medium.com/media/21fd8629549a203092a820c1b895f0c8/href The next step is to define a function that scrapes an article from its URL, parses it with newspaper3k to extract its title and text content, and gets the article tags with some heuristics. https://medium.com/media/064497b99cda82bbca499adbf79b4a49/href Now that we know how to scrape an article from an URL, we need a place to find all the articles with the tags we are interested in. We can use Medium’s archive pages to find articles with the tags we are interested in. For example, the archive page for the topic Artificial Intelligence for the date January 15th, 2020 is https://medium.com/tag/artificial-intelligence/archive/2020/01/15. We can generalize the URL structure as https://medium.com/tag//archive/// and generate a list of archive pages with random dates for a specific tag to scrape. A working CSS selector is all that’s required to get article URLs for each archive page. https://medium.com/media/010f9b973f1cdc9086e530b4f44bf5b6/href Let’s define some tags to scrape, we start with these six. https://medium.com/media/da3a89e2450a6cf40f06e6508e009995/href Now we can stick together all the pieces and start collecting 50 articles for each tag. Be gentle with scraping, and be patient between each request. https://medium.com/media/2e7a2ecf740af8b4c996521becbca839/href Eventually, we save the scraped articles in a Pandas dataframe. This dataframe will serve as a template for our machine-learning model. https://medium.com/media/9a42c9ba49ddb49778ffbe65118e6074/href Training the Topic Tagging model We’ll need Pandas and NumPy for data manipulation, NLTK to normalize text, and Sklearn for the machine learning model. https://medium.com/media/c3facfe71e5326fb1bc23def78adb557/href This is how our dataframe with the scraped data should look like. Pandas Dataframe with articles and tags. Photo by the author. First, remove all tags from scraped articles that were not used (i.e. We need to remove all tags that we didn’t scrape from the articles. https://medium.com/media/e9133329f3570542475ad64a639dc930/href To give you an idea, this is an example of how the MultiLabelBinarizer works, taken from the Sklearn documentation. >>> mlb.fit_transform([{'sci-fi', 'thriller'}, {'comedy'}])array([[0, 1, 1], [1, 0, 0]])>>> list(mlb.classes_)['comedy', 'sci-fi', 'thriller'] We can now prepare the data for our model. We concatenate the title of the article and its text to predict topics for the model. The text of the article is cleaned up using standard text processing, such as changing it to lowercase, replacing poor characters and eliminating stopwords. A TfidfVectorizer transforms the cleaned text. TF-IDF is also used for document classification. Using TF-IDF in place of raw token frequency in a document decreases its impact and is therefore more informative empirically than tokens which occur less frequently. https://medium.com/media/7d58980c569f9ddba4708e91bcfd641b/href Now that we have prepared the training data, we can train our model. We have a multilabel classification issue. This means that we need to assign labels to each sample using a subset all possible labels. We can do this by training a binary classifier for each topic, exactly what the MultiOutputClassifier does. https://medium.com/media/eb7789d8247431dbb0777f051b3c9d65/href We are at the end! Let’s give the model a try with some mock sentences. This model correctly predicts Artificial Intelligence topics and Programming topics. Photo by the author. This trained model correctly predicts Social Media topics. Photo by the author. These topics match the input text. Good job! There are improvements that could be made. Although we were able to get the message across, there are still many things we missed. Test the data and validate the model. To determine if the model is beneficially modified, we should assess its precision, recall, F1-score, and split the dataset. F1-score is the most common metric for optimizing in multilabel classification settings. However, you can choose any other metric that suits your use case. If the model assists humans with assigning articles to categories, recall might be more crucial than precision, as it is possible for someone else to correct any prediction error. Fine tune the threshold for each binary classification. To optimize your metric, remember to adjust this parameter in the validation set. Multiple websites can be used to provide training data. This tutorial only scraped Medium. But there are other sites to get data. Taxonomies can help you prepare better training data. Data scientists are often too focused on the model, and not enough on quality. Articles and tags make up our training set.