Bing Liu - Web Data Mining - Data-Centric Systems and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Data-Centric Systems and Applications Series Editors M.J. Carey S. Ceri Editorial Board P. Bernstein U. Dayal C. Faloutsos J.C. Freytag G. Gardarin W. Jonker V. Krishnamurthy M.-A. Neimat P. Valduriez G. Weikum K.-Y. Whang J. Widom Bing Liu Web Data Mining Exploring Hyperlinks, Contents, and Usage Data With 177 Figures 123 Bing Liu Department of Computer Science University of Illinois at Chicago 851 S. Morgan Street Chicago, IL 60607-7053 USA liub@cs.uic.edu Library of Congress Control Number: 2006937132 ACM Computing Classification (1998): H.2, H.3, I.2, I.5, E.5 ISBN-10 3-540-37881-2 Springer Berlin Heidelberg New York ISBN-13 978-3-540-37881-5 Springer Berlin Heidelberg New York This work is subject to copyright. All rights are reserved, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilm or in any other way, and storage in data banks. Duplication of this publication or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, in its current version, and permission for use must always be obtained from Springer. Violations are liable for prosecution under the German Copyright Law. Springer is a part of Springer Science+Business Media springer.com © Springer-Verlag Berlin Heidelberg 2007 The use of general descriptive names, registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. Cover Design: KünkelLopka, Heidelberg Typesetting: by the Author Production: LE-TEX Jelonek, Schmidt & Vöckler GbR, Leipzig Printed on acid-free paper 45/3100/YL 543210 To my parents, my wife Yue and children Shelley and Kate Preface The rapid growth of the Web in the last decade makes it the largest publicly accessible data source in the world. Web mining aims to discover useful information or knowledge from Web hyperlinks, page contents, and usage logs. Based on the primary kinds of data used in the mining process, Web mining tasks can be categorized into three main types: Web structure mining, Web content mining and Web usage mining. Web structure mining discovers knowledge from hyperlinks, which represent the structure of the Web. Web content mining extracts useful information/knowledge from Web page contents. Web usage mining mines user access patterns from usage logs, which record clicks made by every user. The goal of this book is to present these tasks, and their core mining algorithms. The book is intended to be a text with a comprehensive coverage, and yet, for each topic, sufficient details are given so that readers can gain a reasonably complete knowledge of its algorithms or techniques without referring to any external materials. Four of the chapters, structured data extraction, information integration, opinion mining, and Web usage mining, make this book unique. These topics are not covered by existing books, but yet they are essential to Web data mining. Traditional Web mining topics such as search, crawling and resource discovery, and link analysis are also covered in detail in this book. Although the book is entitled Web Data Mining, it also includes the main topics of data mining and information retrieval since Web mining uses their algorithms and techniques extensively. The data mining part mainly consists of chapters on association rules and sequential patterns, supervised learning (or classification), and unsupervised learning (or clustering), which are the three most important data mining tasks. The advanced topic of partially (semi-) supervised learning is included as well. For information retrieval, its core topics that are crucial to Web mining are described. This book is thus naturally divided into two parts. The first part, which consists of Chaps. 2–5, covers data mining foundations. The second part, which contains Chaps. 6–12, covers Web specific mining. Two main principles have guided the writing of this book. First, the basic content of the book should be accessible to undergraduate students, and yet there are sufficient in-depth materials for graduate students who plan to VIII Preface pursue Ph.D. degrees in Web data mining or related areas. Few assumptions are made in the book regarding the prerequisite knowledge of readers. One with a basic understanding of algorithms and probability concepts should have no problem with this book. Second, the book should examine the Web mining technology from a practical point of view. This is important because most Web mining tasks have immediate real-world applications. In the past few years, I was fortunate to have worked directly or indirectly with many researchers and engineers in several search engine and e-commerce companies, and also traditional companies that are interested in exploiting the information on the Web in their businesses. During the process, I gained practical experiences and first-hand knowledge of realworld problems. I try to pass those non-confidential pieces of information and knowledge along in the book. The book, thus, should have a good balance of theory and practice. I hope that it will not only be a learning text for students, but also a valuable source of information/knowledge and even ideas for Web mining researchers and practitioners. Acknowledgements Many researchers have assisted me technically in writing this book. Without their help, this book might never have become reality. My deepest thanks goes to Filippo Menczer and Bamshad Mobasher, who were so kind to have helped write two essential chapters of the book. They are both experts in their respective fields. Filippo wrote the chapter on Web crawling and Bamshad wrote the chapter on Web usage mining. I am also very grateful to Wee Sun Lee, who helped a great deal in the writing of Chap. 5 on partially supervised learning. Jian Pei helped with the writing of the PrefixSpan algorithm in Chap. 2, and checked the MS-PS algorithm. Eduard Dragut assisted with the writing of the last section of Chap. 10 and also read the chapter many times. Yuanlin Zhang gave many great suggestions on Chap. 9. I am indebted to all of them. Many other researchers also assisted in various ways. Yang Dai and Rudy Setiono helped with Support Vector Machines (SVM). Chris Ding helped with link analysis. Clement Yu and ChengXiang Zhai read Chap. 6, and Amy Langville read Chap. 7. Kevin C.-C. Chang, Ji-Rong Wen and Clement Yu helped with many aspects of Chap 10. Justin Zobel helped clarify some issues related to index compression, and Ion Muslea helped clarify some issues on wrapper induction. Divy Agrawal, Yunbo Cao, Edward Fox, Hang Li, Xiaoli Li, Zhaohui Tan, Dell Zhang and Zijian Zheng helped check various chapters or sections. I am very grateful. Preface IX Discussions with many researchers helped shape the book as well: Amir Ashkenazi, Imran Aziz, Roberto Bayardo, Wendell Baker, Ling Bao, Jeffrey Benkler, AnHai Doan, Byron Dom, Michael Gamon, Robert Grossman, Jiawei Han, Wynne Hsu, Ronny Kohavi, David D. Lewis, Ian McAllister, Wei-Ying Ma, Marco Maggini, Llew Mason, Kamel Nigan, Julian Qian, Yan Qu, Thomas M. Tirpak, Andrew Tomkins, Alexander Tuzhilin, Weimin Xiao, Gu Xu, Philip S. Yu, and Mohammed Zaki. My former and current students, Gao Cong, Minqing Hu, Nitin Jindal, Xin Li, Yiming Ma, Yanhong Zhai and Kaidi Zhao checked many algorithms and made numerous corrections. Some chapters of the book have been used in my graduate classes at the University of Illinois at Chicago. I thank the students in these classes for implementing several algorithms. Their questions helped me improve and, in some cases, correct the algorithms. It is not possible to list all their names. Here, I would particularly like to thank John Castano, Xiaowen Ding, Murthy Ganapathibhotla, Cynthia Kersey, Hari Prasad Divyakotti, Ravikanth Turlapati, Srikanth Tadikonda, Makio Tamura, Haisheng Wang, and Chad Williams for pointing out errors in texts, examples or algorithms. Michael Bombyk from DePaul University also found several typing errors. It was a pleasure working with the helpful staff at Springer. I thank my editor Ralf Gerstner who asked me in early 2005 whether I was interested in writing a book on Web mining. It has been a wonderful experience working with him since. I also thank my copyeditor Mike Nugent for helping me improve the presentation, and my production editor Michael Reinfarth for guiding me through the final production process. Two anonymous reviewers also gave me many insightful comments. The Department of Computer Science at the University of Illinois at Chicago provided computing resources and a supportive environment for this project. Finally, I thank my parents, brother and sister for their constant supports and encouragements. My greatest gratitude goes to my own family: Yue, Shelley and Kate. They have helped me in so many ways. Despite their young ages, Shelley and Kate actually read many parts of the book and caught numerous typing errors. My wife has taken care of almost everything at home and put up with me and the long hours that I have spent on this book. I dedicate this book to them. Bing Liu Table of Contents 1. Introduction ································································· 1 1.1. What is the World Wide Web? ······································ 1 1.2. A Brief History of the Web and the Internet ·················· 2 1.3. Web Data Mining ··························································· 4 1.3.1. What is Data Mining? ················································· 6 1.3.2. What is Web Mining? ················································· 6 1.4. Summary of Chapters ··················································· 8 1.5. How to Read this Book ················································ 11 Bibliographic Notes ································································ 12 Part I: Data Mining Foundations 2. Association Rules and Sequential Patterns ············ 13 2.1. Basic Concepts of Association Rules ··························· 13 2.2. Apriori Algorithm ··························································· 16 2.2.1. 2.2.2 Frequent Itemset Generation ··································· 16 Association Rule Generation ··································· 20 2.3. Data Formats for Association Rule Mining ··················· 22 2.4. Mining with Multiple Minimum Supports ······················· 22 2.4.1 2.4.2. 2.4.3. Extended Model ······················································· 24 Mining Algorithm ······················································ 26 Rule Generation ······················································· 31 2.5. Mining Class Association Rules ··································· 32 2.5.1. 2.5.2. 2.5.3. Problem Definition ···················································· 32 Mining Algorithm ······················································ 34 Mining with Multiple Minimum Supports ·················· 37 XII Table of Contents 2.6. Basic Concepts of Sequential Patterns ······················· 37 2.7. Mining Sequential Patterns Based on GSP··················· 39 2.7.1. 2.7.2. GSP Algorithm ························································· 39 Mining with Multiple Minimum Supports ·················· 41 2.8. Mining Sequential Patterns Based on PrefixSpan ········ 45 2.8.1. 2.8.2. PrefixSpan Algorithm ··············································· 46 Mining with Multiple Minimum Supports ·················· 48 2.9. Generating Rules from Sequential Patterns·················· 49 2.9.1. 2.9.2. 2.9.3. Sequential Rules ······················································ 50 Label Sequential Rules ············································ 50 Class Sequential Rules ············································ 51 Bibliographic Notes ································································ 52 3. Supervised Learning ·················································· 55 3.1. Basic Concepts ··································································· 55 3.2. Decision Tree Induction ···················································· 59 3.2.1. 3.2.2. 3.2.3. 3.2.4. Learning Algorithm ··················································· Impurity Function ····················································· Handling of Continuous Attributes ··························· Some Other Issues ·················································· 62 63 67 68 3.3. Classifier Evaluation ·························································· 71 3.3.1. 3.3.2. Evaluation Methods ················································· 71 Precision, Recall, F-score and Breakeven Point ····· 73 3.4. Rule Induction ····································································· 75 3.4.1. 3.4.2. 3.4.3. Sequential Covering ················································· 75 Rule Learning: Learn-One-Rule Function················· 78 Discussion ································································ 81 3.5. Classification Based on Associations ····························· 81 3.5.1. 3.5.2. 3.5.3. Classification Using Class Association Rules ·········· 82 Class Association Rules as Features ······················ 86 Classification Using Normal Association Rules ······· 86 3.6. Naïve Bayesian Classification ·········································· 87 3.7. Naïve Bayesian Text Classification ································· 91 3.7.1. 3.7.2. 3.7.3. Probabilistic Framework ··········································· 92 Naïve Bayesian Model ············································· 93 Discussion ································································ 96 3.8. Support Vector Machines ················································· 97 3.8.1. Linear SVM: Separable Case ·································· 99 Table of Contents 3.8.2. 3.8.3. XIII Linear SVM: Non-Separable Case ························· 105 Nonlinear SVM: Kernel Functions ·························· 108 3.9. K-Nearest Neighbor Learning ········································· 112 3.10. Ensemble of Classifiers ··················································· 113 3.10.1. Bagging ·································································· 114 3.10.2. Boosting ································································· 114 Bibliographic Notes ···································································· 115 4. Unsupervised Learning ············································ 117 4.1. Basic Concepts ··························································· 117 4.2. K-means Clustering ···················································· 120 4.2.1. 4.2.2. 4.2.3. K-means Algorithm ················································ 120 Disk Version of the K-means Algorithm ················· 123 Strengths and Weaknesses ··································· 124 4.3. Representation of Clusters ············································· 128 4.3.1. 4.3.2 Common Ways of Representing Clusters ·············· 129 Clusters of Arbitrary Shapes ·································· 130 4.4. Hierarchical Clustering ···················································· 131 4.4.1. 4.4.2. 4.4.3. 4.4.4. Single-Link Method ················································ Complete-Link Method ··········································· Average-Link Method ············································· Strengths and Weaknesses ··································· 133 133 134 134 4.5. Distance Functions ·························································· 135 4.5.1. 4.5.2. 4.5.3. Numeric Attributes ················································· 135 Binary and Nominal Attributes ······························· 136 Text Documents ····················································· 138 4.6. Data Standardization ······················································· 139 4.7. Handling of Mixed Attributes ·········································· 141 4.8. Which Clustering Algorithm to Use? ····························· 143 4.9. Cluster Evaluation ···························································· 143 4.10. Discovering Holes and Data Regions ··························· 146 Bibliographic Notes ···································································· 149 5. Partially Supervised Learning ································ 151 5.1. Learning from Labeled and Unlabeled Examples ······ 151 5.1.1. EM Algorithm with Naïve Bayesian Classification · 153 XIV Table of Contents 5.1.2. 5.1.3. 5.1.4. 5.1.5. 5.1.6. Co-Training ···························································· Self-Training ··························································· Transductive Support Vector Machines ················· Graph-Based Methods ··········································· Discussion ······························································ 156 158 159 160 164 5.2. Learning from Positive and Unlabeled Examples ······· 165 5.2.1. 5.2.2. 5.2.3. 5.2.4. 5.2.5. Applications of PU Learning ··································· Theoretical Foundation ·········································· Building Classifiers: Two-Step Approach ··············· Building Classifiers: Direct Approach ····················· Discussion ······························································ 165 168 169 175 178 Appendix: Derivation of EM for Naïve Bayesian Classification ·· 179 Bibliographic Notes ···································································· 181 Part II: Web Mining 6. Information Retrieval and Web Search ··················· 183 6.1. Basic Concepts of Information Retrieval ······················ 184 6.2. Information Retrieval Models ······································· 187 6.2.1. 6.2.2. 6.2.3. Boolean Model ······················································· 188 Vector Space Model ··············································· 188 Statistical Language Model ···································· 191 6.3. Relevance Feedback ······················································· 192 6.4. Evaluation Measures ······················································· 195 6.5. Text and Web Page Pre-Processing ····························· 199 6.5.1. 6.5.2. 6.5.3. 6.5.4. 6.5.5. Stopword Removal ················································· Stemming ······························································· Other Pre-Processing Tasks for Text ···················· Web Page Pre-Processing ····································· Duplicate Detection ················································ 199 200 200 201 203 6.6. Inverted Index and Its Compression ····························· 204 6.6.1. 6.6.2. 6.6.3. 6.6.4. Inverted Index ························································ Search Using an Inverted Index ···························· Index Construction ················································· Index Compression ················································ 204 206 207 209 Table of Contents XV 6.7. Latent Semantic Indexing ··············································· 215 6.7.1. 6.7.2. 6.7.3. 6.7.4. Singular Value Decomposition ······························· Query and Retrieval ··············································· An Example ···························································· Discussion ······························································ 215 218 219 221 6.8. Web Search ······································································ 222 6.9. Meta-Search: Combining Multiple Rankings ·············· 225 6.9.1. 6.9.2. Combination Using Similarity Scores ····················· 226 Combination Using Rank Positions ························ 227 6.10. Web Spamming ································································ 229 6.10.1. 6.10.2. 6.10.3. 6.10.4. Content Spamming ················································ Link Spamming ······················································ Hiding Techniques ················································· Combating Spam ··················································· 230 231 233 234 Bibliographic Notes ··································································· 235 7. Link Analysis ···························································· 237 7.1. Social Network Analysis ·················································· 238 7.1.1 7.1.2 Centrality ································································ 238 Prestige ·································································· 241 7.2. Co-Citation and Bibliographic Coupling ························ 243 7.2.1. 7.2.2. Co-Citation ····························································· 244 Bibliographic Coupling ··········································· 245 7.3. PageRank ·········································································· 245 7.3.1. 7.3.2. 7.3.3. PageRank Algorithm ·············································· 246 Strengths and Weaknesses of PageRank ·············· 253 Timed PageRank ···················································· 254 7.4. HITS ··················································································· 255 7.4.1. 7.4.2. 7.4.3. 7.4.4. HITS Algorithm ······················································· Finding Other Eigenvectors ··································· Relationships with Co-Citation and Bibliographic Coupling ································································· Strengths and Weaknesses of HITS ······················ 256 259 259 260 7.5. Community Discovery ····················································· 261 7.5.1. 7.5.2. 7.5.3. 7.5.4. 7.5.5. Problem Definition ·················································· Bipartite Core Communities ··································· Maximum Flow Communities ································· Email Communities Based on Betweenness ········· Overlapping Communities of Named Entities ········ 262 264 265 268 270 XVI Table of Contents Bibliographic Notes ···································································· 271 8. Web Crawling ···························································· 273 8.1. A Basic Crawler Algorithm ·········································· 274 8.1.1. 8.1.2. Breadth-First Crawlers ··········································· 275 Preferential Crawlers ············································· 276 8.2. Implementation Issues ···················································· 277 8.2.1. 8.2.2. 8.2.3. 8.2.4. 8.2.5. 8.2.6. 8.2.7. Fetching ································································· Parsing ··································································· Stopword Removal and Stemming ························ Link Extraction and Canonicalization ····················· Spider Traps ··························································· Page Repository ···················································· Concurrency ··························································· 277 278 280 280 282 283 284 8.3. Universal Crawlers ··························································· 285 8.3.1. 8.3.2. Scalability ······························································· 286 Coverage vs Freshness vs Importance ················· 288 8.4. Focused Crawlers ···························································· 289 8.5. Topical Crawlers ······························································· 292 8.5.1. 8.5.2. 8.5.3. Topical Locality and Cues ······································ 294 Best-First Variations ··············································· 300 Adaptation ······························································ 303 8.6. Evaluation ·········································································· 310 8.7. Crawler Ethics and Conflicts ·········································· 315 8.8. Some New Developments ·············································· 318 Bibliographic Notes ······························································ 320 9. Structured Data Extraction: Wrapper Generation · 323 9.1 Preliminaries ····································································· 324 9.1.1. 9.1.2. 9.1.3. Two Types of Data Rich Pages ····························· 324 Data Model ····························································· 326 HTML Mark-Up Encoding of Data Instances ········· 328 9.2. Wrapper Induction ···························································· 330 9.2.1. 9.2.2. 9.2.3. 9.2.4. Extraction from a Page ·········································· Learning Extraction Rules ······································ Identifying Informative Examples ··························· Wrapper Maintenance ············································ 330 333 337 338 Table of Contents XVII 9.3. Instance-Based Wrapper Learning ································ 338 9.4. Automatic Wrapper Generation: Problems ·················· 341 9.4.1. 9.4.2. Two Extraction Problems ······································· 342 Patterns as Regular Expressions ··························· 343 9.5. String Matching and Tree Matching ······························ 344 9.5.1. 9.5.2. String Edit Distance ··············································· 344 Tree Matching ························································ 346 9.6. Multiple Alignment ···························································· 350 9.6.1. 9.6.2. Center Star Method ················································ 350 Partial Tree Alignment ··········································· 351 9.7. Building DOM Trees ························································ 356 9.8. Extraction Based on a Single List Page: Flat Data Records ···························································· 357 9.8.1. 9.8.2. 9.8.3. 9.8.4. 9.8.5. 9.8.6. Two Observations about Data Records ················· Mining Data Regions ·············································· Identifying Data Records in Data Regions ············· Data Item Alignment and Extraction ······················ Making Use of Visual Information ·························· Some Other Techniques ········································ 358 359 364 365 366 366 9.9. Extraction Based on a Single List Page: Nested Data Records ······················································ 367 9.10. Extraction Based on Multiple Pages ····························· 373 9.10.1. Using Techniques in Previous Sections ················ 373 9.10.2. RoadRunner Algorithm ·········································· 374 9.11. Some Other Issues ·························································· 375 9.11.1. 9.11.2. 9.11.3. 9.11.4. 9.11.5. Extraction from Other Pages ·································· Disjunction or Optional ··········································· A Set Type or a Tuple Type ··································· Labeling and Integration ········································ Domain Specific Extraction ···································· 375 376 377 378 378 9.12. Discussion ········································································· 379 Bibliographic Notes ···································································· 379 10. Information Integration ············································ 381 10.1. Introduction to Schema Matching ································ 382 10.2. Pre-Processing for Schema Matching ························· 384 10.3. Schema-Level Match ····················································· 385 XVIII Table of Contents 10.3.1. Linguistic Approaches ·········································· 385 10.3.2. Constraint Based Approaches ····························· 386 10.4. Domain and Instance-Level Matching ························· 387 10.5. Combining Similarities ··················································· 390 10.6. 1:m Match ········································································ 391 10.7. Some Other Issues ························································· 392 10.7.1. 10.7.2. 10.7.3 10.7.4 Reuse of Previous Match Results ························· Matching a Large Number of Schemas ················ Schema Match Results ········································ User Interactions ·················································· 392 393 393 394 10.8. Integration of Web Query Interfaces ···························· 394 10.8.1. A Clustering Based Approach ······························ 397 10.8.2. A Correlation Based Approach······························ 400 10.8.3. An Instance Based Approach································ 403 10.9. Constructing a Unified Global Query Interface ·········· 406 10.9.1. Structural Appropriateness and the Merge Algorithm ···················································· 406 10.9.2. Lexical Appropriateness ········································ 408 10.9.3. Instance Appropriateness······································ 409 Bibliographic Notes ···································································· 410 11. Opinion Mining ······································································ 411 11.1. Sentiment Classification ················································ 412 11.1.1. Classification Based on Sentiment Phrases ········ 413 11.1.2. Classification Using Text Classification Methods · 415 11.1.3. Classification Using a Score Function ·················· 416 11.2. Feature-Based Opinion Mining and Summarization ·· 417 11.2.1. Problem Definition ················································ 11.2.2. Object Feature Extraction······································ 11.2.3. Feature Extraction from Pros and Cons of Format 1 ···························································· 11.2.4. Feature Extraction from Reviews of of Formats 2 and 3 ················································ 11.2.5. Opinion Orientation Classification ························ 418 424 425 429 430 11.3. Comparative Sentence and Relation Mining ·············· 432 11.3.1. Problem Definition ················································ 433 11.3.2. Identification of Gradable Comparative Sentences ···························································· 435 Table of Contents XIX 11.3.3. Extraction of Comparative Relations····················· 437 11.4. Opinion Search ································································ 439 11.5. Opinion Spam ·································································· 441 11.5.1. 11.5.2. 11.5.3. 11.5.4. Objectives and Actions of Opinion Spamming ····· Types of Spam and Spammers ···························· Hiding Techniques················································· Spam Detection ···················································· 441 442 443 444 Bibliographic Notes ···································································· 446 12. Web Usage Mining ······························································· 449 12.1. Data Collection and Pre-Processing ···························· 450 12.1.1 Sources and Types of Data ·································· 452 12.1.2 Key Elements of Web Usage Data Pre-Processing ····················································· 455 12.2 Data Modeling for Web Usage Mining ························ 462 12.3 Discovery and Analysis of Web Usage Patterns ······· 466 12.3.1. 12.3.2. 12.3.3 12.3.4 12.3.5. Session and Visitor Analysis ································ 466 Cluster Analysis and Visitor Segmentation ·········· 467 Association and Correlation Analysis ··················· 471 Analysis of Sequential and Navigational Patterns 475 Classification and Prediction Based on Web User Transactions ························································· 479 12.4. Discussion and Outlook ················································· 482 Bibliographic Notes ···································································· 482 References ····················································································· 485 Index ·································································································· 517 1 Introduction When you read this book, you, without doubt, already know what the World Wide Web is and have used it extensively. The World Wide Web (or the Web for short) has impacted on almost every aspect of our lives. It is the biggest and most widely known information source that is easily accessible and searchable. It consists of billions of interconnected documents (called Web pages) which are authored by millions of people. Since its inception, the Web has dramatically changed our information seeking behavior. Before the Web, finding information means asking a friend or an expert, or buying/borrowing a book to read. However, with the Web, everything is only a few clicks away from the comfort of our homes or offices. Not only can we find needed information on the Web, but we can also easily share our information and knowledge with others. The Web has also become an important channel for conducting businesses. We can buy almost anything from online stores without needing to go to a physical shop. The Web also provides convenient means for us to communicate with each other, to express our views and opinions on anything, and to discuss with people from anywhere in the world. The Web is truly a virtual society. In this first chapter, we introduce the Web, its history, and the topics that we will study in the book. 1.1 What is the World Wide Web? The World Wide Web is officially defined as a “wide-area hypermedia information retrieval initiative aiming to give universal access to a large universe of documents.” In simpler terms, the Web is an Internet-based computer network that allows users of one computer to access information stored on another through the world-wide network called the Internet. The Web's implementation follows a standard client-server model. In this model, a user relies on a program (called the client) to connect to a remote machine (called the server) where the data is stored. Navigating through the Web is done by means of a client program called the browser, e.g., Netscape, Internet Explorer, Firefox, etc. Web browsers work by sending requests to remote servers for information and then interpreting 2 1 Introduction the returned documents written in HTML and laying out the text and graphics on the user’s computer screen on the client side. The operation of the Web relies on the structure of its hypertext documents. Hypertext allows Web page authors to link their documents to other related documents residing on computers anywhere in the world. To view these documents, one simply follows the links (called hyperlinks). The idea of hypertext was invented by Ted Nelson in 1965 [403], who also created the well known hypertext system Xanadu (http://xanadu. com/). Hypertext that also allows other media (e.g., image, audio and video files) is called hypermedia. 1.2 A Brief History of the Web and the Internet Creation of the Web: The Web was invented in 1989 by Tim BernersLee, who, at that time, worked at CERN (Centre European pour la Recherche Nucleaire, or European Laboratory for Particle Physics) in Switzerland. He coined the term “World Wide Web,” wrote the first World Wide Web server, httpd, and the first client program (a browser and editor), “WorldWideWeb”. It began in March 1989 when Tim Berners-Lee submitted a proposal titled “Information Management: A Proposal” to his superiors at CERN. In the proposal, he discussed the disadvantages of hierarchical information organization and outlined the advantages of a hypertext-based system. The proposal called for a simple protocol that could request information stored in remote systems through networks, and for a scheme by which information could be exchanged in a common format and documents of individuals could be linked by hyperlinks to other documents. It also proposed methods for reading text and graphics using the display technology at CERN at that time. The proposal essentially outlined a distributed hypertext system, which is the basic architecture of the Web. Initially, the proposal did not receive the needed support. However, in 1990, Berners-Lee re-circulated the proposal and received the support to begin the work. With this project, Berners-Lee and his team at CERN laid the foundation for the future development of the Web as a distributed hypertext system. They introduced their server and browser, the protocol used for communication between clients and the server, the HyperText Transfer Protocol (HTTP), the HyperText Markup Language (HTML) used for authoring Web documents, and the Universal Resource Locator (URL). And so it began. 1.2 A Brief History of the Web and the Internet 3 Mosaic and Netscape Browsers: The next significant event in the development of the Web was the arrival of Mosaic. In February of 1993, Marc Andreesen from the University of Illinois’ NCSA (National Center for Supercomputing Applications) and his team released the first "Mosaic for X" graphical Web browser for UNIX. A few months later, different versions of Mosaic were released for Macintosh and Windows operating systems. This was an important event. For the first time, a Web client, with a consistent and simple point-and-click graphical user interface, was implemented for the three most popular operating systems available at the time. It soon made big splashes outside the academic circle where it had begun. In mid-1994, Silicon Graphics founder Jim Clark collaborated with Marc Andreessen, and they founded the company Mosaic Communications (later renamed as Netscape Communications). Within a few months, the Netscape browser was released to the public, which started the explosive growth of the Web. The Internet Explorer from Microsoft entered the market in August, 1995 and began to challenge Netscape. The creation of the World Wide Web by Tim Berners-Lee followed by the release of the Mosaic browser are often regarded as the two most significant contributing factors to the success and popularity of the Web. Internet: The Web would not be possible without the Internet, which provides the communication network for the Web to function. The Internet started with the computer network ARPANET in the Cold War era. It was produced as the result of a project in the United States aiming at maintaining control over its missiles and bombers after a nuclear attack. It was supported by Advanced Research Projects Agency (ARPA), which was part of the Department of Defense in the United States. The first ARPANET connections were made in 1969, and in 1972, it was demonstrated at the First International Conference on Computers and Communication, held in Washington D.C. At the conference, ARPA scientists linked computers together from 40 different locations. In 1973, Vinton Cerf and Bob Kahn started to develop the protocol later to be called TCP/IP (Transmission Control Protocol/Internet Protocol). In the next year, they published the paper “Transmission Control Protocol”, which marked the beginning of TCP/IP. This new protocol allowed diverse computer networks to interconnect and communicate with each other. In subsequent years, many networks were built, and many competing techniques and protocols were proposed and developed. However, ARPANET was still the backbone to the entire system. During the period, the network scene was chaotic. In 1982, the TCP/IP was finally adopted, and the Internet, which is a connected set of networks using the TCP/IP protocol, was born. 4 1 Introduction Search Engines: With information being shared worldwide, there was a need for individuals to find information in an orderly and efficient manner. Thus began the development of search engines. The search system Excite was introduced in 1993 by six Stanford University students. EINet Galaxy was established in 1994 as part of the MCC Research Consortium at the University of Texas. Jerry Yang and David Filo created Yahoo! in 1994, which started out as a listing of their favorite Web sites, and offered directory search. In subsequent years, many search systems emerged, e.g., Lycos, Inforseek, AltaVista, Inktomi, Ask Jeeves, Northernlight, etc. Google was launched in 1998 by Sergey Brin and Larry Page based on their research project at Stanford University. Microsoft started to commit to search in 2003, and launched the MSN search engine in spring 2005. It used search engines from others before. Yahoo! provided a general search capability in 2004 after it purchased Inktomi in 2003. W3C (The World Wide Web Consortium): W3C was formed in the December of 1994 by MIT and CERN as an international organization to lead the development of the Web. W3C's main objective was “to promote standards for the evolution of the Web and interoperability between WWW products by producing specifications and reference software.” The first International Conference on World Wide Web (WWW) was also held in 1994, which has been a yearly event ever since. From 1995 to 2001, the growth of the Web boomed. Investors saw commercial opportunities and became involved. Numerous businesses started on the Web, which led to irrational developments. Finally, the bubble burst in 2001. However, the development of the Web was not stopped, but has only become more rational since. 1.3 Web Data Mining The rapid growth of the Web in the last decade makes it the largest publicly accessible data source in the world. The Web has many unique characteristics, which make mining useful information and knowledge a fascinating and challenging task. Let us review some of these characteristics. 1. The amount of data/information on the Web is huge and still growing. The coverage of the information is also very wide and diverse. One can find information on almost anything on the Web. 2. Data of all types exist on the Web, e.g., structured tables, semistructured Web pages, unstructured texts, and multimedia files (images, audios, and videos). 1.3 Web Data Mining 5 3. Information on the Web is heterogeneous. Due to the diverse authorship of Web pages, multiple pages may present the same or similar information using completely different words and/or formats. This makes integration of information from multiple pages a challenging problem. 4. A significant amount of information on the Web is linked. Hyperlinks exist among Web pages within a site and across different sites. Within a site, hyperlinks serve as information organization mechanisms. Across different sites, hyperlinks represent implicit conveyance of authority to the target pages. That is, those pages that are linked (or pointed) to by many other pages are usually high quality pages or authoritative pages simply because many people trust them. 5. The information on the Web is noisy. The noise comes from two main sources. First, a typical Web page contains many pieces of information, e.g., the main content of the page, navigation links, advertisements, copyright notices, privacy policies, etc. For a particular application, only part of the information is useful. The rest is considered noise. To perform fine-grain Web information analysis and data mining, the noise should be removed. Second, due to the fact that the Web does not have quality control of information, i.e., one can write almost anything that one likes, a large amount of information on the Web is of low quality, erroneous, or even misleading. 6. The Web is also about services. Most commercial Web sites allow people to perform useful operations at their sites, e.g., to purchase products, to pay bills, and to fill in forms. 7. The Web is dynamic. Information on the Web changes constantly. Keeping up with the change and monitoring the change are important issues for many applications. 8. The Web is a virtual society. The Web is not only about data, information and services, but also about interactions among people, organizations and automated systems. One can communicate with people anywhere in the world easily and instantly, and also express one’s views on anything in Internet forums, blogs and review sites. All these characteristics present both challenges and opportunities for mining and discovery of information and knowledge from the Web. In this book, we only focus on mining textual data. For mining of images, videos and audios, please refer to [143, 441]. To explore information mining on the Web, it is necessary to know data mining, which has been applied in many Web mining tasks. However, Web mining is not entirely an application of data mining. Due to the richness and diversity of information and other Web specific characteristics discussed above, Web mining has developed many of its own algorithms. 6 1 Introduction 1.3.1 What is Data Mining? Data mining is also called knowledge discovery in databases (KDD). It is commonly defined as the process of discovering useful patterns or knowledge from data sources, e.g., databases, texts, images, the Web, etc. The patterns must be valid, potentially useful, and understandable. Data mining is a multi-disciplinary field involving machine learning, statistics, databases, artificial intelligence, information retrieval, and visualization. There are many data mining tasks. Some of the common ones are supervised learning (or classification), unsupervised learning (or clustering), association rule mining, and sequential pattern mining. We will study all of them in this book. A data mining application usually starts with an understanding of the application domain by data analysts (data miners), who then identify suitable data sources and the target data. With the data, data mining can be performed, which is usually carried out in three main steps: Pre-processing: The raw data is usually not suitable for mining due to various reasons. It may need to be cleaned in order to remove noises or abnormalities. The data may also be too large and/or involve many irrelevant attributes, which call for data reduction through sampling and attribute selection. Details about data pre-processing can be found in any standard data mining textbook. Data mining: The processed data is then fed to a data mining algorithm which will produce patterns or knowledge. Post-processing: In many applications, not all discovered patterns are useful. This step identifies those useful ones for applications. Various evaluation and visualization techniques are used to make the decision. The whole process (also called the data mining process) is almost always iterative. It usually takes many rounds to achieve final satisfactory results, which are then incorporated into real-world operational tasks. Traditional data mining uses structured data stored in relational tables, spread sheets, or flat files in the tabular form. With the growth of the Web and text documents, Web mining and text mining are becoming increasingly important and popular. Web mining is the focus of this book. 1.3.2 What is Web Mining? Web mining aims to discover useful information or knowledge from the Web hyperlink structure, page content, and usage data. Although Web mining uses many data mining techniques, as mentioned above it is not 1.3 Web Data Mining 7 purely an application of traditional data mining due to the heterogeneity and semi-structured or unstructured nature of the Web data. Many new mining tasks and algorithms were invented in the past decade. Based on the primary kinds of data used in the mining process, Web mining tasks can be categorized into three types: Web structure mining, Web content mining and Web usage mining. Web structure mining: Web structure mining discovers useful knowledge from hyperlinks (or links for short), which represent the structure of the Web. For example, from the links, we can discover important Web pages, which, incidentally, is a key technology used in search engines. We can also discover communities of users who share common interests. Traditional data mining does not perform such tasks because there is usually no link structure in a relational table. Web content mining: Web content mining extracts or mines useful information or knowledge from Web page contents. For example, we can automatically classify and cluster Web pages according to their topics. These tasks are similar to those in traditional data mining. However, we can also discover patterns in Web pages to extract useful data such as descriptions of products, postings of forums, etc, for many purposes. Furthermore, we can mine customer reviews and forum postings to discover consumer sentiments. These are not traditional data mining tasks. Web usage mining: Web usage mining refers to the discovery of user access patterns from Web usage logs, which record every click made by each user. Web usage mining applies many data mining algorithms. One of the key issues in Web usage mining is the pre-processing of clickstream data in usage logs in order to produce the right data for mining. In this book, we will study all these three types of mining. However, due to the richness and diversity of information on the Web, there are a large number of Web mining tasks. We will not be able to cover them all. We will only focus on some important tasks and their algorithms. The Web mining process is similar to the data mining process. The difference is usually in the data collection. In traditional data mining, the data is often already collected and stored in a data warehouse. For Web mining, data collection can be a substantial task, especially for Web structure and content mining, which involves crawling a large number of target Web pages. We will devote a whole chapter on crawling. Once the data is collected, we go through the same three-step process: data pre-processing, Web data mining and post-processing. However, the techniques used for each step can be quite different from those used in traditional data mining. 8 1 Introduction 1.4 Summary of Chapters This book consists of two main parts. The first part, which includes Chaps. 2–5, covers the major topics of data mining. The second part, which comprises the rest of the chapters, covers Web mining (including a chapter on Web search). In the Web mining part, Chaps. 7 and 8 are on Web structure mining, which are closely related to Web search (Chap. 6). Since it is difficult to draw a boundary between Web search and Web mining, Web search and mining are put together. Chaps 9–11 are on Web content mining, and Chap. 12 is on Web usage mining. Below we give a brief introduction to each chapter. Chapter 2 – Association Rules and Sequential Patterns: This chapter studies two important data mining models that have been used in many Web mining tasks, especially in Web usage and content mining. Association rule mining finds sets of data items that occur together frequently. Sequential pattern mining finds sets of data items that occur together frequently in some sequences. Clearly, they can be used to find regularities in the Web data. For example, in Web usage mining, association rule mining can be used to find users’ visit and purchase patterns, and sequential pattern mining can be used to find users’ navigation patterns. Chapter 3 – Supervised Learning: Supervised learning is perhaps the most frequently used mining/learning technique in both practical data mining and Web mining. It is also called classification, which aims to learn a classification function (called a classifier) from data that are labeled with pre-defined classes or categories. The resulting classifier is then applied to classify future data instances into these classes. Due to the fact that the data instances used for learning (called the training data) are labeled with pre-defined classes, the method is called supervised learning. Chapter 4 – Unsupervised Learning: In unsupervised learning, the data used for learning has no pre-defined classes. The learning algorithm has to find the hidden structures or regularities in the data. One of the key unsupervised learning techniques is clustering, which organizes data instances into groups or clusters according to their similarities (or differences). Clustering is widely used in Web mining. For example, we can cluster Web pages into groups, where each group may represent a particular topic. We can also cluster documents into a hierarchy of clusters, which may represent a topic hierarchy. Chapter 5 – Partially Supervised Learning: Supervised learning requires a large number of labeled data instances to learn an accurate classifier. Labeling, which is often done manually, is labor intensive and time 1.4 Summary of Chapters 9 consuming. To reduce the manual labeling effort, learning from labeled and unlabeled examples (or LU learning) was proposed to use a small set of labeled examples (data instances) and a large set of unlabeled examples for learning. This model is also called semi-supervised learning. Another learning model that we will study is called learning from positive and unlabeled examples (or PU learning), which is for two-class classification. However, there are no labeled negative examples for learning. This model is useful in many situations. For example, we have a set of Web mining papers and we want to identify other Web mining papers in a research paper repository which contains all kinds of papers. The set of Web mining papers can be treated as the positive data, and the papers in the research repository can be treated as the unlabeled data. Chapter 6 – Information Retrieval and Web Search: Search is probably the largest application on the Web. It has its root in information retrieval (or IR for short), which is a field of study that helps the user find needed information from a large collection of text documents. Given a query (e.g., a set of keywords), which expresses the user’s information need, an IR system finds a set of documents that is relevant to the query from its underlying collection. This is also how a Web search engine works. Web search brings IR to a new height. It applies some IR techniques, but also presents a host of interesting problems due to special characteristics of the Web data. First of all, Web pages are not the same as plain text documents because they are semi-structured and contain hyperlinks. Thus, new methods have been designed to produce better Web IR (or search) systems. Another major issue is efficiency. Document collections used in traditional IR systems are not large, but the number of pages on the Web is huge. For example, Google claimed that it indexed more than 8 billion pages when this book was written. Web users demand very fast responses. No matter how effective a retrieval algorithm is, if the retrieval cannot be done extremely efficiently, few people will use it. In the chapter, several other search related issues will also be discussed. Chapter 7 – Link Analysis: Hyperlinks are a special feature of the Web, which have been exploited for many purposes, especially for Web search. Google’s success is largely attributed to its hyperlink-based ranking algorithm called PageRank, which is originated from social network analysis. In this chapter, we will first introduce some main concepts of social network analysis and then describe two most well known Web link analysis algorithms, PageRank and HITS. In addition, we will also study several community finding algorithms. When Web pages link to one another, they form Web communities, which are groups of content creators that share 10 1 Introduction some common interests. Communities not only manifest in hyperlinks, but also in other contexts such as emails and Web page contents. Chapter 8 – Web Crawling: A Web crawler is a program that automatically traverses the Web’s hyperlink structure and downloads each linked page to a local storage. Crawling is often the first step of Web mining or in building a Web search engine. Although conceptually easy, building a practical crawler is by no means simple. Due to efficiency and many other concerns, it involves a great deal of engineering. There are two types of crawlers: universal crawlers and topic crawlers. A universal crawler downloads all pages irrespective of their contents, while a topic crawler downloads only pages of certain topics. The difficulty in topic crawling is how to recognize such pages. We will study several techniques for this purpose. Chapter 9 – Structured Data Extraction: Wrapper Generation: A large number of pages on the Web contain structured data, which are usually data records retrieved from underlying databases and displayed in Web pages following some fixed templates. Structured data often represent their host pages’ essential information, e.g., lists of products and services. Extracting such data allows one to provide value added services, e.g., comparative shopping, and meta-search. There are two main approaches to extraction. One is the supervised approach, which uses supervised learning to learn data extraction rules. The other is the unsupervised pattern discovery approach, which finds repeated patterns (hidden templates) in Web pages for data extraction. Chapter 10 – Information Integration: Due to diverse authorships of the Web, different Web sites typically use different words or terms to express the same or similar information. In order to make use of the data or information extracted from multiple sites to provide value added services, we need to semantically integrate the data/information from these sites in order to produce consistent and coherent databases. Intuitively, integration means to match columns in different data tables that contain the same type of information (e.g., product names) and to match data values that are semantically the same but expressed differently in different sites. Chapter 11 – Opinion Mining: Apart from structured data, the Web also contains a huge amount of unstructured text. Analyzing such text is also of great importance. It is perhaps even more important than extracting structured data because of the sheer volume of valuable information of almost any imaginable types contained in it. This chapter will only focus on mining people’s opinions or sentiments expressed in product reviews, forum discussions and blogs. The task is not only technically challenging, 1.5 How to Read this Book 11 but also very useful in practice because businesses and organizations always want to know consumer opinions on their products and services. Chapter 12 – Web Usage Mining: Web usage mining aims to study user clicks and their applications to e-commerce and business intelligence. The objective is to capture and model behavioral patterns and profiles of users who interact with a Web site. Such patterns can be used to better understand the behaviors of different user segments, to improve the organization and structure of the site, and to create personalized experiences for users by providing dynamic recommendations of products and services. 1.5 How to Read this Book This book is a textbook although two chapters are contributed by two other researchers. The contents of the two chapters have been carefully edited and integrated into the common framework of the whole book. The book is suitable for both graduate students and senior undergraduate students in the fields of computer science, information science, engineering, statistics, and social science. It can also be used as a reference by researchers and practitioners who are interested in or are working in the field of Web mining, data mining or text mining. As mentioned earlier, the book is divided into two parts. Part I (Chaps. 2–5) covers the major topics of data mining. Text classification and clustering are included in this part as well. Part II, which includes the rest of the chapters, covers Web mining (and search). In general, all chapters in Part II require some techniques in Part I. Within each part, the dependency is minimal except Chap. 5, which needs several techniques from Chap. 4. To Instructors: This book can be used as a class text for a one-semester course on Web data mining. In this case, there are two possibilities. If the students already have data mining or machine learning background, the chapters in Part I can be skipped. If the students do not have any data mining background, I recommend covering some selected sections from each chapter of Part I before going to Part II. The chapters in Part II can be covered in any sequence. You can also select a subset of the chapters according to your needs. The book may also be used as a class text for an introductory data mining course where Web mining concepts and techniques are introduced. In this case, I recommend first covering all the chapters in Part I and then selectively covering some chapters or sections from each chapter in Part II depending on needs. It is usually a good idea to cover some sections of 12 1 Introduction Chaps. 6 and 7 as search engines fascinate most students. I also recommend including one or two lectures on data pre-processing for data mining since the topic is important for practical data mining applications but is not covered in this book. You can find teaching materials on data preprocessing from most introductory data mining books. Supporting Materials: Updates to chapters and teaching materials, including lecture slides, data sets, implemented algorithms, and other resources, are available at http://www.springer.com/3-540-37881-2. Bibliographic Notes The W3C Web site (http://www.w3.org/) is the most authoritative resource site for information on Web developments, standards and guidelines. The history of the Web and hypertext, and Tim Berners-Lee’s original proposal can all be found there. Many other sites also contain information about the history of the Web, the Internet and search engines, e.g., http://www.elsop. com/wrc/h_web.htm, http://www.zeltser.com/web-history/, http://www.isoc. org/internet/history/, http://www.livinginternet.com, http://www.w3c.rl.ac.uk/ primers/history/origins.htm and http://searchenginewatch.com/. There are some earlier introductory texts on Web mining, e.g., those by Baldi et al. [33] and Chakrabarti [85]. There are also several application oriented books, e.g., those by Linoff and Berry [338], and Thuraisingham [515], and edited volumes by Djeraba et al. [143], Scime [480], and Zhong et al. [617]. On data mining, there are many textbooks, e.g., those by Duda et al. [155], Dunham [156], Han and Kamber [218], Hand et al. [221], Larose [305], Langley [302], Mitchell [385], Roiger and Geatz [467], Tan et al. [512], and Witten and Frank [549]. Application oriented books include those by Berry and Linoff [49], Pyle [450], Parr Rud [468], and Tang and MacLennan [514]. Several edited volumes exist as well, e.g., those by Fayyad et al. [174], Grossman et al. [208], and Wang et al. [533]. Latest research results on Web mining can be found in a large number of conferences and journals (too many to list) due to the interdisciplinary nature of the field. All the journals and conferences related to the Web technology, information retrieval, data mining, databases, artificial intelligence, and machine learning may contain Web mining related papers. 2 Association Rules and Sequential Patterns Association rules are an important class of regularities in data. Mining of association rules is a fundamental data mining task. It is perhaps the most important model invented and extensively studied by the database and data mining community. Its objective is to find all co-occurrence relationships, called associations, among data items. Since it was first introduced in 1993 by Agrawal et al. [9], it has attracted a great deal of attention. Many efficient algorithms, extensions and applications have been reported. The classic application of association rule mining is the market basket data analysis, which aims to discover how items purchased by customers in a supermarket (or a store) are associated. An example association rule is Cheese Beer [support = 10%, confidence = 80%]. The rule says that 10% customers buy Cheese and Beer together, and those who buy Cheese also buy Beer 80% of the time. Support and confidence are two measures of rule strength, which we will define later. This mining model is in fact very general and can be used in many applications. For example, in the context of the Web and text documents, it can be used to find word co-occurrence relationships and Web usage patterns as we will see in later chapters. Association rule mining, however, does not consider the sequence in which the items are purchased. Sequential pattern mining takes care of that. An example of a sequential pattern is “5% of customers buy bed first, then mattress and then pillows”. The items are not purchased at the same time, but one after another. Such patterns are useful in Web usage mining for analyzing clickstreams in server logs. They are also useful for finding language or linguistic patterns from natural language texts. 2.1 Basic Concepts of Association Rules The problem of mining association rules can be stated as follows: Let I = {i1, i2, …, im} be a set of items. Let T = (t1, t2, …, tn) be a set of transactions (the database), where each transaction ti is a set of items such that ti I. An association rule is an implication of the form, 14 2 Association Rules and Sequential Patterns X Y, where X I, Y I, and X Y= . X (or Y) is a set of items, called an itemset. Example 1: We want to analyze how the items sold in a supermarket are related to one another. I is the set of all items sold in the supermarket. A transaction is simply a set of items purchased in a basket by a customer. For example, a transaction may be: {Beef, Chicken, Cheese}, which means that a customer purchased three items in a basket, Beef, Chicken, and Cheese. An association rule may be: Beef, Chicken Cheese, where {Beef, Chicken} is X and {Cheese} is Y. For simplicity, brackets “{” and “}” are usually omitted in transactions and rules. A transaction ti T is said to contain an itemset X if X is a subset of ti (we also say that the itemset X covers ti). The support count of X in T (denoted by X.count) is the number of transactions in T that contain X. The strength of a rule is measured by its support and confidence. Support: The support of a rule, X Y, is the percentage of transactions in T that contains X Y, and can be seen as an estimate of the probability, Pr(X Y). The rule support thus determines how frequent the rule is applicable in the transaction set T. Let n be the number of transactions in T. The support of the rule X Y is computed as follows: support (X Y ).count . n (1) Support is a useful measure because if it is too low, the rule may just occur due to chance. Furthermore, in a business environment, a rule covering too few cases (or transactions) may not be useful because it does not make business sense to act on such a rule (not profitable). Confidence: The confidence of a rule, X Y, is the percentage of transactions in T that contain X also contain Y. It can be seen as an estimate of the conditional probability, Pr(Y | X). It is computed as follows: confidence (X Y ).count . X .count (2) Confidence thus determines the predictability of the rule. If the confidence of a rule is too low, one cannot reliably infer or predict Y from X. A rule with low predictability is of limited use. 2.1 Basic Concepts of Association Rules 15 Objective: Given a transaction set T, the problem of mining association rules is to discover all association rules in T that have support and confidence greater than or equal to the user-specified minimum support (denoted by minsup) and minimum confidence (denoted by minconf). The keyword here is “all”, i.e., association rule mining is complete. Previous methods for rule mining typically generate only a subset of rules based on various heuristics (see Chap. 3). Example 2: Figure 2.1 shows a set of seven transactions. Each transaction ti is a set of items purchased in a basket in a store by a customer. The set I is the set of all items sold in the store. t1: t2: t3: t4: t5: t6: t7: Beef, Chicken, Milk Beef, Cheese Cheese, Boots Beef, Chicken, Cheese Beef, Chicken, Clothes, Cheese, Milk Chicken, Clothes, Milk Chicken, Milk, Clothes Fig. 2.1. An example of a transaction set Given the user-specified minsup = 30% and minconf = 80%, the following association rule (sup is the support, and conf is the confidence) Chicken, Clothes Milk [sup = 3/7, conf = 3/3] is valid as its support is 42.84% (> 30%) and its confidence is 100% (> 80%). The rule below is also valid, whose consequent has two items: Clothes Milk, Chicken [sup = 3/7, conf = 3/3]. Clearly, more association rules can be discovered, as we will see later. We note that the data representation in the transaction form of Fig. 2.1 is a simplistic view of shopping baskets. For example, the quantity and price of each item are not considered in the model. We also note that a text document or even a sentence in a single document can be treated as a transaction without considering word sequence and the number of occurrences of each word. Hence, given a set of documents or a set of sentences, we can find word co-occurrence relations. A large number of association rule mining algorithms have been reported in the literature, which have different mining efficiencies. Their resulting sets of rules are, however, all the same based on the definition of association rules. That is, given a transaction data set T, a minimum support and a minimum confidence, the set of association rules existing in T is 16 2 Association Rules and Sequential Patterns uniquely determined. Any algorithm should find the same set of rules although their computational efficiencies and memory requirements may be different. The best known mining algorithm is the Apriori algorithm proposed in [11], which we study next. 2.2 Apriori Algorithm The Apriori algorithm works in two steps: 1. Generate all frequent itemsets: A frequent itemset is an itemset that has transaction support above minsup. 2. Generate all confident association rules from the frequent itemsets: A confident association rule is a rule with confidence above minconf. We call the number of items in an itemset its size, and an itemset of size k a k-itemset. Following Example 2 above, {Chicken, Clothes, Milk} is a frequent 3-itemset as its support is 3/7 (minsup = 30%). From the itemset, we can generate the following three association rules (minconf = 80%): Rule 1: Rule 2: Rule 3: Chicken, Clothes Milk Clothes, Milk Chicken Clothes Milk, Chicken [sup = 3/7, conf = 3/3] [sup = 3/7, conf = 3/3] [sup = 3/7, conf = 3/3]. Below, we discuss the two steps in turn. 2.2.1 Frequent Itemset Generation The Apriori algorithm relies on the apriori or downward closure property to efficiently generate all frequent itemsets. Downward Closure Property: If an itemset has minimum support, then every non-empty subset of this itemset also has minimum support. The idea is simple because if a transaction contains a set of items X, then it must contain any non-empty subset of X. This property and the minsup threshold prune a large number of itemsets that cannot be frequent. To ensure efficient itemset generation, the algorithm assumes that the items in I are sorted in lexicographic order (a total order). The order is used throughout the algorithm in each itemset. We use the notation {w[1], w[2], …, w[k]} to represent a k-itemset w consisting of items w[1], w[2], …, w[k], where w[1] < w[2] < … < w[k] according to the total order. The Apriori algorithm for frequent itemset generation, which is given in Fig. 2.2, is based on level-wise search. It generates all frequent itemsets by 2.2 Apriori Algorithm Algorithm Apriori(T) 1 C1 init-pass(T); 2 F1 {f | f C1, f.count/n minsup}; 3 for (k = 2; Fk 1 ; k++) do 4 Ck candidate-gen(Fk 1); 5 for each transaction t T do 6 for each candidate c Ck do 7 if c is contained in t then 8 c.count++; 9 endfor 10 endfor 11 Fk {c Ck | c.count/n minsup} 12 endfor 13 return F k Fk; 17 // the first pass over T // n is the no. of transactions in T // subsequent passes over T // scan the data once Fig. 2.2. The Apriori algorithm for generating frequent itemsets Function candidate-gen(Fk 1) ; // initialize the set of candidates 1 Ck // find all pairs of frequent itemsets 2 forall f1, f2 Fk 1 3 with f1 = {i1, … , ik 2, ik 1} // that differ only in the last item 4 and f2 = {i1, … , ik 2, i’k 1} // according to the lexicographic order 5 and ik 1 < i’k 1 do // join the two itemsets f1 and f2 6 c {i1, …, ik 1, i’k 1}; // add the new itemset c to the candidates 7 Ck Ck {c}; 8 for each (k 1)-subset s of c do 9 if (s Fk 1) then 10 delete c from Ck; // delete c from the candidates 11 endfor 12 endfor 13 return Ck; // return the generated candidates Fig. 2.3. The candidate-gen function making multiple passes over the data. In the first pass, it counts the supports of individual items (line 1) and determines whether each of them is frequent (line 2). F1 is the set of frequent 1-itemsets. In each subsequent pass k, there are three steps: 1. It starts with the seed set of itemsets Fk 1 found to be frequent in the (k 1)-th pass. It uses this seed set to generate candidate itemsets Ck (line 4), which are possible frequent itemsets. This is done using the candidate-gen() function. 2. The transaction database is then scanned and the actual support of each candidate itemset c in Ck is counted (lines 5–10). Note that we do not need to load the whole data into memory before processing. Instead, at 18 2 Association Rules and Sequential Patterns any time, only one transaction resides in memory. This is a very important feature of the algorithm. It makes the algorithm scalable to huge data sets, which cannot be loaded into memory. 3. At the end of the pass or scan, it determines which of the candidate itemsets are actually frequent (line 11). The final output of the algorithm is the set F of all frequent itemsets (line 13). The candidate-gen() function is discussed below. Candidate-gen function: The candidate generation function is given in Fig. 2.3. It consists of two steps, the join step and the pruning step. Join step (lines 2–6 in Fig. 2.3): This step joins two frequent (k 1)itemsets to produce a possible candidate c (line 6). The two frequent itemsets f1 and f2 have exactly the same items except the last one (lines 3–5). c is added to the set of candidates Ck (line 7). Pruning step (lines 8–11 in Fig. 2.3): A candidate c from the join step may not be a final candidate. This step determines whether all the k 1 subsets (there are k of them) of c are in Fk 1. If anyone of them is not in Fk 1, c cannot be frequent according to the downward closure property, and is thus deleted from Ck. The correctness of the candidate-gen() function is easy to show (see [11]). Here, we use an example to illustrate the working of the function. Example 3: Let the set of frequent itemsets at level 3 be F3 = {{1, 2, 3}, {1, 2, 4}, {1, 3, 4}, {1, 3, 5}, {2, 3, 4}}. For simplicity, we use numbers to represent items. The join step (which generates candidates for level 4) will produce two candidate itemsets, {1, 2, 3, 4} and {1, 3, 4, 5}. {1, 2, 3, 4} is generated by joining the first and the second itemsets in F3 as their first and second items are the same respectively. {1, 3, 4, 5} is generated by joining {1, 3, 4} and {1, 3, 5}. After the pruning step, we have only: C4 = {{1, 2, 3, 4}} because {1, 4, 5} is not in F3 and thus {1, 3, 4, 5} cannot be frequent. Example 4: Let us see a complete running example of the Apriori algorithm based on the transactions in Fig. 2.1. We use minsup = 30%. F1: {{Beef}:4, {Cheese}:4, {Chicken}:5, {Clothes}:3, {Milk}:4} Note: the number after each frequent itemset is the support count of the itemset, i.e., the number of transactions containing the itemset. A minimum support count of 3 is sufficient because the support of 3/7 is greater than 30%, where 7 is the total number of transactions. 2.2 Apriori Algorithm C2: {{Beef, Cheese}, {Beef, Chicken}, {Beef, Clothes}, {Beef, Milk}, {Cheese, Chicken}, {Cheese, Clothes}, {Cheese, Milk}, {Chicken, Clothes}, {Chicken, Milk}, {Clothes, Milk}} F2: {{Beef, Chicken}:3, {Beef, Cheese}:3, {Chicken, Clothes}:3, {Chicken, Milk}:4, {Clothes, Milk}:3} C3: 19 {{Chicken, Clothes, Milk}} Note: {Beef, Cheese, Chicken} is also produced in line 6 of Fig. 2.3. However, {Cheese, Chicken} is not in F2, and thus the itemset {Beef, Cheese, Chicken} is not included in C3. F3: {{Chicken, Clothes, Milk}:3}. Finally, some remarks about the Apriori algorithm are in order: Theoretically, this is an exponential algorithm. Let the number of items in I be m. The space of all itemsets is O(2m) because each item may or may not be in an itemset. However, the mining algorithm exploits the sparseness of the data and the high minimum support value to make the mining possible and efficient. The sparseness of the data in the context of market basket analysis means that the store sells a lot of items, but each shopper only purchases a few of them. The algorithm can scale up to large data sets as it does not load the entire data into the memory. It only scans the data K times, where K is the size of the largest itemset. In practice, K is often small (e.g., < 10). This scale-up property is very important in practice because many real-world data sets are so large that they cannot be loaded into the main memory. The algorithm is based on level-wise search. It has the flexibility to stop at any level. This is useful in practice because in many applications, long frequent itemsets or rules are not needed as they are hard to use. As mentioned earlier, once a transaction set T, a minsup and a minconf are given, the set of frequent itemsets that can be found in T is uniquely determined. Any algorithm should find the same set of frequent itemsets. This property about association rule mining does not hold for many other data mining tasks, e.g., classification or clustering, for which different algorithms may produce very different results. The main problem with association rule mining is that it often produces a huge number of itemsets (and rules), tens of thousands, or more, which makes it hard for the user to analyze them to find those useful ones. This is called the interestingness problem. Researchers have proposed several methods to tackle this problem (see Bibliographic Notes). An efficient implementation of the Apriori algorithm involves sophisticated data structures and programming techniques, which are beyond the 20 2 Association Rules and Sequential Patterns scope of this book. Apart from the Apriori algorithm, there is a large number of other algorithms, e.g., FP-growth [220] and many others. 2.2.2 Association Rule Generation In many applications, frequent itemsets are already useful and sufficient. Then, we do not need to generate association rules. In applications where rules are desired, we use frequent itemsets to generate all association rules. Compared with frequent itemset generation, rule generation is relatively simple. To generate rules for every frequent itemset f, we use all nonempty subsets of f. For each such subset , we output a rule of the form (f confidence ) f .count (f ).count , if minconf , (3) where f.count (or (f ).count) is the support count of f (or (f )). The support of the rule is f.count/n, where n is the number of transactions in the transaction set T. All the support counts needed for confidence computation are available because if f is frequent, then any of its non-empty subsets is also frequent and its support count has been recorded in the mining process. Thus, no data scan is needed in rule generation. This exhaustive rule generation strategy is, however, inefficient. To design an efficient algorithm, we observe that the support count of f in the above confidence computation does not change as changes. It follows that for a rule (f ) to hold, all rules of the form (f sub) sub must also hold, where sub is a non-empty subset of , because the support count of (f ). sub) must be less than or equal to the support count of (f For example, given an itemset {A, B, C, D}, if the rule (A, B C, D) holds, then the rules (A, B, C D) and (A, B, D C) must also hold. Thus, for a given frequent itemset f, if a rule with consequent holds, then so do rules with consequents that are subsets of . This is similar to the downward closure property that, if an itemset is frequent, then so are all its subsets. Therefore, from the frequent itemset f, we first generate all rules with one item in the consequent. We then use the consequents of these rules and the function candidate-gen() (Fig. 2.3) to generate all possible consequents with two items that can appear in a rule, and so on. An algorithm using this idea is given in Fig. 2.4. Note that all 1-item consequent rules (rules with one item in the consequent) are first generated in line 2 of the function genRules(). The confidence is computed using (3). 2.2 Apriori Algorithm 21 Algorithm genRules(F) // F is the set of all frequent itemsets 1 for each frequent k-itemset fk in F, k 2 do 2 output every 1-item consequent rule of fk with confidence minconf and support fk.count / n // n is the total number of transactions in T 3 H1 {consequents of all 1-item consequent rules derived from fk above}; 4 ap-genRules(fk, H1); 5 endfor Procedure ap-genRules(fk, Hm) // Hm is the set of m-item consequents ) then 1 if (k > m + 1) AND (Hm 2 Hm+1 candidate-gen(Hm); 3 for each hm+1 in Hm+1 do 4 conf fk.count / (fk hm+1).count; 5 if (conf minconf) then 6 output the rule (fk hm+1) hm+1 with confidence = conf and support = fk.count / n; // n is the total number of transactions in T 7 else 8 delete hm+1 from Hm+1; 9 endfor 10 ap-genRules(fk, Hm+1); 11 endif Fig. 2.4. The association rule generation algorithm Example 5: We again use transactions in Fig. 2.1, minsup = 30% and minconf = 80%. The frequent itemsets are as follows (see Example 4): F1: F2: F3: {{Beef}:4, {Cheese}:4, {Chicken}:5, {Clothes}:3, {Milk}:4} {{Beef, Cheese}:3, {Beef, Chicken}:3, {Chicken, Clothes}:3, {Chicken, Milk}:4, {Clothes, Milk}:3} {{Chicken, Clothes, Milk}:3}. We use only the itemset in F3 to generate rules (generating rules from each itemset in F2 can be done in the same way). The itemset in F3 generates the following possible 1-item consequent rules: Rule 1: Rule 2: Rule 3: Chicken, Clothes Milk [sup = 3/7, conf = 3/3] Chicken, Milk Clothes [sup = 3/7, conf = 3/4] Clothes, Milk Chicken [sup = 3/7, conf = 3/3]. Due to the minconf requirement, only Rule 1 and Rule 3 are output in line 2 of the algorithm genRules(). Thus, H1 = {{Chicken}, {Milk}}. The function ap-genRules() is then called. Line 2 of ap-genRules() produces H2 = {{Chicken, Milk}}. The following rule is then generated: Rule 4: Clothes Milk, Chicken [sup = 3/7, conf = 3/3]. 22 2 Association Rules and Sequential Patterns Thus, three association rules are generated from the frequent itemset {Chicken, Clothes, Milk} in F3, namely Rule 1, Rule 3 and Rule 4. 2.3 Data Formats for Association Rule Mining So far, we have used only transaction data for mining association rules. Market basket data sets are naturally of this format. Text documents can be seen as transaction data as well. Each document is a transaction, and each distinctive word is an item. Duplicate words are removed. However, mining can also be performed on relational tables. We just need to convert a table data set to a transaction data set, which is fairly straightforward if each attribute in the table takes categorical values. We simply change each value to an attribute–value pair. Example 6: The table data in Fig. 2.5(A) can be converted to the transaction data in Fig. 2.5(B). Each attribute–value pair is considered an item. Using only values is not sufficient in the transaction form because different attributes may have the same values. For example, without including attribute names, value a’s for Attribute1 and Attribute2 are not distinguishable. After the conversion, Fig. 2.5(B) can be used in mining. If an attribute takes numerical values, it becomes complex. We need to first discretize its value range into intervals, and treat each interval as a categorical value. For example, an attribute’s value range is from 1–100. We may want to divide it into 5 equal-sized intervals, 1–20, 21–40, 41–60, 61–80, and 81–100. Each interval is then treated as a categorical value. Discretization can be done manually based on expert knowledge or automatically. There are several existing algorithms [151, 501]. A point to note is that for a table data set, the join step of the candidate generation function (Fig. 2.3) needs to be slightly modified in order to ensure that it does not join two itemsets to produce a candidate itemset containing two items from the same attribute. Clearly, we can also convert a transaction data set to a table data set using a binary representation and treating each item in I as an attribute. If a transaction contains an item, its attribute value is 1, and 0 otherwise. 2.4 Mining with Multiple Minimum Supports The key element that makes association rule mining practical is the minsup threshold. It is used to prune the search space and to limit the number of frequent itemsets and rules generated. However, using only a single min- 2.4 Mining with Multiple Minimum Supports Attribute1 a b Attribute2 a n 23 Atribute3 x y (A) Table data t1: t2: (Attribute1, a), (Attribute2, a), (Attribute3, x) (Attribute1, b), (Attribute2, n), (Attribute3, y) (B) Transaction data Fig. 2.5. From a table data set to a transaction data set sup implicitly assumes that all items in the data are of the same nature and/or have similar frequencies in the database. This is often not the case in real-life applications. In many applications, some items appear very frequently in the data, while some other items rarely appear. If the frequencies of items vary a great deal, we will encounter two problems [344]: 1. If the minsup is set too high, we will not find rules that involve infrequent items or rare items in the data. 2. In order to find rules that involve both frequent and rare items, we have to set the minsup very low. However, this may cause combinatorial explosion and make mining impossible because those frequent items will be associated with one another in all possible ways. Let us use an example to illustrate the above problem with a very low minsup, which will actually introduce another problem. Example 7: In a supermarket transaction data set, in order to find rules involving those infrequently purchased items such as FoodProcessor and CookingPan (they generate more profits per item), we need to set the minsup very low. Let us use only frequent itemsets in this example as they are generated first and rules are produced from them. They are also the source of all the problems. Now assume we set a very low minsup of 0.005%. We find the following meaningful frequent itemset: {FoodProcessor, CookingPan} [sup = 0.006%]. However, this low minsup may also cause the following two meaningless itemsets being discovered: f1: {Bread, Cheese, Egg, Bagel, Milk, Sugar, Butter} [sup = 0.007%], f2: {Bread, Egg, Milk, CookingPan} [sup = 0.006%]. Knowing that 0.007% of the customers buy the seven items in f1 together is useless because all these items are so frequently purchased in a supermar- 24 2 Association Rules and Sequential Patterns ket. Worst still, they will almost certainly cause combinatorial explosion! For itemsets involving such items to be useful, their supports have to be much higher. Similarly, knowing that 0.006% of the customers buy the four items in f2 together is also meaningless because Bread, Egg and Milk are purchased on almost every grocery shopping trip. This dilemma is called the rare item problem. Using a single minsup for the whole data set is inadequate because it cannot capture the inherent natures and/or frequency differences of items in the database. By the natures of items we mean that some items, by nature, appear more frequently than others. For example, in a supermarket, people buy FoodProcessor and CookingPan much less frequently than Bread and Milk. The situation is the same for online stores. In general, those durable and/or expensive goods are bought less often, but each of them generates more profit. It is thus important to capture rules involving less frequent items. However, we must do so without allowing frequent items to produce too many meaningless rules with very low supports and cause combinatorial explosion [344]. One common solution to this problem is to partition the data into several smaller blocks (subsets), each of which contains only items of similar frequencies. Mining is then done separately for each block using a different minsup. This approach is, however, not satisfactory because itemsets or rules that involve items across different blocks will not be found. A better solution is to allow the user to specify multiple minimum supports, i.e., to specify a different minimum item support (MIS) to each item. Thus, different itemsets need to satisfy different minimum supports depending on what items are in the itemsets. This model thus enables us to achieve our objective of finding itemsets involving rare items without causing frequent items to generate too many meaningless itemsets. This method helps solve the problem of f1. To deal with the problem of f2, we prevent itemsets that contain both very frequent items and very rare items from being generated. A constraint will be introduced to realize this. An interesting by-product of this extended model is that it enables the user to easily instruct the algorithm to generate only itemsets that contain certain items but not itemsets that contain only the other items. This can be done by setting the MIS values to more than 100% (e.g., 101%) for these other items. This capability is very useful in practice because in many applications the user is only interested in certain types of itemsets or rules. 2.4.1 Extended Model To allow multiple minimum supports, the original model in Sect. 2.1 needs to be extended. In the extended model, the minimum support of a rule is 2.4 Mining with Multiple Minimum Supports 25 expressed in terms of minimum item supports (MIS) of the items that appear in the rule. That is, each item in the data can have a MIS value specified by the user. By providing different MIS values for different items, the user effectively expresses different support requirements for different rules. It seems that specifying a MIS value for each item is a difficult task. This is not so as we will see at the end of Sect. 2.4.2. Let MIS(i) be the MIS value of item i. The minimum support of a rule R is the lowest MIS value among the items in the rule. That is, a rule R, i1, i2, …, ik ik+1, …, ir, satisfies its minimum support if the rule’s actual support in the data is greater than or equal to: min(MIS(i1), MIS(i2), …, MIS(ir)). Minimum item supports thus enable us to achieve the goal of having higher minimum supports for rules that involve only frequent items, and having lower minimum supports for rules that involve less frequent items. Example 8: Consider the set of items in a data set, {Bread, Shoes, Clothes}. The user-specified MIS values are as follows: MIS(Bread) = 2% MIS(Clothes) = 0.2% MIS(Shoes) = 0.1%. The following rule doesn’t satisfy its minimum support: Clothes Bread [sup = 0.15%, conf = 70%]. This is so because min(MIS(Bread), MIS(Clothes)) = 0.2%. The following rule satisfies its minimum support: Clothes Shoes [sup = 0.15%, conf = 70%]. because min(MIS(Clothes), MIS(Shoes)) = 0.1%. As we explained earlier, the downward closure property holds the key to pruning in the Apriori algorithm. However, in the new model, if we use the Apriori algorithm to find all frequent itemsets, the downward closure property no longer holds. Example 9: Consider the four items 1, 2, 3 and 4 in a data set. Their minimum item supports are: MIS(1) = 10% MIS(2) = 20% MIS(3) = 5% MIS(4) = 6%. If we find that itemset {1, 2} has a support of 9% at level 2, then it does not satisfy either MIS(1) or MIS(2). Using the Apriori algorithm, this itemset is discarded since it is not frequent. Then, the potentially frequent itemsets {1, 2, 3} and {1, 2, 4} will not be generated for level 3. Clearly, itemsets {1, 26 2 Association Rules and Sequential Patterns 2, 3} and {1, 2, 4} may be frequent because MIS(3) is only 5% and MIS(4) is 6%. It is thus wrong to discard {1, 2}. However, if we do not discard {1, 2}, the downward closure property is lost. Below, we present an algorithm to solve this problem. The essential idea is to sort the items according to their MIS values in ascending order to avoid the problem. Note that MIS values prevent low support itemsets involving only frequent items from being generated because their individual MIS values are all high. To prevent very frequent items and very rare items from appearing in the same itemset, we introduce the support difference constraint. Let sup(i) be the actual support of item i in the data. For each itemset s, the support difference constraint is as follows: maxi s{sup(i)} mini s{sup(i)} , where 0 1 is the user-specified maximum support difference, and it is the same for all itemsets. The constraint basically limits the difference between the largest and the smallest actual supports of items in itemset s to . This constraint can reduce the number of itemsets generated dramatically, and it does not affect the downward closure property. 2.4.2 Mining Algorithm The new algorithm generalizes the Apriori algorithm for finding frequent itemsets. We call the algorithm, MS-Apriori. When there is only one MIS value (for all items), it reduces to the Apriori algorithm. Like Apriori, MS-Apriori is also based on level-wise search. It generates all frequent itemsets by making multiple passes over the data. However, there is an exception in the second pass as we will see later. The key operation in the new algorithm is the sorting of the items in I in ascending order of their MIS values. This order is fixed and used in all subsequent operations of the algorithm. The items in each itemset follow this order. For example, in Example 9 of the four items 1, 2, 3 and 4 and their given MIS values, the items are sorted as follows: 3, 4, 1, 2. This order helps solve the problem identified above. Let Fk denote the set of frequent k-itemsets. Each itemset w is of the following form, {w[1], w[2], …, w[k]}, which consists of items, w[1], w[2], …, w[k], where MIS(w[1]) MIS(w[2]) … MIS(w[k]). The algorithm MS-Apriori is given in Fig. 2.6. Line 1 performs the sorting on I according to the MIS value of each item (stored in MS). Line 2 makes the first pass over the data using the function init-pass(), which takes two arguments, the 2.4 Mining with Multiple Minimum Supports 27 Algorithm MS-Apriori(T, MS, ) // MS stores all MIS values 1M sort(I, MS); // according to MIS(i)’s stored in MS 2L init-pass(M, T); // make the first pass over T 3 F1 {{l} | l L, l.count/n MIS(l)}; // n is the size of T ; k++) do 4 for (k = 2; Fk 1 5 if k = 2 then 6 Ck level2-candidate-gen(L, ) // k = 2 MScandidate-gen(Fk 1, ) 7 else Ck 8 endif; 9 for each transaction t T do 10 for each candidate c Ck do 11 if c is contained in t then // c is a subset of t 12 c.count++ 13 if c – {c[1]} is contained in t then // c without the first item 14 (c – {c[1]}).count++ 15 endfor 16 endfor 17 Fk {c Ck | c.count/n MIS(c[1])} 18 endfor 19 return F k Fk; Fig. 2.6. The MS-Apriori algorithm data set T and the sorted items M, to produce the seeds L for generating candidate itemsets of length 2, i.e., C2. init-pass() has two steps: 1. It first scans the data once to record the support count of each item. 2. It then follows the sorted order to find the first item i in M that meets MIS(i). i is inserted into L. For each subsequent item j in M after i, if j.count/n MIS(i), then j is also inserted into L, where j.count is the support count of j, and n is the total number of transactions in T. Frequent 1-itemsets (F1) are obtained from L (line 3). It is easy to show that all frequent 1-itemsets are in F1. Example 10: Let us follow Example 9 and the given MIS values for the four items. Assume our data set has 100 transactions (not limited to the four items). The first pass over the data gives us the following support counts: {3}.count = 6, {4}.count = 3, {1}.count = 9 and {2}.count = 25. Then, L = {3, 1, 2}, and F1 = {{3}, {2}}. Item 4 is not in L because 4.count/n < MIS(3) (= 5%), and {1} is not in F1 because 1.count / n < MIS(1) (= 10%). For each subsequent pass (or data scan), say pass k, the algorithm performs three operations. 28 2 Association Rules and Sequential Patterns 1. The frequent itemsets in Fk 1 found in the (k–1)th pass are used to generate the candidates Ck using the MScandidate-gen() function (line 7). However, there is a special case, i.e., when k = 2 (line 6), for which the candidate generation function is different, i.e., level2-candidate-gen(). 2. It then scans the data and updates various support counts of the candidates in Ck (line 9–16). For each candidate c, we need to update its support count (lines 11–12) and also the support count of c without the first item (lines 13–14), i.e., c – {c[1]}, which is used in rule generation and will be discussed in Sect. 2.4.3. If rule generation is not required, lines 13 and 14 can be deleted. 3. The frequent itemsets (Fk) for the pass are identified in line 17. We present candidate generation functions level2-candidate-gen() and MScandidate-gen() below. Level2-candidate-gen function: It takes an argument L, and returns a superset of the set of all frequent 2-itemsets. The algorithm is given in Fig. 2.7. Note that in line 5, we use |sup(h) sup(l)| because sup(l) may not be lower than sup(h), although MIS(l) MIS(h). Example 11: Let us continue with Example 10. We set the MIS values of the four items are (in Example 9): MIS(1) = 10% MIS(3) = 5% = 10%. Recall MIS(2) = 20% MIS(4) = 6%. The level2-candidate-gen() function in Fig. 2.7 produces C2 = {{3, 1}}. {1, 2} is not a candidate because the support count of item 1 is only 9 (or 9%), less than MIS(1) (= 10%). Hence, {1, 2} cannot be frequent. {3, 2} is not a candidate because sup(3) = 6% and sup(2) = 25% and their difference is greater than = 10% Note that we must use L rather than F1 because F1 does not contain those items that may satisfy the MIS of an earlier item (in the sorted order) but not the MIS of itself, e.g., item 1 in the above example. Using L, the problem discussed in Sect. 2.4.1 is solved for C2. MScandidate-gen function: The algorithm is given in Fig. 2.8, which is similar to the candidate-gen function in the Apriori algorithm. It also has two steps, the join step and the pruning step. The join step (lines 2–6) is the same as that in the candidate-gen() function. The pruning step (lines 8– 12) is, however, different. For each (k-1)-subset s of c, if s is not in Fk 1, c can be deleted from Ck. However, there is an exception, which is when s does not include c[1] 2.4 Mining with Multiple Minimum Supports 29 Function level2-candidate-gen(L, ) ; // initialize the set of candidates 1 C2 2 for each item l in L in the same order do 3 if l.count/n MIS(l) then 4 for each item h in L that is after l do 5 if h.count/n MIS(l) and |sup(h) sup(l)| then 6 C2 C2 {{l, h}}; // insert the candidate {l, h} into C2 Fig. 2.7. The level2-candidate-gen function Function MScandidate-gen(Fk 1, ) ; // initialize the set of candidates 1 Ck // find all pairs of frequent itemsets 2 forall f1, f2 Fk // that differ only in the last item 3 with f1 = {i1, … , ik 2, ik 1} 4 and f2 = {i1, … , ik 2, i’k 1} do 5 and ik-1 < i’k 1 and |sup(ik-1) sup(i’k 1)| 6 c {i1, …, ik 1, i’k 1}; // join the two itemsets f1 and f2 // insert the candidate itemset c into Ck 7 Ck Ck {c}; 8 for each (k 1)-subset s of c do 9 if (c[1] s) or (MIS(c[2]) = MIS(c[1])) then 10 if (s Fk 1) then 11 delete c from Ck; // delete c from the set of candidates 12 endfor 13 endfor 14 return Ck; // return the generated candidates Fig. 2.8. The MScandidate-gen function (there is only one such s). That is, the first item of c, which has the lowest MIS value, is not in s. Even if s is not in Fk 1, we cannot delete c because we cannot be sure that s does not satisfy MIS(c[1]), although we know that it does not satisfy MIS(c[2]), unless MIS(c[2]) = MIS(c[1]) (line 9). Example 12: Let F3 = {{1, 2, 3}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5}, {1, 4, 5}, {1, 4, 6}, {2, 3, 5}}. Items in each itemset are in the sorted order. The join step produces (we ignore the support difference constraint here) {1, 2, 3, 5}, {1, 3, 4, 5} and {1, 4, 5, 6}. The pruning step deletes {1, 4, 5, 6} because {1, 5, 6} is not in F3. We are then left with C4 = {{1, 2, 3, 5}, {1, 3, 4, 5}}. {1, 3, 4, 5} is not deleted although {3, 4, 5} is not in F3 because the minimum support of {3, 4, 5} is MIS(3), which may be higher than MIS(1). Although {3, 4, 5} does not satisfy MIS(3), we cannot be sure that it does not satisfy MIS(1). However, if MIS(3) = MIS(1), then {1, 3, 4, 5} can also be deleted. 30 2 Association Rules and Sequential Patterns The problem discussed in Sect. 2.4.1 is solved for Ck (k > 2) because, due to the sorting, we do not need to extend a frequent (k 1)-itemset with any item that has a lower MIS value. Let us see a complete example. Example 13: Given the following seven transactions, Beef, Bread Bread, Clothes Bread, Clothes, Milk Cheese, Boots Beef, Bread, Cheese, Shoes Beef, Bread, Cheese, Milk Bread, Milk, Clothes and MIS(Milk) = 50%, MIS(Bread) = 70%, and 25% for all other items. Again, the support difference constraint is not used. The following frequent itemsets are produced: F1 = {{Beef}, {Cheese}, {Clothes}, {Bread}} F2 = {{Beef, Cheese}, {Beef, Bread}, {Cheese, Bread} {Clothes, Bread}, {Clothes, Milk}} F3 = {{Beef, Cheese, Bread}, {Clothes, Milk, Bread}}. To conclude this sub-section, let us further discuss two important issues: 1. Specify MIS values for items: This is usually done in two ways: Assign a MIS value to each item according to its actual support/frequency in the data set T. For example, if the actual support of item i in T is sup(i), then the MIS value for i may be computed with sup(i), where is a parameter (0 1) and is the same for all items in T. Group items into clusters (or blocks). Items in each cluster have similar frequencies. All items in the same cluster are given the same MIS value. We should note that in the extended model frequent itemsets involving items from different clusters will be found. 2. Generate itemsets that must contain certain items: As mentioned earlier, the extended model enables the user to instruct the algorithm to generate itemsets that must contain certain items, or not to generate any itemsets consisting of only the other items. Let us see an example. Example 14: Given the data set in Example 13, if we want to generate frequent itemsets that must contain at least one item in {Boots, Bread, Cheese, Milk, Shoes}, or not to generate itemsets involving only Beef and/or Clothes, we can simply set MIS(Beef) = 101%, and MIS(Clothes) = 101% 2.4 Mining with Multiple Minimum Supports 31 Then the algorithm will not generate the itemsets, {Beef}, {Clothes} and {Beef, Clothes}. However, it will still generate such frequent itemsets as {Cheese, Beef} and {Cheese, Bread, Beef}. In many applications, this feature comes quite handy because the user is often only interested in certain types of itemsets or rules. 2.4.3 Rule Generation Association rules are generated using frequent itemsets. In the case of a single minsup, if f is a frequent itemset and fsub is a subset of f, then fsub must also be a frequent itemset. All their support counts are computed and recorded by the Apriori algorithm. Then, the confidence of each possible rule can be easily calculated without seeing the data again. However, in the case of MS-Apriori, if we only record the support count of each frequent itemset, it is not sufficient. Let us see why. Example 15: Recall in Example 8, we have MIS(Bread) = 2% MIS(Clothes) = 0.2% MIS(Shoes) = 0.1%. If the actual support for the itemset {Clothes, Bread} is 0.15%, and for the itemset {Shoes, Clothes, Bread} is 0.12%, according to MS-Apriori, {Clothes, Bread} is not a frequent itemset since its support is less than MIS(Clothes). However, {Shoes, Clothes, Bread} is a frequent itemset as its actual support is greater than min(MIS(Shoes), MIS(Clothes), MIS(Bread)) = MIS(Shoes)). We now have a problem in computing the confidence of the rule, Clothes, Bread Shoes because the itemset {Clothes, Bread} is not a frequent itemset and thus its support count is not recorded. In fact, we may not be able to compute the confidences of the following rules either: Clothes Shoes, Bread Bread Shoes, Clothes because {Clothes} and {Bread} may not be frequent. Lemma: The above problem may occur only when the item that has the lowest MIS value in the itemset is in the consequent of the rule (which may have multiple items). We call this problem the head-item problem. Proof by contradiction: Let f be a frequent itemset, and a f be the item with the lowest MIS value in f (a is called the head item). Thus, f uses 32 2 Association Rules and Sequential Patterns MIS(a) as its minsup. We want to form a rule, X Y, where X, Y f, X Y = f and X Y = . Our examples above already show that the head-item problem may occur when a Y. Now assume that the problem can also occur when a X. Since a X and X f, a must have the lowest MIS value in X and X must be a frequent itemset, which is ensured by the MSApriori algorithm. Hence, the support count of X is recorded. Since f is a frequent itemset and its support count is also recorded, then we can compute the confidence of X Y. This contradicts our assumption. The lemma indicates that we need to record the support count of f – {a}. This is achieved by lines 13–14 in MS-Apriori (Fig. 2.6). All problems in Example 15 are solved. A similar rule generation function as genRules() in Apriori can be designed to generate rules with multiple minimum supports. 2.5 Mining Class Association Rules The mining models studied so far do not use any targets. That is, any item can appear as a consequent or condition of a rule. However, in some applications, the user is interested in only rules with some fixed target items on the right-hand side. For example, the user has a collection of text documents from some topics (target items), and he/she wants to know what words are correlated with each topic. In [352], a data mining system based entirely on such rules (called class association rules) is reported, which is in production use in Motorola for many different applications. In the Web environment, class association rules are also useful because many types of Web data are in the form of transactions, e.g., search queries issued by users, and pages clicked by visitors. There are often target items as well, e.g., advertisements. Web sites want to know how user activities are associated with advertisements that they may like to view. This touches the issue of classification or prediction, which we will study in the next chapter. 2.5.1 Problem Definition Let T be a transaction data set consisting of n transactions. Each transaction is labeled with a class y. Let I be the set of all items in T, Y be the set of all class labels (or target items) and I Y = . A class association rule (CAR) is an implication of the form X y, where X I, and y Y. The definitions of support and confidence are the same as those for nor- 2.5 Mining Class Association Rules 33 mal association rules. In general, a class association rule is different from a normal association rule in two ways: 1. The consequent of a CAR has only a single item, while the consequent of a normal association rule can have any number of items. 2. The consequent y of a CAR can only be from the class label set Y, i.e., y Y. No item from I can appear as the consequent, and no class label can appear as a rule condition. In contrast, a normal association rule can have any item as a condition or a consequent. Objective: The problem of mining CARs is to generate the complete set of CARs that satisfies the user-specified minimum support (minsup) and minimum confidence (minconf) constraints. Example 16: Figure 2.9 shows a data set which has seven text documents. Each document is a transaction and consists of a set of keywords. Each transaction is also labeled with a topic class (education or sport). I = {Student, Teach, School, City, Game, Baseball, Basketball, Team, Coach, Player, Spectator} Y = {Education, Sport}. Transactions doc 1: doc 2: doc 3: doc 4: doc 5: doc 6: doc 7: Class Student, Teach, School Student, School Teach, School, City, Game Baseball, Basketball Basketball, Player, Spectator Baseball, Coach, Game, Team Basketball, Team, City, Game : Education : Education : Education : Sport : Sport : Sport : Sport Fig. 2.9. An example of a data set for mining class association rules Let minsup = 20% and minconf = 60%. The following are two examples of class association rules: Student, School Game Sport Education [sup= 2/7, conf = 2/2] [sup= 2/7, conf = 2/3]. A question that one may ask is: can we mine the data by simply using the Apriori algorithm and then perform a post-processing of the resulting rules to select only those class association rules? In principle, the answer is yes because CARs are a special type of association rules. However, in practice this is often difficult or even impossible because of combinatorial explosion, i.e., the number of rules generated in this way can be huge. 34 2 Association Rules and Sequential Patterns 2.5.2 Mining Algorithm Unlike normal association rules, CARs can be mined directly in a single step. The key operation is to find all ruleitems that have support above minsup. A ruleitem is of the form: (condset, y), where condset I is a set of items, and y Y is a class label. The support count of a condset (called condsupCount) is the number of transactions in T that contain the condset. The support count of a ruleitem (called rulesupCount) is the number of transactions in T that contain the condset and are labeled with class y. Each ruleitem basically represents a rule: condset y, whose support is (rulesupCount / n), where n is the total number of transactions in T, and whose confidence is (rulesupCount / condsupCount). Ruleitems that satisfy the minsup are called frequent ruleitems, while the rest are called infrequent ruleitems. For example, ({Student, School}, Education) is a ruleitem in T of Fig. 2.9. The support count of the condset {Student, School} is 2, and the support count of the ruleitem is also 2. Then the support of the ruleitem is 2/7 (= 28.6%), and the confidence of the ruleitem is 100%. If minsup = 10%, then the ruleitem satisfies the minsup threshold. We say that it is frequent. If minconf = 80%, then the ruleitem satisfies the minconf threshold. We say that the ruleitem is confident. We thus have the class association rule: Student, School Education [sup= 2/7, conf = 2/2]. The rule generation algorithm, called CAR-Apriori, is given in Fig. 2.10, which is based on the Apriori algorithm. Like the Apriori algorithm, CAR-Apriori generates all the frequent ruleitems by making multiple passes over the data. In the first pass, it computes the support count of each 1-ruleitem (containing only one item in its condset) (line 1). The set of all 1-candidate ruleitems considered is: C1 = {({i}, y) | i I, and y Y}, which basically associates each item in I (or in the transaction data set T) with every class label. Line 2 determines whether the candidate 1ruleitems are frequent. From frequent 1-ruleitems, we generate 1-condition CARs (rules with only one condition) (line 3). In a subsequent pass, say k, it starts with the seed set of (k 1)-ruleitems found to be frequent in the (k 1)-th pass, and uses this seed set to generate new possibly frequent kruleitems, called candidate k-ruleitems (Ck in line 5). The actual support 2.5 Mining Class Association Rules 35 Algorithm CAR-Apriori(T) // the first pass over T 1 C1 init-pass(T); 2 F1 {f | f C1, f. rulesupCount / n minsup}; 3 CAR1 {f | f F1, f.rulesupCount / f.condsupCount minconf}; ; k++) do 4 for (k = 2; Fk 1 5 Ck CARcandidate-gen(Fk 1); 6 for each transaction t T do 7 for each candidate c Ck do 8 if c.condset is contained in t then // c is a subset of t 9 c.condsupCount++; 10 if t.class = c.class then 11 c.rulesupCount++ 12 endfor 13 end-for 14 Fk {c Ck | c.rulesupCount / n minsup}; 15 CARk {f | f Fk, f.rulesupCount / f.condsupCount minconf}; 16 endfor 17 return CAR k CARk; Fig. 2.10. The CAR-Apriori algorithm counts, both condsupCount and rulesupCount, are updated during the scan of the data (lines 6–13) for each candidate k-ruleitem. At the end of the data scan, it determines which of the candidate k-ruleitems in Ck are actually frequent (line 14). From the frequent k-ruleitems, line 15 generates kcondition CARs (class association rules with k conditions). One interesting note about ruleitem generation is that if a ruleitem/rule has a confidence of 100%, then extending the ruleitem with more conditions (adding items to its condset) will also result in rules with 100% confidence although their supports may drop with additional items. In some applications, we may consider these subsequent rules redundant because additional conditions do not provide any more information. Then, we should not extend such ruleitems in candidate generation for the next level, which can reduce the number of generated rules substantially. If desired, redundancy handling can be added in the CAR-Apriori algorithm easily. The CARcandidate-gen() function is very similar to the candidate-gen() function in the Apriori algorithm, and it is thus omitted. The only difference is that in CARcandidate-gen() ruleitems with the same class are joined by joining their condsets. Example 17: Let us work on a complete example using our data in Fig. 2.9. We set minsup = 15%, and minconf = 70% F1: { ({School}, Education):(3, 3), ({Teach}, Education):(2, 2), ({Student}, Education):(2, 2), ({Baseball}, Sport):(2, 2), 36 2 Association Rules and Sequential Patterns ({Basketball}, Sport):(3, 3), ({Team}, Sport):(2, 2)} ({Game}, Sport):(3, 2), Note: The two numbers within the parentheses after each ruleitem are its condSupCount and ruleSupCount respectively. School Education Student Education Teach Education Baseball Sport Basketball Sport Game Sport Team Sport CAR1: [sup = 3/7, conf = 3/3] [sup = 2/7, conf = 2/2] [sup = 2/7, conf = 2/2] [sup = 2/7, conf = 2/2] [sup = 3/7, conf = 3/3] [sup = 2/7, conf = 2/3] [sup = 2/7, conf = 2/2] Note: We do not deal with rule redundancy in this example. C2: { ({School, Student}, Education), ({Student, Teach}, Education), ({Baseball, Game}, Sport), ({Basketball, Game}, Sport), ({Game, Team}, Sport)} ({School, Teach}, Education), ({Baseball, Basketball}, Sport), ({Baseball, Team}, Sport), ({Basketball, Team}, Sport), F2: { ({School, Student}, Education):(2, 2), ({School, Teach}, Education):(2, 2), ({Game, Team}, Sport):(2, 2)} School, Student Education School, Teach Education Game, Team Sport CAR2: [sup = 2/7, conf = 2/2] [sup = 2/7, conf = 2/2] [sup = 2/7, conf = 2/2] We note that for many applications involving target items, the data sets used are relational tables. They need to be converted to transaction forms before mining. We can use the method in Sect. 2.3 for the purpose. Example 18: In Fig. 2.11(A), the data set has three data attributes and a class attribute with two possible values, positive and negative. It is converted to the transaction data in Fig. 2.11(B). Notice that for each class, we only use its original value. There is no need to attach the attribute “Class” Attribute1 a b Attribute2 a n Atribute3 x y Class positive negative (A) Table data t1: t2: (Attribute1, a), (Attribute2, a), (Attribute3, x) (Attribute1, b), (Attribute2, n), (Attribute3, y) : Positive : negative (B) Transaction data Fig. 2.11. Converting a table data set (A) to a transaction data set (B) 2.6 Basic Concepts of Sequential Patterns 37 because there is no ambiguity. As discussed in Sect. 2.3, for each numeric attribute, its value range needs to be discretized into intervals either manually or automatically before conversion and rule mining. There are many discretization algorithms. Interested readers are referred to [151]. 2.5.3 Mining with Multiple Minimum Supports The concept of mining with multiple minimum supports discussed in Sect. 2.4 can be incorporated in class association rule mining in two ways: 1. Multiple minimum class supports: The user can specify different minimum supports for different classes. For example, the user has a data set with two classes, Yes and No. Based on the application requirement, he/she may want all rules of class Yes to have the minimum support of 5% and all rules of class No to have the minimum support of 20%. 2. Multiple minimum item supports: The user can specify a minimum item support for every item (either a class item/label or a non-class item). This is more general and is similar to normal association rule mining discussed in Sect. 2.4. For both approaches, similar mining algorithms to that given in Sect. 2.4 can be devised. The support difference constraint in Sect. 2.4.1 can be incorporated as well. Like normal association rule mining with multiple minimum supports, by setting minimum class and/or item supports to more than 100% for some items, the user effectively instructs the algorithm not to generate rules involving only these items. Finally, although we have discussed only multiple minimum supports so far, we can easily use different minimum confidences for different classes as well, which provides an additional flexibility in applications. 2.6 Basic Concepts of Sequential Patterns Association rule mining does not consider the order of transactions. However, in many applications such orderings are significant. For example, in market basket analysis, it is interesting to know whether people buy some items in sequence, e.g., buying bed first and then buying bed sheets some time later. In Web usage mining, it is useful to find navigational patterns in a Web site from sequences of page visits of users (see Chap. 12). In text mining, considering the ordering of words in a sentence is vital for finding linguistic or language patterns (see Chap. 11). For these applications, association rules will not be appropriate. Sequential patterns are needed. Be- 38 2 Association Rules and Sequential Patterns low, we define the problem of mining sequential patterns and introduce the main concepts involved. Let I = {i1, i2, …, im} be a set of items. A sequence is an ordered list of itemsets. Recall an itemset X is a non-empty set of items X I. We denote a sequence s by a1a2…ar , where ai is an itemset, which is also called an element of s. We denote an element (or an itemset) of a sequence by {x1, x2, …, xk}, where xj I is an item. We assume without loss of generality that items in an element of a sequence are in lexicographic order. An item can occur only once in an element of a sequence, but can occur multiple times in different elements. The size of a sequence is the number of elements (or itemsets) in the sequence. The length of a sequence is the number of items in the sequence. A sequence of length k is called a k-sequence. If an item occurs multiple times in different elements of a sequence, each occurrence contributes to the value of k. A sequence s1 = a1a2…ar is a subsequence of another sequence s2 = b1b2…bv , or s2 is a supersequence of s1, if there exist integers 1 j1 < j2 < … < jr 1 < jr v such that a1 bj1, a2 bj2, …, ar bjr. We also say that s2 contains s1. Example 19: Let I = {1, 2, 3, 4, 5, 6, 7, 8, 9}. The sequence {3}{4, 5}{8} is contained in (or is a subsequence of) {6} {3, 7}{9}{4, 5, 8}{3, 8} because {3} {3, 7}, {4, 5} {4, 5, 8}, and {8} {3, 8}. However, {3}{8} is not contained in {3, 8} or vice versa. The size of the sequence {3}{4, 5}{8} is 3, and the length of the sequence is 4. Objective: Given a set S of input data sequences (or sequence database), the problem of mining sequential patterns is to find all sequences that have a user-specified minimum support. Each such sequence is called a frequent sequence, or a sequential pattern. The support for a sequence is the fraction of total data sequences in S that contains this sequence. Example 20: We use the market basket analysis as an example. Each sequence in this context represents an ordered list of transactions of a particular customer. A transaction is a set of items that the customer purchased at a time (called the transaction time). Then transactions in the sequence are ordered by increasing transaction time. Table 2.1 shows a transaction database which is already sorted according to customer ID (the major key) and transaction time (the minor key). Table 2.2 gives the data sequences (also called customer sequences). Table 2.3 gives the output sequential patterns with the minimum support of 25%, i.e., two customers. 2.7 Mining Sequential Patterns Based on GSP 39 Table 2.1. A set of transactions sorted by customer ID and transaction time Customer ID Transaction Time Transaction (items bought) 1 July 20, 2005 30 1 July 25, 2005 90 2 July 9, 2005 10, 20 2 July 14, 2005 30 2 July 20, 2005 10, 40, 60, 70 3 July 25, 2005 30, 50, 70, 80 4 July 25, 2005 30 4 July 29, 2005 30, 40, 70, 80 4 August 2, 2005 90 5 July 12, 2005 90 Table 2.2. The sequence database produced from the transactions in Table 2.1. Customer ID 1 2 3 4 5 Data Sequence {30} {90} {10, 20} {30} {10, 40, 60, 70} {30, 50, 70, 80} {30} {30, 40, 70, 80} {90} {90} Table 2.3. The final output sequential patterns Sequential Patterns with Support 1-sequences 2-sequences 3-sequences 25% {30} , {40} , {70} , {80} , {90} {30} {40} , {30} {70} , {30}, {90} , {30, 70} , {30, 80} , {40, 70} , {70, 80} {30} {40, 70} , {30, 70, 80} 2.7 Mining Sequential Patterns Based on GSP This section describes two algorithms for mining sequential patterns based on the GSP algorithm in [500]: the original GSP, which uses a single minimum support, and MS-GSP, which uses multiple minimum supports. 2.7.1 GSP Algorithm GSP works in almost the same way as the Apriori algorithm. We still use Fk to store the set of all frequent k-sequences, and Ck to store the set of all 40 2 Association Rules and Sequential Patterns Algorithm GSP(S) 1 C1 init-pass(S); 2 F1 { {f} | f C1, f.count/n minsup}; 3 for (k = 2; Fk 1 ; k++) do 4 Ck candidate-gen-SPM(Fk 1); 5 for each data sequence s S do 6 for each candidate c Ck do 7 if c is contained in s then 8 c.count++; 9 endfor 10 endfor 11 Fk {c Ck | c.count/n minsup} 12 endfor 13 return k Fk; // the first pass over S // n is the number of sequences in S // subsequent passes over S // scan the data once // increment the support count Fig. 2.12. The GSP Algorithm for generating sequential patterns // SPM: Sequential Pattern Mining Function candidate-gen-SPM(Fk 1) 1. Join step. Candidate sequences are generated by joining Fk 1 with Fk 1. A sequence s1 joins with s2 if the subsequence obtained by dropping the first item of s1 is the same as the subsequence obtained by dropping the last item of s2. The candidate sequence generated by joining s1 with s2 is the sequence s1 extended with the last item in s2. There are two cases: the added item forms a separate element if it was a separate element in s2, and is appended at the end of s1 in the merged sequence, and the added item is part of the last element of s1 in the merged sequence otherwise. When joining F1 with F1, we need to add the item in s2 both as part of an itemset and as a separate element. That is, joining {x} with {y} gives us both {x, y} and {x}{y} . Note that x and y in {x, y} are ordered. 2. Prune step. A candidate sequence is pruned if any one of its (k 1)subsequences is infrequent (without minimum support). Fig. 2.13. The candidate-gen-SPM function candidate k-sequences. The algorithm is given in Fig. 2.12. The main difference is in the candidate generation, candidate-gen-SPM(), which is given in Fig. 2.13. We use an example to illustrate the function. Example 21: Table 2.4 shows F3, and C4 after the join and prune steps. In the join step, the sequence {1, 2}{4} joins with {2}{4, 5} to produce {1, 2}{4, 5} , and joins with {2}{4}{6} to produce {1, 2}{4} {6} . The other sequences cannot be joined. For instance, {1}{4, 5} does not join with any sequence since there is no sequence of the form {4, 5}{x} or {4, 5, x} . In the prune step, {1, 2}{4} {6} is removed since {1}{4} {6} is not in F3. 2.7 Mining Sequential Patterns Based on GSP 41 Table 2.4. Candidate generation: an example Frequent 3-sequences {1, 2} {4} {1, 2} {5} {1} {4, 5} {1, 4} {6} {2} {4, 5} {2} {4} {6} 2.7.2 Candidate 4-sequences after joining {1, 2} {4, 5} {1, 2} {4} {6} after pruning {1, 2} {4, 5} Mining with Multiple Minimum Supports As in association rule mining, using a single minimum support in sequential pattern mining is also a limitation for many applications because some items appear very frequently in the data, while some others appear rarely. Example 22: One of the Web mining tasks is the mining of comparative sentences such as “the picture quality of camera X is better than that of camera Y.” from product reviews, forum postings and blogs (see Chap. 11). Such a sentence usually contains a comparative indicator word such as better in the example. We want to discover linguistic patterns involving a set of given comparative indicators, e.g., better, more, less, ahead, win, superior, etc. Some of these indicators (e.g., more and better) appear very frequently in natural language sentences, while some others (e.g., win and ahead) appear rarely. In order to find patterns that contain such rare indicators, we have to use a very low minsup. However, this causes patterns involving frequent indicators to generate a huge number of spurious patterns. Moreover, we need a way to tell the algorithm that we only want patterns that contain at least one comparative indicator. Using GSP with a single minsup is no longer appropriate. The multiple minimum supports model solves both problems nicely. We again use the concept of minimum item supports (MIS). The user is allowed to assign each item a MIS value. By providing different MIS values for different items, the user essentially expresses different support requirements for different sequential patterns. To ease the task of specifying many MIS values by the user, the same strategies as those for mining association rules can also be applied here (see Sect. 2.4.2). Let MIS(i) be the MIS value of item i. The minimum support of a sequential pattern P is the lowest MIS value among the items in the pattern. Let the set of items in P be: i1, i2, …, ir. The minimum support for P is: 42 2 Association Rules and Sequential Patterns Algorithm MS-GSP(S, MS) // MS stores all MIS values 1M sort(I, MS); // according to MIS(i)’s stored in MS 2L init-pass(M, S); // make the first pass over S 3 F1 { {l} | l L, l.count/n MIS(l)}; // n is the size of S 4 for (k = 2; Fk 1 ; k++) do 5 if k = 2 then 6 Ck level2-candidate-gen-SPM(L) 7 else Ck MScandidate-gen-SPM(Fk 1) 8 endif 9 for each data sequence s S do 10 for each candidate c Ck do 11 if c is contained in s then 12 c.count++ 13 if c’ is contained in s, where c’ is c after an occurrence of c.minMISItem is removed from c then 14 c.rest.count++ // c.rest: c without c.minMISItem 15 endfor 16 endfor 17 Fk {c Ck | c.count/n MIS(c.minMISItem)} 18 endfor 19 return F k Fk; Fig. 2.14. The MS-GSP algorithm minsup(P) = min(MIS(i1), MIS(i2), …, MIS(ir)). The new algorithm, called MS-GSP, is given in Fig. 2.14. It generalizes the GSP algorithm in Fig. 2.12. Like GSP, MS-GSP is also based on levelwise search. Line 1 sorts the items in ascending order according to their MIS values stored in MS. Line 2 makes the first pass over the sequence data using the function init-pass(), which performs the same function as that in MS-Apriori to produce the seeds set L for generating the set of candidate sequences of length 2, i.e., C2. Frequent 1-sequences (F1) are obtained from L (line 3). For each subsequent pass, the algorithm works similarly to MS-Apriori. The function level2-candidate-gen-SPM() can be designed based on level2-candidate-gen in MS-Apriori and the join step in Fig. 2.13. MScandidate-gen-SPM() is, however, complex, which we will discuss shortly. In line 13, c.minMISItem gives the item that has the lowest MIS value in the candidate sequence c. Unlike that in MS-Apriori, where the first item in each itemset has the lowest MIS value, in sequential pattern mining the item with the lowest MIS value may appear anywhere in a sequence. Similar to those in MS-Apriori, lines 13 and 14 are used to ensure that all sequential rules can be generated after MS-GSP without scanning the original data. Note that in traditional sequential pattern mining, sequential rules are not defined. We will define several types in Sect. 2.9. 2.7 Mining Sequential Patterns Based on GSP 43 Let us now discuss MScandidate-gen-SPM(). In MS-Apriori, the ordering of items is not important and thus we put the item with the lowest MIS value in each itemset as the first item of the itemset, which simplifies the join step. However, for sequential pattern mining, we cannot artificially put the item with the lowest MIS value as the first item in a sequence because the ordering of items is significant. This causes problems for joining. Example 23: Assume we have a sequence s1 = {1, 2}{4} in F3, from which we want to generate candidate sequences for the next level. Suppose that item 1 has the lowest MIS value in s1. We use the candidate generation function in Fig. 2.13. Assume also that the sequence s2 = {2}{4, 5} is not in F3 because its minimum support is not satisfied. Then we will not generate the candidate {1, 2}{4, 5} . However, {1, 2}{4, 5} can be frequent because items 2, 4, and 5 may have higher MIS values than item 1. To deal with this problem, let us make an observation. The problem only occurs when the first item in the sequence s1 or the last item in the sequence s2 is the only item with the lowest MIS value, i.e., no other item in s1 (or s2) has the same lowest MIS value. If the item (say x) with the lowest MIS value is not the first item in s1, then s2 must contain x, and the candidate generation function in Fig. 2.13 will still be applicable. The same reasoning goes for the last item of s2. Thus, we only need special treatment for these two cases. Let us see how to deal with the first case, i.e., the first item is the only item with the lowest MIS value. We use an example to develop the idea. Assume we have the frequent 3-sequence of s1 = {1, 2}{4} . Based on the algorithm in Fig. 2.13, s1 may be extended to generate two possible candidates using {2}{4}{x} and {2}{4, x} c1 = {1, 2}{4}{x} and c2 = {1, 2}{4, x} , where x is an item. However, {2}{4}{x} and {2}{4, x} may not be frequent because items 2, 4, and x may have higher MIS values than item 1, but we still need to generate c1 and c2 because they can be frequent. A different join strategy is thus needed. We observe that for c1 to be frequent, the subsequence s2 = {1}{4}{x} must be frequent. Then, we can use s1 and s2 to generate c1. c2 can be generated in a similar manner with s2 = {1}{4, x} . s2 is basically the subsequence of c1 (or c2) without the second item. Here we assume that the MIS value of x is higher than item 1. Otherwise, it falls into the second case. Let us see the same problem for the case where the last item has the only lowest MIS value. Again, we use an example to illustrate. Assume we have the frequent 3-sequence s2 = {3, 5}{1} . It can be extended to produce two possible candidates based on the algorithm in Fig. 2.13, 44 2 Association Rules and Sequential Patterns Function MScandidate-gen-SPM(Fk 1) 1 Join Step. Candidate sequences are generated by joining Fk 1 with Fk 1. 2 if the MIS value of the first item in a sequence (denoted by s1) is less than (<) the MIS value of every other item in s1 then // s1 and s2 can be equal Sequence s1 joins with s2 if (1) the subsequences obtained by dropping the second item of s1 and the last item of s2 are the same, and (2) the MIS value of the last item of s2 is greater than that of the first item of s1. Candidate sequences are generated by extending s1 with the last item of s2: if the last item l in s2 is a separate element then {l} is appended at the end of s1 as a separate element to form a candidate sequence c1. if (the length and the size of s1 are both 2) AND (the last item of s2 is greater than the last item of s1) then // maintain lexicographic order l is added at the end of the last element of s1 to form another candidate sequence c2. else if ((the length of s1 is 2 and the size of s1 is 1) AND (the last item of s2 is greater than the last item of s1)) OR (the length of s1 is greater than 2) then the last item in s2 is added at the end of the last element of s1 to form the candidate sequence c2. 3 elseif the MIS value of the last item in a sequence (denoted by s2) is less than (<) the MIS value of every other item in s2 then A similar method to the one above can be used in the reverse order. 4 else use the Join Step in Fig. 2.13 5 Prune step: A candidate sequence is pruned if any one of its (k 1)subsequences is infrequent (without minimum support) except the subsequence that does not contain the item with strictly the lowest MIS value. Fig. 2.15. The MScandidate-gen-SPM function c1 = {x}{3, 5}{1} , and c2 = {x, 3, 5}{1} . For c1 to be frequent, the subsequence s1 = {x}{3}{1} has to be frequent (we assume that the MIS value of x is higher than that of item 1). Thus, we can use s1 and s2 to generate c1. c2 can be generated with s1 = {x, 3}{1} . s1 is basically the subsequence of c1 (or c2) without the second last item. The MScandidate-gen-SPM() function is given in Fig. 2.15, which is self-explanatory. Some special treatments are needed for 2-sequences because the same s1 (or s2) may generate two candidate sequences. We use two examples to show the working of the function. Example 24: Consider the items 1, 2, 3, 4, 5, and 6 with their MIS values, MIS(1) = 0.03 MIS(4) = 0.07 MIS(2) = 0.05 MIS(5) = 0.08 MIS(3) = 0.03 MIS(6) = 0.09. 2.8 Mining Sequential Patterns Based on PrefixSpan 45 The data set has 100 sequences. The following frequent 3-sequences are in F3 with their actual support counts attached after “:”: (a). {1}{4}{5} :4 (d). {1}{5, 6} :5 (g). {5, 6}{3} :5 (b). {1}{4}{6} :5 (e). {1}{6}{3} :4 (h). {5}{4}{3} :4 (c). {1}{5}{6} :6 (f). {6}{3}{6} :9 (i). {4}{5}{3} :7. For sequence (a) (= s1), item 1 has the lowest MIS value. It cannot join with sequence (b) because condition (1) in Fig. 2.15 is not satisfied. However, (a) can join with (c) to produce the candidate sequence, {1}{4}{5}{6} . (a) can also join with (d) to produce {1}{4}{5, 6} . (b) can join with (e) to produce {1}{4}{6}{3} , which is pruned subsequently because {1}{4}{3} is infrequent. (d) and (e) can be joined to give {1}{5, 6}{3} , but it is pruned because {1}{5}{3} does not exist. (e) can join with (f) to produce {1}{6}{3}{6} which is done in line 4 because both item 1 and item 3 in (e) have the same MIS value. However, it is pruned because {1}{3}{6} is infrequent. We do not join (d) and (g), although they can be joined based on the algorithm in Fig. 2.13, because the first item of (d) has the lowest MIS value and we use a different join method for such sequences. Now we look at 3-sequences whose last item has strictly the lowest MIS value. (i) (= s1) can join with (h) (= s2) to produce {4}{5}{4}{3} . However, it is pruned because {4}{4}{3} is not in F3. Example 25: Now we consider generating candidates from frequent 2sequences, which is special as we noted earlier. We use the same items and MIS values in Example 24. The following frequent 2-sequences are in F2 with their actual support counts attached after “:”: (a). {1}{5} :6 (d). {1, 5} :6 (b). {1}{6} :7 (e). {1, 6} :6. (c) {5}{4} :8 (a) can join with (b) to produce both {1}{5}{6} and {1}{5, 6} . (b) can join with (d) to produce {1, 5}{6} . (e) can join with (a) to produce {1, 6}{5} . Clearly, there are other joins. Again, (a) will not join with (c). Note that the support difference constraint in Sect. 2.4.1 can also be included. We omitted it to simplify the algorithm as it is already complex. Also, the user can instruct the algorithm to generate only certain sequential patterns or not to generate others by setting the MIS values suitably. 2.8 Mining Sequential Patterns Based on PrefixSpan We now introduce another sequential pattern mining algorithm, called PrefixSpan [439], which does not generate candidates. Different from the GSP 46 2 Association Rules and Sequential Patterns algorithm [500], which can be regarded as performing breadth-first search to find all sequential patterns, PrefixSpan performs depth-first search. 2.8.1 PrefixSpan Algorithm It is easy to introduce the original PrefixSpan algorithm using an example. Example 26: Consider again mining sequential patterns from Table 2.2 with minsup = 25%. PrefixSpan first sorts all items in each element (or itemset) as shown in the table. Then, by one scan of the sequence database, it finds all frequent items, i.e., 30, 40, 70, 80 and 90. The corresponding length one sequential patterns are {30} , {40} , {70} , {80} and {90} . We notice that the complete set of sequential patterns can actually be divided into five mutually exclusive subsets: the subset with prefix {30} , the subset with prefix {40} , the subset with prefix {70} , the subset with prefix {80} , and the subset with prefix {90} . We only need to find the five subsets one by one. To find sequential patterns having prefix {30} , the algorithm extends the prefix by adding items to it one at a time. To add the next item x, there are two possibilities, i.e., x joining the last itemset of the prefix (i.e., {30, x} ) and x forming a separate itemset (i.e., {30}{x} ). PrefixSpan performs the task by first forming the {30} -projected database and then finding all the cases of the two types in the projected database. The projected database is produced as follows: If a sequence contains item 30, then the suffix following the first 30 is extracted as a sequence in the projected database. Furthermore, since infrequent items cannot appear in a sequential pattern, all infrequent items are removed from the projection. The first sequence in our example, {30}{90} , is projected to {90} . The second sequence, {10, 20}{30}{10, 40, 60, 70} , is projected to {40, 70} , where the infrequent items 10 and 60 are removed. The third sequence {30, 50, 70, 80} is projected to {_, 70, 80} , where the infrequent item 50 is removed. Note that the underline symbol “_” in this projection denotes that the items (only 30 in this case) in the last itemset of the prefix are in the same itemset as items 50, 70 and 80 in the sequence. The fourth sequence is projected to {30, 40, 70, 80}{90} . The projection of the last sequence is empty since it does not contain item 30. The final projected database for prefix {30} contains the following sequences: {90} , {40, 70} , {_, 70, 80} , and {30, 40, 70, 80}{90} By scanning the projected database once, PrefixSpan finds all possible one item extensions to the prefix, i.e., all x’s for {30, x} and all x’s for {30}{x} . Let us discuss the details. 2.8 Mining Sequential Patterns Based on PrefixSpan 47 Find All Frequent Patterns of the Form {30, x} : Two templates {_, x} and {30, x} are used to match each projected sequence to accumulate the support count for each possible x (here x matches any item). If in the same sequence multiple matches are found with the same x, they are only counted once. Note that in general, the second template should use the last itemset in the prefix rather than only its last item. In our example, they are the same because there is only one item in the last itemset of the prefix. Find All Frequent Patterns of the Form {30}{x} : In this case, x’s are frequent items in the projected database that are not in the same itemset as the last item of the prefix. Let us continue with our example. It is easy to check that both items 70 and 80 are in the same itemset as 30. That is, we have two frequent sequences {30, 70} and {30, 80} . The support count of {30, 70} is 2 based on the projected database; one from the projected sequence {_, 70, 80} (a {_, x} match) and one from the projected sequence {30, 40, 70, 80}{90} (a {30, x} match). In both cases, the x’s are the same, i.e., 70. Similarly, the support count of {30, 80} is 2 as well and thus frequent. It is also easy to check that items 40, 70, and 90 are also frequent but not in the same itemset as 30. Thus, {30}{40} , {30}{70} , and {30}{90} are three sequential patterns. The set of sequential patterns having prefix {30} can be further divided into five mutually exclusive subsets: the ones with prefixes {30, 70} , {30, 80} , {30}{40} , {30}{70} , and {30}{90} . We can recursively find the five subsets by forming their corresponding projected databases. For example, to find sequential patterns having prefix {30}{40} , we can form the {30}{40} -projected database containing projections {_, 70} and {_, 70, 80}{90} . Template {_, x} has two matches and in both cases x is 70. Thus, {30}{40, 70} is output as a sequential pattern. Since there is no other frequent item in this projected database, the prefix cannot grow longer. The depth-first search returns from this branch. After completing the mining of the {30} -projected database, we find all sequential patterns with prefix {30} , i.e., {30} , {30}{40} , {30}{40, 70} , {30}{70} , {30}{90} , {30, 70} , {30, 80} and {30, 70, 80} By forming and mining the {40} -, {70} -, {80} - and {90} -projected databases, the remaining sequential patterns can be found. The pseudo code of PrefixSpan can be found in [439]. Comparing to the breadth-first search of GSP, the key advantage of PrefixSpan is that it does not generate any candidates. It only counts the frequency of local items. With a low minimum support, a huge number of candidates can be generated by GSP, which can cause memory and computational problems. 48 2.8.2 2 Association Rules and Sequential Patterns Mining with Multiple Minimum Supports The PrefixSpan algorithm can be adapted to mine with multiple minimum supports. Again, let MIS(i) be the user-specified minimum item support of item i. Let be the user-specified support difference threshold in the support difference constraint (Sect. 2.4.1), i.e., |sup(i) – sup(j)| , where i and j are items in the same sequential pattern, and sup(x) is the actual support of item x in the sequence database S. PrefixSpan can be modified as follows. We call the modified algorithm MS-PS. 1. Find every item i whose actual support in the sequence database S is at least MIS(i). i is called a frequent item. 2. Sort all the discovered frequent items in ascending order according to their MIS values. Let i1, …, iu be the frequent items in the sorted order. 3. For each item ik in the above sorted order, (i) identify all the data sequences in S that contain ik and at the same time remove every item j in each sequence that does not satisfy |sup(j) – sup(ik)| . The resulting set of sequences is denoted by Sk. Note that we are not using ik as the prefix to project the database S. (ii) call the function r-PrefixSpan(ik, Sk, count(MIS(ik))) (restricted PrefixSpan), which finds all sequential patterns that contain ik, i.e., no pattern that does not contain ik should be generated. r-PrefixSpan() uses count(MIS(ik)) (the minimum support count in terms of the number of sequences) as the only minimum support for mining in Sk. The sequence count is easier to use than the MIS value in percentage, but they are equivalent. Once the complete set of such patterns is found from Sk, All occurrences of ik are removed from S. r-PrefixSpan() is almost the same as PrefixSpan with one important difference. During each recursive call, either the prefix or every sequence in the projected database must contain ik because, as we stated above, this function finds only those frequent sequences that contain ik. Another minor difference is that the support difference constraint needs to be checked during each projection as sup(ik) may not be the lowest in the pattern. Example 27: Consider mining sequential patterns from Table 2.5. Let MIS(20) = 30% (3 sequences in minimum support count), MIS(30) = 20% (2 sequences), MIS(40) = 30% (3 sequences), and the MIS values for the rest of the items be 15% (2 sequences). We ignore the support difference constraint as it is simple. In step 1, we find three frequent items, 20, 30 and 40. After sorting in step 2, we have (30, 20, 40). We then go to step 3. In the first iteration of step 3, we work on i1 = 30. Step 3(i) gives us the second, fourth and sixth sequences in Table 2.5, i.e., 2.9 Generating Rules from Sequential Patterns 49 Table 2.5. An example of a sequence database Sequence ID 1 2 3 4 5 6 7 Data Sequence {20, 50} {40}{30}{40, 60} {40, 90, 120} {30}{20, 40}{40, 100} {20, 40}{10} {40}{30}{110} {20}{80}{70} S1 = { {40}{30}{40, 60} , {30}{20, 40}{40, 100} , {40}{30}{110} }. We then run r-PrefixSpan(30, S1, 3) in step 3(ii). The frequent items in S1 are 30, and 40. They both have the support of 3 sequences. The length one frequent sequence is only {30} . {40} is not included because we require that every frequent sequence must contain 30. We next find frequent sequences having prefix {30} . The database S1 is projected to give {40} and {40}{40} . 20, 60 and 100 have been removed because their supports in S1 are less than the required support for item 30 (i.e., 3 sequences). For the same reason, the projection of {40}{30}{110} is empty. Thus, we find a length two frequent sequence {30}{40} . In this case, there is no item in the same itemset as 30 to form a frequent sequence of the form {30, x} . Next, we find frequent sequences with prefix {40} . We again project S1, which gives us only {30}{40} and {30} . {40, 100} is not included because it does not contain 30. This projection gives us another length two frequent sequence {40}{30} . The first iteration of step 3 ends. In the second iteration of step 3, we work on i2 = 20. Step 3(i) gives us the first, fourth, fifth and seventh sequences in Table 2.5 with item 30 removed, S2 = { {20, 50} , {20, 40}{40, 100} , {20, 40}{10} , {20}{80}{70} }. It is easy to see that only item 20 is frequent, and thus only a length one frequent sequence is generated, {20} . In the third iteration of step 3, we work on i3 = 40. We can verify that again only one frequent sequence, i.e., {40} , is found. The final set of sequential patterns generated from the sequence database in Table 2.5 is { {30} , {20} , {40} , {40}{30} , {30}{40} }. 2.9 Generating Rules from Sequential Patterns In classic sequential pattern mining, no rules are generated. It is, however, possible to define and generate many types of rules. This section intro- 50 2 Association Rules and Sequential Patterns duces only three types, sequential rules, label sequential rules and class sequential rules, which have been used in Web usage mining and Web content mining (see Chaps. 11 and 12). 2.9.1 Sequential Rules A sequential rule (SR) is an implication of the form, X Y, where Y is a sequence and X is a proper subsequence of Y, i.e., X is a subsequence of Y and the length Y is greater than the length of X. The support of a sequential rule, X Y, in a sequence database S is the fraction of sequences in S that contain Y. The confidence of a sequential rule, X Y, in S is the proportion of sequences in S that contain X also contain Y. Given a minimum support and a minimum confidence, according to the downward closure property, all the rules can be generated from frequent sequences without going to the original sequence data. Let us see an example of a sequential rule found from the data sequences in Table 2.6. Table 2.6. An example of a sequence database for mining sequential rules 1 2 3 4 5 Data Sequence {1}{3}{5}{7, 8, 9} {1}{3}{6}{7, 8} {1, 6}{7} {1}{3}{5, 6} {1}{3}{4} Example 28: Given the sequence database in Table 2.6, the minimum support of 30% and the minimum confidence of 60%, one of the sequential rules found is the following, {1}{7} {1}{3}{7, 8} [sup = 2/5, conf = 2/3] Data sequences 1, 2 and 3 contain {1}{7} , and data sequences 1 and 2 contain {1}{3}{7, 8} . If multiple minimum supports are used, we can employ the results of multiple minimum support pattern mining to generate all the rules. 2.9.2 Label Sequential Rules Sequential rules may not be restrictive enough in some applications. We introduce a special kind of sequential rules called label sequential rules. A label sequential rule (LSR) is of the form, X Y, where Y is a sequence 2.9 Generating Rules from Sequential Patterns 51 and X is a sequence produced from Y by replacing some of its items with wildcards. A wildcard is denoted by an “*” which matches any item. These replaced items are usually very important and are called labels. The labels are a small subset of all the items in the data. Example 29: Given the sequence database in Table 2.6, the minimum support of 30% and the minimum confidence of 60%, one of the label sequential rules found is the following, {1}{*}{7, *} {1}{3}{7, 8} [sup = 2/5, conf = 2/2]. Notice the confidence change compared to the rule in Example 28. The supports of the two rules are the same. In this case, data sequences 1 and 2 contain {1}{*}{7, *} , and they also contain {1}{3}{7, 8} . Items 3 and 8 are labels. LSRs are useful because in some applications we need to predict the labels in an input sequence, e.g., items 3 and 8 above. The confidence of the rule simply gives us the estimated probability that the two “*”s are 3 and 8 given that an input sequence contains {1}{*}{7, *} . We will see an application of LSRs in Chap. 11, where we want to predict whether a word in a comparative sentence is an entity (e.g., a product name), which is a label. Note that due to the use of wildcards, frequent sequences alone are not sufficient for computing rule confidences. Scanning the data is needed. Notice also that the same pattern may appear in a data sequence multiple times. Rule confidences thus can be defined in different ways according to application needs. The wildcards may also be restricted to match only certain types of items to make the label prediction meaningful and unambiguous (see some examples in Chap. 11). 2.9.3 Class Sequential Rules Class sequential rules (CSR) are analogous to class association rules (CAR). Let S be a set of data sequences. Each sequence is also labeled with a class y. Let I be the set of all items in S, and Y be the set of all class labels, I Y = . Thus, the input data D for mining is represented with {(s1, y1), (s2, y2), …, (sn, yn)}, where si is a sequence in S and yi Y is its class. A class sequential rule (CSR) is of the form X y, where X is a sequence, and y Y. y, if X is a subsequence A data instance (si, yi) is said to cover a CSR, X of si. A data instance (si, yi) is said to satisfy a CSR if X is a subsequence of si and yi = y. 52 2 Association Rules and Sequential Patterns Example 30: Table 2.7 gives an example of a sequence database with five data sequences and two classes, c1 and c2. Using the minimum support of 30% and the minimum confidence of 60%, one of the discovered CSRs is: {1}{3}{7, 8} c1 [sup = 2/5, conf = 2/3]. Data sequences 1 and 2 satisfy the rule, and data sequences 1, 2 and 5 cover the rule. Table 2.7. An example of a sequence database for mining CSRs 1 2 3 4 5 Data Sequence {1}{3}{5}{7, 8, 9} {1}{3}{6}{7, 8} {1, 6}{9} {3}{5, 6} {1}{3}{4}{7, 8} Class c1 c1 c2 c2 c2 As in class association rule mining, we can modify the GSP and PrefixSpan algorithms to produce algorithms for mining all CSRs. Similarly, we can also use multiple minimum class supports and/or multiple minimum item supports as in class association rule mining. Bibliographic Notes Association rule mining was introduced in 1993 by Agrawal et al. [9]. Since then, numerous research papers have been published on the topic. This short chapter only introduces some basics, and it, by no means, does justice to the huge body of work in the area. The bibliographic notes here should help you explore further. Since given a data set, a minimum support and a minimum confidence, the solution (the set of frequent itemsets or the set of rules) is unique, most papers improve the mining efficiency. The most well-known algorithm is the Apriori algorithm proposed by Agrawal and Srikant [11], which has been studied in this chapter. Another important algorithm is the FPgrowth algorithm proposed by Han et al. [220]. The algorithm compresses the data and stores it in memory using a frequent pattern tree. It then mines all frequent itemsets without candidate generation. Other notable algorithms include those by Agarwal et al. [2], Mannila et al. [361], Park et al. [435], Zaki et al. [589], etc. An efficiency comparison of various algorithms was reported by Zheng et al. [616]. Apart from performance improvements, several variations of the original model were also proposed. Srikant and Agrawal [499], and Han and Fu Bibliographic Notes 53 [217] proposed two algorithms for mining generalized association rules or multi-level association rules. Liu et al. [344] extended the original model to take multiple minimum supports, which was also studied by Wang et al. [534], Seno and Karypis [482], Xiong et al. [562], etc. Srikant et al. [502] proposed to mine association rules with item constraints. The model restricts the rules that should be generated. Ng et al. [408] generalized the idea, which was followed by many subsequent papers on the topic of constrained rule mining. It is well known that association rule mining often generates a huge number of frequent itemsets and rules. Bayardo [42], and Lin and Kedem [334] introduced the problem of mining maximal frequent itemsets, which are itemsets with no frequent supersets. Improved algorithms are reported in many papers [e.g., 2, 73]. Since maximal pattern mining only finds longest patterns, the support information of their subsets, which are obviously also frequent, is not found. As a result, association rules cannot be generated. The next significant development was the mining of closed frequent itemsets studied by Pasquier et al. [436], Zaki and Hsiao [588], and Wang et al. [529]. Closed itemsets are better than maximal frequent itemsets because closed frequent itemsets provide a lossless concise representation of all frequent itemsets. Other developments on association rules include cyclic association rules proposed by Ozden et al. [420], periodic patterns by Yang et al. [571], negative association rules by Savasere [476] and Wu et al. [560], weighted association rules by Wang et al. [539], association rules with numerical variables by Webb [541], class association rules by Liu et al. [343], high-performance rule mining by Buehrer et al. [72] and many others. Recently, Cong et al. [112, 113] introduced association rule mining from bioinformatics data, which typically have a very large number of attributes (more than ten thousands) but only a very small number of records or transactions (less than 100). Another major research area on association rules is the interestingness of discovered rules. Since an association rule miner often generates a huge number of rules, it is very difficult, if not impossible, for human users to inspect them in order to find those truly interesting rules. Researchers have proposed many techniques to help users identify such rules easily [e.g., 43, 283, 342, 345, 346, 352, 421, 492, 511, 522, 535, 565]. A deployed data mining system that uses some of the techniques is reported in [352]. Regarding sequential pattern mining, the first algorithm was proposed by Agrawal and Srikant [12], which was a direct application of the Apriori algorithm. Improvements were made subsequently by several researchers, e.g., Ayres et al. [29], Pei et al. [439], Srikant and Agrawal [500], Zaki [586], etc. The MS-GSP and MS-PS algorithms for mining sequential pat- 54 2 Association Rules and Sequential Patterns terns with multiple minimum supports and the support difference constraint are introduced in this book. Label and class sequential rules have been used in [255, 256] for mining comparative sentences from text documents. There are several publicly available implementations of algorithms for mining frequent itemsets, maximal frequent itemsets, closed frequent itemsets, and sequential patterns from various research groups, most notably from those of Jiawei Han, Johnanne Gehrke, and Mohammed Zaki. There were also two workshops dedicated to frequent itemset mining organized by Roberto Bayardo, Bart Goethals, and Mohammed J. Zaki, which reported many efficient implementations. The workshop Web sites are http://fimi.cs.helsinki.fi/fimi03/ and http://fimi.cs.helsinki.fi/fimi04/. 3 Supervised Learning Supervised learning has been a great success in real-world applications. It is used in almost every domain, including text and Web domains. Supervised learning is also called classification or inductive learning in machine learning. This type of learning is analogous to human learning from past experiences to gain new knowledge in order to improve our ability to perform real-world tasks. However, since computers do not have “experiences”, machine learning learns from data, which are collected in the past and represent past experiences in some real-world applications. There are several types of supervised learning tasks. In this chapter, we focus on one particular type, namely, learning a target function that can be used to predict the values of a discrete class attribute. This type of learning has been the focus of the machine learning research and is perhaps also the most widely used learning paradigm in practice. This chapter introduces a number of such supervised learning techniques. They are used in almost every Web mining application. We will see their uses from Chaps. 6–12. 3.1 Basic Concepts A data set used in the learning task consists of a set of data records, which are described by a set of attributes A = {A1, A2, …, A|A|}, where |A| denotes the number of attributes or the size of the set A. The data set also has a special target attribute C, which is called the class attribute. In our subsequent discussions, we consider C separately from attributes in A due to its special status, i.e., we assume that C is not in A. The class attribute C has a set of discrete values, i.e., C = {c1, c2, …, c|C|}, where |C| is the number of classes and |C| 2. A class value is also called a class label. A data set for learning is simply a relational table. Each data record describes a piece of “past experience”. In the machine learning and data mining literature, a data record is also called an example, an instance, a case or a vector. A data set basically consists of a set of examples or instances. Given a data set D, the objective of learning is to produce a classification/prediction function to relate values of attributes in A and classes in C. The function can be used to predict the class values/labels of the future 56 3 Supervised Learning data. The function is also called a classification model, a predictive model or simply a classifier. We will use these terms interchangeably in this book. It should be noted that the function/model can be in any form, e.g., a decision tree, a set of rules, a Bayesian model or a hyperplane. Example 1: Table 3.1 shows a small loan application data set. It has four attributes. The first attribute is Age, which has three possible values, young, middle and old. The second attribute is Has_Job, which indicates whether an applicant has a job. Its possible values are true (has a job) and false (does not have a job). The third attribute is Own_house, which shows whether an applicant owns a house. The fourth attribute is Credit_rating, which has three possible values, fair, good and excellent. The last column is the Class attribute, which shows whether each loan application was approved (denoted by Yes) or not (denoted by No) in the past. Table 3.1. A loan application data set ID Age Has_job Own_house Credit_rating Class 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 young young young young young middle middle middle middle middle old old old old old false false true true false false false true false false false false true true false false false false true false false false true true true true true false false false fair good good fair fair fair good good excellent excellent excellent good good excellent fair No No Yes Yes No No No Yes Yes Yes Yes Yes Yes Yes No We want to learn a classification model from this data set that can be used to classify future loan applications. That is, when a new customer comes into the bank to apply for a loan, after inputting his/her age, whether he/she has a job, whether he/she owns a house, and his/her credit rating, the classification model should predict whether his/her loan application should be approved. Our learning task is called supervised learning because the class labels (e.g., Yes and No values of the class attribute in Table 3.1) are provided in 3.1 Basic Concepts 57 the data. It is as if some teacher tells us the classes. This is in contrast to the unsupervised learning, where the classes are not known and the learning algorithm needs to automatically generate classes. Unsupervised learning is the topic of the next chapter. The data set used for learning is called the training data (or the training set). After a model is learned or built from the training data by a learning algorithm, it is evaluated using a set of test data (or unseen data) to assess the model accuracy. It is important to note that the test data is not used in learning the classification model. The examples in the test data usually also have class labels. That is why the test data can be used to assess the accuracy of the learned model because we can check whether the class predicted for each test case by the model is the same as the actual class of the test case. In order to learn and also to test, the available data (which has classes) for learning is usually split into two disjoint subsets, the training set (for learning) and the test set (for testing). We will discuss this further in Sect. 3.3. The accuracy of a classification model on a test set is defined as: Accuracy Number of correct classifications Total number of test cases , (1) where a correct classification means that the learned model predicts the same class as the original class of the test case. There are also other measures that can be used. We will discuss them in Sect. 3.3. We pause here to raises two important questions: 1. What do we mean by learning by a computer system? 2. What is the relationship between the training and the test data? We answer the first question first. Given a data set D representing past “experiences”, a task T and a performance measure M, a computer system is said to learn from the data to perform the task T if after learning the system’s performance on the task T improves as measured by M. In other words, the learned model or knowledge helps the system to perform the task better as compared to no learning. Learning is the process of building the model or extracting the knowledge. We use the data set in Example 1 to explain the idea. The task is to predict whether a loan application should be approved. The performance measure M is the accuracy in Equation (1). With the data set in Table 3.1, if there is no learning, all we can do is to guess randomly or to simply take the majority class (which is the Yes class). Suppose we use the majority class and announce that every future instance or case belongs to the class Yes. If the future data are drawn from the same distribution as the existing training data in Table 3.1, the estimated classification/prediction accuracy 58 3 Supervised Learning on the future data is 9/15 = 0.6 as there are 9 Yes class examples out of the total of 15 examples in Table 3.1. The question is: can we do better with learning? If the learned model can indeed improve the accuracy, then the learning is said to be effective. The second question in fact touches the fundamental assumption of machine learning, especially the theoretical study of machine learning. The assumption is that the distribution of training examples is identical to the distribution of test examples (including future unseen examples). In practical applications, this assumption is often violated to a certain degree. Strong violations will clearly result in poor classification accuracy, which is quite intuitive because if the test data behave very differently from the training data then the learned model will not perform well on the test data. To achieve good accuracy on the test data, training examples must be sufficiently representative of the test data. We now illustrate the steps of learning in Fig. 3.1 based on the preceding discussions. In step 1, a learning algorithm uses the training data to generate a classification model. This step is also called the training step or training phase. In step 2, the learned model is tested using the test set to obtain the classification accuracy. This step is called the testing step or testing phase. If the accuracy of the learned model on the test data is satisfactory, the model can be used in real-world tasks to predict classes of new cases (which do not have classes). If the accuracy is not satisfactory, we need to go back and choose a different learning algorithm and/or do some further processing of the data (this step is called data pre-processing, not shown in the figure). A practical learning task typically involves many iterations of these steps before a satisfactory model is built. It is also possible that we are unable to build a satisfactory model due to a high degree of randomness in the data or limitations of current learning algorithms. Training data Learning algorithm Step 1: Training model Test data Accuracy Step 2: Testing Fig. 3.1. The basic learning process: training and testing From the next section onward, we study several supervised learning algorithms, except Sect. 3.3, which focuses on model/classifier evaluation. We note that throughout the chapter we assume that the training and test data are available for learning. However, in many text and Web page related learning tasks, this is not true. Usually, we need to collect raw data, 3.2 Decision Tree Induction 59 design attributes and compute attribute values from the raw data. The reason is that the raw data in text and Web applications are often not suitable for learning either because their formats are not right or because there are no obvious attributes in the raw text documents or Web pages. 3.2 Decision Tree Induction Decision tree learning is one of the most widely used techniques for classification. Its classification accuracy is competitive with other learning methods, and it is very efficient. The learned classification model is represented as a tree, called a decision tree. The techniques presented in this section are based on the C4.5 system from Quinlan [453]. Example 2: Figure 3.2 shows a possible decision tree learnt from the data in Table 3.1. The tree has two types of nodes, decision nodes (which are internal nodes) and leaf nodes. A decision node specifies some test (i.e., asks a question) on a single attribute. A leaf node indicates a class. Age? Young Has_job? true false middle old Own_house? true false Credit_rating? fair good excellent Yes No Yes No No Yes Yes (2/2) (3/3) (3/3) (2/2) (1/1) (2/2) (2/2) Fig. 3.2. A decision tree for the data in Table 3.1 The root node of the decision tree in Fig. 3.2 is Age, which basically asks the question: what is the age of the applicant? It has three possible answers or outcomes, which are the three possible values of Age. These three values form three tree branches/edges. The other internal nodes have the same meaning. Each leaf node gives a class value (Yes or No). (x/y) below each class means that x out of y training examples that reach this leaf node have the class of the leaf. For instance, the class of the left most leaf node is Yes. Two training examples (examples 3 and 4 in Table 3.1) reach here and both of them are of class Yes. To use the decision tree in testing, we traverse the tree top-down according to the attribute values of the given test instance until we reach a leaf node. The class of the leaf is the predicted class of the test instance. 60 3 Supervised Learning Example 3: We use the tree to predict the class of the following new instance, which describes a new loan applicant. Age young Has_job false Own_house false Credit-rating good Class ? Going through the decision tree, we find that the predicted class is No as we reach the second leaf node from the left. A decision tree is constructed by partitioning the training data so that the resulting subsets are as pure as possible. A pure subset is one that contains only training examples of a single class. If we apply all the training data in Table 3.1 on the tree in Fig. 3.2, we will see that the training examples reaching each leaf node form a subset of examples that have the same class as the class of the leaf. In fact, we can see that from the x and y values in (x/y). We will discuss the decision tree building algorithm in Sect. 3.2.1. An interesting question is: Is the tree in Fig. 3.2 unique for the data in Table 3.1? The answer is no. In fact, there are many possible trees that can be learned from the data. For example, Fig. 3.3 gives another decision tree, which is much smaller and is also able to partition the training data perfectly according to their classes. Own_house? true Yes false Has_job? (6/6) true false Yes No (3/3) (6/6) Fig. 3.3. A smaller tree for the data set in Table 3.1 In practice, one wants to have a small and accurate tree for many reasons. A smaller tree is more general and also tends to be more accurate (we will discuss this later). It is also easier to understand by human users. In many applications, the user understanding of the classifier is important. For example, in some medical applications, doctors want to understand the model that classifies whether a person has a particular disease. It is not satisfactory to simply produce a classification because without understanding why the decision is made the doctor may not trust the system and/or does not gain useful knowledge. It is useful to note that in both Fig. 3.2 and Fig. 3.3, the training examples that reach each leaf node all have the same class (see the values of 3.2 Decision Tree Induction 61 (x/y) at each leaf node). However, for most real-life data sets, this is usually not the case. That is, the examples that reach a particular leaf node are not of the same class, i.e., x y. The value of x/y is, in fact, the confidence (conf) value used in association rule mining, and x is the support count. This suggests that a decision tree can be converted to a set of if-then rules. Yes, indeed. The conversion is done as follows: Each path from the root to a leaf forms a rule. All the decision nodes along the path form the conditions of the rule and the leaf node or the class forms the consequent. For each rule, a support and confidence can be attached. Note that in most classification systems, these two values are not provided. We add them here to see the connection of association rules and decision trees. Example 4: The tree in Fig. 3.3 generates three rules. “,” means “and”. Own_house = true Class =Yes Own_house = false, Has_job = true Own_house = false, Has_job = false [sup=6/15, conf=6/6] Class = Yes [sup=3/15, conf=3/3] Class = No [sup=6/15, conf=6/6]. We can see that these rules are of the same format as association rules. However, the rules above are only a small subset of the rules that can be found in the data of Table 3.1. For instance, the decision tree in Fig. 3.3 does not find the following rule: Age = young, Has_job = false Class = No [sup=3/15, conf=3/3]. Thus, we say that a decision tree only finds a subset of rules that exist in data, which is sufficient for classification. The objective of association rule mining is to find all rules subject to some minimum support and minimum confidence constraints. Thus, the two methods have different objectives. We will discuss these issues again in Sect. 3.5 when we show that association rules can be used for classification as well, which is obvious. An interesting and important property of a decision tree and its resulting set of rules is that the tree paths or the rules are mutually exclusive and exhaustive. This means that every data instance is covered by a single rule (a tree path) and a single rule only. By covering a data instance, we mean that the instance satisfies the conditions of the rule. We also say that a decision tree generalizes the data as a tree is a smaller (more compact) description of the data, i.e., it captures the key regularities in the data. Then, the problem becomes building the best tree that is small and accurate. It turns out that finding the best tree that models the data is a NP-complete problem [248]. All existing algorithms use heuristic methods for tree building. Below, we study one of the most successful techniques. 62 3 Supervised Learning . Algorithm decisionTree(D, A, T) 1 if D contains only training examples of the same class cj C then 2 make T a leaf node labeled with class cj; 3 elseif A = then 4 make T a leaf node labeled with cj, which is the most frequent class in D 5 else // D contains examples belonging to a mixture of classes. We select a single // attribute to partition D into subsets so that each subset is purer 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 p0 = impurityEval-1(D); for each attribute Ai A (={A1, A2, …, Ak}) do pi = impurityEval-2(Ai, D) endfor Select Ag {A1, A2, …, Ak} that gives the biggest impurity reduction, computed using p0 – pi; if p0 – pg < threshold then // Ag does not significantly reduce impurity p0 make T a leaf node labeled with cj, the most frequent class in D. else // Ag is able to reduce impurity p0 Make T a decision node on Ag; Let the possible values of Ag be v1, v2, …, vm. Partition D into m disjoint subsets D1, D2, …, Dm based on the m values of Ag. for each Dj in {D1, D2, …, Dm} do if Dj then create a branch (edge) node Tj for vj as a child node of T; decisionTree(Dj, A {Ag}, Tj) // Ag is removed endif endfor endif endif Fig. 3.4. A decision tree learning algorithm 3.2.1 Learning Algorithm As indicated earlier, a decision tree T simply partitions the training data set D into disjoint subsets so that each subset is as pure as possible (of the same class). The learning of a tree is typically done using the divide-andconquer strategy that recursively partitions the data to produce the tree. At the beginning, all the examples are at the root. As the tree grows, the examples are sub-divided recursively. A decision tree learning algorithm is given in Fig. 3.4. For now, we assume that every attribute in D takes discrete values. This assumption is not necessary as we will see later. The stopping criteria of the recursion are in lines 1–4 in Fig. 3.4. The algorithm stops when all the training examples in the current data are of the same class, or when every attribute has been used along the current tree 3.2 Decision Tree Induction 63 path. In tree learning, each successive recursion chooses the best attribute to partition the data at the current node according to the values of the attribute. The best attribute is selected based on a function that aims to minimize the impurity after the partitioning (lines 7–11). In other words, it maximizes the purity. The key in decision tree learning is thus the choice of the impurity function, which is used in lines 7, 9 and 11 in Fig. 3.4. The recursive recall of the algorithm is in line 20, which takes the subset of training examples at the node for further partitioning to extend the tree. This is a greedy algorithm with no backtracking. Once a node is created, it will not be revised or revisited no matter what happens subsequently. 3.2.2 Impurity Function Before presenting the impurity function, we use an example to show what the impurity function aims to do intuitively. Example 5: Figure 3.5 shows two possible root nodes for the data in Table 3.1. Age? Young No: 3 Yes: 2 middle No: 2 Yes: 3 (A) Own_house? true old No: 1 Yes: 4 false No: 6 Yes: 3 No: 0 Yes: 6 (B) Fig. 3.5. Two possible root nodes or two possible attributes for the root node Fig. 3.5(A) uses Age as the root node, and Fig. 3.5(B) uses Own_house as the root node. Their possible values (or outcomes) are the branches. At each branch, we listed the number of training examples of each class (No or Yes) that land or reach there. Fig. 3.5(B) is obviously a better choice for the root. From a prediction or classification point of view, Fig. 3.5(B) makes fewer mistakes than Fig. 3.5(A). In Fig. 3.5(B), when Own_house = true every example has the class Yes. When Own_house = false, if we take majority class (the most frequent class), which is No, we make three mistakes/errors. If we look at Fig. 3.5(A), the situation is worse. If we take the majority class for each branch, we make five mistakes (marked in bold). Thus, we say that the impurity of the tree in Fig. 3.5(A) is higher than the tree in Fig. 3.5(B). To learn a decision tree, we prefer Own_house to Age to be the root node. Instead of counting the number of mistakes or errors, C4.5 uses a more principled approach to perform this evaluation on every attribute in order to choose the best attribute to build the tree. 64 3 Supervised Learning The most popular impurity functions used for decision tree learning are information gain and information gain ratio, which are used in C4.5 as two options. Let us first discuss information gain, which can be extended slightly to produce information gain ratio. The information gain measure is based on the entropy function from information theory [484]: |C | entropy ( D) Pr(c j ) log 2 Pr(c j ) (2) j1 |C | Pr(c j ) 1, j1 where Pr(cj) is the probability of class cj in data set D, which is the number of examples of class cj in D divided by the total number of examples in D. In the entropy computation, we define 0log0 = 0. The unit of entropy is bit. Let us use an example to get a feeling of what this function does. Example 6: Assume we have a data set D with only two classes, positive and negative. Let us see the entropy values for three different compositions of positive and negative examples: 1. The data set D has 50% positive examples (Pr(positive) = 0.5) and 50% negative examples (Pr(negative) = 0.5). entropy ( D) 0.5 log 2 0.5 0.5 log 2 0.5 1. 2. The data set D has 20% positive examples (Pr(positive) = 0.2) and 80% negative examples (Pr(negative) = 0.8). entropy ( D ) 0.2 log 2 0.2 0.8 log 2 0.8 0.722. 3. The data set D has 100% positive examples (Pr(positive) = 1) and no negative examples, (Pr(negative) = 0). entropy ( D ) 1 log 2 1 0 log 2 0 0. We can see a trend: When the data becomes purer and purer, the entropy value becomes smaller and smaller. In fact, it can be shown that for this binary case (two classes), when Pr(positive) = 0.5 and Pr(negative) = 0.5 the entropy has the maximum value, i.e., 1 bit. When all the data in D belong to one class the entropy has the minimum value, 0 bit. It is clear that the entropy measures the amount of impurity or disorder in the data. That is exactly what we need in decision tree learning. We now describe the information gain measure, which uses the entropy function. 3.2 Decision Tree Induction 65 Information Gain The idea is the following: 1. Given a data set D, we first use the entropy function (Equation 2) to compute the impurity value of D, which is entropy(D). The impurityEval-1 function in line 7 of Fig. 3.4 performs this task. 2. Then, we want to know which attribute can reduce the impurity most if it is used to partition D. To find out, every attribute is evaluated (lines 8–10 in Fig. 3.4). Let the number of possible values of the attribute Ai be v. If we are going to use Ai to partition the data D, we will divide D into v disjoint subsets D1, D2, …, Dv. The entropy after the partition is v | Dj | j1 |D| entropy Ai ( D) entropy ( D j ). (3) The impurityEval-2 function in line 9 of Fig. 3.4 performs this task. 3. The information gain of attribute Ai is computed with: gain( D, Ai ) entropy ( D) entropy Ai ( D). (4) Clearly, the gain criterion measures the reduction in impurity or disorder. The gain measure is used in line 11 of Fig. 3.4, which chooses attribute Ag resulting in the largest reduction in impurity. If the gain of Ag is too small, the algorithm stops for the branch (line 12). Normally a threshold is used here. If choosing Ag is able to reduce impurity significantly, Ag is employed to partition the data to extend the tree further, and so on (lines 15– 21 in Fig. 3.4). The process goes on recursively by building sub-trees using D1, D2, …, Dm (line 20). For subsequent tree extensions, we do not need Ag any more, as all training examples in each branch has the same Ag value. Example 7: Let us compute the gain values for attributes Age, Own_house and Credit_Rating using the whole data set D in Table 3.1, i.e., we evaluate for the root node of a decision tree. First, we compute the entropy of D. Since D has 6 No class training examples, and 9 Yes class training examples, we have entropy( D) 6 69 9 log 2 log 2 15 15 15 15 0.971. We then try Age, which partitions the data into 3 subsets (as Age has three possible values) D1 (with Age=young), D2 (with Age=middle), and D3 (with Age=old). Each subset has five training examples. In Fig. 3.5, we also see the number of No class examples and the number of Yes examples in each subset (or in each branch). 66 3 Supervised Learning entropy Age ( D ) 5 5 5 entropy ( D1 ) entropy ( D2 ) entropy ( D3 ) 15 15 15 5 5 5 0.971 0.971 0.722 0.888. 15 15 15 Likewise, we compute for Own_house, which partitions D into two subsets, D1 (with Own_house=true) and D2 (with Own_house=false). entropy Own _ house ( D ) 6 9 entropy ( D1 ) entropy ( D 2 ) 15 15 6 9 0 0 .918 0 .551 . 15 15 Similarly, we obtain entropyHas_job(D) = 0.647, and entropyCredit_rating(D) = 0.608. The gains for the attributes are: gain(D, Age) = 0.971 0.888 = 0.083 gain(D, Own_house) = 0.971 0.551 = 0.420 gain(D, Has_job) = 0.971 0.647 = 0.324 gain(D, Credit_rating) = 0.971 0.608 = 0.363. Own_house is the best attribute for the root node. Figure 3.5(B) shows the root node using Own_house. Since the left branch has only one class (Yes) of data, it results in a leaf node (line 1 in Fig. 3.4). For Own_house = false, further extension is needed. The process is the same as above, but we only use the subset of the data with Own_house = false, i.e., D2. Information Gain Ratio The gain criterion tends to favor attributes with many possible values. An extreme situation is that the data contain an ID attribute that is an identification of each example. If we consider using this ID attribute to partition the data, each training example will form a subset and has only one class, which results in entropyID(D) = 0. So the gain by using this attribute is maximal. From a prediction point of review, such a partition is useless. Gain ratio (Equation 5) remedies this bias by normalizing the gain using the entropy of the data with respect to the values of the attribute. Our previous entropy computations are done with respect to the class attribute: gainRatio( D, Ai ) s j1 gain( D, Ai ) | Dj | |D | log 2 j |D| |D| (5) where s is the number of possible values of Ai, and Dj is the subset of data 3.2 Decision Tree Induction 67 that has the jth value of Ai. |Dj|/|D| corresponds to the probability of Equation (2). Using Equation (5), we simply choose the attribute with the highest gainRatio value to extend the tree. This method works because if Ai has too many values the denominator will be large. For instance, in our above example of the ID attribute, the denominator will be log2|D|. The denominator is called the split info in C4.5. One note is that the split info can be 0 or very small. Some heuristic solutions can be devised to deal with it (see [453]). 3.2.3 Handling of Continuous Attributes It seems that the decision tree algorithm can only handle discrete attributes. In fact, continuous attributes can be dealt with easily as well. In a real life data set, there are often both discrete attributes and continuous attributes. Handling both types in an algorithm is an important advantage. To apply the decision tree building method, we can divide the value range of attribute Ai into intervals at a particular tree node. Each interval can then be considered a discrete value. Based on the intervals, gain or gainRatio is evaluated in the same way as in the discrete case. Clearly, we can divide Ai into any number of intervals at a tree node. However, two intervals are usually sufficient. This binary split is used in C4.5. We need to find a threshold value for the division. Clearly, we should choose the threshold that maximizes the gain (or gainRatio). We need to examine all possible thresholds. This is not a problem because although for a continuous attribute Ai the number of possible values that it can take is infinite, the number of actual values that appear in the data is always finite. Let the set of distinctive values of attribute Ai that occur in the data be {v1, v2, …, vr}, which are sorted in ascending order. Clearly, any threshold value lying between vi and vi+1 will have the same effect of dividing the training examples into those whose value of attribute Ai lies in {v1, v2, …, vi} and those whose value lies in {vi+1, vi+2, …, vr}. There are thus only r 1 possible splits on Ai, which can all be evaluated. The threshold value can be the middle point between vi and vi+1, or just on the “right side” of value vi, which results in two intervals Ai vi and Ai > vi. This latter approach is used in C4.5. The advantage of this approach is that the values appearing in the tree actually occur in the data. The threshold value that maximizes the gain (gainRatio) value is selected. We can modify the algorithm in Fig. 3.4 (lines 8–11) easily to accommodate this computation so that both discrete and continuous attributes are considered. A change to line 20 of the algorithm in Fig. 3.4 is also needed. For a continuous attribute, we do not remove attribute Ag because an interval can 68 3 Supervised Learning be further split recursively in subsequent tree extensions. Thus, the same continuous attribute may appear multiple times in a tree path (see Example 9), which does not happen for a discrete attribute. From a geometric point of view, a decision tree built with only continuous attributes represents a partitioning of the data space. A series of splits from the root node to a leaf node represents a hyper-rectangle. Each side of the hyper-rectangle is an axis-parallel hyperplane. Example 8: The hyper-rectangular regions in Fig. 3.6(A), which partitions the space, are produced by the decision tree in Fig. 3.6(B). There are two classes in the data, represented by empty circles and filled rectangles. Y X 2 >2 Y 2.6 2.5 2.5 2 > 2.5 2.6 >2 X Y 2 Y > 2.6 3 >3 X 0 2 3 4 (A) A partition of the data space 4 X >4 (B). The decision tree Fig. 3.6. A partitioning of the data space and its corresponding decision tree Handling of continuous (numeric) attributes has an impact on the efficiency of the decision tree algorithm. With only discrete attributes the algorithm grows linearly with the size of the data set D. However, sorting of a continuous attribute takes |D|log|D| time, which can dominate the tree learning process. Sorting is important as it ensures that gain or gainRatio can be computed in one pass of the data. 3.2.4 Some Other Issues We now discuss several other issues in decision tree learning. Tree Pruning and Overfitting: A decision tree algorithm recursively partitions the data until there is no impurity or there is no attribute left. This process may result in trees that are very deep and many tree leaves may cover very few training examples. If we use such a tree to predict the training set, the accuracy will be very high. However, when it is used to classify unseen test set, the accuracy may be very low. The learning is thus not effective, i.e., the decision tree does not generalize the data well. This 3.2 Decision Tree Induction 69 phenomenon is called overfitting. More specifically, we say that a classifier f1 overfits the data if there is another classifier f2 such that f1 achieves a higher accuracy on the training data than f2, but a lower accuracy on the unseen test data than f2 [385]. Overfitting is usually caused by noise in the data, i.e., wrong class values/labels and/or wrong values of attributes, but it may also be due to the complexity and randomness of the application domain. These problems cause the decision tree algorithm to refine the tree by extending it to very deep using many attributes. To reduce overfitting in the context of decision tree learning, we perform pruning of the tree, i.e., to delete some branches or sub-trees and replace them with leaves of majority classes. There are two main methods to do this, stopping early in tree building (which is also called pre-pruning) and pruning the tree after it is built (which is called post-pruning). Postpruning has been shown more effective. Early-stopping can be dangerous because it is not clear what will happen if the tree is extended further (without stopping). Post-pruning is more effective because after we have extended the tree to the fullest, it becomes clearer which branches/subtrees may not be useful (overfit the data). The general idea of post-pruning is to estimate the error of each tree node. If the estimated error for a node is less than the estimated error of its extended sub-tree, then the sub-tree is pruned. Most existing tree learning algorithms take this approach. See [453] for a technique called the pessimistic error based pruning. Example 9: In Fig. 3.6(B), the sub-tree representing the rectangular region X 2, Y > 2.5, Y 2.6 in Fig. 3.6(A) is very likely to be overfitting. The region is very small and contains only a single data point, which may be an error (or noise) in the data collection. If it is pruned, we obtain Fig. 3.7(A) and (B). Y 2 X >2 2.6 2.5 2 Y >2 X 2 3 >3 X 0 2 3 4 4 X (A) A partition of the data space (B). The decision tree Fig. 3.7. The data space partition and the decision tree after pruning >4 70 3 Supervised Learning Another common approach to pruning is to use a separate set of data called the validation set, which is not used in training and neither in testing. After a tree is built, it is used to classify the validation set. Then, we can find the errors at each node on the validation set. This enables us to know what to prune based on the errors at each node. Rule Pruning: We noted earlier that a decision tree can be converted to a set of rules. In fact, C4.5 also prunes the rules to simplify them and to reduce overfitting. First, the tree (C4.5 uses the unpruned tree) is converted to a set of rules in the way discussed in Example 4. Rule pruning is then performed by removing some conditions to make the rules shorter and fewer (after pruning some rules may become redundant). In most cases, pruning results in a more accurate rule set as shorter rules are less likely to overfit the training data. Pruning is also called generalization as it makes rules more general (with fewer conditions). A rule with more conditions is more specific than a rule with fewer conditions. Example 10: The sub-tree below X Rule 1: Rule 2: Rule 3: X X X 2, Y > 2.5, Y > 2.6 2, Y > 2.5, Y 2.6 2, Y 2.5 2 in Fig. 3.6(B) produces these rules: O Note that Y > 2.5 in Rule 1 is not useful because of Y > 2.6, and thus Rule 1 should be Rule 1: X 2, Y > 2.6 In pruning, we may be able to delete the conditions Y > 2.6 from Rule 1 to produce: X 2 Then Rule 2 and Rule 3 become redundant and can be removed. A useful point to note is that after pruning the resulting set of rules may no longer be mutually exclusive and exhaustive. There may be data points that satisfy the conditions of more than one rule, and if inaccurate rules are discarded, of no rules. An ordering of the rules is thus needed to ensure that when classifying a test case only one rule will be applied to determine the class of the test case. To deal with the situation that a test case does not satisfy the conditions of any rule, a default class is used, which is usually the majority class. Handling Missing Attribute Values: In many practical data sets, some attribute values are missing or not available due to various reasons. There are many ways to deal with the problem. For example, we can fill each 3.3 Classifier Evaluation 71 missing value with the special value “unknown” or the most frequent value of the attribute if the attribute is discrete. If the attribute is continuous, use the mean of the attribute for each missing value. The decision tree algorithm in C4.5 takes another approach. At a tree node, distribute the training example with missing value for the attribute to each branch of the tree proportionally according to the distribution of the training examples that have values for the attribute. Handling Skewed Class Distribution: In many applications, the proportions of data for different classes can be very different. For instance, in a data set of intrusion detection in computer networks, the proportion of intrusion cases is extremely small (< 1%) compared with normal cases. Directly applying the decision tree algorithm for classification or prediction of intrusions is usually not effective. The resulting decision tree often consists of a single leaf node “normal”, which is useless for intrusion detection. One way to deal with the problem is to over sample the intrusion examples to increase its proportion. Another solution is to rank the new cases according to how likely they may be intrusions. The human users can then investigate the top ranked cases. 3.3 Classifier Evaluation After a classifier is constructed, it needs to be evaluated for accuracy. Effective evaluation is crucial because without knowing the approximate accuracy of a classifier, it cannot be used in real-world tasks. There are many ways to evaluate a classifier, and there are also many measures. The main measure is the classification accuracy (Equation 1), which is the number of correctly classified instances in the test set divided by the total number of instances in the test set. Some researchers also use the error rate, which is 1 – accuracy. Clearly, if we have several classifiers, the one with the highest accuracy is preferred. Statistical significance tests may be used to check whether one classifier’s accuracy is significantly better than that of another given the same training and test data sets. Below, we first present several common methods for classifier evaluation, and then introduce some other evaluation measures. 3.3.1 Evaluation Methods Holdout Set: The available data D is divided into two disjoint subsets, the training set Dtrain and the test set Dtest, D = Dtrain Dtest and Dtrain Dtest = 72 3 Supervised Learning . The test set is also called the holdout set. This method is mainly used when the data set D is large. Note that the examples in the original data set D are all labeled with classes. As we discussed earlier, the training set is used for learning a classifier while the test set is used for evaluating the resulting classifier. The training set should not be used to evaluate the classifier as the classifier is biased toward the training set. That is, the classifier may overfit the training set, which results in very high accuracy on the training set but low accuracy on the test set. Using the unseen test set gives an unbiased estimate of the classification accuracy. As for what percentage of the data should be used for training and what percentage for testing, it depends on the data set size. 50–50 and two thirds for training and one third for testing are commonly used. To partition D into training and test sets, we can use a few approaches: 1. We randomly sample a set of training examples from D for learning and use the rest for testing. 2. If the data is collected over time, then we can use the earlier part of the data for training/learning and the later part of the data for testing. In many applications, this is a more suitable approach because when the classifier is used in the real-world the data are from the future. This approach thus better reflects the dynamic aspects of applications. Multiple Random Sampling: When the available data set is small, using the above methods can be unreliable because the test set would be too small to be representative. One approach to deal with the problem is to perform the above random sampling n times. Each time a different training set and a different test set are produced. This produces n accuracies. The final estimated accuracy on the data is the average of the n accuracies. Cross-Validation: When the data set is small, the n-fold cross-validation method is very commonly used. In this method, the available data is partitioned into n equal-size disjoint subsets. Each subset is then used as the test set and the remaining n 1 subsets are combined as the training set to learn a classifier. This procedure is then run n times, which gives n accuracies. The final estimated accuracy of learning from this data set is the average of the n accuracies. 10-fold and 5-fold cross-validations are often used. A special case of cross-validation is the leave-one-out cross-validation. In this method, each fold of the cross validation has only a single test example and all the rest of the data is used in training. That is, if the original data has m examples, then this is m-fold cross-validation. This method is normally used when the available data is very small. It is not efficient for a large data set as m classifiers need to be built. 3.3 Classifier Evaluation 73 In Sect. 3.2.4, we mentioned that a validation set can be used to prune a decision tree or a set of rules. If a validation set is employed for that purpose, it should not be used in testing. In that case, the available data is divided into three subsets, a training set, a validation set and a test set. Apart from using a validation set to help tree or rule pruning, a validation set is also used frequently to estimate parameters in learning algorithms. In such cases, the values that give the best accuracy on the validation set are used as the final values of the parameters. Cross-validation can be used for parameter estimating as well. Then a separate validation set is not needed. Instead, the whole training set is used in cross-validation. 3.3.2 Precision, Recall, F-score and Breakeven Point In some applications, we are only interested in one class. This is particularly true for text and Web applications. For example, we may be interested in only the documents or web pages of a particular topic. Also, in classification involving skewed or highly imbalanced data, e.g., network intrusion and financial fraud detection, we are typically interested in only the minority class. The class that the user is interested in is commonly called the positive class, and the rest negative classes (the negative classes may be combined into one negative class). Accuracy is not a suitable measure in such cases because we may achieve a very high accuracy, but may not identify a single intrusion. For instance, 99% of the cases are normal in an intrusion detection data set. Then a classifier can achieve 99% accuracy without doing anything by simply classifying every test case as “not intrusion”. This is, however, useless. Precision and recall are more suitable in such applications because they measure how precise and how complete the classification is on the positive class. It is convenient to introduce these measures using a confusion matrix (Table 3.2). A confusion matrix contains information about actual and predicted results given by a classifier. Table 3.2. Confusion matrix of a classifier Actual positive Actual negative Classified positive TP FP Classified negative FN TN where TP: the number of correct classifications of the positive examples (true positive) FN: the number of incorrect classifications of positive examples (false negative) FP: the number of incorrect classifications of negative examples (false positive) TN: the number of correct classifications of negative examples (true negative) 74 3 Supervised Learning Based on the confusion matrix, the precision (p) and recall (r) of the positive class are defined as follows: p TP . TP FP TP . TP FN r (6) In words, precision p is the number of correctly classified positive examples divided by the total number of examples that are classified as positive. Recall r is the number of correctly classified positive examples divided by the total number of actual positive examples in the test set. The intuitive meanings of these two measures are quite obvious. However, it is hard to compare classifiers based on two measures, which are not functionally related. For a test set, the precision may be very high but the recall can be very low, and vice versa. Example 11: A test data set has 100 positive examples and 1000 negative examples. After classification using a classifier, we have the following confusion matrix (Table 3.3), Table 3.3. Confusion matrix of a classifier Actual positive Actual negative Classified positive 1 0 Classified negative 99 1000 This confusion matrix gives the precision p = 100% and the recall r = 1% because we only classified one positive example correctly and classified no negative examples wrongly. Although in theory precision and recall are not related, in practice high precision is achieved almost always at the expense of recall and high recall is achieved at the expense of precision. In an application, which measure is more important depends on the nature of the application. If we need a single measure to compare different classifiers, the F-score is often used: F 2 pr pr (7) The F-score (also called the F1-score) is the harmonic mean of precision and recall. F 2 1 p 1 r (8) 3.4 Rule Induction 75 The harmonic mean of two numbers tends to be closer to the smaller of the two. Thus, for the F-score to be high, both p and r must be high. There is also another measure, called precision and recall breakeven point, which is used in the information retrieval community. The breakeven point is when the precision and the recall are equal. This measure assumes that the test cases can be ranked by the classifier based on their likelihoods of being positive. For instance, in decision tree classification, we can use the confidence of each leaf node as the value to rank test cases. Example 12: We have the following ranking of 20 test documents. 1 represents the highest rank and 20 represents the lowest rank. “+” (“ ”) represents an actual positive (negative) documents. 1 + 2 + 3 + 4 5 + 6 7 + 8 9 + 10 11 12 13 14 15 16 17 18 19 20 + + + + Assume that the test set has 10 positive examples. At rank 1: At rank 2: … At rank 9: At rank 10: p = 1/1 = 100% p = 2/2 = 100% … p = 6/9 = 66.7% p = 7/10 = 70% r = 1/10 = 10% r = 2/10 = 20% … r = 6/10 = 60% r = 7/10 = 70% The breakeven point is p = r = 70%. Note that interpolation is needed if such a point cannot be found. 3.4 Rule Induction In Sect. 3.2, we showed that a decision tree can be converted to a set of rules. Clearly, the set of rules can be used for classification as the tree. A natural question is whether it is possible to learn classification rules directly. The answer is yes. The process of learning such rules is called rule induction or rule learning. We study two approaches in the section. 3.4.1 Sequential Covering Most rule induction systems use an algorithm called sequential covering. A classifier built with this algorithm consists of a list of rules, which is also called a decision list [463]. In the list, the ordering of the rules is significant. The basic idea of sequential covering is to learn a list of rules sequentially, one at a time, to cover the training data. After each rule is learned, 76 3 Supervised Learning the training examples covered by the rule are removed. Only the remaining data are used to find subsequent rules. Recall that a rule covers an example if the example satisfies the conditions of the rule. We study two specific algorithms based on this general strategy. The first algorithm is based on the CN2 system [104], and the second algorithm is based on the ideas in FOIL [452], I-REP [189], REP [70], and RIPPER [106] systems. Many ideas are also taken from [385]. Algorithm 1 (Ordered Rules) This algorithm learns each rule without pre-fixing a class. That is, in each iteration, a rule of any class may be found. Thus rules of different classes may intermix in the final rule list. The ordering of rules is important. This algorithm is given in Fig. 3.8. D is the training data. RuleList is the list of rules, which is initialized to empty set (line 1). Rule is the best rule found in each iteration. The function learn-one-rule-1() learns the Rule (lines 2 and 6). The stopping criteria for the while-loop can be of various kinds. Here we use D = or Rule is NULL (a rule is not learned). Once a rule is learned from the data, it is inserted into RuleList at the end (line 4). All the training examples that are covered by the rule are removed from the data (line 5). The remaining data is used to find the next rule and so on. After rule learning ends, a default class is inserted at the end of RuleList. This is because there may still be some training examples that are not covered by any rule as no good rule can be found from them, or because some test cases may not be covered by any rule and thus cannot be classified. The final list of rules is as follows: <r1, r2, …, rk, default-class> (9) where ri is a rule. Algorithm 2 (Ordered Classes) This algorithm learns all rules for each class together. After rule learning for one class is completed, it moves to the next class. Thus all rules for each class appear together in the rule list. The sequence of rules for each class is unimportant, but the rule subsets for different classes are ordered. Typically, the algorithm finds rules for the least frequent class first, then the second least frequent class and so on. This ensures that some rules are learned for rare classes. Otherwise, they may be dominated by frequent classes and end up with no rules if considered after frequent classes. The algorithm is given in Fig. 3.9. The data set D is split into two subsets, Pos and Neg, where Pos contains all the examples of class c from D, 3.4 Rule Induction 77 Algorithm sequential-covering-1(D) 1 RuleList ; 2 Rule learn-one-rule-1(D); 3 while Rule is not NULL AND D do 4 RuleList insert Rule at the end of RuleList; 5 Remove from D the examples covered by Rule; 6 Rule learn-one-rule-1(D) 7 endwhile 8 insert a default class c at the end of RuleList, where c is the majority class in D; 9 return RuleList Fig. 3.8. The first rule learning algorithm based on sequential covering Algorithm sequential-covering-2(D, C) 1 RuleList ; // empty rule set at the beginning 2 for each class c C do 3 prepare data (Pos, Neg), where Pos contains all the examples of class c from D, and Neg contains the rest of the examples in D; while Pos do 4 5 Rule learn-one-rule-2(Pos, Neg, c); if Rule is NULL then 6 7 exit-while-loop 8 else RuleList insert Rule at the end of RuleList; 9 Remove examples covered by Rule from (Pos, Neg) endif 10 endwhile 11 12 endfor 13 return RuleList Fig. 3.9. The second rule learning algorithm based on sequential covering and Neg the rest of the examples in D (line 3). c is the class that the algorithm is working on now. Two stopping conditions for rule learning of each class are in line 4 and line 6. The other parts of the algorithm are quite similar to those of the first algorithm in Fig. 3.8. Both learn-one-rule1() and learn-one-rule-2() functions are described in Sect. 3.4.2. Use of Rules for Classification To use a list of rules for classification is straightforward. For a test case, we simply try each rule in the list sequentially. The class of the first rule that covers this test case is assigned as the class of the test case. Clearly, if no rule applies to the test case, the default class is used. 78 3.4.2 3 Supervised Learning Rule Learning: Learn-One-Rule Function We now present the function learn-one-rule(), which works as follows: It starts with an empty set of conditions. In the first iteration, one condition is added. In order to find the best condition to add, all possible conditions are tried, which form candidate rules. A condition is of the form Ai op v, where Ai is an attribute and v is a value of Ai. We also called it an attribute-value pair. For a discrete attribute, op is “=”. For a continuous attribute, op {>, }. The algorithm evaluates all the candidates to find the best one (the rest are discarded). After the first best condition is added, it tries to add the second condition and so on in the same fashion until some stopping condition is satisfied. Note that we omit the rule class here because it is implied, i.e., the majority class of the data covered by the conditions. This is a heuristic and greedy algorithm in that after a condition is added, it will not be changed or removed through backtracking. Ideally, we would want to try all possible combinations of attributes and values. However, this is not practical as the number of possibilities grows exponentially. Hence, in practice, the above greedy algorithm is used. However, instead of keeping only the best set of conditions, we can improve the function a little by keeping k best sets of conditions (k > 1) in each iteration. This is called the beam search (k beams), which ensures that a larger space is explored. Below, we present two specific implementations of the algorithm, namely learn-one-rule-1() and learn-one-rule-2(). learn-onerule-1() is used in the sequential-covering-1 algorithm, and learn-one-rule2() is used in the sequential-covering-2 algorithm. Learn-One-Rule-1 This function uses beam search (Fig. 3.10). The number of beams is k. BestCond stores the conditions of the rule to be returned. The class is omitted as it is the majority class of the data covered by BestCond. candidateCondSet stores the current best condition sets (which are the frontier beams) and its size is less than or equal to k. Each condition set contains a set of conditions connected by “and” (conjunction). newCandidateCondSet stores all the new candidate condition sets after adding each attribute-value pair (a possible condition) to every candidate in candidateCondSet (lines 5–11). Lines 13–17 update the BestCond. Specifically, an evaluation function is used to assess whether each new candidate condition set is better than the existing best condition set BestCond (line 14). If so, it replaces the current BestCond (line 15). Line 18 updates candidateCondSet, which selects k new best condition sets (new beams). Once the final BestCond is found, it is evaluated to see if it is significantly better than without any condition ( ) using a threshold (line 20). If 3.4 Rule Induction 79 Function learn-one-rule-1(D) BestCond ; // rule with no condition. candidateCondSet {BestCond}; attributeValuePairs the set of all attribute-value pairs in D of the form (Ai op v), where Ai is an attribute and v is a value or an interval; 4 while candidateCondSet do 5 newCandidateCondSet ; for each candidate cond in candidateCondSet do 6 for each attribute-value pair a in attributeValuePairs do 7 8 newCond cond {a}; 9 newCandidateCondSet newCandidateCondSet {newCond} 10 endfor 11 endfor 12 remove duplicates and inconsistencies, e.g., {Ai = v1, Ai = v2}; 13 for each candidate newCond in newCandidateCondSet do if evaluation(newCond, D) > evaluation(BestCond, D) then 14 15 BestCond newCond endif 16 17 endfor 18 candidateCondSet the k best members of newCandidateCondSet according to the results of the evaluation function; 19 endwhile 20 if evaluation(BestCond, D) – evaluation( , D) > threshold then 21 return the rule: “BestCond c” where is c the majority class of the data covered by BestCond 22 else return NULL 23 endif 1 2 3 Fig. 3.10. The learn-one-rule-1 function Function evaluation(BestCond, D) D the subset of training examples in D covered by BestCond; |C | 2 entropy ( D ' ) Pr( c j ) log 2 Pr( c j ) ; j1 1 3 return – entropy(D’) // since entropy measures impurity. Fig. 3.11. The entropy based evaluation function yes, a rule will be formed using BestCond and the most frequent (or the majority) class of the data covered by BestCond (line 21). If not, NULL is returned to indicate that no significant rule is found. The evaluation() function (Fig. 3.11) uses the entropy function as in the decision tree learning. Other evaluation functions are possible too. Note that when BestCond = , it covers every example in D, i.e., D = D . 80 1 2 3 4 5 6 7 3 Supervised Learning Function learn-one-rule-2(Pos, Neg, class) split (Pos, Neg) into (GrowPos, GrowNeg) and (PrunePos, PruneNeg) BestRule GrowRule(GrowPos, GrowNeg, class) // grow a new rule BestRule PruneRule(BestRule, PrunePos, PruneNeg) // prune the rule if the error rate of BestRule on (PrunePos, PruneNeg) exceeds 50% then return NULL endif return BestRule Fig. 3.12. The learn-one-rule-2() function Learn-One-Rule-2 In the learn-one-rule-2() function (Fig. 3.12), a rule is first generated and then it is pruned. This method starts by splitting the positive and negative training data Pos and Neg, into growing and pruning sets. The growing sets, GrowPos and GrowNeg, are used to generate a rule, called BestRule. The pruning sets, PrunePos and PruneNeg are used to prune the rule because BestRule may overfit the data. Note that PrunePos and PruneNeg are actually validation sets discussed in Sects. 3.2.4 and 3.3.1. growRule() function: growRule() generates a rule (called BestRule) by repeatedly adding a condition to its condition set that maximizes an evaluation function until the rule covers only some positive examples in GrowPos but no negative examples in GrowNeg. This is basically the same as lines 4–17 in Fig. 3.10, but without beam search (i.e., only the best rule is kept in each iteration). Let the current partially developed rule be R: R: av1, .., avk class where each avj is a condition (an attribute-value pair). By adding a new condition avk+1, we obtain the rule R+: av1, .., avk, avk+1 class. The evaluation function for R+ is the following information gain criterion (which is different from the gain function used in decision tree learning): gain( R, R ) p1 log 2 p1 p1 n1 log 2 p0 p 0 n0 (10) where p0 (respectively, n0) is the number of positive (negative) examples covered by R in Pos (Neg), and p1 (n1) is the number of positive (negative) examples covered by R+ in Pos (Neg). The GrowRule() function simply returns the rule R+ that maximizes the gain. PruneRule() function: To prune a rule, we consider deleting every subset of conditions from the BestRule, and choose the deletion that maximizes: 3.5 Classification Based on Associations v( BestRule, PrunePos, PruneNeg ) pn , pn 81 (11) where p (respectively n) is the number of examples in PrunePos (PruneNeg) covered by the current rule (after a deletion). 3.4.3 Discussion Separate-and-Conquer vs. Divide-and-Conquer: Decision tree learning is said to use the divide-and-conquer strategy. At each step, all attributes are evaluated and one is selected to partition/divide the data into m disjoint subsets, where m is the number of values of the attribute. Rule induction discussed in this section is said to use the separate-and-conquer strategy, which evaluates all attribute-value pairs (conditions) (which are much larger in number than the number of attributes) and selects only one. Thus, each step of divide-and-conquer expands m rules, while each step of separate-and-conquer expands only one rule. Due to both effects, the separateand-conquer strategy is much slower than the divide-and-conquer strategy. Rule Understandability: If-then rules are easy to understand by human users. However, a word of caution about rules generated by sequential covering is in order. Such rules can be misleading because the covered data are removed after each rule is generated. Thus the rules in the rule list are not independent of each other. A rule r may be of high quality in the context of the data D from which r was generated. However, it may be a weak rule with a very low accuracy (confidence) in the context of the whole data set D (D D) because many training examples that can be covered by r have already been removed by rules generated before r. If you want to understand the rules and possibly use them in some real-world tasks, you should be aware of this fact. 3.5 Classification Based on Associations In Sect. 3.2, we showed that a decision tree can be converted to a set of rules, and in Sect. 3.4, we saw that a set of rules may also be found directly for classification. It is thus only natural to expect that association rules, in particular class association rules (CAR), may be used for classification too. Yes, indeed! In fact, normal association rules can be employed for classification as well as we will see in Sect. 3.5.3. CBA, which stands for Classification Based on Associations, is the first reported system that uses 82 3 Supervised Learning association rules for classification [343]. In this section, we describe three approaches to employing association rules for classification: 1. Using class association rules for classification directly. 2. Using class association rules as features or attributes. 3. Using normal (or classic) association rules for classification. The first two approaches can be applied to tabular data or transactional data. The last approach is usually employed for transactional data only. All these methods are useful in the Web environment as many types of Web data are in the form of transactions, e.g., search queries issued by users, and Web pages clicked by visitors. Transactional data sets are difficult to handle by traditional classification techniques, but are very natural for association rules. Below, we describe the three approaches in turn. We should note that various sequential rules can be used for classification in similar ways as well if sequential data sets are involved. 3.5.1 Classification Using Class Association Rules Recall that a class association rule (CAR) is an association rule with only a class label on the right-hand side of the rule as its consequent (Sect. 2.5). For instance, from the data in Table 3.1, the following rule can be found: Own_house = false, Has_job = true Class = Yes [sup=3/15, conf=3/3], which was also a rule from the decision tree in Fig. 3.3. In fact, there is no difference between rules from a decision tree (or a rule induction system) and CARs if we consider only categorical (or discrete) attributes (more on this later). The differences are in the mining processes and the final rule sets. CAR mining finds all rules in data that satisfy the user-specified minimum support (minsup) and minimum confidence (minconf) constraints. A decision tree or a rule induction system finds only a subset of the rules (expressed as a tree or a list of rules) for classification. Example 13: Recall that the decision tree in Fig. 3.3 gives the following three rules: Own_house = true Class =Yes Own_house = false, Has_job = true Own_house = false, Has_job = false [sup=6/15, conf=6/6] Class=Yes [sup=3/15, conf=3/3] Class=No [sup=6/15, conf=6/6]. However, there are many other rules that exist in data, e.g., Age = young, Has_job = true Class=Yes Class=No Age = young, Has_job = false Class=No Credit_rating = fair [sup=2/15, conf=2/2] [sup=3/15, conf=3/3] [sup=4/15, conf=4/5] 3.5 Classification Based on Associations 83 and many more, if we use minsup = 2/15 = 13.3% and minconf = 70%. In many cases, rules that are not in the decision tree (or a rule list) may be able to perform classification more accurately. Empirical comparisons reported by several researchers show that classification using CARs can perform more accurately on many data sets than decision trees and rule induction systems (see Bibliographic Notes for references). The complete set of rules from CAR mining is also beneficial from a rule usage point of view. In some applications, the user wants to act on some interesting rules. For example, in an application for finding causes of product problems, more rules are preferred to fewer rules because. With more rules, the user is more likely to find rules that indicate causes of the problems. Such rules may not be generated by a decision tree or a rule induction system. A deployed data mining system based on CARs is reported in [352]. We should, however, also bear in mind of the following: 1. Decision tree learning and rule induction do not use the minsup or minconf constraint. Thus, some rules that they find can have very low supports, which, of course, are likely to be pruned because the chance that they overfit the training data is high. Although we can set a low minsup for CAR mining, it may cause combinatorial explosion. In practice, in addition to minsup and minconf, a limit on the total number of rules to be generated may be used to further control the CAR generation process. When the number of generated rules reaches the limit, the algorithm stops. However, with this limit, we may not be able to generate long rules (with many conditions). Recall that the Apriori algorithm works in a level-wise fashion, i.e., short rules are generated before long rules. In some applications, this might not be an issue as short rules are often preferred and are sufficient for classification or for action. Long rules normally have very low supports and tend to overfit the data. However, in some other applications, long rules can be useful. 2. CAR mining does not use continuous (numeric) attributes, while decision trees deal with continuous attributes naturally. Rule induction can use continuous attributes as well. There is still no satisfactory method to deal with such attributes directly in association rule mining. Fortunately, many attribute discretization algorithms exist that can automatically discretize the value range of a continuous attribute into suitable intervals [e.g., 151, 172], which are then considered as discrete values. Mining Class Association Rules for Classification There are many techniques that use CARs to build classifiers. Before describing them, let us first discuss some issues related to CAR mining for 84 3 Supervised Learning classification. Since a CAR mining algorithm has been discussed in Sect. 2.5, we will not repeat it here. Rule Pruning: CAR rules are highly redundant, and many of them are not statistically significant (which can cause overfitting). Rule pruning is thus needed. The idea of pruning CARs is basically the same as that in decision tree building or rule induction. Thus, we will not discuss it further (see [343, 328] for some of the pruning methods). Multiple Minimum Class Supports: As discussed in Sect. 2.5.3, a single minsup is inadequate for mining CARs because many practical classification data sets have uneven class distributions, i.e., some classes cover a large proportion of the data, while others cover only a very small proportion (which are called rare or infrequent classes). Example 14: Suppose we have a dataset with two classes, Y and N. 99% of the data belong to the Y class, and only 1% of the data belong to the N class. If we set minsup = 1.5%, we will not find any rule for class N. To solve the problem, we need to lower down the minsup. Suppose we set minsup = 0.2%. Then, we may find a huge number of overfitting rules for class Y because minsup = 0.2% is too low for class Y. Multiple minimum class supports can be applied to deal with the problem. We can assign a different minimum class support minsupi for each class ci, i.e., all the rules of class ci must satisfy minsupi. Alternatively, we can provide one single total minsup, denoted by t_minsup, which is then distributed to each class according to the class distribution: minsupi = t_minsup sup(ci) (12) where sup(ci) is the support of class ci in training data. The formula gives frequent classes higher minsups and infrequent classes lower minsups. Parameter Selection: The parameters used in CAR mining are the minimum supports and the minimum confidences. Note that a different minimum confidence may also be used for each class. However, minimum confidences do not affect the classification much because classifiers tend to use high confidence rules. One minimum confidence is sufficient as long as it is not set too high. To determine the best minsupi for each class ci, we can try a range of values to build classifiers and then use a validation set to select the final value. Cross-validation may be used as well. Data Formats: The algorithm for CAR mining given in Sect. 2.5.2 is for mining transaction data sets. However, many classification data sets are in the table format. As we discussed in Sect. 2.3, a tabular data set can be easily converted to a transaction data set. 3.5 Classification Based on Associations 85 Classifier Building After all CAR rules are found, a classifier is built using the rules. There are many existing methods, which can be grouped into three categories. Use the Strongest Rule: This is perhaps the simplest strategy. It simply uses CARs directly for classification. For each test instance, it finds the strongest rule that covers the instance. Recall that a rule covers an instance if the instance satisfies the conditions of the rule. The class of the strongest rule is then assigned as the class of the test instance. The strength of a rule can be measured in various ways, e.g., based on confidence, 2 test, or a combination of both support and confidence values. Select a Subset of the Rules to Build a Classifier: The representative method of this category is the one used in the CBA system. The method is similar to the sequential covering method, but applied to class association rules with additional enhancements as discussed above. Let the set of all discovered CARs be S. Let the training data set be D. The basic idea is to select a subset L ( S) of high confidence rules to cover the training data D. The set of selected rules, including a default class, is then used as the classifier. The selection of rules is based on a total order defined on the rules in S. Definition: Given two rules, ri and rj, ri rj (also called ri precedes rj or ri has a higher precedence than rj) if 1. the confidence of ri is greater than that of rj, or 2. their confidences are the same, but the support of ri is greater than that of rj, or 3. both the confidences and supports of ri and rj are the same, but ri is generated earlier than rj. A CBA classifier L is of the form: L = <r1, r2, …, rk, default-class> where ri S, ra rb if b > a. In classifying a test case, the first rule that satisfies the case classifies it. If no rule applies to the case, it takes the default class (default-class). A simplified version of the algorithm for building such a classifier is given in Fig. 3.13. The classifier is the RuleList. This algorithm can be easily implemented by making one pass through the training data for every rule. However, this is extremely inefficient for large data sets. An efficient algorithm that makes at most two passes over the data is given in [343]. Combine Multiple Rules: Like the first approach, this approach does not take any additional step to build a classifier. At the classification time, for 86 1 2 3 4 5 6 7 8 9 3 Supervised Learning Algorithm CBA(S, D) // sorting is done according to the precedence S = sort(S); // the rule list classifier RuleList = ; for each rule r S in sequence do if D AND r classifies at least one example in D correctly then delete from D all training examples covered by r; add r at the end of RuleList endif endfor add the majority class as the default class at the end of RuleList Fig. 3.13. A simple classifier building algorithm each test instance, the system first finds the subset of rules that covers the instance. If all the rules in the subset have the same class, the class is assigned to the test instance. If the rules have different classes, the system divides the rules into groups according to their classes, i.e., all rules of the same class are in the same group. The system then compares the aggregated effects of the rule groups and finds the strongest group. The class label of the strongest group is assigned to the test instance. To measure the strength of each rule group, there again can be many possible techniques. For example, the CMAR system uses a weighted 2 measure [328]. 3.5.2 Class Association Rules as Features In the above two methods, rules are directly used for classification. In this method, rules are used as features to augment the original data or simply form a new data set, which is then fed to a traditional classification algorithm, e.g., decision trees or the naïve Bayesian method. To use CARs as features, only the conditional part of each rule is needed, and it is often treated as a Boolean feature/attribute. If a data instance in the original data contains the conditional part, the value of the feature/attribute is set to 1, and otherwise it is set to 0. Several applications of this method have been reported [23, 131, 255, 314]. The reason that this approach is helpful is that CARs capture multi-attribute or multi-item correlations with class labels. Many classification algorithms do not find such correlations (e.g., naïve Bayesian), but they can be quite useful. 3.5.3 Classification Using Normal Association Rules Not only can class association rules be used for classification, but also normal association rules. For example, association rules are commonly 3.6 Naïve Bayesian Classification 87 used in e-commerce Web sites for product recommendations, which work as follows: When a customer purchases some products, the system will recommend him/her some other related products based on what he/she has already purchased (see Chap. 12). Recommendation is essentially a classification or prediction problem. It predicts what a customer is likely to buy. Association rules are naturally applicable to such applications. The classification process is the following: 1. The system first uses previous purchase transactions (the same as market basket transactions) to mine association rules. In this case, there are no fixed classes. Any item can appear on the left-hand side or the righthand side of a rule. For recommendation purposes, usually only one item appears on the right-hand side of a rule. 2. At the prediction (e.g., recommendation) time, given a transaction (e.g., a set of items already purchased by a customer), all the rules that cover the transaction are selected. The strongest rule is chosen and the item on the right-hand side of the rule (i.e., the consequent) is then the predicted item and recommended to the user. If multiple rules are very strong, multiple items can be recommended. This method is basically the same as the “use the strongest rule” method described in Sect. 3.5.1. Again, the rule strength can be measured in various ways, e.g., confidence, 2 test, or a combination of both support and confidence. For example, in [337], the product of support and confidence is used as the rule strength. Clearly, the other two methods discussed in Sect. 3.5.1 can be applied as well. The key advantage of using association rules for recommendation is that they can predict any item since any item can be the class item on the righthand side. Traditional classification algorithms only work with a single fixed class attribute, and are not easily applicable to recommendations. Finally, we note that multiple minimum supports (Sect. 2.4) can be of significant help. Otherwise, rare items will never be recommended, which causes the coverage problem (see Sect. 12.3.3). It is shown in [389] that using multiple minimum supports can dramatically increase the coverage. 3.6 Naïve Bayesian Classification Supervised learning can be naturally studied from a probabilistic point of view. The task of classification can be regarded as estimating the class posterior probabilities given a test example d, i.e., Pr(C= cj | d). (13) 88 3 Supervised Learning We then see which class cj is more probable. The class with the highest probability is assigned to the example d. Formally, let A1, A2, …, A|A| be the set of attributes with discrete values in the data set D. Let C be the class attribute with |C| values, c1, c2, …, c|C|. Given a test example d with observed attribute values a1 through a|A|, where ai is a possible value of Ai (or a member of the domain of Ai), i.e., d = <A1=a1, ..., A|A|=a|A|>. The prediction is the class cj such that Pr(C=cj | A1=a1, ..., A|A|=a|A|) is maximal. cj is called a maximum a posteriori (MAP) hypothesis. By Bayes’ rule, the above quantity (13) can be expressed as Pr(C c j | A1 Pr( A1 a1 ,..., A| A| a| A| | C Pr( A1 a1 ,..., A| A| Pr( A1 a1 ,..., A| A| a1 ,..., A| A| a| A| ) c j ) Pr(C a| A| | C cj) (14) a| A| ) c j ) Pr(C cj) |C | Pr( A1 a1 ,..., A| A| a| A| | C ck ) Pr(C . ck ) k1 Pr(C=cj) is the class prior probability of cj, which can be estimated from the training data. It is simply the fraction of the data in D with class cj. If we are only interested in making a classification, Pr(A1=a1, ..., A|A|=a|A|) is irrelevant for decision making because it is the same for every class. Thus, only Pr(A1=a1 ... A|A|=a|A| | C=cj) needs to be computed, which can be written as Pr(A1=a1, ..., A|A|=a|A| | C=cj) (15) = Pr(A1=a1 | A2=a2, ..., A|A|=a|A|, C=cj) Pr(A2=a2, ..., A|A|=a|A| | C=cj). Recursively, the second term above (i.e., Pr(A2=a2, ..., A|A|=a|A||C=cj)) can be written in the same way (i.e., Pr(A2=a2|A3=a3 ..., A|A|=a|A|, C=cj) Pr(A3=a3, ..., A|A|=a|A||C=cj)), and so on. However, to further our derivation, we need to make an important assumption. Conditional independence assumption: We assume that all attributes are conditionally independent given the class C = cj. Formally, we assume, Pr(A1=a1 | A2=a2, ..., A|A|=a|A|, C=cj) = Pr(A1=a1 | C=cj) (16) and similarly for A2 through A|A|. We then obtain | A| Pr( A1 a1 ,..., A| A| a| A| | C cj) Pr( Ai i1 ai | C cj) (17) 3.6 Naïve Bayesian Classification Pr(C c j | A1 a1 ,..., A| A| a| A| ) (18) | A| Pr(C cj ) Pr( Ai ai | C cj) i1 | A| |C | Pr(C . ck ) k1 89 Pr( Ai ai | C ck ) i1 Next, we need to estimate the prior probabilities Pr(C=cj) and the conditional probabilities Pr(Ai=ai | C=cj) from the training data, which are straightforward. Pr(C Pr( Ai a i | C number of examples of class c j cj) (19) total number of examples in the data set number of examples with Ai cj) ai and class c j number of examples of class c j . (20) If we only need a decision on the most probable class for each test instance, we only need the numerator of Equation (18) since the denominator is the same for every class. Thus, given a test case, we compute the following to decide the most probable class for the test case: | A| c arg max Pr(C cj cj) Pr( Ai ai | C cj) (21) i1 Example 15: Suppose that we have the training data set in Fig. 3.14, which has two attributes A and B, and the class C. We can compute all the probability values required to learn a naïve Bayesian classifier. A B C m m g h g g g h h m b s q s q q s b q b t t t t t f f f f f Fig. 3.14. An example of a training data set 90 3 Supervised Learning Pr(C = t) = 1/2, Pr(C= f) = 1/2 Pr(A=m | C=t) = 2/5 Pr(A=m | C=f) = 1/5 Pr(B=b | C=t) = 1/5 Pr(B=b | C=f) = 2/5 Pr(A=g | C=t) = 2/5 Pr(A=g | C=f) = 2/5 Pr(B=s | C=t) = 2/5 Pr(B=s | C=f) = 1/5 Pr(A=h | C=t) = 1/5 Pr(A=h | C=f) =2/5 Pr(B=q | C=t) = 2/5 Pr(B=q | C=f) = 2/5 Now we have a test example: A=m B=q C=? We want to know its class. Equation (21) is applied. For C = t, we have 2 Pr(C t) Pr( A j aj |C t) aj | C f) j1 122 255 2 . 25 For class C = f, we have 2 Pr(C f) Pr( A j j1 112 255 1 . 25 Since C = t is more probable, t is the predicted class of the test case. It is easy to see that the probabilities (i.e., Pr(C=cj) and Pr(Ai=ai | C=cj)) required to build a naïve Bayesian classifier can be found in one scan of the data. Thus, the algorithm is linear in the number of training examples, which is one of the great strengths of the naïve Bayes, i.e., it is extremely efficient. In terms of classification accuracy, although the algorithm makes the strong assumption of conditional independence, several researchers have shown that its classification accuracies are surprisingly strong. See experimental comparisons of various techniques in [148, 285, 349]. To learn practical naïve Bayesian classifiers, we still need to address some additional issues: how to handle numeric attributes, zero counts, and missing values. Below, we deal with each of them in turn. Numeric Attributes: The above formulation of the naïve Bayesian learning assumes that all attributes are categorical. However, most real-life data sets have numeric attributes. Therefore, in order to use the naïve Bayeisan algorithm, each numeric attribute needs to be discretized into intervals. This is the same as for class association rule mining. Existing discretization algorithms in [e.g., 151, 172] can be used. Zero Counts: It is possible that a particular attribute value in the test set never occurs together with a class in the training set. This is problematic because it will result in a 0 probability, which wipes out all the other probabilities Pr(Ai=ai | C=cj) when they are multiplied according to Equation 3.7 Naïve Bayesian Text Classification 91 (21) or Equation (18). A principled solution to this problem is to incorporate a small-sample correction into all probabilities. Let nij be the number of examples that have both Ai = ai and C = cj. Let nj be the total number of examples with C=cj in the training data set. The uncorrected estimate of Pr(Ai=ai | C=cj) is nij/nj, and the corrected estimate is Pr( Ai ai | C cj) nij nj (22) mi where mi is the number of values of attribute Ai (e.g., 2 for a Boolean attribute), and is a multiplicative factor, which is commonly set to = 1/n, where n is the total number of examples in the training set D [148, 285]. When = 1, we get the well known Laplace’s law of succession [204]. The general form of correction (also called smoothing) in Equation (22) is called the Lidstone’s law of succession [330]. Applying the correction = 1/n, the probabilities of Example 15 are revised. For example, Pr(A=m | C=t) = (2+1/10) / (5 + 3*1/10) = 2.1/5.3 = 0.396 Pr(B=b | C=t) = (1+1/10) / (5 + 3*1/10) = 1.1/5.3 = 0.208. Missing Values: Missing values are ignored, both in computing the probability estimates in training and in classifying test instances. 3.7 Naïve Bayesian Text Classification Text classification or categorization is the problem of learning classification models from training documents labeled with pre-defined classes. That learned models are then used to classify future documents. For example, we have a set of news articles of three classes or topics, Sport, Politics, and Science. We want to learn a classifier that is able to classify future news articles into these classes. Due to the rapid growth of online documents in organizations and on the Web, automated document classification is an important problem. Although the techniques discussed in the previous sections can be applied to text classification, it has been shown that they are not as effective as the methods presented in this section and in the next two sections. In this section, we study a naïve Bayesian learning method that is specifically formulated for texts, which makes use of text specific characteristics. However, the ideas are similar to those in Sect. 3.6. Below, we first present a probabilistic framework for texts, and then study the naïve Bayesian equations for their classification. There are several slight variations of this model. This section is mainly based on the formulation given in [365]. 92 3.7.1 3 Supervised Learning Probabilistic Framework The naïve Bayesian learning method for text classification is derived based on a probabilistic generative model. It assumes that each document is generated by a parametric distribution governed by a set of hidden parameters. Training data is used to estimate these parameters. The parameters are then applied to classify each test document using Bayes’ rule by calculating the posterior probability that the distribution associated with a class (represented by the unobserved class variable) would have generated the given document. Classification then becomes a simple matter of selecting the most probable class. The generative model is based on two assumptions: 1. The data (or the text documents) are generated by a mixture model. 2. There is one-to-one correspondence between mixture components and document classes. A mixture model models the data with a number of statistical distributions. Intuitively, each distribution corresponds to a data cluster and the parameters of the distribution provide a description of the corresponding cluster. Each distribution in a mixture model is also called a mixture component (the distribution can be of any kind). Figure 3.15 plots two probability density functions of a mixture of two Gaussian distributions that generate a 1-dimensional data set of two classes, one distribution per class, whose parameters (denoted by i) are the mean ( i) and the standard deviation ( i), i.e., i = ( i, i). class 1 class 2 Fig. 3.15. Probability density functions of the two distributions in the mixture model Let the number of mixture components (or distributions) in a mixture model be K, and the jth distribution have the parameters j. Let be the set of parameters of all components, = { 1, 2, …, K, 1, 2, …, K}, where j is the mixture weight (or mixture probability) of the mixture component j and j is the set of parameters of component j. The mixture 3.7 Naïve Bayesian Text Classification weights are subject to the constraint K j1 j 1. 93 The meaning of mixture weights (or probabilities) will be clear below. Let us see how the mixture model generates a collection of documents. Recall the classes C in our classification problem are c1, c2, …, c|C|. Since we assume that there is one-to-one correspondence between mixture components and classes, each class corresponds to a mixture component. Thus |C| = K, and the jth mixture component can be represented by its corresponding class cj and is parameterized by j. The mixture weights are class prior probabilities, i.e., j = Pr(cj| ). The mixture model generates each document di by: 1. first selecting a mixture component (or class) according to class prior probabilities (i.e., mixture weights), j = Pr(cj| ); 2. then having this selected mixture component (cj) generate a document di according to its parameters, with distribution Pr(di|cj; ) or more precisely Pr(di|cj; j). The probability that a document di is generated by the mixture model can be written as the sum of total probability over all mixture components. Note that to simplify the notation, we use cj instead of C = cj as in the previous section: |C | Pr(d i | ) Pr(c j | ) Pr(d i | c j ; ). (23) j1 Since each document is attached with its class label, we can now derive the naïve Bayesian model for text classification. Note that in the above probability expressions, we include to represent their dependency on as we employ a generative model. In an actual implementation, we need not be concerned with , i.e., it can be ignored. 3.7.2 Naïve Bayesian Model A text document consists of a sequence of sentences, and each sentence consists of a sequence of words. However, due to the complexity of modeling word sequence and their relationships, several assumptions are made in the derivation of the Bayesian classifier. That is also why we call the final classification model, naïve Bayesian classification. Specifically, the naïve Bayesian classification treats each document as a “bag” of words. The generative model makes the following assumptions: 94 3 Supervised Learning 1. Words of a document are generated independently of the context, that is, independently of the other words in the same document given the class label. This is the familiar naïve Bayesian assumption used before. 2. The probability of a word is independent of its position in the document. For example, the probability of seeing the word “student” in the first position of the document is the same as seeing it in any other position. The document length is chosen independent of its class. With these assumptions, each document can be regarded as generated by a multinomial distribution. In other words, each document is drawn from a multinomial distribution of words with as many independent trials as the length of the document. The words are from a given vocabulary V = {w1, w2, …, w|V|}, |V| being the number of words in the vocabulary. To see why this is a multinomial distribution, we give a short introduction to the multinomial distribution. A multinomial trial is a process that can result in any of k outcomes, where k 2. Each outcome of a multinomial trial has a probability of occurrence. The probabilities of the k outcomes are denoted by p1, p2, …, pk. For example, the rolling of a die is a multinomial trial, with six possible outcomes 1, 2, 3, 4, 5, 6. For a fair die, p1 = p2 = … = pk = 1/6. Now assume n independent trials are conducted, each with the k possible outcomes and the k probabilities, p1, p2, …, pk. Let us number the outcomes 1, 2, 3, …, k. For each outcome, let Xt denote the number of trials that result in that outcome. Then, X1, X2, …, Xk are discrete random variables. The collection of X1, X2, …, Xk is said to have the multinomial distribution with parameters, n, p1, p2, …, pk. In our context, n corresponds to the length of a document, and the outcomes correspond to all the words in the vocabulary V (k = |V|). p1, p2, …, pk correspond to the probabilities of occurrence of the words in V in a document, which are Pr(wt|cj; ). Xt is a random variable representing the number of times that word wt appears in a document. We can thus directly apply the probability function of the multinomial distribution to find the probability of a document given its class (including the probability of document length, Pr(|di|), which is assumed to be independent of class): |V | t1 Pr(d i | c j ; ) Pr( wt | c j ; ) N ti N ti ! Pr(| d i |) | d i |! (24) where Nti is the number of times that word wt occurs in document di and |V | |V | N ti | d i | , and t1 Pr( wt | c j ; ) 1. t1 (25) 3.7 Naïve Bayesian Text Classification 95 The parameters j of the generative component for each class cj are the probabilities of all words wt in V, written as Pr(wt|cj; ), and the probabilities of document lengths, which are the same for all classes (or mixture components) due to our assumption. Parameter Estimation: The parameters can be estimated from the training data D = {D1, D2, …, D|C|}, where Dj is the subset of data for class cj (recall |C| is the number of classes). The vocabulary V is the set of all distinctive words in D. Note that we do not need to estimate the probability of each document length as it is not used in our final classifier. The estimate of is written as ˆ . The parameters are estimated based on empirical counts. The estimated probability of word wt given class cj is simply the number of times that wt occurs in the training data Dj (of class cj) divided by the total number of word occurrences in the training data for that class: | D| Pr( wt | c j ; ˆ ) i1 N ti Pr(c j | d i ) |V | | D| s1 i (26) . N si Pr(c j | d i ) 1 In Equation (26), we do not use Dj explicitly. Instead, we include Pr(cj|di) to achieve the same effect because Pr(cj|di) = 1 for each document in Dj and Pr(cj|di) = 0 for documents of other classes. Again, Nti is the number of times that word wt occurs in document di. In order to handle 0 counts for infrequently occurring words that do not appear in the training set, but may appear in the test set, we need to smooth the probability to avoid probabilities of 0 or 1. This is the same problem as in Sect. 3.6. The standard way of doing this is to augment the count of each 1) or a fraction of a word distinctive word with a small quantity (0 in both the numerator and denominator. Thus, any word will have at least a very small probability of occurrence. | D| Pr( wt | c j ; ˆ ) |V | i1 |V | s1 N ti Pr(c j | d i ) | D| i . (27) N si Pr(c j | d i ) 1 This is called the Lidstone smoothing (Lidsone’s law of succession). When = 1, the smoothing is known as the Laplace smoothing. Many experiments have shown that < 1 works better for text classification [7]. The best value for a data set can be found through experiments using a validation set or through cross-validation. Finally, class prior probabilities, which are mixture weights j, can be easily estimated using the training data as well. 96 3 Supervised Learning | D| Pr(c j | ˆ ) i1 Pr(c j | d i ) |D| (28) . Classification: Given the estimated parameters, at the classification time, we need to compute the probability of each class cj for the test document di. That is, we compute the probability that a particular mixture component cj generated the given document di. Using Bayes rule and Equations (23), (24), (27), and (28), we have Pr(c j | ˆ ) Pr( d i | c j ; ˆ ) Pr(d | ˆ ) Pr(c j | d i ; ˆ ) (29) i Pr(c j | ˆ ) |d i | k1 Pr( wd i ,k | c j ; ˆ ) |C | |d i | r k Pr(cr | ˆ ) 1 Pr( wd i , k | cr ; ˆ ) 1 , where wdi,k is the word in position k of document di (which is the same as using wt and Nti). If the final classifier is to classify each document into a single class, the class with the highest posterior probability is selected: arg max c j 3.7.3 C Pr(c j | d i ; ˆ ). (30) Discussion Most assumptions made by naïve Bayesian learning are violated in practice. For example, words in a document are clearly not independent of each other. The mixture model assumption of one-to-one correspondence between classes and mixture components may not be true either because a class may contain documents from multiple topics. Despite such violations, researchers have shown that naïve Bayesian learning produces very accurate models. Naïve Bayesian learning is also very efficient. It scans the training data only once to estimate all the probabilities required for classification. It can be used as an incremental algorithm as well. The model can be updated easily as new data comes in because the probabilities can be conveniently revised. Naïve Bayesian learning is thus widely used for text classification. The naïve Bayesian formulation presented here is based on a mixture of multinomial distributions. There is also a formulation based on multivariate Bernoulli distributions in which each word in the vocabulary is a binary feature, i.e., it either appears or does not appear in the document. 3.8 Support Vector Machines 97 Thus, it does not consider the number of times that a word occurs in a document. Experimental comparisons show that multinomial formulation consistently produces more accurate classifiers [365]. 3.8 Support Vector Machines Support vector machines (SVM) is another type of learning system [525], which has many desirable qualities that make it one of most popular algorithms. It not only has a solid theoretical foundation, but also performs classification more accurately than most other algorithms in many applications, especially those applications involving very high dimensional data. For instance, it has been shown by several researchers that SVM is perhaps the most accurate algorithm for text classification. It is also widely used in Web page classification and bioinformatics applications. In general, SVM is a linear learning system that builds two-class classifiers. Let the set of training examples D be {(x1, y1), (x2, y2), …, (xn, yn)}, where xi = (xi1, xi2, …, xir) is a r-dimensional input vector in a real-valued r space X , yi is its class label (output value) and yi {1, 1}. 1 denotes the positive class and 1 denotes the negative class. Note that we use slightly different notations in this section. For instance, we use y instead of c to represent a class because y is commonly used to represent classes in the SVM literature. Similarly, each data instance is called an input vector and denoted by a bold face letter. In the following, we use bold face letters for all vectors. To build a classifier, SVM finds a linear function of the form (31) f(x) = w x + b so that an input vector xi is assigned to the positive class if f(xi) the negative class otherwise, i.e., yi 1 if w x i 1 if w x i b0 b0 0, and to (32) r . w = (w1, w2, …, wr) Hence, f(x) is a real-valued function f: X r is called the weight vector. b is called the bias. w x is the dot product of w and x (or Euclidean inner product). Without using vector notation, Equation (31) can be written as: f(x1, x2, …, xr) = w1x1+w2x2 + … + wrxr + b, 98 3 Supervised Learning where xi is the variable representing the ith coordinate of the vector x. For convenience, we will use the vector notation from now on. In essence, SVM finds a hyperplane (33) w x +b=0 that separates positive and negative training examples. This hyperplane is called the decision boundary or decision surface. Geometrically, the hyperplane w x + b = 0 divides the input space into two half spaces: one half for positive examples and the other half for negative examples. Recall that a hyperplane is commonly called a line in a 2-dimensional space and a plane in a 3-dimensional space. Fig. 3.16(A) shows an example in a 2-dimensional space. Positive instances (also called positive data points or simply positive points) are represented with small filled rectangles, and negative examples are represented with small empty circles. The thick line in the middle is the decision boundary hyperplane (a line in this case), which separates positive (above the line) and negative (below the line) data points. Equation (31), which is also called the decision rule of the SVM classifier, is used to make classification decisions on test instances. y=1 w x +b=0 y= 1 (A) (B) Fig. 3.16. (A) A linearly separable data set and (B) possible decision boundaries Fig. 3.16(A) raises two interesting questions: 1. There are an infinite number of lines that can separate the positive and negative data points as illustrated by Fig. 3.16(B). Which line should we choose? 2. A hyperplane classifier is only applicable if the positive and negative data can be linearly separated. How can we deal with nonlinear separations or data sets that require nonlinear decision boundaries? The SVM framework provides good answers to both questions. Briefly, for question 1, SVM chooses the hyperplane that maximizes the margin (the 3.8 Support Vector Machines 99 gap) between positive and negative data points, which will be defined formally shortly. For question 2, SVM uses kernel functions. Before we dive into the details, we should note that SVM requires numeric data and only builds two-class classifiers. At the end of the section, we will discuss how these limitations may be addressed. 3.8.1 Linear SVM: Separable Case This sub-section studies the simplest case of linear SVM. It is assumed that the positive and negative data points are linearly separable. From linear algebra, we know that in w x + b = 0, w defines a direction perpendicular to the hyperplane (see Fig. 3.17). w is also called the normal vector (or simply normal) of the hyperplane. Without changing the normal vector w, varying b moves the hyperplane parallel to itself. Note also that w x + b = 0 has an inherent degree of freedom. We can + rescale the hyperplane to w x + b = 0 for (positive real numbers) without changing the function/hyperplane. w x +b=0 y=1 d d+ x+ w x margin |b| || w || H+: w x + b = 1 y= 1 H: w x +b= 1 Fig. 3.17. Separating hyperplanes and margin of SVM: Support vectors are circled Since SVM maximizes the margin between positive and negative data points, let us find the margin. Let d+ (respectively d ) be the shortest distance from the separating hyperplane ( w x + b = 0) to the closest positive (negative) data point. The margin of the separating hyperplane is d++d . SVM looks for the separating hyperplane with the largest margin, which is also called the maximal margin hyperplane, as the final decision boundary. The reason for choosing this hyperplane to be the decision boundary is because theoretical results from structural risk minimization in 100 3 Supervised Learning computational learning theory show that maximizing the margin minimizes the upper bound of classification errors. Let us consider a positive data point (x+, 1) and a negative (x , 1) that are closest to the hyperplane <w x> + b = 0. We define two parallel hyperplanes, H+ and H , that pass through x+ and x respectively. H+ and H are also parallel to <w x> + b = 0. We can rescale w and b to obtain H+: w x+ + b = 1 H: w x +b= 1 w xi + b 1 such that w xi + b (34) (35) if yi = 1 if yi = 1, which indicate that no training data fall between hyperplanes H+ and H . Now let us compute the distance between the two margin hyperplanes H+ and H . Their distance is the margin (d+ + d ). Recall from vector space in linear algebra that the (perpendicular) Euclidean distance from a point xi to a hyperplane w x + b = 0 is: | w xi b | , || w || (36) where ||w|| is the Euclidean norm of w, || w || ww w1 2 w2 2 ... wr 2 (37) To compute d+, instead of computing the distance from x+ to the separating hyperplane w x + b = 0, we pick up any point xs on w x + b = 0 and compute the distance from xs to w x+ + b = 1 by applying Equation 36 and noticing that w xs + b = 0, d | w xs b 1 | || w || 1 || w || (38) Likewise, we can compute the distance of xs to w x+ + b = 1 to obtain d = 1/||w||. Thus, the decision boundary w x + b = 0 lies half way between H+ and H . The margin is thus margin d d 2 || w || (39) In fact, we can compute the margin in many ways. For example, it can be computed by finding the distances from the origin to the three hyperplanes, or by projecting the vector (x2 x1+) to the normal vector w. 3.8 Support Vector Machines 101 Since SVM looks for the separating hyperplane that maximizes the margin, this gives us an optimization problem. Since maximizing the margin is the same as minimizing ||w||2/2 = w w /2. We have the following linear separable SVM formulation. Definition (Linear SVM: Separable Case): Given a set of linearly separable training examples, D = {(x1, y1), (x2, y2), …, (xn, yn)}, learning is to solve the following constrained minimization problem, ww 2 Subject to : yi ( w x i Minimize : (40) b) 1, i 1, 2, ..., n Note that the constraint yi ( w x i w xi + b w xi + b b 1, i 1, 2, ..., n summarizes: for yi = 1 for yi = 1. 1 Solving the problem (40) will produce the solutions for w and b, which in turn give us the maximal margin hyperplane w x + b = 0 with the margin 2/||w||. A full description of the solution method requires a significant amount of optimization theory, which is beyond the scope of this book. We will only use those relevant results from optimization without giving formal definitions, theorems or proofs. Since the objective function is quadratic and convex and the constraints are linear in the parameters w and b, we can use the standard Lagrangian multiplier method to solve it. Instead of optimizing only the objective function (which is called unconstrained optimization), we need to optimize the Lagrangian of the problem, which considers the constraints at the same time. The need to consider constraints is obvious because they restrict the feasible solutions. Since our inequality constraints are expressed using “ ”, the Lagrangian is formed by the constraints multiplied by positive Lagrange multipliers and subtracted from the objective function, i.e., LP 1 ww 2 n i [ yi ( w x i b) 1] (41) i1 where i 0 are the Lagrange multipliers. The optimization theory says that an optimal solution to (41) must satisfy certain conditions, called Kuhn–Tucker conditions, which play a 102 3 Supervised Learning central role in constrained optimization. Here, we give a brief introduction to these conditions. Let the general optimization problem be Minimize : f ( x ) Subject to : g i ( x ) bi , i 1, 2, ..., n (42) where f is the objective function and gi is a constraint function (which is different from yi in (40) as yi is not a function but a class label of 1 or 1). The Lagrangian of (42) is, n LP f ( x) i [ g i (x) bi )] (43) i1 An optimal solution to the problem in (42) must satisfy the following necessary (but not sufficient) conditions: LP xj 0, j 1, 2, ..., r g i (x) bi i (44) 0, i 1, 2, ..., n 0, i 1, 2, ..., n i (bi g i (x i )) 0, i 1, 2, ..., n (45) (46) (47) These conditions are called the Kuhn–Tucker conditions. Note that (45) is simply the original set of constraints in (42). The condition (47) is called the complementarity condition, which implies that at the solution point, If i > 0 then If gi(x) > bi then gi(x) = bi. i = 0. These mean that for active constraints, i > 0, whereas for inactive constraints i = 0. As we will see later, they give some very desirable properties to SVM. Let us come back to our problem. For the minimization problem (40), the Kuhn–Tucker conditions are (48)–(52): LP wj n wj LP b yi ( w x i yi i xij 0, j 1, 2, ..., r (48) i1 n yi i 0 (49) i1 b) 1 0, i 1, 2, ..., n (50) 3.8 Support Vector Machines i 0, i 1, 2, ..., n i ( yi ( w xi 103 (51) b) 1) 0, i 1, 2, ..., n (52) Inequality (50) is the original set of constraints. We also note that although there is a Lagrange multiplier i for each training data point, the complementarity condition (52) shows that only those data points on the margin hyperplanes (i.e., H+ and H ) can have i > 0 since for them yi( w xi + b) – 1 = 0. These data points are called support vectors, which give the name to the algorithm, support vector machines. All the other data points have i = 0. In general, Kuhn–Tucker conditions are necessary for an optimal solution, but not sufficient. However, for our minimization problem with a convex objective function and a set of linear constraints, the Kuhn–Tucker conditions are both necessary and sufficient for an optimal solution. Solving the optimization problem is still a difficult task due to the inequality constraints. However, the Lagrangian treatment of the convex optimization problem leads to an alternative dual formulation of the problem, which is easier to solve than the original problem, which is called the primal problem (LP is called the primal Lagrangian). The concept of duality is widely used in the optimization literature. The aim is to provide an alternative formulation of the problem which is more convenient to solve computationally and/or has some theoretical significance. In the context of SVM, the dual problem is not only easy to solve computationally, but also crucial for using kernel functions to deal with nonlinear decision boundaries as we do not need to compute w explicitly (which will be clear later). Transforming from the primal to its corresponding dual can be done by setting to zero the partial derivatives of the Lagrangian (41) with respect to the primal variables (i.e., w and b), and substituting the resulting relations back into the Lagrangian. This is to simply substitute (48), which is n wj yi i xij , j 1, 2, ..., r (53) i1 and (49), which is n yi i 0, (54) i1 into the original Lagrangian (41) to eliminate the primal variables, which gives us the dual objective function (denoted by LD), 104 3 Supervised Learning n LD i i1 1n yi y j 2 i, j 1 i j xi x j . (55) LD contains only dual variables and must be maximized under the simpler constraints, (48) and (49), and i 0. Note that (48) is not needed as it has already been substituted into the objective function LD. Hence, the dual of the primal Equation (40) is n Maximize: LD 1n yi y j 2 i, j 1 i i1 i j xi x j . (56) n Subject to: yi i 0 i1 i 0, i 1, 2, ..., n. This dual formulation is called the Wolfe dual. For our convex objective function and linear constraints of the primal, it has the property that the i’s at the maximum of LD gives w and b occurring at the minimum of LP (the primal). Solving (56) requires numerical techniques and clever strategies beyond the scope of this book. After solving (56), we obtain the values for i, which are used to compute the weight vector w and the bias b using Equations (48) and (52) respectively. Instead of depending on one support vector ( i > 0) to compute b, in practice all support vectors are used to compute b, and then take their average as the final value for b. This is because the values of i are computed numerically and can have numerical errors. Our final decision boundary (maximal margin hyperplane) is wx b yi i b xi x 0 (57) i sv where sv is the set of indices of the support vectors in the training data. Testing: We apply (57) for classification. Given a test instance z, we classify it using the following: sign( w z b) sign yi i xi z b. (58) i sv If (58) returns 1, then the test instance z is classified as positive; otherwise, it is classified as negative. 3.8 Support Vector Machines 3.8.2 105 Linear SVM: Non-separable Case The linear separable case is the ideal situation. In practice, however, the training data is almost always noisy, i.e., containing errors due to various reasons. For example, some examples may be labeled incorrectly. Furthermore, practical problems may have some degree of randomness. Even for two identical input vectors, their labels may be different. For SVM to be useful, it must allow noise in the training data. However, with noisy data the linear separable SVM will not find a solution because the constraints cannot be satisfied. For example, in Fig. 3.18, there is a negative point (circled) in the positive region, and a positive point in the negative region. Clearly, no solution can be found for this problem. Recall that the primal for the linear separable case was: ww 2 Subject to : yi ( w x i Minimize : (59) b) 1, i 1, 2, ..., n. To allow errors in data, we can relax the margin constraints by introducing slack variables, i ( 0) as follows: w xi + b w xi + b 1 i 1+ i for yi = 1 for yi = 1. Thus we have the new constraints: Subject to: yi( w xi + b) 1 i, i =1, 2, …, n, i 0, i =1, 2, …, n. The geometric interpretation is shown in Fig. 3.18, which has two error data points xa and xb (circled) in wrong regions. w x +b=0 w xb b || w || |b| || w || a xa || w || Fig. 3.18. The non-separable case: xa and xb are error data points 106 3 Supervised Learning We also need to penalize the errors in the objective function. A natural way is to assign an extra cost for errors to change the objective function to ww Minimize : 2 k n C (60) i i1 where C 0 is a user specified parameter. The resulting optimization problem is still a convex programming problem. k = 1 is commonly used, which has the advantage that neither i nor its Lagrangian multipliers appear in the dual formulation. We only discuss the k = 1 case below. The new optimization problem becomes: n ww C i 2 i1 Subject to : yi ( w x i b) 1 Minimize : (61) 1, 2, ..., n i, i 0, i 1, 2, ..., n. i This formulation is called the soft-margin SVM. The primal Lagrangian (denoted by LP) of this formulation is as follows LP n 1 ww 2 n C n i i i1 [ yi ( w x i b) 1 i1 i ii (62) i1 where i, i 0 are the Lagrange multipliers. The Kuhn–Tucker conditions for optimality are the following: n LP wj wj yi i xij 0, j 1, 2, ..., r (63) i1 n LP b yi (64) 0 i i1 LP C i i 0, i 1, 2, ..., n (65) i yi ( w x i b) 1 i 0, i 1, 2, ..., n (66) i 0, i 1, 2, ..., n (67) i 0, i 1, 2, ..., n (68) i 0, i 1, 2, ..., n (69) i ( yi ( ii w xi b) 1 0, i 1, 2, ..., n i) 0, i 1, 2, ..., n (70) (71) 3.8 Support Vector Machines 107 As the linear separable case, we then transform the primal to its dual by setting to zero the partial derivatives of the Lagrangian (62) with respect to the primal variables (i.e., w, b and i), and substituting the resulting relations back into the Lagrangian. That is, we substitute Equations (63), (64) and (65) into the primal Lagrangian (62). From Equation (65), C i i = 0, we can deduce that i C because i 0. Thus, the dual of (61) is n Maximize: LD ( ) 1n yi y j 2 i, j 1 i i1 i j xi x j (72) n Subject to: yi i 0 i1 0 C , i 1, 2, ..., n. i Interestingly, i and its Lagrange multipliers i are not in the dual and the objective function is identical to that for the separable case. The only difference is the constraint i C (inferred from C i i = 0 and i 0). The dual problem (72) can also be solved numerically, and the resulting i values are then used to compute w and b. w is computed using Equation (63) and b is computed using the Kuhn–Tucker complementarity conditions (70) and (71). Since we do not have values for i, we need to get around it. From Equations (65), (70) and (71), we observe that if 0 < i < C then both i = 0 and yi ( w x i b) 1 i ) 0. Thus, we can use any training data point for which 0 < i < C and Equation (70) (with i = 0) to compute b: b 1 yi n yi i (73) xi x j . i1 Again, due to numerical errors, we can compute all possible b’s and then take their average as the final b value. Note that Equations (65), (70) and (71) in fact tell us more: i=0 0< i<C i=C yi( w xi + b) 1 and yi( w xi + b) = 1 and yi( w xi + b) 1 and =0 =0 0 i i i (74) Similar to support vectors for the separable case, (74) shows one of the most important properties of SVM: the solution is sparse in i. Most training data points are outside the margin area and their i’s in the solution are 0. Only those data points that are on the margin (i.e., yi( w xi + b) = 1, which are support vectors in the separable case), inside the margin (i.e., i 108 3 Supervised Learning = C and yi( w xi + b) < 1), or errors are non-zero. Without this sparsity property, SVM would not be practical for large data sets. The final decision boundary is (we note that many i’s are 0) n wx b yi i xi x b 0. (75) i1 The decision rule for classification (testing) is the same as the separable case, i.e., sign( w x + b). We notice that for both Equations (75) and (73), w does not need to be explicitly computed. This is crucial for using kernel functions to handle nonlinear decision boundaries. Finally, we still have the problem of determining the parameter C. The value of C is usually chosen by trying a range of values on the training set to build multiple classifiers and then to test them on a validation set before selecting the one that gives the best classification result on the validation set. Cross-validation is commonly used as well. 3.8.3 Nonlinear SVM: Kernel Functions The SVM formulations discussed so far require that positive and negative examples can be linearly separated, i.e., the decision boundary must be a hyperplane. However, for many real-life data sets, the decision boundaries are nonlinear. To deal with nonlinearly separable data, the same formulation and solution techniques as for the linear case are still used. We only transform the input data from its original space into another space (usually of a much higher dimensional space) so that a linear decision boundary can separate positive and negative examples in the transformed space, which is called the feature space. The original data space is called the input space. Thus, the basic idea is to map the data in the input space X to a feature space F via a nonlinear mapping , :X x F (x). (76) After the mapping, the original training data set {(x1, y1), (x2, y2), …, (xn, yn)} becomes: {( (x1), y1), ( (x2), y2), …, ( (xn), yn)}. (77) The same linear SVM solution method is then applied to F. Figure 3.19 illustrates the process. In the input space (figure on the left), the training examples cannot be linearly separated. In the transformed feature space (figure on the right), they can be separated linearly. 3.8 Support Vector Machines Input space X Feature space F x x o o x x o 109 (x) (x) (x) (o) (o) (o) x o (x) (x) (o) Fig. 3.19. Transformation from the input space to the feature space With the transformation, the optimization problem in (61) becomes n ww C i 2 i1 Subject to : yi ( w (x i ) b) 1 Minimize : (78) i , i 1, 2, ..., n 0, i 1, 2, ..., n i Its corresponding dual is n Maximize: LD 1n yi y j 2 i, j 1 i i1 i j (x i ) (x j ) . (79) n Subject to: yi 0 i i1 0 i C , i 1, 2, ..., n. The final decision rule for classification (testing) is n yi i (x i ) (x) b (80) i1 Example 16: Suppose our input space is 2-dimensional, and we choose the following transformation (mapping): ( x1 , x2 ) 2 2 ( x1 , x2 , 2 x1 x2 ) (81) The training example ((2, 3), 1) in the input space is transformed to the following training example in the feature space: ((4, 9, 8.5), 1). The potential problem with this approach of transforming the input data explicitly to a feature space and then applying the linear SVM is that it 110 3 Supervised Learning may suffer from the curse of dimensionality. The number of dimensions in the feature space can be huge with some useful transformations (see below) even with reasonable numbers of attributes in the input space. This makes it computationally infeasible to handle. Fortunately, explicit transformations can be avoided if we notice that in the dual representation both the construction of the optimal hyperplane (79) in F and the evaluation of the corresponding decision/classification function (80) only require the evaluation of dot products (x) (z) and never the mapped vector (x) in its explicit form. This is a crucial point. Thus, if we have a way to compute the dot product (x) (z) in the feature space F using the input vectors x and z directly, then we would not need to know the feature vector (x) or even the mapping function itself. In SVM, this is done through the use of kernel functions, denoted by K, K(x, z) = (x) (82) (z) , which are exactly the functions for computing dot products in the transformed feature space using input vectors x and z. An example of a kernel function is the polynomial kernel, K(x, z) = x z d. (83) Example 17: Let us compute this kernel with degree d = 2 in a 2dimensional space. Let x = (x1, x2) and z = (z1, z2). xz 2 x 2 z 2 )2 ( x1 z1 2 x1 z1 2 2 2 x1 z1 x 2 z 2 2 2 x2 z2 2 2 (84) 2 ( x1 , x 2 , 2 x1 x 2 ) ( z1 , z 2 , 2 z1 z 2 ) ( x) 2 (z ) , 2 where (x) ( x1 , x 2 , 2 x1 x 2 ), which shows that the kernel x z 2 is a dot product in the transformed feature space. The number of dimensions in the feature space is 3. Note that (x) is actually the mapping function used in Example 16. Incidentally, in general the number of dimensions in the feard1 , ture space for the polynomial kernel function K(x, z) = x z d is d which is a huge number even with a reasonable number (r) of attributes in the input space. Fortunately, by using the kernel function in (83), the huge number of dimensions in the feature space does not matter. The derivation in (84) is only for illustration purposes. We do not need to find the mapping function. We can simply apply the kernel function di- 3.8 Support Vector Machines 111 rectly. That is, we replace all the dot products (x) (z) in (79) and (80) with the kernel function K(x, z) (e.g., the polynomial kernel in (83)). This strategy of directly using a kernel function to replace dot products in the feature space is called the kernel trick. We would never need to explicitly know what is. However, the question is, how do we know whether a function is a kernel without performing the derivation such as that in (84)? That is, how do we know that a kernel function is indeed a dot product in some feature space? This question is answered by a theorem called the Mercer’s theorem, which we will not discuss here. See [118] for details. It is clear that the idea of kernel generalizes the dot product in the input space. The dot product is also a kernel with the feature map being the identity (85) K(x, z) = x z . Commonly used kernels include Polynomial: K ( x, z ) ( x z Gaussian RBF: K (x, z ) e where ,d N, and ||x z||2 2 )d (86) (87) > 0. Summary SVM is a linear learning system that finds the maximal margin decision boundary to separate positive and negative examples. Learning is formulated as a quadratic optimization problem. Nonlinear decision boundaries are found via a transformation of the original data to a much higher dimensional feature space. However, this transformation is never explicitly done. Instead, kernel functions are used to compute dot products required in learning without the need to even know the transformation function. Due to the separation of the learning algorithm and kernel functions, kernels can be studied independently from the learning algorithm. One can design and experiment with different kernel functions without touching the underlying learning algorithm. SVM also has some limitations: 1. It works only in real-valued space. For a categorical attribute, we need to convert its categorical values to numeric values. One way to do this is to create an extra binary attribute for each categorical value, and set the attribute value to 1 if the categorical value appears, and 0 otherwise. 112 3 Supervised Learning 2. It allows only two classes, i.e., binary classification. For multiple class classification problems, several strategies can be applied, e.g., oneagainst-rest, and error-correcting output coding [138]. 3. The hyperplane produced by SVM is hard to understand by users. It is difficult to picture where the hyperplane is in a high-dimensional space. The matter is made worse by kernels. Thus, SVM is commonly used in applications that do not required human understanding. 3.9 K-Nearest Neighbor Learning All the previous learning methods learn some kinds of models from the data, e.g., decision trees, sets of rules, posterior probabilities, and hyperplanes. These learning methods are often called eager learning methods as they learn models of the data before testing. In contrast, k-nearest neighbor (kNN) is a lazy learning method in the sense that no model is learned from the training data. Learning only occurs when a test example needs to be classified. The idea of kNN is extremely simple and yet quite effective in many applications, e.g., text classification. It works as follows: Again let D be the training data set. Nothing will be done on the training examples. When a test instance d is presented, the algorithm compares d with every training example in D to compute the similarity or distance between them. The k most similar (closest) examples in D are then selected. This set of examples is called the k nearest neighbors of d. d then takes the most frequent class among the k nearest neighbors. Note that k = 1 is usually not sufficient for determining the class of d due to noise and outliers in the data. A set of nearest neighbors is needed to accurately decide the class. The general kNN algorithm is given in Fig. 3.20. Algorithm kNN(D, d, k) 1 Compute the distance between d and every example in D; 2 Choose the k examples in D that are nearest to d, denote the set by P ( D); 3 Assign d the class that is the most frequent class in P (or the majority class). Fig. 3.20. The k-nearest neighbor algorithm The key component of a kNN algorithm is the distance/similarity function, which is chosen based on applications and the nature of the data. For relational data, the Euclidean distance is commonly used. For text documents, cosine similarity is a popular choice. We will introduce these distance functions and many others in the next chapter. The number of nearest neighbors k is usually determined by using a validation set, or through cross validation on the training data. That is, a 3.10 Ensemble of Classifiers 113 range of k values are tried, and the k value that gives the best accuracy on the validation set (or cross validation) is selected. Figure 3.21 illustrates the importance of choosing the right k. Example 18: In Fig. 3.21, we have two classes of data, positive (filled squares) and negative (empty circles). If 1-nearest neighbor is used, the test data point will be classified as negative, and if 2-nearest neighbors are used, the class cannot be decided. If 3-nearest neighbors are used, the class is positive as two positive examples are in the 3-nearest neighbors. 1-nearst neighbor 2-nearst neighbor 3-nearst neighbor Fig. 3.21. An illustration of k-nearest neighbor classification Despite its simplicity, researchers have showed that the classification accuracy of kNN can be quite strong and in many cases as accurate as those elaborated methods. For instance, it is showed in [574] that kNN performs equally well as SVM for some text classification tasks. kNN is also very flexible. It can work with any arbitrarily shaped decision boundaries. kNN is, however, slow at the classification time. Due to the fact that there is no model building, each test instance is compared with every training example at the classification time, which can be quite time consuming especially when the training set D and the test set are large. Another disadvantage is that kNN does not produce an understandable model. It is thus not applicable if an understandable model is required in the application. 3.10 Ensemble of Classifiers So far, we have studied many individual classifier building techniques. A natural question to ask is: can we build many classifiers and then combine them to produce a better classifier? Yes, in many cases. This section describes two well known ensemble techniques, bagging and boosting. In both these methods, many classifiers are built and the final classification decision for each test instance is made based on some forms of voting of the committee of classifiers. 114 3 Supervised Learning 3.10.1 Bagging Given a training set D with n examples and a base learning algorithm, bagging (for Bootstrap Aggregating) works as follows [63]: Training: 1. Create k bootstrap samples S1, S2, and Sk. Each sample is produced by drawing n examples at random from D with replacement. Such a sample is called a bootstrap replicate of the original training set D. On average, each sample Si contains 63.2% of the original examples in D, with some examples appearing multiple times. 2. Build a classifier based on each sample Si. This gives us k classifiers. All the classifiers are built using the same base learning algorithm. Testing: Classify each test (or new) instance by voting of the k classifiers (equal weights). The majority class is assigned as the class of the instance. Bagging can improve the accuracy significantly for unstable learning algorithms, i.e., a slight change in the training data resulting in a major change in the output classifier. Decision tree and rule induction methods are examples of unstable learning methods. k-nearest neighbor and naïve Bayesian methods are examples of stable techniques. For stable classifiers, Bagging may sometime degrade the accuracy. 3.10.2 Boosting Boosting is a family of ensemble techniques, which, like bagging, also manipulates the training examples and produces multiple classifiers to improve the classification accuracy [477]. Here we only describe the popular AdaBoost algorithm given in [186]. Unlike bagging, AdaBoost assigns a weight to each training example. Training: AdaBoost produces a sequence of classifiers (also using the same base learner). Each classifier is dependent on the previous one, and focuses on the previous one’s errors. Training examples that are incorrectly classified by the previous classifiers are given higher weights. Let the original training set D be {(x1, y1), (x2, y2), …, (xn, yn)}, where xi is an input vector, yi is its class label and yi Y (the set of class labels). With a weight attached to each example, we have, {(x1, y1, w1), (x2, y2, w2), …, (xn, yn, wn)}, and i wi = 1. The AdaBoost algorithm is given in Fig. 3.22. The algorithm builds a sequence of k classifiers (k is specified by the user) using a base learner, called BaseLeaner in line 3. Initially, the weight Bibliographic Notes AdaBoost(D, Y, BaseLeaner, k) 1. Initialize D1(wi) 1/n for all i; 2. for t = 1 to k do 3. ft BaseLearner(Dt); 4. et Dt ( wi ) ; 115 // initialize the weights // build a new classifier ft // compute the error of ft i: f t ( Dt ( x i )) yi 5. 6. 7. 8. 9. 10 if et > ½ then k k – 1; exit-loop else et / (1 et); t Dt+1(wi) // if the error is too large, // remove the iteration and // exit Dt(wi) if f t ( Dt (xi )) otherwise t 1 11. Dt 1 ( wi ) Dt+1(wi) n i yi ; // update the weights // normalize the weights D ( wi ) 1 t1 12. endif 13. endfor 14. f final (x) arg max yY log t: f t ( x ) y 1 // the final output classifier t Fig. 3.22. The AdaBoost algorithm for each training example is 1/n (line 1). In each iteration, the training data set becomes Dt, which is the same as D, but with different weights. Each iteration builds a new classifier ft (line 3). The error of ft is calculated in line 4. If it is too large, delete the iteration and exit (lines 5–7). Lines 9–11 update and normalize the weights for building the next classifier. Testing: For each test case, the results of the series of classifiers are combined to determine the final class of the test case, which is shown in line 14 of Fig. 3.22 (a weighted voting). Boosting works better than bagging in most cases as shown in [454]. It also tends to improve performance more when the base learner is unstable. Bibliographic Notes Supervised learning has been studied extensively by the machine learning community. The book by Mitchell [385] covers most learning techniques and is easy to read. Duda et al.’s pattern classification book is also a great 116 3 Supervised Learning reference [155]. Most data mining books have one or two chapters on supervised learning, e.g., those by Han and Kamber [218], Hand et al. [221], Tan et al. [512], and Witten and Frank [549]. For decision tree induction, Quinlan’s book [453] has all the details and the code of his popular decision tree system C4.5. Other well-known systems include CART by Breiman et al. [62] and CHAD by Kass [270]. Scaling up of decision tree algorithms was also studied in several papers. These algorithms can have the data on disk, and are thus able to run with huge data sets. See [195] for an algorithm and also additional references. Rule induction algorithms generate rules directly from the data. Wellknown systems include AQ by Michalski et al. [381], CN2 by Clark and Niblett [104], FOIL by Quinlan [452], FOCL by Pazzani et al. [438], IREP by Furnkranz and Widmer [189], and RIPPER by Cohen [106]. Using association rules to build classifiers was proposed by Liu et al. in [343], which also reported the CBA system. CBA selects a small subset of class association rules as the classifier. Other classifier building techniques include combining multiple rules by Li et al. [328], using rules as features by Meretakis and Wüthrich [379], Antonie and Zaiane [23], Deshpande and Karpis [131], Jindal and Liu [255], and Lesh et al. [314], generating a subset of rules by Cong et al. [112, 113], Wang et al. [536], Yin and Han [578], and Zaki and Aggarwal [587]. Other systems include those by Dong et al. [149], Li et al. [319, 320], Yang et al. [570], etc. The naïve Bayesian classification model described in Sect. 3.6 is based on the papers by Domingos and Pazzani [148], Kohavi et al. [285] and Langley et al [301]. The naïve Bayesian classification for text discussed in Sect. 3.7 is based on the multinomial formulation given by McCallum and Nigam [365]. This model was also used earlier by Lewis and Gale [317], Li and Yamanishi [318], and Nigam et al. [413]. Another formulation of naïve Bayes is based on the multivariate Bernoulli model, which was used in Lewis [316], and Robertson and Sparck-Jones [464]. Support vector machines (SVM) was first introduced by Vapnik and his colleagues in 1992 [59]. Further details were given in his 1995 book [525]. Two other books on SVM and kernel methods are those by Cristianini and Shawe-Taylor [118] and Scholkopf and Smola [479]. The discussion of SVM in this chapter is heavily influenced by Cristianini and ShaweTaylor’s book and the tutorial paper by Burges [74]. Two popular SVM systems are SVMLight (available at http://svmlight.joachims.org/) and LIBSVM (available at http://www.csie.ntu.edu.tw/~cjlin/libsvm/). Existing classifier ensemble methods include bagging by Breiman [63], boosting by Schapire [477] and Freund and Schapire [186], random forest also by Breiman [65], stacking by Wolpert [552], random trees by Fan [169], and many others. 4 Unsupervised Learning Supervised learning discovers patterns in the data that relate data attributes to a class attribute. These patterns are then utilized to predict the values of the class attribute of future data instances. These classes indicate some real-world predictive or classification tasks such as determining whether a news article belongs to the category of sports or politics, or whether a patient has a particular disease. However, in some other applications, the data have no class attributes. The user wants to explore the data to find some intrinsic structures in them. Clustering is one technology for finding such structures. It organizes data instances into similarity groups, called clusters such that the data instances in the same cluster are similar to each other and data instances in different clusters are very different from each other. Clustering is often called unsupervised learning, because unlike supervised learning, class values denoting an a priori partition or grouping of the data are not given. Note that according to this definition, we can also say that association rule mining is an unsupervised learning task. However, due to historical reasons, clustering is closely associated and even synonymous with unsupervised learning while association rule mining is not. We follow this convention, and describe some main clustering techniques in this chapter. Clustering has been shown to be one of the most commonly used data analysis techniques. It also has a long history, and has been used in almost every field, e.g., medicine, psychology, botany, sociology, biology, archeology, marketing, insurance, library science, etc. In recent years, due to the rapid increase of online documents and the expansion of the Web, text document clustering too has become a very important task. In Chap. 12, we will also see that clustering is very useful in Web usage mining. 4.1 Basic Concepts Clustering is the process of organizing data instances into groups whose members are similar in some way. A cluster is therefore a collection of data instances which are “similar” to each other and are “dissimilar” to 118 4 Unsupervised Learning data instances in other clusters. In the clustering literature, a data instance is also called an object as the instance may represent an object in the realworld. It is also called a data point as it can be seen as a point in an rdimension space, where r is the number of attributes in the data. Fig. 4.1 shows a 2-dimensional data set. We can clearly see three groups of data points. Each group is a cluster. The task of clustering is to find the three clusters hidden in the data. Although it is easy for a human to visually detect clusters in a 2-dimensional or even 3-demensional space, it becomes very hard, if not impossible, to detect clusters visually as the number of dimensions increases. Additionally, in many applications, clusters are not as clear-cut or well separated as the three clusters in Fig. 4.1. Automatic techniques are thus needed for clustering. Fig. 4.1. Three natural groups or clusters of data points After seeing the example in Fig. 4.1, you may ask the question: What is clustering for? To answer it, let us see some application examples from different domains. Example 1: A company wants to conduct a marketing campaign to promote its products. The most effective strategy is to design a set of personalized marketing materials for each individual customer according to his/her profile and financial situation. However, this is too expensive for a large number of customers. At the other extreme, the company designs only one set of marketing materials to be used for all customers. This onesize-fits-all approach, however, may not be effective. The most costeffective approach is to segment the customers into a small number of groups according to their similarities and design some targeted marketing materials for each group. This segmentation task is commonly done using clustering algorithms, which partition customers into similarity groups. In marketing research, clustering is often called segmentation. Example 2: A company wants to produce and sell T-shirts. Similar to the case above, on one extreme, for each customer it can measure his/her size and have a T-shirt tailor-made for him/her. Obviously, this T-shirt is going to be expensive. On the other extreme, only one size of T-shirts is made. 4.1 Basic Concepts 119 Since this size may not fit most people, the company might not be able to sell as many T-shirts. Again, the most cost effective way is to group people based on their sizes and make a different generalized size of T-shirts for each group. This is why we see small, medium and large size T-shirts in shopping malls, and seldom see T-shirts with only a single size. The method used to group people according to their sizes is clustering. The process is usually as follows: The T-shirt manufacturer first samples a large number of people and measure their sizes to produce a measurement database. It then clusters the data, which partitions the data into some similarity subsets, i.e., clusters. For each cluster, it computes the average of the sizes and then uses the average to mass-produce T-shirts for all people of similar size. Example 3: Everyday, news agencies around the world generate a large number of news articles. If a Web site wants to collect these news articles to provide an integrated news service, it has to organize the collected articles according to some topic hierarchy. The question is: What should the topics be, and how should they be organized? One possibility is to employ a group of human editors to do the job. However, the manual organization is costly and very time consuming, which makes it unsuitable for news and other time sensitive information. Throwing all the news articles to the readers with no organization is clearly not an option. Although classification is able to classify news articles according to predefined topics, it is not applicable here because classification needs training data, which have to be manually labeled with topic classes. Since news topics change constantly and rapidly, the training data would need to change constantly as well, which is infeasible via manual labeling. Clustering is clearly a solution for this problem because it automatically groups a stream of news articles based on their content similarities. Hierarchical clustering algorithms can also organize documents hierarchically, i.e., each topic may contain sub-topics and so on. Topic hierarchies are particularly useful for texts. The above three examples indicate two types of clustering, partitional and hierarchical. Indeed, these are the two most important types of clustering approaches. We will study some specific algorithms of these two types of clustering. Our discussion and examples above also indicate that clustering needs a similarity function to measure how similar two data points (or objects) are, or alternatively a distance function to measure the distance between two data points. We will use distance functions in this chapter. The goal of clustering is thus to discover the intrinsic grouping of the input data through the use of a clustering algorithm and a distance function. 120 1 2 3 4 5 6 7 8 4 Unsupervised Learning Algorithm k-means(k, D) choose k data points as the initial centroids (cluster centers) repeat for each data point x D do compute the distance from x to each centroid; // a centroid represents a cluster assign x to the closest centroid endfor re-compute the centroid using the current cluster memberships until the stopping criterion is met Fig. 4.2. The k-means algorithm 4.2 K-means Clustering The k-means algorithm is the best known partitional clustering algorithm. It is perhaps also the most widely used among all clustering algorithms due to its simplicity and efficiency. Given a set of data points and the required number of k clusters (k is specified by the user), this algorithm iteratively partitions the data into k clusters based on a distance function. 4.2.1 K-means Algorithm Let the set of data points (or instances) D be {x1, x2, …, xn}, r , and r where xi = (xi1, xi2, …, xir) is a vector in a real-valued space X is the number of attributes in the data (or the number of dimensions of the data space). The k-means algorithm partitions the given data into k clusters. Each cluster has a cluster center, which is also called the cluster centroid. The centroid, usually used to represent the cluster, is simply the mean of all the data points in the cluster, which gives the name to the algorithm, i.e., since there are k clusters, thus k means. Figure 4.2 gives the kmeans clustering algorithm. At the beginning, the algorithm randomly selects k data points as the seed centroids. It then computes the distance between each seed centroid and every data point. Each data point is assigned to the centroid that is closest to it. A centroid and its data points therefore represent a cluster. Once all the data points in the data are assigned, the centroid for each cluster is re-computed using the data points in the current cluster. This process repeats until a stopping criterion is met. The stopping (or convergence) criterion can be any one of the following: 4.2 K-means Clustering 121 1. no (or minimum) re-assignments of data points to different clusters. 2. no (or minimum) change of centroids. 3. minimum decrease in the sum of squared error (SSE), k dist (x, m j ) 2 , SSE (1) j 1 x Cj where k is the number of required clusters, Cj is the jth cluster, mj is the centroid of cluster Cj (the mean vector of all the data points in Cj), and dist(x, mj) is the distance between data point x and centroid mj. The k-means algorithm can be used for any application data set where the mean can be defined and computed. In Euclidean space, the mean of a cluster is computed with: 1 | C j | xi mj xi , (2) Cj where |Cj| is the number of data points in cluster Cj. The distance from a data point xi to a cluster mean (centroid) mj is computed with dist (x i , m j ) || x i m j || ( xi1 m j1 ) (3) 2 ( xi 2 m j 2 ) 2 ... ( xir 2 m jr ) . Example 4: Figure 4.3(A) shows a set of data points in a 2-dimensional space. We want to find 2 clusters from the data, i.e., k = 2. First, two data points (each marked with a cross) are randomly selected to be the initial centroids (or seeds) shown in Fig. 4.3(A). The algorithm then goes to the first iteration (the repeat-loop). Iteration 1: Each data point is assigned to its closest centroid to form 2 clusters. The resulting clusters are given in Fig. 4.3(B). Then the centroids are re-computed based on the data points in the current clusters (Fig. 4.3(C)). This leads to iteration 2. Iteration 2: Again, each data point is assigned to its closest new centroid to form two new clusters shown in Fig. 4.3(D). The centroids are then recomputed. The new centroids are shown in Fig. 4.3(E). Iteration 3: The same operations are performed as in the first two iterations. Since there is no re-assignment of data points to different clusters in this iteration, the algorithm ends. The final clusters are those given in Fig. 4.3(G). The set of data points in each cluster and its centroid are output to the user. 122 4 Unsupervised Learning + + (A). Random selection of k seeds (or centroids) + + + + Iteration 1: (B). Cluster assignment + + + Iteration 2: (D). Cluster assignment + (C). Re-compute centroids + Iteration 3: (F). Cluster assignment + (E). Re-compute centroids + + (G). Re-compute centroids Fig. 4.3. The working of the k-means algorithm through an example One problem with the k-means algorithm is that some clusters may become empty during the clustering process since no data point is assigned to them. Such clusters are called empty clusters. To deal with an empty cluster, we can choose a data point as the replacement centroid, e.g., a data point that is furthest from the centroid of a large cluster. If the sum of the squared error (SSE) is used as the stopping criterion, the cluster with the largest squared error may be used to find another centroid. 4.2 K-means Clustering 4.2.2 123 Disk Version of the K-means Algorithm The k-means algorithm may be implemented in such a way that it does not need to load the entire data set into the main memory, which is useful for large data sets. Notice that the centroids for the k clusters can be computed incrementally in each iteration because the summation in Equation (2) can be calculated separately first. During the clustering process, the number of data points in each cluster can be counted incrementally as well. This gives us a disk based implementation of the algorithm (Fig. 4.4), which produces exactly the same clusters as that in Fig. 4.2, but with the data on disk. In each for-loop, the algorithm simply scans the data once. The whole clustering process thus scans the data t times, where t is the number of iterations before convergence, which is usually not very large (< 50). In applications, it is quite common to set a limit on the number of iterations because later iterations typically result in only minor changes to the clusters. Thus, this algorithm may be used to cluster large data sets which cannot be loaded into the main memory. Although there are several special algorithms that scale-up clustering algorithms to large data sets, they all require sophisticated techniques. 1 2 3 4 5 6 Algorithm disk-k-means(k, D) Choose k data points as the initial centriods mj, j = 1, …, k; repeat initialize sj 0, j = 1, …, k; // 0 is a vector with all 0’s // nj is the number of points in cluster j initialize nj 0, j = 1, …, k; for each data point x D do j arg min dist ( x , m i ); i {1, 2 ,... k } 7 assign x to the cluster j; 8 sj sj + x ; 9 nj nj + 1; 10 endfor 11 mj sj/nj, j = 1, …, k; 12 until the stopping criterion is met Fig. 4.4. A simple disk version of the k-means algorithm Let us give some explanations of this algorithm. Line 1 does exactly the same thing as the algorithm in Fig. 4.2. Line 3 initializes vector sj which is used to incrementally compute the sum in Equation (2) (line 8). Line 4 initializes nj which records the number of data points assigned to cluster j (line 9). Lines 6 and 7 perform exactly the same tasks as lines 4 and 5 in the original algorithm in Fig. 4.2. Line 11 re-computes the centroids, 124 4 Unsupervised Learning which are used in the next iteration. Any of the three stopping criteria may be used here. If the sum of squared error is applied, we can modify the algorithm slightly to compute the sum of square error incrementally. 4.2.3 Strengths and Weaknesses The main strengths of the k-means algorithm are its simplicity and efficiency. It is easy to understand and easy to implement. Its time complexity is O(tkn), where n is the number of data points, k is the number of clusters, and t is the number of iterations. Since both k and t are normally much smaller than n. The k-means algorithm is considered a linear algorithm in the number of data points. The weaknesses and ways to address them are as follows: 1. The algorithm is only applicable to data sets where the notion of the mean is defined. Thus, it is difficult to apply to categorical data sets. There is, however, a variation of the k-means algorithm called k-modes, which clusters categorical data. The algorithm uses the mode instead of the mean as the centroid. Assuming that the data instances are described by r categorical attributes, the mode of a cluster Cj is a tuple mj = (mj1, mj2, …, mjr) where mji is the most frequent value of the ith attribute of the data instances in cluster Cj. The similarity (or distance) between a data instance and a mode is the number of values that they match (or do not match). 2. The user needs to specify the number of clusters k in advance. In practice, several k values are tried and the one that gives the most desirable result is selected. We will discuss the evaluation of clusters later. 3. The algorithm is sensitive to outliers. Outliers are data points that are very far away from other data points. Outliers could be errors in the data recording or some special data points with very different values. For example, in an employee data set, the salary of the Chief-ExecutiveOfficer (CEO) of the company may be considered as an outlier because its value could be many times larger than everyone else. Since the kmeans algorithm uses the mean as the centroid of each cluster, outliers may result in undesirable clusters as the following example shows. Example 5: In Fig. 4.5(A), due to an outlier data point, the two resulting clusters do not reflect the natural groupings in the data. The ideal clusters are shown in Fig. 4.5(B). The outlier should be identified and reported to the user. There are several methods for dealing with outliers. One simple method is to remove some data points in the clustering process that are 4.2 K-means Clustering 125 much further away from the centroids than other data points. To be safe, we may want to monitor these possible outliers over a few iterations and then decide whether to remove them. It is possible that a very small cluster of data points may be outliers. Usually, a threshold value is used to make the decision. outlier + + (A): Undesirable clusters outlier + + (B): Ideal clusters Fig. 4.5. Clustering with and without the effect of outliers Another method is to perform random sampling. Since in sampling we only choose a small subset of the data points, the chance of selecting an outlier is very small. We can use the sample to do a pre-clustering and then assign the rest of the data points to these clusters, which may be done in any of the three ways below: Assign each remaining data point to the centroid closest to it. This is the simplest method. Use the clusters produced from the sample to perform supervised learning (classification). Each cluster is regarded as a class. The clustered sample is thus treated as the training data for learning. The resulting classifier is then applied to classify the remaining data points into appropriate classes or clusters. Use the clusters produced from the sample as seeds to perform semisupervised learning. Semi-supervised learning is a new learning model that learns from a small set of labeled examples (with classes) and a large set of unlabeled examples (without classes). In our case, the clustered sample data are used as the labeled set and the remaining data points are used as the unlabeled set. The results of the learn- 126 4 Unsupervised Learning ing naturally cluster all the remaining data points. We will study this technique in the next chapter. 4. The algorithm is sensitive to initial seeds, which are the initially selected centroids. Different initial seeds may result in different clusters. Thus, if the sum of squared error is used as the stopping criterion, the algorithm only achieves local optimal. The global optimal is computationally infeasible for large data sets. Example 6: Figure 4.6 shows the clustering process of a 2-dimensional data set. The goal is to find two clusters. The randomly selected initial seeds are marked with crosses in Fig. 4.6(A). Figure 4.6(B) gives the clustering result of the first iteration. Figure 4.6(C) gives the result of the second iteration. Since there is no re-assignment of data points, the algorithm stops. + + (A). Random selection of seeds (centroids) + + + + (B). Iteration 1 (C). Iteration 2 Fig. 4.6. Poor initial seeds (centroids) If the initial seeds are different, we may obtain entirely different clusters as Fig. 4.7 shows. Figure 4.7 uses the same data as Fig. 4.6, but different initial seeds (Fig. 4.7(A)). After two iterations, the algorithm ends, and the final clusters are given in Fig. 4.7(C). These two clusters are more reasonable than the two clusters in Fig. 4.6(C), which indicates that the choice of the initial seeds in Fig. 4.6(A) is poor. To select good initial seeds, researchers have proposed several methods. One simple method is to first compute the mean m (the centroid) of the entire data set (any random data point rather than the mean can be 4.2 K-means Clustering 127 used as well). Then the first seed data point x1 is selected to be the furthest from the mean m. The second data point x2 is selected to be the furthest from x1. Each subsequent data point xi is selected such that the sum of distances from xi to those already selected data points is the largest. However, if the data has outliers, the method will not work well. To deal with outliers, again, we can randomly select a small sample of the data and perform the same operation on the sample. As we discussed above, since the number of outliers is small, the chance that they show up in the sample is very small. + + (A). Random selection of k seeds (centroids) + + (B). Iteration 1 + + (C). Iteration 2 Fig. 4.7. Good initial seeds (centroids) Another method is to sample the data and use the sample to perform hierarchical clustering, which we will discuss in Sect. 4.4. The centroids of the resulting k clusters are used as the initial seeds. Yet another approach is to manually select seeds. This may not be a difficult task for text clustering applications because it is easy for human users to read some documents and pick some good seeds. These seeds may help improve the clustering result significantly and also enable the system to produce clusters that meet the user’s needs. 5. The k-means algorithm is not suitable for discovering clusters that are not hyper-ellipsoids (or hyper-spheres). Example 7: Figure 4.8(A) shows a 2-dimensional data set. There are two irregular shaped clusters. However, the two clusters are not hyper- 128 4 Unsupervised Learning ellipsoids, which means that the k-means algorithm will not be able to find them. Instead, it may find the two clusters shown in Fig. 4.8(B). The question is: are the two clusters in Fig. 4.8(B) necessarily bad? The answer is no. It depends on the application. It is not true that a clustering algorithm that is able to find arbitrarily shaped clusters is always better. We will discuss this issue in Sect. 4.3.2. + + (A): Two natural clusters (B): k-means clusters Fig. 4.8. Natural (but irregular) clusters and k-means clusters Despite these weaknesses, k-means is still the most popular algorithm in practice due to its simplicity, efficiency and the fact that other clustering algorithms have their own lists of weaknesses. There is no clear evidence showing that any other clustering algorithm performs better than the kmeans algorithm in general, although it may be more suitable for some specific types of data or applications than k-means. Note also that comparing different clustering algorithms is a very difficult task because unlike supervised learning, nobody knows what the correct clusters are, especially in high dimensional spaces. Although there are several cluster evaluation methods, they all have drawbacks. We will discuss the evaluation issue in Sect. 4.9. 4.3 Representation of Clusters Once a set of clusters is found, the next task is to find a way to represent the clusters. In some applications, outputting the set of data points that makes up the cluster to the user is sufficient. However, in other applications that involve decision making, the resulting clusters need to be represented in a compact and understandable way, which also facilitates the evaluation of the resulting clusters. 4.3 Representation of Clusters 4.3.1 129 Common Ways of Representing Clusters There are three main ways to represent clusters: 1. Use the centroid of each cluster to represent the cluster. This is the most popular way. The centroid tells where the center of the cluster is. One may also compute the radius and standard deviation of the cluster to determine the spread in each dimension. The centroid representation alone works well if the clusters are of the hyper-spherical shape. If clusters are elongated or are of other shapes, centroids may not be suitable. 2. Use classification models to represent clusters. In this method, we treat each cluster as a class. That is, all the data points in a cluster are regarded as having the same class label, e.g., the cluster ID. We then run a supervised learning algorithm on the data to find a classification model. For example, we may use the decision tree learning to distinguish the clusters. The resulting tree or set of rules provide an understandable representation of the clusters. Figure 4.9 shows a partitioning produced by a decision tree algorithm. The original clustering gave three clusters. Data points in cluster 1 are represented by 1’s, data points in cluster 2 are represented by 2’s, and data points in cluster 3 are represented by 3’s. We can see that the three clusters are separated and each can be represented with a rule. x2 cluster 1 x > 2, y > 1.5 cluster 2 x > 2, y 1.5 cluster 3 y 1.5 2 22 222 22 2 2 2 2 1 1 11 1 1 11 1 1 111 3 33 3 3 3 33 3 2 x Fig. 4.9. Description of clusters using rules We make two remarks about this representation method: The partitioning in Fig. 4.9 is an ideal case as each cluster is represented by a single rectangle (or rule). However, in most applications, the situation may not be so ideal. A cluster may be split into a few 130 4 Unsupervised Learning hyper-rectangles or rules. However, there is usually a dominant or large rule which covers most of the data points in the cluster. One can use the set of rules to evaluate the clusters to see whether they conform to some existing domain knowledge or intuition. 3. Use frequent values in each cluster to represent it. This method is mainly for clustering of categorical data (e.g., in the k-modes clustering). It is also the key method used in text clustering, where a small set of frequent words in each cluster is selected to represent the cluster. 4.3.2 Clusters of Arbitrary Shapes Hyper-elliptical and hyper-spherical clusters are usually easy to represent, using their centroids together with spreads (e.g., standard deviations), rules, or a combination of both. However, other arbitrary shaped clusters, like the natural clusters shown in Fig. 4.8(A), are hard to represent especially in high dimensional spaces. A common criticism about an algorithm like k-means is that it is not able to find arbitrarily shaped clusters. However, this criticism may not be as bad as it sounds because whether one type of clustering is desirable or not depends on the application. Let us use the natural clusters in Fig. 4.8(A) to discuss this issue together with an artificial application. Example 8: Assume that the data shown in Fig. 4.8(A) is the measurement data of people’s physical sizes. We want to group people based on their sizes into only two groups in order to mass-produce T-shirts of only 2 sizes (say large and small). Even if the measurement data indicate two natural clusters as in Fig. 4.8(A), it is difficult to use the clusters because we need centroids of the clusters to design T-shirts. The clusters in Fig. 4.8(B) are in fact better because they provide us the centroids that are representative of the surrounding data points. If we use the centroids of the two natural clusters as shown in Fig. 4.10 to make T-shirts, it is clearly inappropriate because they are too near to each other in this case. In general, it does not make sense to define the concept of center or centroid for an irregularly shaped cluster. Note that clusters of arbitrary shapes can be found by neighborhood search algorithms such as some hierarchical clustering methods (see the next section), and density-based clustering methods [164]. Due to the difficulty of representing an arbitrarily shaped cluster, an algorithm that finds such clusters may only output a list of data points in each cluster, which are not as easy to use. These kinds of clusters are more useful in spatial and image processing applications, but less useful in others. 4.4 Hierarchical Clustering 131 + + Fig. 4.10. Two natural clusters and their centroids 4.4 Hierarchical Clustering Hierarchical clustering is another major clustering approach. It has a number of desirable properties which make it popular. It clusters by producing a nested sequence of clusters like a tree (also called a dendrogram). Singleton clusters (individual data points) are at the bottom of the tree and one root cluster is at the top, which covers all data points. Each internal cluster node contains child cluster nodes. Sibling clusters partition the data points covered by their common parent. Figure 4.11 shows an example. 9 8 6 1 7 2 3 4 5 Fig. 4.11. An illustration of hierarchical clustering At the bottom of the tree, there are 5 clusters (5 data points). At the next level, cluster 6 contains data points 1 and 2, and cluster 7 contains data points 4 and 5. As we move up the tree, we have fewer and fewer clusters. Since the whole clustering tree is stored, the user can choose to view clusters at any level of the tree. 132 4 Unsupervised Learning There are two main types of hierarchical clustering methods: Agglomerative (bottom up) clustering: It builds the dendrogram (tree) from the bottom level, and merges the most similar (or nearest) pair of clusters at each level to go one level up. The process continues until all the data points are merged into a single cluster (i.e., the root cluster). Divisive (top down) clustering: It starts with all data points in one cluster, the root. It then splits the root into a set of child clusters. Each child cluster is recursively divided further until only singleton clusters of individual data points remain, i.e., each cluster with only a single point. Agglomerative methods are much more popular than divisive methods. We will focus on agglomerative hierarchical clustering. The general agglomerative algorithm is given in Fig. 4.12. 1 2 2 3 4 5 12 Algorithm Agglomerative(D) Make each data point in the data set D a cluster, Compute all pair-wise distances of x1, x2, …, xn D; repeat find two clusters that are nearest to each other; merge the two clusters form a new cluster c; compute the distance from c to all other clusters; until there is only one cluster left Fig. 4.12. The agglomerative hierarchical clustering algorithm Example 9: Figure 4.13 illustrates the working of the algorithm. The data points are in a 2-dimensional space. Figure 4.13(A) shows the sequence of nested clusters, and Fig. 4.13(B) gives the dendrogram. 4 4 p1 3 p2 1 p3 3 2 p4 p5 1 p1 (A). Nested clusters 2 p2 p3 p4 (B) Dendrogram Fig. 4.13. The working of an agglomerative hierarchical clustering algorithm p5 4.4 Hierarchical Clustering 133 Unlike the k-means algorithm, which uses only the centroids in distance computation, hierarchical clustering may use anyone of several methods to determine the distance between two clusters. We introduce them next. 4.4.1 Single-Link Method In single-link (or single linkage) hierarchical clustering, the distance between two clusters is the distance between two closest data points in the two clusters (one data point from each cluster). In other words, the singlelink clustering merges the two clusters in each step whose two nearest data points (or members) have the smallest distance, i.e., the two clusters with the smallest minimum pair-wise distance. The single-link method is suitable for finding non-elliptical shape clusters. However, it can be sensitive to noise in the data, which may cause the chain effect and produce straggly clusters. Figure 4.14 illustrates this situation. The noisy data points (represented with filled circles) in the middle connect two natural clusters and split one of them. Fig. 4.14. The chain effect of the single-link method With suitable data structures, single-link hierarchical clustering can be done in O(n2) time, where n is the number of data points. This is much slower than the k-means method, which performs clustering in linear time. 4.4.2 Complete-Link Method In complete-link (or complete linkage) clustering, the distance between two clusters is the maximum of all pair-wise distances between the data points in the two clusters. In other words, the complete-link clustering merges the two clusters in each step whose two furthest data points have the smallest distance, i.e., the two clusters with the smallest maximum pair-wise distance. Figure 4.15 shows the clusters produced by completelink clustering using the same data as in Fig. 4.14. 134 4 Unsupervised Learning Fig. 4.15. Clustering using the complete-link method Although the complete-link method does not have the problem of chain effects, it can be sensitive to outliers. Despite this limitation, it has been observed that the complete-link method usually produces better clusters than the single-link method. The worse case time complexity of the complete-link clustering is O(n2log n), where n is the number of data points. 4.4.3 Average-Link Method This is a compromise between the sensitivity of complete-link clustering to outliers and the tendency of single-link clustering to form long chains that do not correspond to the intuitive notion of clusters as compact, spherical objects. In this method, the distance between two clusters is the average distance of all pair-wise distances between the data points in two clusters. The time complexity of this method is also O(n2log n). Apart from the above three popular methods, there are several others. The following two methods are also commonly used: Centroid method: In this method, the distance between two clusters is the distance between their centroids. Ward's method: In this method, the distance between two clusters is defined as the increase in the sum of squared error (distances) from that of two clusters to that of one merged cluster. Thus, the clusters to be merged in the next step are the ones that will increase the sum the least. Recall that the sum of squared error (SSE) is one of the measures used in the k-means clustering (Equation (1)). 4.4.4. Strengths and Weaknesses Hierarchical clustering has several advantages compared to the k-means and other partitioning clustering methods. It is able to take any form of distance or similarity function. Moreover, unlike the k-means algorithm which only gives k clusters at the end, the hierarchy of clusters from hier- 4.5 Distance Functions 135 archical clustering enables the user to explore clusters at any level of detail (or granularity). In many applications, this resulting hierarchy can be very useful in its own right. For example, in text document clustering, the cluster hierarchy may represent a topic hierarchy in the documents. Some studies have shown that agglomerative hierarchical clustering often produces better clusters than the k-means method. It can also find clusters of arbitrary shapes, e.g., using the single-link method. Hierarchical clustering also has several weaknesses. As we discussed with the individual methods, the single-link method may suffer from the chain effect, and the complete-link method is sensitive to outliers. The main shortcomings of all hierarchical clustering methods are their computation complexities and space requirements, which are at least quadratic. Compared to the k-means algorithm, this is very inefficient and not practical for large data sets. One can use sampling to deal with the problems. A small sample is taken to do clustering and then the rest of the data points are assigned to each cluster either by distance comparison or by supervised learning (see Sect. 4.3.1). Some scale-up methods may also be applied to large data sets. The main idea of the scale-up methods is to find many small clusters first using an efficient algorithm, and then to use the centroids of these small clusters to represent the clusters to perform the final hierarchical clustering (see the BIRCH method in [610]). 4.5 Distance Functions Distance or similarity functions play a central role in all clustering algorithms. Numerous distance functions have been reported in the literature and used in applications. Different distance functions are also used for different types of attributes (also called variables). 4.5.1 Numeric Attributes The most commonly used distance functions for numeric attributes are the Euclidean distance and Manhattan (city block) distance. Both distance measures are special cases of a more general distance function called the Minkowski distance. We use dist(xi, xj) to denote the distance between two data points of r dimensions. The Minkowski distance is: dist (x i , x j ) (| xi1 x j1 | h where h is a positive integer. | xi 2 x j2 | h ... | xir 1 hh x jr | ) , (4) 136 4 Unsupervised Learning If h = 2, it is the Euclidean distance, dist (x i , x j ) ( xi1 x j1 ) 2 ( xi 2 x j 2 ) 2 ... ( xir x jr ) 2 . (5) If h = 1, it is the Manhattan distance, dist (x i , x j ) | xi1 x j1 | | xi 2 x j 2 | ... | xir x jr | . (6) Other common distance functions include: Weighted Euclidean distance: A weight is associated with each attribute to express its importance in relation to other attributes. dist (x i , x j ) w1 ( xi1 x j1 ) 2 x j 2 ) 2 ... wr ( xir w2 ( xi 2 x jr ) 2 . (7) Squared Euclidean distance: the standard Euclidean distance is squared in order to place progressively greater weights on data points that are further apart. The distance is dist (x i , x j ) ( xi1 x j1 ) 2 ( xi 2 x j 2 ) 2 ... ( xir x jr ) 2 . (8) Chebychev distance: This distance measure is appropriate in cases where one wants to define two data points as “different” if they are different on any one of the attributes. The Chebychev distance is dist (x i , x j ) 4.5.2 max(| xi1 x j1 |, | xi 2 x j 2 |, ..., | xir x jr |). (9) Binary and Nominal Attributes The above distance measures are only appropriate for numeric attributes. For binary and nominal attributes (also called unordered categorical attributes), we need different functions. Let us discuss binary attributes first. A binary attribute has two states or values, usually represented by 1 and 0. The two states have no numerical ordering. For example, Gender has two values, male and female, which have no ordering relations but are just different. Existing distance functions for binary attributes are based on the proportion of value matches in two data points. A match means that, for a particular attribute, both data points have the same value. It is easy to use a confusion matrix to introduce these measures. Given the ith and jth data points, xi and xj, we can construct the confusion matrix in Fig. 4.16. To give the distance functions, we further divide binary attributes into symmetric and asymmetric attributes. For different types of attributes, different distance functions need to be used [271]: 4.5 Distance Functions 137 Data point xj 1 0 1 a b a+b 0 c d c+d a+c Data point xi b+d a+b+c+d a: the number of attributes with the value of 1 for both data points. b: the number of attributes for which xif = 1 and xjf = 0, where xif (xjf) is the value of the fth attribute of the data point xi (xj). c: the number of attributes for which xif = 0 and xjf = 1. d: the number of attributes with the value of 0 for both data points. Fig. 4.16. Confusion matrix of two data points with only binary attributes Symmetric attributes: A binary attribute is symmetric if both of its states (0 and 1) have equal importance, and carry the same weight, e.g., male and female of the attribute Gender. The most commonly used distance function for symmetric attributes is the simple matching distance, which is the proportion of mismatches (Equation (10)) of their values. We assume that every attribute in the data set is a symmetric attribute. dist ( xi , x j ) bc abcd (10) We can also weight some components in Equation (10) according to application needs. For example, we may want mismatches to carry twice the weight of matches, or vice versa: dist (x i , x j ) 2(b c) a d 2(b c) (11) dist (x i , x j ) bc 2(a d ) b c (12) Example 10: Given the following two data points, where each attribute is a symmetric binary attribute, x1 1 1 1 0 1 0 0 x2 0 1 1 0 0 1 0 the distance computed based on the simple matching distance is 138 4 Unsupervised Learning dist (x i , x j ) 21 2212 3 7 0.429. (13) Asymmetric attributes: A binary attribute is asymmetric if one of the states is more important or valuable than the other. By convention, we use state 1 to represent the more important state, which is typically the rare or infrequent state. The most commonly used distance measure for asymmetric attributes is the Jaccard distance: bc . abc dist (x i , x j ) (14) Similarly, we can vary the Jaccard distance by giving more weight to (b+c) or more weight to a to express different emphases. dist (x i , x j ) 2(b c) . a 2(b c) (15) dist (x i , x j ) bc . 2a b c (16) Note that there is also a Jaccard coefficient, which measures similarity (rather than distance) and is defined as a / (a+b+c). For general nominal attributes with more than two states or values, the commonly used distance measure is also based on the simple matching distance. Given two data points xi and xj, let the number of attributes be r, and the number of values that match in xi and xj be q: dist (x i , x j ) rq . r (17) As that for binary attributes, we can give higher weights to different components in Equation (17) according to different application characteristics. 4.5.3 Text Documents Although a text document consists of a sequence of sentences and each sentence consists of a sequence of words, a document is usually considered as a “bag” of words in document clustering. The sequence and the position information of words are ignored. Thus a document can be represented as a vector just like a normal data point. However, we use similarity to compare two documents rather than distance. The most commonly used simi- 4.6 Data Standardization 139 larity function is the cosine similarity. We will study this similarity measure in Sect. 6.2.2 when we discuss information retrieval and Web search. 4.6 Data Standardization One of the most important steps in data pre-processing for clustering is to standardize the data. For example, using the Euclidean distance, standardization of attributes is highly recommended so that all attributes can have equal impact on the distance computation. This is to avoid obtaining clusters that are dominated by attributes with the largest amounts of variation. Example 11: In a 2-dimensional data set, the value range of one attribute is from 0 to 1, while the value range of the other attribute is from 0 to 1000. Consider the following pair of data points xi: (0.1, 20) and xj: (0.9, 720). The Euclidean distance between the two points is dist (xi , x j ) (0.9 0.1) 2 (720 20) 2 700.000457, (18) which is almost completely dominated by (720 20) = 700. To deal with the problem, we standardize the attributes, e.g., to force the attributes to have a common value range. If both attributes are forced to have a scale within the range 0 1, the values 20 and 720 become 0.02 and 0.72. The distance on the first dimension becomes 0.8 and the distance on the second dimension 0.7, which are more equitable. Then, dist(xi, xj) = 1.063. This example shows that standardizing attributes is important. In fact, different types of attributes require different treatments. We list these treatments below. Interval-scaled attributes: These are numeric/continuous attributes. Their values are real numbers following a linear scale. Examples of such attributes are age, height, weight, cost, etc. The idea is that intervals keep the same importance through out the scale. For example, the difference in age between 10 and 20 is the same as that between 40 and 50. There are two main approaches to standardize interval scaled attributes, range and z-score. The range method divides each value by the range of valid values of the attribute so that the transformed value ranges between 0 and 1. Given the value xif of the fth attribute of the ith data point, the new value rg(xif) is, rg ( xif ) xif min( f ) max( f ) min( f ) , (19) 140 4 Unsupervised Learning where min(f) and max(f) are the minimum value and maximum value of attribute f respectively. max(f) min(f) is the value range of the valid values of attribute f. The z-score method transforms an attribute value based on the mean and the standard deviation of the attribute. That is, the z-score of the value indicates how far and in what direction the value deviates from the mean of the attribute, expressed in units of the standard deviation of the attribute. The standard deviation of attribute f, denoted by f, is computed with: n i1 f ( xif f n1 )2 (20) , where n is the number of data points in the data set, xif is the same as above, and f is the mean of attribute f, which is computed with: f 1 n n (21) x. i 1 if Given the value xif, its z-score (the new value after transformation) is z(xif), z ( xif ) xif f . (22) f Ratio-Scaled Attributes: These are also numeric attributes taking real values. However, unlike interval-scaled attributes, their scales are not linear. For example, the total amount of microorganisms that evolve in a time t is approximately given by AeBt, where A and B are some positive constants. This formula is referred to as exponential growth. If we have such attributes in a data set for clustering, we have one of the following two options: 1. Treat it as an interval-scaled attribute. This is often not recommended due to scale distortion. 2. Perform logarithmic transformation to each value, xif, i.e., log( xif ). (23) After the transformation, the attribute can be treated as an intervalscaled attribute. Nominal (Unordered Categorical) Attributes: As we discussed in Sect. 4.5.2, the value of such an attribute can take anyone of a set of states (also 4.7 Handling of Mixed Attributes 141 called categories). The states have no logical or numerical ordering. For example, the attribute fruit may have the possible values, Apple, Orange, and Pear, which have no ordering. A binary attribute is a special case of a nominal attribute with only two states or values. Although nominal attributes are not standardized as numeric attributes, it is sometime useful to convert a nominal attribute to a set of binary attributes. Let the number of values of a nominal attribute be v. We can then create v binary attributes to represent them, i.e., one binary attribute for each value. If a data instance for the nominal attribute takes a particular value, the value of its corresponding binary attribute is set to 1, otherwise it is set to 0. The resulting binary attributes can be used as numeric attributes. We will discuss this again in Sect. 4.7. Example 12: For the nominal attribute fruit, we create three binary attributes called, Apple, Orange, and Pear in the new data. If a particular data instance in the original data has Apple as the value for fruit, then in the transformed data, we set the value of the attribute Apple to 1, and the values of attributes Orange and Pear to 0. Ordinal (Ordered Categorical) Attributes: An ordinal attribute is like a nominal attribute, but its values have a numerical ordering. For example, the age attribute may have the values, Young, Middle-Age and Old. The common approach to distance computation is to treat ordinal attributes as interval-scaled attributes and use the same methods as for interval-scaled attributes to standardize the values of ordinal attributes. 4.7 Handling of Mixed Attributes So far, we have assumed that a data set contains only one type of attributes. However, in practice, a data set may contain mixed attributes. That is, it may contain any subset of the six types of attributes, interval-scaled, symmetric binary, asymmetric binary, ratio-scaled, ordinal and nominal attributes. Clustering a data set involving mixed attributes is a challenging problem. One way to deal with such a data set is to choose a dominant attribute type and then convert the attributes of other types to this type. For example, if most attributes in a data set are interval-scaled, we can convert ordinal attributes and ratio-scaled attributes to interval-scaled attributes as discussed above. It is also appropriate to treat symmetric binary attributes as interval-scaled attributes. However, it does not make much sense to convert a nominal attribute with more than two values or an asymmetric binary attribute to an interval-scaled attribute, but it is still frequently done in 142 4 Unsupervised Learning practice by assigning some numbers to them according to some hidden ordering. For instance, in the example of Apple, Orange, and Pear, one may order them according to their prices, and thus make the attribute fruit an ordinal attribute or even an interval-scaled attribute. In the previous section, we also saw that a nominal attribute can be converted to a set of (symmetric) binary attributes, which in turn can be regarded as intervalscaled attributes. Another method of handling mixed attributes is to compute the distance of each attribute of the two data points separately and then combine all the individual distances to produce an overall distance. We describe one such method, which is due to Gower [205] and is also described in [218, 271]. We describe the combination formula first (Equation (24)) and then present the methods to compute individual distances. r f1 r dist (x i , x j ) f1 f ij d ijf f ij . (24) This distance value is between 0 and 1. r is the number of attributes in the data set. The indicator ijf is 1 if both values xif and xjf for attribute f are non-missing, and it is set to 0 otherwise. It is also set to 0 if attribute f is asymmetric and the match is 0–0. Equation (24) cannot be computed if all f ’s are 0. In such a case, some default value may be used or one of the ij data points is removed. d ijf is the distance contributed by attribute f, and it is in the range 0–1. If f is a binary or nominal attribute, 1 if xif x jf 0 otherwise d ijf (25) If all the attributes are nominal, Equation (24) reduces to Equation (17). The same is true for symmetric binary attributes, in which we recover the simple matching distance (Equation (10)). When the attributes are all asymmetric, we obtain the Jaccard distance (Equation (14)). If attribute f is interval-scaled, we use dijf | xif x jf | (26) Rf where Rf is the value range of attribute f, which is Rf max( f ) min( f ) (27) 4.9 Cluster Evaluation 143 Ordinal attributes and ratio-scaled attributes are handled in the same way after conversion. If all the attributes are interval-scaled, Equation (24) becomes the Manhattan distance assuming that all attribute values are standardized by dividing their values with the ranges of their corresponding attributes. 4.8 Which Clustering Algorithm to Use? Clustering research and application has a long history. Over the years, a vast collection of clustering algorithms has been designed. This chapter only introduced several of the main algorithms. Given an application data set, choosing the “best” clustering algorithm to cluster the data is a challenge. Every clustering algorithm has limitations and works well with only certain data distributions. However, it is very hard, if not impossible, to know what distribution the application data follows. Worse still, the application data set may not fully follow any “ideal” structure or distribution required by the algorithms. Apart from choosing a suitable clustering algorithm from a large collection of available algorithms, deciding how to standardize the data, to choose a suitable distance function and to select other parameter values (e.g., k in the k-means algorithm) are complex as well. Due to these complexities, the common practice is to run several algorithms using different distance functions and parameter settings, and then to carefully analyze and compare the results. The interpretation of the results should be based on insight into the meaning of the original data together with knowledge of the algorithms used. That is, it is crucial that the user of a clustering algorithm fully understands the algorithm and its limitations. He/she should also have the domain expertise to examine the clustering results. In many cases, generating cluster descriptions using a supervised learning method (e.g., decision tree induction) can be particularly helpful to the analysis and comparison. 4.9 Cluster Evaluation After a set of clusters is found, we need to assess the goodness of the clusters. Unlike classification, where it is easy to measure accuracy using labeled test data, for clustering nobody knows what the correct clusters are given a data set. Thus, the quality of a clustering is much harder to evaluate. We introduce a few commonly used evaluation methods below. 144 4 Unsupervised Learning User Inspection: A panel of experts is asked to inspect the resulting clusters and to score them. Since this process is subjective, we take the average of the scores from all the experts as the final score of the clustering. This manual inspection is obviously a labor intensive and time consuming task. It is subjective as well. However, in most applications, some level of manual inspection is necessary because no other existing evaluation methods are able to guarantee the quality of the final clusters. It should be noted that direct user inspection may be easy for certain types of data, but not for others. For example, user inspection is not hard for text documents because one can read them easily. However, for a relational table with only numbers, staring at the data instances in each cluster makes no sense. The user can only meaningfully study the centroids of the clusters, or rules that characterize the clusters generated by a decision tree algorithm or some other supervised learning methods (see Sect. 4.3.1). Ground Truth: In this method, classification data sets are used to evaluate clustering algorithms. Recall that a classification data set has several classes, and each data instance/point is labeled with one class. Using such a data set for cluster evaluation, we make the assumption that each class corresponds to a cluster. For example, if a data set has three classes, we assume that it has three clusters, and we request the clustering algorithm to also produce three clusters. After clustering, we compare the cluster memberships with the class memberships to determine how good the clustering is. A variety of measures can be used to assess the clustering quality, e.g., entropy, purity, precision, recall, and F-score. To facilitate evaluation, a confusion matrix can be constructed from the resulting clusters. From the matrix, various measurements can be computed. Let the set of classes in the data set D be C = (c1, c2, …, ck). The clustering method also produces k clusters, which partition D into k disjoint subsets, D1, D2, …, Dk. Entropy: For each cluster, we can measure its entropy as follows: k entropy( Di ) Pri (c j ) log 2 Pri (c j ), (28) j1 where Pri(cj) is the proportion of class cj data points in cluster i or Di. The total entropy of the whole clustering (which considers all clusters) is k entropytotal ( D ) i | Di | entropy ( Di ). 1 |D| (29) 4.9 Cluster Evaluation 145 Purity: This measures the extent that a cluster contains only one class of data. The purity of each cluster is computed with purity ( Di ) max (Pri (c j )). (30) j The total purity of the whole clustering (considering all clusters) is k purity total ( D ) i | Di | purity ( Di ). 1 |D| (31) Precision, recall, and F-score can be computed as well for each cluster based on the class that is the most frequent in the cluster. Note that these measures are based on a single class (see Sect. 3.3.2). Example 13: Assume we have a text collection D of 900 documents from three topics (or three classes), Science, Sports, and Politics. Each class has 300 documents, and each document is labeled with one of the topics (classes). We use this collection to perform clustering to find three clusters. Class/topic labels are not used in clustering. After clustering, we want to measure the effectiveness of the clustering algorithm. First, a confusion matrix (Fig. 4.17) is constructed based on the clustering results. From Fig. 4.17, we see that cluster 1 has 250 Science documents, 20 Sports documents, and 10 Politics documents. The entries of the other rows have similar meanings. The last two columns list the entropy and purity values of each cluster and also the total entropy and purity of the whole clustering (last row). We observe that cluster 1, which contains mainly Science documents, is a much better (or purer) cluster than the other two. This fact is also reflected by both their entropy and purity values. Cluster Science Sports Politics 1 2 3 Total 250 20 30 300 20 180 100 300 10 80 210 300 Entropy Purity 0.589 1.198 1.257 1.031 0.893 0.643 0.617 0.711 Fig. 4.17. Confusion matrix with entropy and purity values Obviously, we can use the total entropy or the total purity to compare different clustering results from the same algorithm with different parameter settings or from different algorithms. Precision and recall may be computed similarly for each cluster. For example, the precision of Science documents in cluster 1 is 0.89. The recall 146 4 Unsupervised Learning of Science documents in cluster 1 is 0.83. The F-score for Science documents is thus 0.86. A final remark about this evaluation method is that although an algorithm may perform well on some labeled data sets, there is no guarantee that it will perform well on the actual application data at hand, which has no class labels. However, the fact that it performs well on some labeled data sets does give us some confidence on the quality of the algorithm. This evaluation method is said to be based on external data or information. There are also methods that evaluate clusters based on the internal information in the clusters (without using external data with class labels). These methods measure intra-cluster cohesion (compactness) and intercluster separation (isolation). Cohesion measures how near the data points in a cluster are to the cluster centroid. Sum of squared error (SSE) is a commonly used measure. Separation measures how far apart different cluster centroids are from one another. Any distance functions can be used for the purpose. We should note, however, that good values for these measurements do not always mean good clusters. In most applications, expert judgments are still the key. Clustering evaluation remains to be a very difficult problem. Indirect Evaluation: In some applications, clustering is not the primary task. Instead, it is used to help perform another more important task. Then, we can use the performance on the primary task to determine which clustering method is the best for the task. For instance, in a Web usage mining application, the primary task is to recommend books to online shoppers. If the shoppers can be clustered according to their profiles and their past purchasing history, we may be able to provide better recommendations. A few clustering methods can be tried, and their results are then evaluated based on how well they help with the recommendation task. Of course, here we assume that the recommendation results can be reliably evaluated. 4.10 Discovering Holes and Data Regions In this section, we wander a little to discuss something related but quite different from the preceding algorithms. We show that unsupervised learning tasks may be performed by using supervised learning techniques [350]. In clustering, data points are grouped into clusters according to their distances (or similarities). However, clusters only represent one aspect of the hidden knowledge in data. Another aspect that we have not studied is the holes. If we treat data instances as points in an r-dimensional space, a hole 4.10 Discovering Holes and Data Regions 147 is simply a region in the space that contains no or few data points. The existence of holes is due to the following two reasons: 1. insufficient data in certain areas, and/or 2. certain attribute-value combinations are not possible or seldom occur. Although clusters are important, holes in the space can be quite useful too. For example, in a disease database we may find that certain symptoms and/or test values do not occur together, or when a certain medicine is used, some test values never go beyond certain ranges. Discovery of such information can be of great importance in medical domains because it could mean the discovery of a cure to a disease or some biological laws. The technique discussed in this section aims to divide the data space into two types of regions, data regions (also called dense regions) and empty regions (also called sparse regions). A data region is an area in the space that contains a concentration of data points and can be regarded as a cluster. An empty region is a hole. A supervised learning technique similar to decision tree induction is used to separate the two types of regions. The algorithm (called CLTree for CLuser Tree [350]) works for numeric attributes, but can be extended to discrete or categorical attributes. Decision tree learning is a popular technique for classifying data of various classes. For a decision tree algorithm to work, we need at least two classes of data. A clustering data set, however, has no class label for each data point. Thus, the technique is not directly applicable. However, the problem can be dealt with by a simple idea. We can regard each data instance/point in the data set as having a class label Y. We assume that the data space is uniformly distributed with another type of points, called non-existing points, which we will label N. With the N points added to the original data space, our problem of partitioning the data space into data regions and empty regions becomes a supervised classification problem. The decision tree algorithm can be adapted to solve the problem. Let us use an example to illustrate the idea. Example 14: Figure 4.18(A) gives a 2-dimensional space with 24 data (Y) points. Two data regions (clusters) exist in the space. We then add some uniformly distributed N points (represented by “o”) to the data space (Fig. 4.18(B)). With the augmented data set, we can run a decision tree algorithm to obtain the partitioning of the space in Fig. 4.18(B). Data regions and empty regions are separated. Each region is a rectangle, which can be expressed as a rule. The reason that this technique works is that if there are clusters (or dense data regions) in the data space, the data points cannot be uniformly distributed in the entire space. By adding some uniformly distributed N 148 4 Unsupervised Learning points, we can isolate data regions because within each data region there are significantly more Y points than N points. The decision tree technique is well known for this partitioning task. (A): The original data space (B). Partitioning with added N points (C). Partitioning without adding N points. Fig. 4.18. Separating data and empty regions using a decision tree An interesting question is: can the task be performed without physically adding the N points to the original data? The answer is yes. Physically adding N points increases the size of the data and thus the running time. A more important issue is that it is unlikely that we can have points truly uniformly distributed in a high-dimensional space as we would need an exponential number of them. Fortunately, we do not need to physically add any N points. We can compute them when needed. The CLTree method is able to produce the partitioning in Fig. 4.18(C) with no N points added. The details are quite involved. Interested readers can refer to [350]. This method has some interesting characteristics: It provides descriptions or representations of the resulting data regions and empty regions in terms of hyper-rectangles, which can be expressed as rules as we have seen in Sect. 3.2 of Chap. 3 and in Sect. 4.3.1. Many applications require such descriptions, which can be easily interpreted by users. It automatically detects outliers, which are data points in empty regions. It may not use all attributes in the data just as in decision tree building Bibliographic Notes 149 for supervised learning. That is, it can automatically determine what attributes are important and what are not. This means that it can perform subspace clustering, i.e., finding clusters that exist in some subspaces (represented by some subsets of the attributes) of the original space. This method also has limitations. The main limitation is that data regions of irregular shapes are hard to handle since decision tree learning only generates hyper-rectangles (formed by axis-parallel hyper-planes), which are rules. Hence, an irregularly shaped data or empty region may be split into several hyper-rectangles. Post-processing is needed to join them if desired (see [350] for additional details). Bibliographic Notes Clustering or unsupervised learning has a long history and a very large body of work. This chapter described only some widely used core algorithms. Most other algorithms are variations or extensions of these methods. For a comprehensive coverage of clustering, please refer to several books dedicated to clustering, e.g., those by Everitt [167], Hartigan [222], Jain and Dubes [252], Kaufman and Rousseeuw [271], and Mirkin [383]. Most data mining texts also have excellent coverage of clustering techniques, e.g., Han and Kamber [218] and Tan et al. [512], which have influenced the writing of this chapter. Below, we review some more recent developments on clustering and give some further readings. A density-based clustering algorithm based on local data densities was proposed by Ester et al. [164] and Xu et al. [564] for finding clusters of arbitrary shapes. Hinneburg and Keim [239], Sheikholeslami et al. [485] and Wang et al. [538] proposed several grid-based clustering methods which first partition the space into small grids. A popular neural network clustering algorithm is the Self-Organizing Map (SOM) by Kohonen [287]. Fuzzy clustering (e.g., fuzzy c-means) was studied by Bezdek [50] and Dunn [157]. Cheeseman et al. [94] and Moore [396] studied clustering using mixture models. The method assumes that clusters are a mixture of Gaussians and uses the EM algorithm [127] to learn a mixture density. We will see in Chap. 5 that EM based partially supervised learning algorithms are basically clustering methods with some given initial seeds. Most clustering algorithms work on numeric data. Categorical data and/or transaction data clustering were investigated by Barbará et al. [36], Ganti et al. [193], Gibson et al. [197], Guha et al. [212], Wang et al. [537], etc. A related area in artificial intelligence is the conceptual clustering, which was studied by Fisher [178], Misha et al. [384] and many others. 150 4 Unsupervised Learning Many clustering algorithms, e.g., hierarchical clustering algorithms, have high time complexities and are thus not suitable for large data sets. Scaling up such algorithms becomes an important issue for large applications. Several researchers have designed techniques to scale up clustering algorithms, e.g., Bradley et al. [61], Guha et al. [211], Ng and Han [406], and Zhang et al. [610]. In recent years, there were quite a few new developments in clustering. The first one is subspace clustering. Traditional clustering algorithms use the whole space to find clusters, but natural clusters may exist in only some sub-spaces. That is, some clusters may only use certain subsets of the attributes. This problem was investigated by Agrawal et al. [8], Aggarwal et al. [4], Aggarwal and Yu [5], Cheng et al. [99], Liu et al. [350], Zaki et al. [590], and many others. The second new research is semi-supervised clustering, which means that the user can provide some initial information to guide the clustering process. For example, the user can select some initial seeds [39] and/or specify some constraints, e.g., must-link (two points must be in the same cluster) and cannot-link (two points cannot be in the same cluster) [528]. The third is the spectral clustering, which emerged from several fields, e.g., VLSI [17] and computer vision [481, 489, 542]. It clusters data points by computing eigenvectors of the similarity matrix. Recently, it was also studied in machine learning and data mining [141, 404, 594]. Yet another new research is co-clustering, which simultaneously clusters both rows and columns. This approach was studied by Cheng and Church [100], Dhillon [134], Dhillon et al. [135], and Hartigan [223]. Regarding document and Web page clustering, most implementations are still based on k-means and hierarchical clustering methods, or their variations but using text specific similarity or distance functions. Steinbach et al. [506], and Zhao and Karypis [614, 615] experimented with k-means and agglomerative hierarchical clustering methods and also proposed some improvements. Many researchers also worked on clustering of search engine results (or snippets) to organize search results into different topics, e.g., Hearst and Pedersen [233], Kummamuru et al. [294], Leouski and Croft [311], Zamir and Etzioni [591, 592], and Zeng et al. [593]. 5 Partially Supervised Learning With Wee Sun Lee In supervised learning, the learning algorithm uses labeled training examples from every class to generate a classification function. One of the drawbacks of this classic paradigm is that a large number of labeled examples are needed in order to learn accurately. Since labeling is often done manually, it can be very labor intensive and time consuming. In this chapter, we study two partially supervised learning tasks. As their names suggest, these two learning tasks do not need full supervision, and thus are able to reduce the labeling effort. The first is the task of learning from labeled and unlabeled examples, which is commonly known as semisupervised learning. In this chapter, we also call it LU learning (L and U stand for “labeled” and “unlabeled” respectively). In this learning setting, there is a small set of labeled examples of every class, and a large set of unlabeled examples. The objective is to make use of the unlabeled examples to improve learning. The second is the task of learning from positive and unlabeled examples. This problem assumes two-class classification. However, the training data only has a set of labeled positive examples and a set of unlabeled examples, but no labeled negative examples. In this chapter, we also call this problem PU learning (P and U stand for “positive” and “unlabeled” respectively). The objective is to build an accurate classifier without labeling any negative examples. We study these two problems in the context of text classification and Web page classification in this chapter. However, the general ideas and the algorithms are also applicable to other kinds of classification tasks. 5.1 Learning from Labeled and Unlabeled Examples As we described in Chap. 3, the common approach to learning a classification function is to label a set of examples with some pre-defined categories or classes, and then use a learning algorithm to produce a classifier. This classifier is applied to assign classes to future instances (or test data). In the context of text classification and Web page classification, the examples are text documents and Web pages. This approach to building a classifier 152 5 Partially Supervised Learning is called supervised learning because the training documents/pages have been labeled with pre-defined classes. The main bottleneck of building such a classifier is that a large, often prohibitive, number of labeled training documents are needed to build accurate classifiers. In text classification, the labeling is typically done manually by reading the documents, which is a time consuming task. However, we cannot eliminate labeling completely because without it a machine learning algorithm will not know what the user is interested in. Although unsupervised learning or clustering may help to some extent, clustering does not guarantee to produce the categorization results required by the user. This raises an important question: Can the manual labeling effort be reduced, and can other sources of information be used so that the number of labeled examples required for learning would not be too large? This section addresses the problem of learning from a small set of labeled examples and a large set of unlabeled examples, i.e., LU learning. Thus, in this setting only a small set of examples needs to be labeled for each class. However, since a small set of labeled examples is not sufficient for building an accurate classifier, a large number of unlabeled examples are utilized to help. One key point to note is that although the number may be small, every class must have some labeled examples. In many applications, unlabeled examples are easy to come by. This is especially true for online documents. For example, if we want to build a classifier to classify news articles into different categories or classes, it is fairly easy to collect a huge number of unlabeled news articles from the Web. In fact, in many cases, the new data that need to be classified (which have no class labels) can be used as the unlabeled examples. The question is: why do the unlabeled data help? In the context of text classification, one reason is that the unlabeled data provide information on the joint probability distribution over words. For example, using only the labeled data we find that documents containing the word “homework” tend to belong to a particular class. If we use this fact to classify the unlabeled documents, we may find that “lecture” co-occurs with “homework” frequently in the unlabeled set. Then, “lecture” may also be an indicative word for the class. Such correlations provide a helpful source of information to increase classification accuracy, especially when the labeled data are scarce. Several researchers have shown that unlabeled data help learning. That is, under certain conditions using both labeled and unlabeled data in learning is better than using a small set of labeled data alone. Their techniques can thus alleviate the labor-intensive labeling effort. We now study some of these learning techniques, and also discuss their limitations. 5.1 Learning from Labeled and Unlabeled Examples 5.1.1 153 EM Algorithm with Naïve Bayesian Classification One of the LU learning techniques uses the Expectation–Maximization (EM) algorithm [127]. EM is a popular iterative algorithm for maximum likelihood estimation in problems with missing data. The EM algorithm consists of two steps, the Expectation step (or E-step), and the Maximization step (or M-step). The E-step basically fills in the missing data based on the current estimation of the parameters. The M-step, which maximizes the likelihood, re-estimates the parameters. This leads to the next iteration of the algorithm, and so on. EM converges to a local minimum when the model parameters stabilize. The ability of EM to work with missing data is exactly what is needed for learning from labeled and unlabeled examples. The documents in the labeled set (denoted by L) all have class labels (or values). The documents in the unlabeled set (denoted by U) can be regarded as having missing class labels. We can use EM to estimate them based on the current model, i.e., to assign probabilistic class labels to each document di in U, i.e., Pr(cj|di). After a number of iterations, all probabilities will converge. Note that the EM algorithm is not really a specific “algorithm”, but is a framework or strategy. It simply runs a base algorithm iteratively. We will use the naïve Bayesian (NB) algorithm discussed in Sect. 3.7 as the base algorithm, and run it iteratively. The parameters that EM estimates in this case are the probability of each word given a class and the class prior probabilities (see Equation (27) and (28) in Sect. 3.7 of Chap. 3). Although it is quite involved to derive the EM algorithm with the NB classifier, it is fairly straightforward to implement and to apply the algorithm. That is, we use a NB classifier in each iteration of EM, Equation (29) in Chap. 3 for the E-step, and Equations (27) and (28) in Chap. 3 for the M-step. Specifically, we first build a NB classifier f using the labeled examples in L. We then use f to classify the unlabeled examples in U, more accurately to assign a probability to each class for every unlabeled example, i.e., Pr(cj|di), which takes the value in [0, 1] instead of {0, 1}. Some explanations are in order here. Let the set of classes be C = {c1, c2, …, c|C|}. Each iteration of EM will assign every example di in U a probability distribution on the classes that it may belong to. That is, it assigns di the class probabilities of Pr(c1|di), Pr(c2|di), …, Pr(c|C||di). This is different from the example in the labeled set L, where each document belongs to only a single class ck, i.e., Pr(ck|di) = 1 and Pr(cj|di) = 0 for j k. Based on the assignments of Pr(cj|di) to each document in U, a new NB classifier can be constructed. This new classifier can use both the labeled set L and the unlabeled set U as the examples in U now have probabilistic 154 5 Partially Supervised Learning Algorithm EM(L, U) 1 Learn an initial naïve Bayesian classifier f from only the labeled set L (using Equations (27) and (28) in Chap. 3); 2 repeat // E-Step 3 for each example di in U do 4 Using the current classifier f to compute Pr(cj|di) (using Equation (29) in Chap. 3). 5 end // M-Step 6 learn a new naïve Bayesian classifier f from L U by computing Pr(cj) and Pr(wt|cj) (using Equations (27) and (28) in Chap. 3). 7 until the classifier parameters stabilize Return the classifier f from the last iteration. Fig. 5.1. The EM algorithm with naïve Bayesian classification labels, Pr(cj|di). This leads to the next iteration. The process continues until the classifier parameters (Pr(wt|cj) and Pr(cj)) no longer change (or have minimum changes). The EM algorithm with NB classification was proposed for LU learning by Nigam et al. [413]. The algorithm is shown in Fig. 5.1. EM here can also be seen as a clustering method with some initial seeds (labeled data) in each cluster. The class labels of the seeds indicate the class labels of the resulting clusters. The derivation of the EM algorithm in Fig. 5.1 is quite involved and is given as an appendix at the end of this chapter. Two assumptions are made in the derivation. They are in fact the two mixture model assumptions in Sect. 3.7 of Chap. 3 for deriving the naïve Bayesian classifier for text classification, i.e., 1. the data is generated by a mixture model, and 2. there is a one-to-one correspondence between mixture components and classes. It has been shown that the EM algorithm in Fig. 5.1 works well if the two mixture model assumptions for a particular data set are true. Note that although naïve Bayesian classification makes additional assumptions as we discussed in Sect. 3.7 of Chap. 3, it performs surprisingly well despite the obvious violation of the assumptions. The two mixture model assumptions, however, can cause major problems when they do not hold. In many reallife situations, they may be violated. It is often the case that a class (or topic) contains a number of sub-classes (or sub-topics). For example, the class Sports may contain documents about different sub-classes of sports, 5.1 Learning from Labeled and Unlabeled Examples 155 e.g., Baseball, Basketball, Tennis, and Softball. Worse still, a class cj may even contain documents from completely different topics, e.g., Science, Politics, and Sports. The first assumption above is usually not a problem. The second assumption is critical. If the condition holds, EM works very well and is particularly useful when the labeled set is very small, e.g., fewer than five labeled documents per class. In such cases, every iteration of EM is able to improve the classifier dramatically. However, if the second condition does not hold, the classifier from each iteration can become worse and worse. That is, the unlabeled set hurts learning instead of helping it. Two methods are proposed to remedy the situation. Weighting the Unlabeled Data: In LU learning, the labeled set is small, but the unlabeled set is very large. So the EM’s parameter estimation is almost completely determined by the unlabeled set after the first iteration. This means that EM essentially performs unsupervised clustering. When the two mixture model assumptions are true, the natural clusters of the data are in correspondence with the class labels. The resulting clusters can be used as the classifier. However, when the assumptions are not true, the clustering can go very wrong, i.e., the clustering may not converge to mixture components corresponding to the given classes, and are therefore detrimental to classification accuracy. In order to reduce the effect of the problem, we can weight down the unlabeled data during parameter estimation (EM iterations). Specifically, we change the computation of Pr(wt|cj) (Equation (27) in Chap. 3) to the following, where the counts of the unlabeled documents are decreased by a factor of , 0 1: | D| Pr( wt | c j ) |V | i1 |V | s1 (i ) N ti Pr(c j | d i ) | D| i1 , (1) (i ) N ti Pr(c j | d i ) where (i ) if d i U 1 if d i L. (2) When = 1, each unlabeled document is weighted the same as a labeled document. When = 0, the unlabeled data are not considered. The value of can be chosen based on leave-one-out cross-validation accuracy on the labeled training data. The value that gives the best result is used. Finding Mixture Components: Instead of weighting unlabeled data low, we can attack the problem head on, i.e., by finding the mixture components (sub-classes) of the class. For example, the original class Sports may con- 156 5 Partially Supervised Learning sist of documents from Baseball, Tennis, and Basketball, which are three mixture components (sub-classes or sub-topics) of Sports. Instead of using the original class, we try to find these components and treat each of them as a class. That is, if we can find the three mixture components, we can use them to replace the class Sports. There are several automatic approaches for identifying mixture components. For example, a hierarchical clustering technique was proposed in [111] to find the mixture components, which showed good performances. A simple approach based on leave-one-out cross-validation on the labeled training set was also given in [413]. Manually identifying different components may not be a bad option for text documents because one only needs to read the documents in the labeled set (or some sampled unlabeled documents), which is very small. 5.1.2 Co-Training Co-training is another approach to learning from labeled and unlabeled examples. This approach assumes that the set of attributes (or features) in the data can be partitioned into two subsets. Each of them is sufficient for learning the target classification function. For example, in Web page classification, one can build a classifier using either the text appearing on the page itself, or the anchor text attached to hyperlinks pointing to the page from other pages on the Web. This means that we can use the same training data to build two classifiers using two subsets of features. Traditional learning algorithms do not consider this division of features (attributes), or feature redundancy. All the features are pooled together in learning. In some cases, feature selection algorithms are applied to remove redundant features. Co-training exploits this feature division to learn separate classifiers over each of the feature sets, and utilizes the fact that the two classifiers must agree on their labeling of the unlabeled data to do LU learning. Blum and Mitchell [55] formalize the co-training setting and provide a theoretical guarantee for accurate learning subject to certain assumptions. In the formalization, we have an example (data) space X = X1 X2, where X1 and X2 provide two different “views” of the example. That is, each example x (represented as a vector) is given as a pair (x1, x2). This simply means that the set of features (or attributes) is partitioned into two subsets. Each “view” or feature subset is sufficient for correct classification. Under some further assumptions, it was proven that co-training algorithms can learn from unlabeled data starting from only a weak classifier built using the small set of labeled training documents. 5.1 Learning from Labeled and Unlabeled Examples 157 The first assumption is that the example distribution is compatible with the target functions; that is, for most examples, the target classification functions over the feature sets predict the same label. In other words, if f denotes the combined classifier, f1 denotes the classifier learned from X1, f2 denotes the classifier learned from X2 and c is the actual class label of example x, then f(x) = f1(x1) = f2(x2) = c for most examples. The second assumption is that the features in one set of an example are conditionally independent of the features in the other set, given the class of the example. In the case of Web page classification, this assumes that the words on a Web page are not related to the words on its incoming hyperlinks, except through the class of the Web page. This is a somewhat unrealistic assumption in practice. The co-training algorithm explicitly uses the feature split to learn from labeled and unlabeled data. The algorithm is iterative. The main idea is that in each iteration, it learns a classifier from the labeled set L with each subset of the features, and then applies the classifier to classify (or label) the unlabeled examples in U. A number (ni) of most confidently classified examples in U from each class ci are added to L. This process ends when U becomes empty (or a fixed number of iterations is reached). In practice, we can set a different ni for a different class ci depending on class distributions. For example, if a data set has one third of class 1 examples and two thirds of class 2 examples, we can set n1 = 1 and n2 = 2. The whole co-training algorithm is shown in Fig. 5.2. Lines 2 and 3 build two classifiers f1 and f2 from the two “views” of the data respectively. f1 and f2 are then applied to classify the unlabeled examples in U (lines 4 and 5). Some most confidently classified examples are removed from U and added to L. The algorithm then goes to the next iteration. Algorithm co-training(L, U) 1 repeat 2 Learn a classifier f1 using L based on only x1 portion of the examples x. 3 Learn a classifier f2 using L based on only x2 portion of the examples x. 4 Apply f1 to classify the examples in U, for each class ci, pick ni examples that f1 most confidently classifies as class ci, and add them to L. 5 Apply f2 to classify the examples in U, for each class ci, pick ni examples that f2 most confidently classifies as class ci, and add them to L. 6 until U becomes empty or a fixed number of iterations are reached Fig. 5.2. A co-training algorithm When the co-training algorithm ends, it returns two classifiers. At classification time, for each test example the two classifiers are applied separately and their scores are combined to decide the class. For naïve Bayesian classifiers, we multiply the two probability scores, i.e., 158 5 Partially Supervised Learning Pr(cj|x) = Pr(cj|x1)Pr(cj|x2) (3) The key idea of co-training is that classifier f1 adds examples to the labeled set that are used for learning f2 based on the X2 view, and vice versa. Due to the conditional independence assumption, the examples added by f1 can be considered as new and random examples for learning f2 based on the X2 view. Then the learning will progress. The situation is illustrated in Fig. 5.3. This example has classes, positive and negative, and assumes linear separation of the two classes. In the X1 view (Fig. 5.3(A)), the circled examples are most confident positive and negative examples classified (or labeled) by f1 in the unlabeled set U. In the X2 view (Fig. 5.3(B)), these circled examples appear randomly. With these random examples from U added to L, a better f2 will be learned in the next iteration. + ++ + + + + ++ + + + + + ++ + +++ +++ ++ + ++ ++ + + + + + + + ++ + + (A) X1 view: data in U labeled by h1 (B) X2 view: the same data Fig. 5.3. Two views of co-training. However, if the added examples to L are not random examples in the X2 space but very similar to the situation in Fig. 5.3(A), then these examples are not informative to learning. That is, if the two subsets of features are correlated given the class or the conditional independence assumption is violated, the added examples will not be random but isolated in a specific region similar to those in Fig. 5.3(A). Then they will not be as useful or informative to learning. Consequently, co-training will not be effective. In [411], it is shown that co-training produces more accurate classifiers than the EM algorithm presented in the previous section, even for data sets whose feature division does not completely satisfy the strict requirements of compatibility and conditional independence. 5.1.3 Self-Training Self-training, which is similar to both EM and co-training, is another method for LU learning. It is an incremental algorithm that does not use the split of features. Initially, a classifier (e.g., naïve Bayesian classifier) is 5.1 Learning from Labeled and Unlabeled Examples 159 trained with the small labeled set considering all features. The classifier is then applied to classify the unlabeled set. Those most confidently classified (or unlabeled) documents of each class, together with their predicted class labels, are added to the labeled set. The classifier is then re-trained and the procedure is repeated. This process iterates until all the unlabeled documents are given class labels. The basic idea of this method is that the classifier uses its own predictions to teach itself. 5.1.4 Transductive Support Vector Machines Support vector machines (SVM) is one of the most effective methods for text classification. One way to use unlabeled data in training SVM is by selecting the labels of the unlabeled data in such a way that the resulting margin of the classifier is maximized. Training for the purpose of labeling known (unlabeled) test instances is referred to as transduction, giving rise to the name transductive SVM [526]. An example of how transduction can change the decision boundary is shown in Fig. 5.4. In this example, the old decision boundary, constructed using only labeled data, would have a very small margin on the unlabeled data. By utilizing the unlabeled data in the training process, a classifier that has the largest margin on both the labeled and unlabeled data can be obtained. x+ y=1 Old decision boundary x y= 1 New decision boundary Fig. 5.4. The old decision boundary (before the addition of unlabeled data) and the new decision boundary created by transductive SVM. The unlabeled data are indicated by circles around them The main difficulty with applying transductive SVM is the computational complexity. When all the labels are observed, training SVM is a convex optimization problem that can be solved efficiently. The problem 160 5 Partially Supervised Learning of assigning labels to unlabeled examples in such a way that the resulting margin of the classifier is maximized can no longer be solved efficiently. To solve the problem, Joachims [259] used a sub-optimal iterative method that starts by learning a classifier using only the labeled data. The method then treats a subset of unlabeled instances that are most confidently labeled positive by the learned classifier as initial positive examples while the rest of the unlabeled examples are treated as initial negative examples. The number of instances to label as positive can be specified by the user to change the precision–recall trade-off and is maintained throughout the iterations. The method then tries to improve the soft margin cost function by iteratively changing the labels of some of the instances and retraining the SVM. The ratio of positive to negative instances is maintained by selecting one positively labeled instance p and one negatively labeled instance q to change in each iteration. It was shown in [259] that if the two instances are selected such that the slack variables p > 0, q > 0 and p + q > 2, the soft margin cost function will decreases at each iteration. Further improvements described in [259] include allowing the soft margin error of unlabeled examples to be penalized differently from the soft margin error of the labeled examples and penalizing the soft margin error on the positive unlabeled examples differently from the soft margin error on the negative unlabeled examples. The penalty on the unlabeled examples is also iteratively increased from a small value to the desired value. This may improve the chances of finding a good local optimum as it may be easier to improve the cost function when the penalty is small. The method was applied successfully to text classification problems. Like other methods of learning from labeled and unlabeled examples, transductive SVM can be sensitive to its assumptions. When the large margin assumption is correct on the dataset, it may improve performance but when the assumption is incorrect, it can decrease performance compared to supervised learning. As an example, the transductive SVM performed poorly using small labeled data sets when separating Project Web pages from other types of university Web pages in [259]. It was conjectured that, with a small number of labeled data, separating the Web pages according to some of the underlying topics of the Web pages may give a larger margin than separating them according to whether the Web pages are Project pages or not. 5.1.5 Graph-Based Methods Graph-based LU learning methods can be viewed as extensions of nearest neighbor supervised learning algorithms that work with both labeled and 5.1 Learning from Labeled and Unlabeled Examples 161 unlabeled instances. The basic idea in these methods is to treat labeled and unlabeled instances as vertices in a graph where a similarity function is used to define the edge weights between instances. The graph, with similar instances connected by larger weights, is then used to help label the unlabeled instances in such a way that labels of vertices connected by edges with large weights tend to agree with each other. Methods used for constructing the graphs include connecting each instance to its k-nearest neighbors, connecting each instance to other instances within a certain distance and using a fully connected graph with an exponentially decreasing similarity function such as the Gaussian function to assign the weights. The assumptions used in these methods are similar to those of the nearest neighbor classifier, that is, near neighbors should have the same labels and we have a good measure of similarity between instances. We discuss three types of graph-based LU learning methods below: mincut, Gaussian fields and spectral graph transducer. All three methods work on binary classification problems but, like the support vector machines, can be used with strategies such as one-against-rest for solving multiple class classification problems. Mincut: This method was proposed by Blum and Chalwa [54]. A weighted graph G = (V, E, W) is constructed first, where V consists of the labeled and unlabeled instances, E consists of edges between the instances and W is a function on the edges with W(i, j) = wij denoting the similarity of instances i and j. The vertices associated with labeled instances are then given values from {0, 1} consistent with their binary labels. The idea in the mincut algorithm is to find an assignment of values vi from the set {0, 1} to the unlabeled instances in V such that the cost function wij | vi v j | (i , j ) E is minimized. The advantage of this formulation is that the problem can be solved in polynomial time even though it is a combinatorial optimization problem. One way to do this is to transform the problem into a max-flow problem (see [116] for a description of the max-flow problem). To do that, we convert the graph into a flow network by introducing a source vertex v+ and a sink vertex v , where the source vertex is connected by edges with infinite capacities to the positive labeled instances while the sink vertex is connected by edges with infinite capacities to the negative labeled instances. The other edge weights in the graph are also treated as edge capacities in the flow network. A cut of the network is a partition of the vertices into two subsets V+ and V such that v+ V+ and v V . A minimum cut is a partition that has the smallest sum of capacities in the edges connecting V+ and V . Finding a minimum cut is equivalent to minimizing the function wij | vi v j | since all the vertices are assigned values from {0, (i , j ) E 162 5 Partially Supervised Learning 1}. Max-flow algorithms can be used to efficiently find a mincut of the network in time O(|V|3). Gaussian Fields: Instead of minimizing proposed minimizing (i , j ) E wij (vi v j )2 (i , j ) E wij | vi v j | , Zhu et al. [619] with the value of the vertices being selected from [0, 1] instead of {0, 1}. The advantage of using this formulation is that it allows the solution to be obtained using linear algebra. Let W be the weight matrix corresponding to the graph, W W LL W LU WUL WUU (4) , where WLL , WLU , WUL and WUU are sub-matrices with the subscript L denoting labeled instances and the subscript U denoting the unlabeled instances. Let D be a diagonal matrix where Dii wij is the sum of the enj tries in row (or column) i. We also form a vector v, consisting of values assigned to the labeled and unlabeled instances. The labeled instances are assigned fixed values in {0, 1} consistent with their labels while the values vi assigned to the unlabeled instances are chosen to minimize w (vi v j ) 2 . The solution can be written as ( i , j ) E ij vU ( DUU WUU ) 1WUL v L , (5) where vU is the part of the vector v that contains values assigned to the unlabeled instances, vL is the part of the vector that contains values assigned to labeled instances and DUU is the sub-matrix of D consisting of sum of entries of rows in W associated with unlabeled instances. The optimization problem wij (vi v j ) 2 can also be written in ma(i , j ) E trix form as vT v where = D W is known as the combinatorial Laplacian of the graph. The matrix is known to be positive semidefinite, so it can be viewed as an inverse covariance matrix of a multivariate Gaussian random variable, giving rise to the name Gaussian field. Spectral Graph Transducer: One potential problem with the mincut formulation is that the mincut cost function tends to prefer unbalanced cuts where the number of instances in either the positive or negative class vastly outnumbers the number of instances in the other class. Unbalanced cuts tend to have a lower cost in the mincut formulation because the number of edges between V+ and V is maximized when the sizes of V+ and V are equal and is small when either one of them is small. For example, if 5.1 Learning from Labeled and Unlabeled Examples 163 we have n vertices and V+ contains a single element, then there are potentially n 1 edges between V+ and V . In contrast, if V+ and V are the same size, then there are potentially n2/4 edges between the two sets of vertices. Let cut(V+, V ) be the sum of the edge weights connecting V+ and V . To mitigate the effect of preferring unbalanced cut, Joachims [261] proposed to minimize a cost function of normalized cut cut (V ,V ) , where the cut | V || V | value is normalized by the number of edges between the two sets. Minimizing this cost function is computationally difficult, so Joachims [261] proposed minimizing a relaxed version of the problem. Let be the combinatorial Laplacian of the graph. It can be shown that minimizing the normalized cut (with no labeled data) using and number of instances ( and are specified by the user) in the two partitions is equivalent to minimizing vT v for vi { +, }, where and . (6) Instead of using vi { +, }, Joachims [261] proposed to allow vi to take real values under the constraint vT1=0 and vTv=n, where 1 is the all one vector. To make sure that the labeled instances are properly classified, a term (v )TC(v ) is added to the cost function, where C is a diagonal matrix with non-zero entries only for labeled instances and is the target vector for approximation by v. The components of that correspond to positive and negative instances are set to + and respectively, while the components of that correspond to unlabeled instances do not affect the cost function because their corresponding diagonal entries of C are set to zero. The values of the non-zero entries of C can be set by the user to give different misclassification costs to each instance. This gives the combined optimization problem of min v vT v c( v s.t. )T C ( v vT 1 0 and vT v ) (7) n where c gives a trade-off between the cost for the labeled and unlabeled parts. The solution of Equation (7) is obtained using spectral methods. The Gaussian field method and spectral graph transduction have been applied to the natural language processing problem of word sense disambiguation in [414, 442]. Word sense disambiguation is the problem of assigning appropriate meanings to words (which may have multiple meanings) according to the context that they appear in. Although some improvements are observed, the success of these methods is still limited. 164 5.1.6 5 Partially Supervised Learning Discussion We discuss two issues: (1) whether the unlabeled set U is always helpful and (2) the evaluation of LU learning. Does the Unlabeled Set Always Help? The answer is no. As we have seen, all approaches make strong assumptions. For example, EM makes two mixture model assumptions, and co-training makes the feature split assumption. When the assumptions are true for an application data set, unlabeled data can help learning (even dramatically). When the assumptions are not true, the unlabeled data may harm learning. Automatically detecting bad match of the problem structure with the model assumptions in advance is, however, very hard and remains an open problem. A related issue is that researchers have not shown that when the labeled data set is sufficiently large, the unlabeled data still help. Manual labeling more text documents may not be as difficult as it seems in some applications, especially when the number of classes is small. In most cases, to label a document one does not even need to read the entire document (if it is long). Typically, the first few sentences can already tell its class. Compounded with the problem of inability to decide whether the unlabeled data indeed help classification, practical applications of LU learning are still limited. Evaluation: The evaluation of LU learning is commonly done in the same way as traditional classification. However, there is a problem with the availability of sufficient test data. In practice, users always want to have a reasonable guarantee on the predictive accuracy of a classification system before they are willing to use the system. This means that test data sets have to be used to evaluate the system. Existing algorithms for LU learning assume that there is a large set of labeled test data for this purpose. However, this contradicts the LU learning problem statement, which says that the labeled set is very small. If we can ask the user to label more data, then we do not need LU learning because some examples of the test set can be used in training. Evaluation without more labeled data is also an open problem. One may look at this problem in another way. We first use the classifier generated by LU learning to classify the unlabeled set or a new test set and then sample some classified documents to be checked manually in order to estimate the classification accuracy. If classification is sufficiently accurate, the results of the classifier will be used. Otherwise, improvements need to be made. In this case, additional labeled data obtained during manual inspection can be added to the original labeled set. You see we end up doing more labeling! Hopefully, we do not have to do too much labeling. 5.2 Learning from Positive and Unlabeled Examples 165 5.2 Learning from Positive and Unlabeled Examples In some applications, the problem is to identify a particular class P of documents from a set of mixed documents, which contains documents of class P and also other kinds of documents. We call the class of documents that one is interested in the positive class documents, or simply positive documents. We call the rest of the documents the negative class documents or simply negative documents. This problem can be seen as a classification problem with two classes, positive and negative. However, there are no labeled negative documents for training. The problem is stated more formally as follows, Problem Statement: Given a set P of positive documents that we are interested in, and a set U of unlabeled documents (the mixed set), which contains both positive documents and negative documents, we want to build a classifier using P and U that can identify positive documents in U or in a separate test set in other words, we want to accurately classify positive and negative documents in U or in the test (or future) data set. This problem is called PU learning. Note that the set U can be used in both training and testing because U is unlabeled. The key feature of this problem is that there is no labeled negative document for learning. Traditional supervised learning algorithms are thus not directly applicable because they all require both labeled positive and labeled negative documents to build a classifier. This is also the case for LU learning, although the labeled set for each class may be very small. 5.2.1 Applications of PU Learning The PU learning problem occurs frequently in Web and text retrieval applications because most of the time the user is only interested in Web pages or text documents of a particular topic. For example, one may be interested in only travel related pages (positive pages). Then all the other types of pages are negative pages. Let us use a concrete example to show the actual setting of a PU learning application. Example 1: We want to build a repository of data mining research papers. We can start with an initial set of papers from a data mining conference or journal, which are positive examples. We then want to find data mining papers from online journals and conference series in the fields of databases and artificial intelligence. Journals and conferences in these fields all contain some data mining papers. They also contain many other types of papers. The problem is how to extract data mining papers from such confer- 166 5 Partially Supervised Learning ences and journals, or in other words, how to classify the papers from these sources into data mining papers and non-data mining papers without labeling any negative papers in any source. In practical applications, positive documents are usually available because if one has worked on a particular task for some time one should have accumulated many related documents. Even if no positive document is available initially, collecting some from the Web or any other source is relatively easy. One can then use this set to find the same class of documents from other sources without manually labeling any negative documents. PU learning is particularly useful in the following situations: 1. Learning with multiple unlabeled sets: In some applications, one needs to find positive documents from a large number of document collections. For example, we want to identify Web pages that sell printers. We can easily obtain a set of positive pages from an online merchant, e.g., amazon.com. Then we want to find printer pages from other merchants. We can crawl each site one by one, and extract printer pages from each site using PU learning. We do not need to manually label negative pages (non-printer pages) from any site. Although it may not be hard to label some negative pages from a single site, it is difficult to label for every site. Note that in general the classifier built based on the negative pages from one site s1 may not be used to classify pages from another site s2 because the negative pages in s2 can be very different from the negative pages in s1. The reason is that although both sites sell printers, the other products that they sell can be quite different. Thus using the classifier built for s1 to classify pages in s2 may violate the fundamental assumption of machine learning: the distribution of training examples is identical to the distribution of test examples. As a consequence, we may obtain poor accuracy results. 2. Learning with unreliable negative examples: This situation often occurs in experimental sciences. For example, in biology, biologists perform experiments to determine some biological functions. They are often quite confident about positive cases that they have discovered. However, they may not be confident about negative cases because laboratory results can be affected by all kinds of conditions. The negative cases are thus unreliable. It is perhaps more appropriate to treat such negative cases as unlabeled examples than negative examples. PU learning is also useful for the following seemingly unrelated problems: Detecting unexpected documents in the test set: In many applications, the test set (or future instance set in practice) contains some classes of documents that are not seen in the training set. For instance, our training 5.2 Learning from Positive and Unlabeled Examples 167 set has only two classes, Sports and Science. Then, a learning algorithm will produce a classifier to separate Sports and Science documents. However, in the test set, some other types of documents, e.g., Politics and Religion, may appear, which are called unexpected documents. In traditional classification, those Politics and Religion documents in the test set will be classified as either Sports or Science documents, which is clearly inappropriate. In PU learning, we can remove Politics and Religion documents by treating the whole training set as the positive data, and the whole test set as the unlabeled data. A study of this problem is reported in [323]. Detecting outliers: Traditional outlier detection algorithms are mainly based on clustering. During clustering, those data points that are too far away from cluster centroids are considered outliers. PU learning can be applied to outlier detection as follows: A random sample is drawn from the original data. The sample is treated as the set of positive examples, and the remaining data is treated as the set of unlabeled examples. The method given in [323] can then be applied. This strategy may work because since the number of outliers is small, the chance of selecting them during sampling will be extremely small. To make the method more robust, one can run the technique multiple times using multiple random samples. Before discussing theoretical foundations of PU learning, let us first develop some intuition on why PU learning is possible and why unlabeled data are helpful. Figure 5.5 shows the idea. ++ ++ + ++ + + ++ + +++ (A) With only positive data ++ ++ + ++ + + ++ + ++ + (B) With both positive and unlabeled data Fig. 5.5. Unlabeled data are helpful In Fig. 5.5(A), we see only positive documents (data points) represented with +’s. We assume that a linear classifier is sufficient for the classification task. In this case, it is hard to know where to draw the line to separate positive and negative examples because we do not know where the negative examples might be. There are infinite possibilities. However, if the unlabeled data (represented by small circles) are added to the space (Fig. 5.5(B)), it is very clear where the separation line should be. Let us now discuss the theoretical result of PU learning. 168 5 Partially Supervised Learning 5.2.2 Theoretical Foundation Let (xi, yi) be random variables drawn independently from probability distribution D(x,y) where y { 1, 1} is the conditional random variable that we wish to estimate given x. xi represents a document, and yi is its class, which can be 1 (positive) or –1 (negative). Let Dx|y=1 be the conditional distribution from which the positive examples are independently drawn and let Dx be the marginal distribution from which unlabeled examples are independently drawn. Our objective is to learn a classification function f that can separate positive and negative documents. Since learning is to produce a classifier that has the minimum probability of error, Pr(f(x) y), let us rewrite it into a more useful form, Pr(f(x) y) = Pr(f(x)=1 and y= 1) + Pr(f(x)= 1 and y=1). (8) The first term can be rewritten as Pr(f(x)=1 and y= 1) = Pr(f(x)=1) – Pr(f(x)=1 and y=1) = Pr(f(x)=1) – (Pr(y=1) – Pr(f(x)= 1 and y=1)). (9) Substituting (9) into Equation (8), we obtain Pr(f(x) y) = Pr(f(x)=1) – Pr(y=1) + 2Pr(f(x)= 1|y=1)Pr(y=1). (10) Since Pr(y = 1) is constant (although it is unknown), we can minimize the probability of error by minimizing Pr(f(x)=1) + 2Pr(f(x)= 1|y =1)Pr(y=1). (11) If we can hold Pr(f(x)= 1|y=1) small, then learning is approximately the same as minimizing Pr(f(x)=1). Holding Pr(f(x)= 1|y=1) small while minimizing Pr(f(x)=1) is approximately the same as minimizing Pru(f(x)=1) (on the unlabeled set U) while holding PrP(f(x)=1) r (on the positive set P), where r is the recall, i.e., Pr(f(x)=1|y=1). Note that (PrP(f(x)=1) r) is the same as (PrP(f(x)= 1) 1–r). Two theorems given by Liu et al. [348] state these formally and show that in both the noiseless case (P has no error) and the noisy case (P contains errors, i.e., some negative documents) reasonably good learning results can be achieved if the problem is posed as a constrained optimization problem where the algorithm tries to minimize the number of unlabeled examples labeled positive subject to the constraint that the fraction of errors on the positive examples is no more than 1 r. 5.2 Learning from Positive and Unlabeled Examples 169 Example 2: Figure 5.6 illustrates the constrained optimization problem. Assume that positive and negative documents can be linearly separated. Positive documents are represented with +’s, and unlabeled documents with small circles. Assume also that the positive set has no error and we want the recall r on the positive set to be 100%. Each line in the figure is a possible linear classifier. Every document on the left of each line will be labeled (classified) by the line as positive, and every document on the right will be labeled as negative. Lines 1 and 2 are clearly not solutions because the constraint “the fraction of errors on the positive examples must be no more than 1 r (= 0)” is violated, although the number of unlabeled examples labeled (classified) as positive is minimized by line 1. Lines 4, 5, and 6 are poor solutions too because the number of unlabeled examples labeled as positive is not minimized by any of them. Line 3 is the optimal solution. Under the constraint that no positive example is labeled negative, line 3 minimizes the number of unlabeled examples labeled as positive. o o oo o o o o oo o o o o oo o o ooo o o +o +o + o o+ + o+ +o + ++ o 1 2 3 4 5 6 Fig. 5.6. An illustration of the constrained optimization problem Based on the constrained optimization idea, two kinds of approaches have been proposed to build PU classifiers: the two-step approach and the direct approach. In the actual learning algorithms, the user may not need to specify a desired recall level r on the positive set because some of these algorithms have their evaluation methods that can automatically determine whether a good solution has been found. 5.2.3 Building Classifiers: Two-Step Approach As its name suggests the two-step approach works in two steps: 1. Identifying a set of reliable negative documents (denoted by RN) from the unlabeled set U. 2. Building a classifier using P, RN and U RN. This step may apply an existing learning algorithm once or iteratively depending on the quality and the size of the RN set. 170 5 Partially Supervised Learning This two-step approach is illustrated in Fig. 5.7. Here, we assume that step 2 uses an iterative algorithm. In step 1, a set of reliable negative documents (RN) is found from the unlabeled set U, which divides U into two subsets, RN and Q (= U RN). Q is called the likely positive set. In step 2, the algorithm iteratively improves the results by adding more documents to RN until a convergence criterion is met. We can see that the process is trying to minimize the number of unlabeled examples labeled positive since Q becomes smaller and smaller while RN becomes larger and larger. In other words, it tries to iteratively increase the number of unlabeled examples that are labeled negative while maintaining the positive examples in P correctly classified. We present several techniques for each step below. Step 2 Step 1 Reliable negative (RN) U …… Q=U RN P P positive P P negative Fig. 5.7. An illustration of the two-step approach Techniques for Step 1 We introduce four methods to extract reliable negative documents from the unlabeled set U. Spy Technique: This technique works by sending some “spy” documents from the positive set P to the unlabeled set U. Figure 5.8 gives the algorithm of the technique, which is used in the S-EM system [348]. The algorithm has three sub-steps: 1. It randomly samples a set S of positive documents from P and put them in U (lines 2 and 3). The default sampling ratio of s% is 15% in S-EM. 5.2 Learning from Positive and Unlabeled Examples 171 Algorithm Spy(P, U) 1. RN ; 2. S Sample(P, s%); 3. Us U S; 4. Ps P – S; 5. Assign each document in Ps the class label 1; 6. Assign each document in Us the class label 1; 7. NB(Us, Ps); // This produces a NB classifier. 8. Classify each document in Us using the NB classifier; 9. Determine a probability threshold t using S; 10. for each document d Us do 11. if its probability Pr(1|d) < t then 12. RN RN {d}; 13. endif 14. endfor Fig. 5.8. The spy technique. The documents in S act as “spy” documents from the positive set to the unlabeled set U. Since the spies behave similarly to the unknown positive documents in U, they allow the algorithm to infer the behavior of the unknown positive documents in U. 2. It randomly samples a set S of positive documents from P and put them in U (lines 2 and 3). The default sampling ratio of s% is 15% in S-EM. The documents in S act as “spy” documents from the positive set to the unlabeled set U. Since the spies behave similarly to the unknown positive documents in U, they allow the algorithm to infer the behavior of the unknown positive documents in U. 3. It runs the naïve Bayesian (NB) algorithm using the set P S as positive and the set U S as negative (lines 3–7). The NB classifier is then applied to classify each document d in U S (or Us), i.e., to assign it a probabilistic class label Pr(1|d), where 1 represents the positive class. 4. It uses the probabilistic labels of the spies to decide which documents are most likely to be negative. A threshold t is employed to make the decision. Those documents in U with lower probabilities (Pr(1|d)) than t are the most likely negative documents, denoted by RN (lines 10–14). We now discuss how to determine t using spies (line 9). Let the set of spies be S = {s1, s2, …, sk}, and the probabilistic labels assigned to each si be Pr(1|si). Intuitively, we can use the minimum probability in S as the threshold value t, i.e., t = min{Pr(1|s1), Pr(1|s2), …, Pr(1|sk)}, which means that we want to retrieve all spy documents. In a noiseless case, using the minimum probability is acceptable. However, most real-life document collections have outliers and noise. Using the minimum prob- 172 5 Partially Supervised Learning ability is unreliable. The reason is that the posterior probability Pr(1|si) of an outlier document si in S could be 0 or smaller than most (or even all) actual negative documents. However, we do not know the noise level of the data. To be safe, the S-EM system uses a large noise level l = 15% as the default. The final classification result is not very sensitive to l as long it is not too small. To determine t, we first sort the documents in S according to their Pr(1|si) values. We then use the selected noise level l to decide t: we select t such that l percent of documents in S have probability less than t. Hence, t is not a fixed value. The actual parameter is in fact l. Note that the reliable negative set RN can also be found through multiple iterations. That is, we run the spy algorithm multiple times. Each time a new random set of spies S is selected from P and a different set of reliable negative documents is obtained, denoted by RNi. The final set of reliable negative documents is the intersection of all RNi. This may be a better technique because we do not need to worry that one set of random spies S may not be chosen well, especially when the set P is not large. 1DNF Technique: The 1DNF method (Fig. 5.9) is used in [583]. It first builds a positive feature set PF containing words that occur in the positive set P more frequently than in the unlabeled set U (lines 1–7). Line 1 collects all the words in U P to obtain a vocabulary V. Lines 8–13 try to identify reliable negative documents from U. A document in U that does not contain any feature in PF is regarded as a reliable negative document. NB Technique: This method is employed in [340]. It simply uses a naïve Bayesian classifier to identify a set of reliable negative documents RN from the unlabeled set U. The algorithm is given in Fig. 5.10. This method may also be run multiple times. Each time we randomly remove a few documents from P to obtain a different set of reliable negative documents, denoted by RNi. The final set of reliable negative documents RN is the intersection of all RNi. Rocchio technique: This method is employed in [321]. The algorithm is the same as that in Fig. 5.10 except that NB is replaced with Rocchio. The Rocchio classification method is described in Sect. 6.3. Techniques for Step 2 There are two approaches for this step. 1. Run a learning algorithm (e.g., NB or SVM) using P and RN. The set of documents in U RN is discarded. This method works well if the reliable negative set RN is sufficiently large and contains mostly negative docu- 5.2 Learning from Positive and Unlabeled Examples 173 Algorithm 1DNF(P, U) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. Assume the word feature set be V = {w1,…, wn}, wi U P; Let positive feature set PF ; for each wi V do // freq(wi, P): number of times if (freq(wi, P) / |P| > freq(wi, U) / |U|) then // that wi appears in P PF PF {wi}; endif endfor; RN U; for each document d U do if wj freq(wj, d) > 0 and wj PF then RN RN – {d} endif endfor Fig. 5.9. The 1DNF technique for step 1 1. 2. 3. 4. Assign each document in P the class label 1; Assign each document in U the class label 1; Build a NB classifier using P and U; Use the classifier to classify U. Those documents in U that are classified as negative form the reliable negative set RN. Fig. 5.10. The NB method for Step 1 ments. The spy technique, NB and Rocchio in step 1 are often able to produce a sufficiently large set of negative documents. The 1DNF technique may only identify a very small set of negative documents. Then running a learning algorithm will not be able to build a good classifier. 2. Run a learning algorithm iteratively till it converges or some stopping criterion is met. This method is used when the set RN is small. We will not discuss the first approach as it is straightforward. SVM usually does very well. Below, we introduce two techniques for the second approach, which are based on EM and SVM respectively. EM Algorithm with Naïve Bayesian Classification: The EM algorithm can be used naturally here [348]. As in LU learning, the Expectation step basically fills in the missing data. In our case, it produces and revises the probabilistic labels of the documents in U RN (see below). The parameters are estimated in the Maximization step after the missing data are filled. This leads to the next iteration of the algorithm. EM converges when its parameters stabilize. Using NB in each iteration, EM employs the same equations as those used in building a NB classifier (Equation (29) for the Expectation step, and Equations (27) and (28) for the Maximization step). 174 5 Partially Supervised Learning Algorithm EM(P, U, RN) Each document in P is assigned the class label 1; Each document in RN is assigned the class label 1; Learn an initial NB classifier f from P and RN (using Equations (27) and (28) in Chap. 3); 4 repeat // E-Step 5 for each example di in U RN do 6 Using the current classifier f to compute Pr(cj|di) using Equation (29) in Chap. 3. 7 end // M-Step 8 learn a new NB classifier f from P, RN and U RN by computing Pr(cj) and Pr(wt|cj) (using Equations (27) and (28) in Chap. 3). 9 until the classifier parameters stabilize 10. Return the classifier f from the last iteration. 1. 2. 3. Fig. 5.11. EM algorithm with the NB classifier Algorithm I-SVM(P, RN, Q) 1. Every document in P is assigned the class label 1; 2. Every document in RN is assigned the class label –1; 3. loop 4. Use P and RN to train a SVM classifier f; 5. Classify Q using f; 6. Let W be the set of documents in Q that is classified as negative; if W = then exit-loop // convergence 7. 8. else Q Q – W; 9. RN RN W; endif; 10. Fig. 5.12. Running SVM iteratively The class probability given to each document in U RN takes the value in [0, 1] instead of {0, 1}. The algorithm is given in Fig. 5.11. The EM algorithm here makes the same mixture model assumptions as in LU learning. Thus, it has the same problem of model mismatch. See the discussions in Sect. 5.1.1. Iterative SVM: In this method, SVM is run iteratively using P, RN and Q (= U RN). The algorithm, called I-SVM, is given in Fig. 5.12. The basic idea is as follows: In each iteration, a new SVM classifier f is constructed from P and RN (line 4). Here RN is regarded as the set of negative examples (line 2). The classifier f is then applied to classify the documents in Q (line 5). The set W of documents in Q that are classified as negative (line 5.2 Learning from Positive and Unlabeled Examples 175 6) is removed from Q (line 8) and added to RN (line 9). The iteration stops when no document in Q is classified as negative, i.e., W = (line 7). The final classifier is the result. This method is used in [321, 582, 583]. Finally, we note again that if the first step is able to identify a large number of reliable negative documents from U, running SVM once in step 2 is sufficient. Iterative approaches may not be necessary, which are also less efficient. The Spy, NB and Rocchio methods for step 1 are often able to identify a large number of reliable negative documents. See [340] for an evaluation of various methods based on two benchmark text collections. Classifier Selection The iterative methods discussed above produce a new classifier at each iteration. However, the classifier at the convergence may not be the best classifier. In general, each iteration of the algorithm gives a classifier that may potentially be a better classifier than the classifier produced at convergence. This is true for both EM and SVM. The main problem with EM is that classes and topics may not have oneto-one correspondence. This is the same problem as in LU learning. SVM may also produce poor classifiers at the convergence because SVM is sensitive to noise. If the RN set is not chosen well or in an iteration some positive documents are classified as negative, then the subsequent iterations may produce very poor results. In such cases, it is often better to stop at an earlier iteration. One simple method is to apply the theory directly. That is, each classifier is applied to classify a positive validation set, Pv. If many documents from Pv (e.g., > 5%) are classified as negative, the algorithm should stop (that is a recall of 95%). If the set P is small, the method can also be applied to P directly. A principled method is given in the next subsection, i.e., Equation (14). 5.2.4 Building Classifiers: Direct Approach We now present a direct approach, called biased-SVM. This approach modifies the SVM formulation slightly so that it is suitable for PU learning. Let the set of training examples be {(x1, y1), (x2, y2), …, (xn, yn)}, where xi is an input vector and yi is its class label, yi {1, 1}. Assume that the first k 1 examples are positive examples P (labeled 1), while the rest are unlabeled examples U, which are treated as negative and labeled 1. Thus, the negative set has errors, i.e., containing positive documents. We consider two cases. 176 5 Partially Supervised Learning 1. Noiseless case: There is no error in the positive examples but only in unlabeled examples. The theoretical result in Sect. 5.2.2 states that if the sample size is large enough, minimizing the number of unlabeled examples classified as positive while constraining the positive examples to be correctly classified will give a good classifier. Following the theory, in this noiseless case, we have this following SVM formulation ww 2 Minimize : n C (12) b 1, i 1,2,..., k 1 Subject to : w xi b) 1 1( w xi 0, i i i ik i i, k , k 1, ..., n k , k 1, ..., n In this formulation, we do not allow any error in the positive set P, which is the first constraint, but allow errors for the negative set (the original unlabeled set), which is the second constraint. Clearly, the formulation follows the theory exactly due to the second term in the objective function. The subscript in the second term starts from k, which is the index of the first unlabeled example. To distinguish this formulation from the classic SVM, we call it the biased-SVM [340]. 2. Noisy case: In practice, the positive set may also contain some errors. Thus, if we allow noise (or error) in positive examples, we have the following soft margin version of the biased-SVM which uses two parameters C+ and C to weigh positive errors and negative errors differently. ww 2 Minimize : Subject to : yi ( w x i i k1 C n i C i1 b) 1 i ik i, (13) i 1,2,..., n 0, i 1, 2, ..., n We can vary C+ and C to achieve our objective. Intuitively, we give a bigger value for C+ and a smaller value for C because the unlabeled set, which is assumed to be negative, contains positive data. We now focus on Equation (13) as it is more realistic in practice. We need to choose values for C+ and C . The common practice is to try a range of values for both C+ and C and use a separate validation set to verify the performance of the resulting classifier. The C+ and C values that give the best classification results on the validation set are selected as the final parameter values for them. Cross-validation is another possible technique for the purpose. Since the need to learn from positive and unlabeled examples 5.2 Learning from Positive and Unlabeled Examples 177 often arises in retrieval situations (retrieving positive documents from the unlabeled set), we employ the commonly used F-score as the performance measure, F = 2pr/(p+r), where p is the precision and r is the recall. Unfortunately it is not clear how to estimate the F-score without labeled negative examples. In [309], Lee and Liu proposed an alternative performance measure to compare different classifiers. It behaves similarly to the Fscore but can be estimated directly from the validation set without the need of labeled negative examples. The measure is r2 , Pr( f (x) 1) (14) where f is the classifier and Pr(f(x)=1) is the probability that a document is classified as positive. It is not easy to see why Equation (14) behaves similarly to the F-score, but we can show that r2/Pr(f(x)=1) = pr/Pr(y=1), where Pr(y=1) is the probability of positive documents. pr/Pr(y=1) behaves similarly to the F-score in the sense that it is large when both p and r are large and is small when either p or r is small. We first write recall (r) and precision (p) in terms of probability: r = Pr(f(x)=1| y=1) (15) p = Pr(y=1| f(x)=1). (16) According to probability theory, we have Pr(f(x)=1|y=1)Pr(y=1) = Pr(y=1| f(x)=1)Pr(f(x)=1), (17) which can be written as r Pr( f (x) 1) p . Pr( y 1) (18) Multiplying both sides by r, we obtain the result: r2 Pr( f ( x) 1) pr . Pr( y 1) (19) The quantity r2/Pr(f(x)=1) can be estimated based on the validation set, which contains both positive and unlabeled documents. r can be estimated using the positive examples in the validation set and Pr(f(x) = 1) can be estimated from the whole validation set. This criterion in fact reflects the theory in Sect. 5.2.2 very well. The quantity is large when r is large and Pr(f(x) = 1) is small, which means that the number of unlabeled examples labeled as positive should be small. In [340], it is shown that biased-SVM works better than two-step techniques. 178 5.2.5 5 Partially Supervised Learning Discussion Does PU Learning Always Work? Theoretical results show that it should if the positive set and the unlabeled set are sufficiently large [348]. This has been confirmed by many experimental studies. Interested readers can find the detailed results in [340, 348], which we summarize below: 1. PU learning can achieve about the same classification results as fully supervised learning (i.e., both labeled positive and negative examples are available for training) when the positive set and the unlabeled set are sufficiently large. This implies that labeled negative examples do not provide much information for learning. When the positive set is very small, PU learning is poorer than fully supervised learning. 2. For the two-step approaches, using SVM for the second step performs better than EM. SVM needs to be run only once if step 1 can extract a large number of reliable negative documents. Both Spy and Rocchio (and to some extent NB as well) are able to do that. Thus, the iterative method in step 2 is not necessary. The generative model of naïve Bayes with EM in the second step can perform very well if the mixture model assumptions hold [348]. However, if the mixture model assumptions do not hold, the classification results can be very poor [340]. Note that SVM is usually called a discriminative model (or classifier) because it does not make any model assumptions. It simply finds a hyperplane to separate positive and negative examples in the training data. 3. Biased-SVM performs slightly better than the 2-step approaches. However, it is very slow in training because SVM needs to be run a large number of times in order to select the best values for C+ and C . Evaluation: Unlike LU learning, here we do not even have labeled negative examples, which makes the evaluation difficult. Although Equation (14) and other heuristics allow a system to choose a “better” classifier among a set of classifiers, it is unable to give the actual accuracy, precision or recall of each classifier. Evaluation is an open problem. The results reported in [340, 348] assume that a set of labeled positive and negative test examples is available, which, of source, is unrealistic in practice because the PU learning model states that no labeled negative example is available. In some cases, the evaluation can be done with some confidence. For example, if the user needs to extract positive documents from many unlabeled sets as in the example of identifying printer pages from multiple Web sites, a PU learning algorithm can be applied to one site and then the user manually checks the classification result to see whether it is satisfactory. If the result is satisfactory, the algorithm can be applied to the rest of the sites without further manual evaluation. Appendix: Derivation of EM for Naïve Bayesian Classification 179 Appendix: Derivation of EM for Naïve Bayesian Classification EM is a method for performing a classical statistical estimation technique called maximum likelihood estimation. In maximum likelihood estimation, the aim is to find the model parameter ˆ that maximizes the likelihood function Pr(Do; ) for observed data Do. In other words, maximum likelihood estimation aims to select the model that is most likely to have generated the observed data. In many cases, such as in the naïve Bayesian classification model, the maximum likelihood estimator is easy to find and has a closed form solution when all components of the data D are observed. However, the problem becomes difficult when the data D actually consists of an observed component Do and an unobserved component Du. In such cases, iterative methods that converge only to a local maximum, such as the EM method, are usually used. Maximizing the log likelihood function logPr(Do; ) produces the same solution as maximizing the likelihood function and is easier to handle mathematically. In the presence of unobserved data Du, the log likelihood function becomes log Pr( Do ; ) log D Pr(Do , Du ; ). Instead of maxiu mizing the log likelihood log Du Pr( Do , Du ; ) directly, at each iteration T, the EM algorithm finds the value plete log likelihood Du E ( Du | Do ; T1 that maximizes the expected com- ) log Pr( Do , Du ; ), (20) where T 1 is the parameter that was produced in iteration T 1. In many cases, such as in the naïve Bayesian model, the expected log likelihood is easy to maximize and has a closed form solution. It can be shown (see [127]) that the log likelihood increases monotonically with each iteration of the EM algorithm. We now derive the EM update for the naïve Bayesian model. We first consider the complete log likelihood, that is, the log likelihood when all variables are observed. The conditional probability of a document given its class is (see Sect. 3.7.2 in Chap. 3) |V | Pr( wt | cj; ) N ti (21) Pr( di | cj; ) Pr(| di |) | di |! Nti! t1 Each document and its class label are assumed to have been sampled independently. Let c(i) be the class label of document i. The likelihood function can hence be written as 180 5 Partially Supervised Learning | D| | D| Pr(di | c ( i ); ) Pr(c ( i ); ) |V | Pr(| di |) | di |! i1 i1 t1 Pr( wt | c ( i ); ) N ti Pr(c ( i ); ). Nti! (22) Taking logs, we have the complete log likelihood function | D | |V | | D| N ti log Pr(wt | c ( i ); ) i1t1 log Pr(c ( i ); ) (23) , i1 where is a constant containing the terms unaffected by . To facilitate the process of taking expectation when some of the class labels are not observed, we introduce indicator variables, hik, that take the value 1 when document i takes the label k and the value 0 otherwise. The complete log likelihood can be written in the following equivalent form | D| |V | |C | | D| |C | hik N ti log Pr(wt | ck ; ) i1t1k1 hik log Pr(ck ; ) . (24) i1k1 When some of the labels are not observed, we take the conditional expectation for the unobserved variables hik with respect to T 1 to get the expected complete log likelihood | D| |V | |C | Pr(ck | d i ; T1 )N ti log Pr(wt | ck ; ) (25) i1t1k1 | D | |C | T1 Pr(ck | d i ; ) log Pr(ck ; ) , i1k1 where, for the observed labels c(i), we use the convention that Pr(ck|di; T 1) takes the value one for ck = c(i) and zero otherwise. We maximize the expected complete log likelihood subject to the coefficients summing to one using the Lagrange multiplier method. The Lagrangian is | D| |V | |C | T1 Pr(ck | di ; )Nti log Pr(wt | ck ; ) i1t1k1 (26) | D| |C | Pr(ck | di ; T1 ) log Pr(ck ; ) i1k1 |C | |V | |C | Pr(ck ; ) 1 k1 |V | tk t1k1 Pr(wt | ck ; ) 1 . t1 Differentiating the Lagrangian with respect to , we get Differentiating with respect to Pr(ck; ), we get |C | k1 Pr(ck ; ) 1. Bibliographic Notes | D| Pr(ck | d i ; T1 Pr(ck ; ) ) 181 (27) for k 1,...,| C | . i1 Summing the left and right-hand side over k and using we get | D | |C | T1 Pr(ck | di ; |C | k1 Pr(ck ; ) 1, ) | D |. Substituting back, we obtain the up- i1k1 date equation | D| T Pr(cj; i1 ) Pr(cj | di; T1 |D| ) (28) . Working similarly, we can get the update equation for Pr(wt|cj; | D| Pr( wt | c j ; T ) i1 N ti Pr(c j | d i ; |V | | D| s1 i1 T1 N si Pr(c j | d i ; ) T1 . T ), (29) ) To handle the 0 count problem (see Sect. 3.7.2 in Chap. 3), we can use Lidstone smoothing (Equation (27) in Chap. 3). Bibliographic Notes Learning with labeled and unlabeled examples (LU learning) using naïve Bayes and EM was proposed by Nigam et al. [413]. They also noted the problem of having mixtures of subclasses in the classes and proposed to identify and use such subclasses as a possible solution. A hierarchical clustering technique was also proposed by Cong et al. [111] for handling the mixture of subclasses problem. Castelli and Cover [83] presented a theoretical study of LU learning using mixture models. Co-training was introduced by Blum and Mitchell [55]. Follow-on works include those by Collins and Singer [109], Goldman and Zhou [201], etc. Generalization error bounds within the Probably Approximately Correct (PAC) framework was given in [121] by Dasgupta et al. Nigam and Ghani [411] examined the importance of feature division in co-training and compared it to the EM algorithm and self-training. Transduction was proposed by Vapnik [526] as learning when the test instances are known. Joachims described a heuristic algorithm and built a system for performing transduction using SVM [259]. The transductive SVM given in [259] can also be used for induction, i.e. classifying future unseen instances. In contrast, most graph-based methods are more suited 182 5 Partially Supervised Learning for transduction, i.e. classifying test instances that are known during training. The graph-based mincut algorithm was introduced by Blum and Chalwa [54]. The graph-based Gaussian field method was proposed by Zhu et al. [619] while the spectral graph transducer was proposed by Joachims [261]. The edited book by Chapelle et al. [93] gives a comprehensive coverage of various LU learning algorithms. On learning from positive and unlabeled examples (or PU learning), Denis [129] reported a theoretical study of PAC learning in this setting under the statistical query model [272], which basically assumes that the proportion of positive instances in the unlabeled set is known. Letouzey et al. [315] presented a learning algorithm based on a modified decision tree method in this model. PU learning is also studied theoretically by Muggleton [398] from the Bayesian framework where the distribution of functions and examples are assumed known. Liu et al. [348] gives another theoretical study, in which both the noiseless case and the noisy case are considered. It was concluded that a reasonable generalization (learning) can be achieved if the problem is posed as a constrained optimization problem (see Sect. 5.2.2). Most existing algorithms for solving the problem are based on this constrained optimization model. Over the years, several practical algorithms were proposed. The first class of algorithms deals with the problem in two steps. These algorithms include S-EM [348], PEBL [582, 583], and Roc-SVM [321], which have been studied in this chapter. The second class of algorithm follows the theoretical result directly. Lee and Liu [309] described a weighted logistic regression technique. Liu et al. [340] described a biased-SVM technique. They both require a performance criterion to determine the quality of the classifier. The criterion is given in [309], which has been presented in Sect. 5.2.4. Liu et al. reported a comprehensive comparison of various techniques in [340]. It was shown that biased-SVM performed better than other techniques. Some other works on PU learning include those of Agichtein [6], Barbara et al. [35], Deng et al. [128], Denise et al. [130], Fung, et al. [188], Li and Liu [322], Zhang and Lee [603], etc. A closely related work to PU learning is one-class SVM, which uses only positive examples to build a classifier. This method was proposed by Scholkopf et al. [478]. Manevitz and Yousef [360] studied text classification using one-class SVM. Li and Liu [321] showed that its accuracy results were poorer than PU learning for text classification. Unlabeled data does help classification significantly. 6 Information Retrieval and Web Search Web search needs no introduction. Due to its convenience and the richness of information on the Web, searching the Web is increasingly becoming the dominant information seeking method. People make fewer and fewer trips to libraries, but more and more searches on the Web. In fact, without effective search engines and rich Web contents, writing this book would have been much harder. Web search has its root in information retrieval (or IR for short), a field of study that helps the user find needed information from a large collection of text documents. Traditional IR assumes that the basic information unit is a document, and a large collection of documents is available to form the text database. On the Web, the documents are Web pages. Retrieving information simply means finding a set of documents that is relevant to the user query. A ranking of the set of documents is usually also performed according to their relevance scores to the query. The most commonly used query format is a list of keywords, which are also called terms. IR is different from data retrieval in databases using SQL queries because the data in databases are highly structured and stored in relational tables, while information in text is unstructured. There is no structured query language like SQL for text retrieval. It is safe to say that Web search is the single most important application of IR. To a great extent, Web search also helped IR. Indeed, the tremendous success of search engines has pushed IR to the center stage. Search is, however, not simply a straightforward application of traditional IR models. It uses some IR results, but it also has its unique techniques and presents many new problems for IR research. First of all, efficiency is a paramount issue for Web search, but is only secondary in traditional IR systems mainly due to the fact that document collections in most IR systems are not very large. However, the number of pages on the Web is huge. For example, Google indexed more than 8 billion pages when this book was written. Web users also demand very fast responses. No matter how effective an algorithm is, if the retrieval cannot be done efficiently, few people will use it. Web pages are also quite different from conventional text documents used in traditional IR systems. First, Web pages have hyperlinks and an- 184 6 Information Retrieval and Web Search chor texts, which do not exist in traditional documents (except citations in research publications). Hyperlinks are extremely important for search and play a central role in search ranking algorithms as we will see in the next chapter. Anchor texts associated with hyperlinks too are crucial because a piece of anchor text is often a more accurate description of the page that its hyperlink points to. Second, Web pages are semi-structured. A Web page is not simply a few paragraphs of text like in a traditional document. A Web page has different fields, e.g., title, metadata, body, etc. The information contained in certain fields (e.g., the title field) is more important than in others. Furthermore, the content in a page is typically organized and presented in several structured blocks (of rectangular shapes). Some blocks are important and some are not (e.g., advertisements, privacy policy, copyright notices, etc). Effectively detecting the main content block(s) of a Web page is useful to Web search because terms appearing in such blocks are more important. Finally, spamming is a major issue on the Web, but not a concern for traditional IR. This is so because the rank position of a page returned by a search engine is extremely important. If a page is relevant to a query but is ranked very low (e.g., below top 30), then the user is unlikely to look at the page. If the page sells a product, then this is bad for the business. In order to improve the ranking of some target pages, “illegitimate” means, called spamming, are often used to boost their rank positions. Detecting and fighting Web spam is a critical issue as it can push low quality (even irrelevant) pages to the top of the search rank, which harms the quality of the search results and the user’s search experience. In this chapter, we first study some information retrieval models and methods that are closely related to Web search. We then dive into some Web search specific issues. 6.1 Basic Concepts of Information Retrieval Information retrieval (IR) is the study of helping users to find information that matches their information needs. Technically, IR studies the acquisition, organization, storage, retrieval, and distribution of information. Historically, IR is about document retrieval, emphasizing document as the basic unit. Fig. 6.1 gives a general architecture of an IR system. In Figure 6.1, the user with information need issues a query (user query) to the retrieval system through the query operations module. The retrieval module uses the document index to retrieve those documents that contain some query terms (such documents are likely to be relevant to the query), compute relevance scores for them, and then rank the retrieved 6.1 Basic Concepts of Information Retrieval 185 documents according to the scores. The ranked documents are then presented to the user. The document collection is also called the text database, which is indexed by the indexer for efficient retrieval. The user User query Document collection Query user operations feedback Executable query Ranked documents Retrieval system indexer Document index Fig. 6.1. A general IR system architecture A user query represents the user’s information needs, which is in one of the following forms: 1. Keyword queries: The user expresses his/her information needs with a list of (at least one) keywords (or terms) aiming to find documents that contain some (at least one) or all the query terms. The terms in the list are assumed to be connected with a “soft” version of the logical AND. For example, if one is interested in finding information about Web mining, one may issue the query ‘Web mining’ to an IR or search engine system. ‘Web mining’ is retreated as ‘Web AND mining’. The retrieval system then finds those likely relevant documents and ranks them suitably to present to the user. Note that a retrieved document does not have to contain all the terms in the query. In some IR systems, the ordering of the words is also significant and will affect the retrieval results. 2. Boolean queries: The user can use Boolean operators, AND, OR, and NOT to construct complex queries. Thus, such queries consist of terms and Boolean operators. For example, ‘data OR Web’ is a Boolean query, which requests documents that contain the word ‘data’ or ‘Web. A page is returned for a Boolean query if the query is logically true in the page (i.e., exact match). Although one can write complex Boolean queries using the three operators, users seldom write such queries. Search engines usually support a restricted version of Boolean queries. 3. Phrase queries: Such a query consists of a sequence of words that makes up a phrase. Each returned document must contain at least one 186 6 Information Retrieval and Web Search instance of the phrase. In a search engine, a phrase query is normally enclosed with double quotes. For example, one can issue the following phrase query (including the double quotes), “Web mining techniques and applications” to find documents that contain the exact phrase. 4. Proximity queries: The proximity query is a relaxed version of the phrase query and can be a combination of terms and phrases. Proximity queries seek the query terms within close proximity to each other. The closeness is used as a factor in ranking the returned documents or pages. For example, a document that contains all query terms close together is considered more relevant than a page in which the query terms are far apart. Some systems allow the user to specify the maximum allowed distance between the query terms. Most search engines consider both term proximity and term ordering in retrieval. 5. Full document queries: When the query is a full document, the user wants to find other documents that are similar to the query document. Some search engines (e.g., Google) allow the user to issue such a query by providing the URL of a query page. Additionally, in the returned results of a search engine, each snippet may have a link called “more like this” or “similar pages.” When the user clicks on the link, a set of pages similar to the page in the snippet is returned. 6. Natural language questions: This is the most complex case, and also the ideal case. The user expresses his/her information need as a natural language question. The system then finds the answer. However, such queries are still hard to handle due to the difficulty of natural language understanding. Nevertheless, this is an active research area, called question answering. Some search systems are starting to provide question answering services on some specific types of questions, e.g., definition questions, which ask for definitions of technical terms. Definition questions are usually easier to answer because there are strong linguistic patterns indicating definition sentences, e.g., “defined as”, “refers to”, etc. Definitions can usually be extracted offline [339, 280]. The query operations module can range from very simple to very complex. In the simplest case, it does nothing but just pass the query to the retrieval engine after some simple pre-processing, e.g., removal of stopwords (words that occur very frequently in text but have little meaning, e.g., “the”, “a”, “in”, etc). We will discuss text pre-processing in Sect. 6.5. In more complex cases, it needs to transform natural language queries into executable queries. It may also accept user feedback and use it to expand and refine the original queries. This is usually called relevance feedback, which will be discussed in Sect. 6.3. The indexer is the module that indexes the original raw documents in some data structures to enable efficient retrieval. The result is the docu- 6.2 Information Retrieval Models 187 ment index. In Sect. 6.6, we study a particular type of indexing scheme, called the inverted index, which is used in search engines and most IR systems. An inverted index is easy to build and very efficient to search. The retrieval system computes a relevance score for each indexed document to the query. According to their relevance scores, the documents are ranked and presented to the user. Note that it usually does not compare the user query with every document in the collection, which is too inefficient. Instead, only a small subset of the documents that contains at least one query term is first found from the index and relevance scores with the user query are then computed only for this subset of documents. 6.2 Information Retrieval Models An IR model governs how a document and a query are represented and how the relevance of a document to a user query is defined. There are four main IR models: Boolean model, vector space model, language model and probabilistic model. The most commonly used models in IR systems and on the Web are the first three models, which we study in this section. Although these three models represent documents and queries differently, they used the same framework. They all treat each document or query as a “bag” of words or terms. Term sequence and position in a sentence or a document are ignored. That is, a document is described by a set of distinctive terms. A term is simply a word whose semantics helps remember the document’s main themes. We should note that the term here may not be a natural language word in a dictionary. Each term is associated with a weight. Given a collection of documents D, let V = {t1, t2, ..., t|V|} be the set of distinctive terms in the collection, where ti is a term. The set V is usually called the vocabulary of the collection, and |V| is its size, i.e., the number of terms in V. A weight wij > 0 is associated with each term ti of a document dj D. For a term that does not appear in document dj, wij = 0. Each document dj is thus represented with a term vector, dj = (w1j, w2j, ..., w|V|j), where each weight wij corresponds to the term ti V, and quantifies the level of importance of ti in document dj. The sequence of the components (or terms) in the vector is not significant. Note that following the convention of this book, a bold lower case letter is used to represent a vector. With this vector representation, a collection of documents is simply represented as a relational table (or a matrix). Each term is an attribute, and each weight is an attribute value. In different retrieval models, wij is computed differently. 188 6.2.1 6 Information Retrieval and Web Search Boolean Model The Boolean model is one of the earliest and simplest information retrieval models. It uses the notion of exact matching to match documents to the user query. Both the query and the retrieval are based on Boolean algebra. Document Representation: In the Boolean model, documents and queries are represented as sets of terms. That is, each term is only considered present or absent in a document. Using the vector representation of the document above, the weight wij ( {0, 1}) of term ti in document dj is 1 if ti appears in document dj, and 0 otherwise, i.e., wij 1 if ti appears in d j 0 otherwise. (1) Boolean Queries: As we mentioned in Sect. 6.1, query terms are combined logically using the Boolean operators AND, OR, and NOT, which have their usual semantics in logic. Thus, a Boolean query has a precise semantics. For instance, the query, ((x AND y) AND (NOT z)) says that a retrieved document must contain both the terms x and y but not z. As another example, the query expression (x OR y) means that at least one of these terms must be in each retrieved document. Here, we assume that x, y and z are terms. In general, they can be Boolean expressions themselves. Document Retrieval: Given a Boolean query, the system retrieves every document that makes the query logically true. Thus, the retrieval is based on the binary decision criterion, i.e., a document is either relevant or irrelevant. Intuitively, this is called exact match. There is no notion of partial match or ranking of the retrieved documents. This is one of the major disadvantages of the Boolean model, which often leads to poor retrieval results. It is quite clear that the frequency of terms and their proximity contribute significantly to the relevance of a document. Due to this problem, the Boolean model is seldom used alone in practice. Most search engines support some limited forms of Boolean retrieval using explicit inclusion and exclusion operators. For example, the following query can be issued to Google, ‘mining –data +“equipment price”’, where + (inclusion) and – (exclusion) are similar to Boolean operators AND and NOT respectively. The operator OR may be supported as well. 6.2.2 Vector Space Model This model is perhaps the best known and most widely used IR model. 6.2 Information Retrieval Models 189 Document Representation A document in the vector space model is represented as a weight vector, in which each component weight is computed based on some variation of TF or TF-IDF scheme. The weight wij of term ti in document dj is no longer in {0, 1} as in the Boolean model, but can be any number. Term Frequency (TF) Scheme: In this method, the weight of a term ti in document dj is the number of times that ti appears in document dj, denoted by fij. Normalization may also be applied (see Equation (2)). The shortcoming of the TF scheme is that it does not consider the situation where a term appears in many documents of the collection. Such a term may not be discriminative. TF-IDF Scheme: This is the most well known weighting scheme, where TF still stands for the term frequency and IDF the inverse document frequency. There are several variations of this scheme. Here we only give the most basic one. Let N be the total number of documents in the system or the collection and dfi be the number of documents in which term ti appears at least once. Let fij be the raw frequency count of term ti in document dj. Then, the normalized term frequency (denoted by tfij) of ti in dj is given by tf ij f ij max{ f1 j , f 2 j ,..., f|V | j } , (2) where the maximum is computed over all terms that appear in document dj. If term ti does not appear in dj then tfij = 0. Recall that |V| is the vocabulary size of the collection. The inverse document frequency (denoted by idfi) of term ti is given by: idf i log N . df i (3) The intuition here is that if a term appears in a large number of documents in the collection, it is probably not important or not discriminative. The final TF-IDF term weight is given by: wij tf ij idf i . (4) Queries A query q is represented in exactly the same way as a document in the document collection. The term weight wiq of each term ti in q can also be 190 6 Information Retrieval and Web Search computed in the same way as in a normal document, or slightly differently. For example, Salton and Buckley [470] suggested the following: wi q 0.5 0.5 f iq log max{ f1q , f 2 q ,..., f |V |q } N . df i (5) Document Retrieval and Relevance Ranking It is often difficult to make a binary decision on whether a document is relevant to a given query. Unlike the Boolean model, the vector space model does not make such a decision. Instead, the documents are ranked according to their degrees of relevance to the query. One way to compute the degree of relevance is to calculate the similarity of the query q to each document dj in the document collection D. There are many similarity measures. The most well known one is the cosine similarity, which is the cosine of the angle between the query vector q and the document vector dj, cosine(d j , q) |V | dj q || d j || || q || i1 wij wiq |V | |V | i i w2 1 ij . (6) w2 1 iq Cosine similarity is also widely used in text/document clustering. The dot product of the two vectors is another similarity measure, sim(d j , q) dj q . (7) Ranking of the documents is done using their similarity values. The top ranked documents are regarded as more relevant to the query. Another way to assess the degree of relevance is to directly compute a relevance score for each document to the query. The Okapi method and its variations are popular techniques in this setting. The Okapi retrieval formula given here is based on that in [465, 493]. It has been shown that Okapi variations are more effective than cosine for short query retrieval. Since it is easier to present the formula directly using the “bag” of words notation of documents than vectors, document dj will be denoted by dj and query q will be denoted by q. Additional notations are as follows: ti is a term fij is the raw frequency count of term ti in document dj fiq is the raw frequency count of term ti in query q N is the total number of documents in the collection dfi is the number of documents that contain the term ti dlj is the document length (in bytes) of dj avdl is the average document length of the collection 6.2 Information Retrieval Models 191 The Okapi relevance score of a document dj for a query q is: okapi ( d j , q ) ln N df i df i ti q , d j 0 .5 0 .5 ( k1 1) f ij dl j k1 (1 b b ) avdl ( k 2 1) f iq f ij k2 f iq , (8) where k1 (between 1.0-2.0), b (usually 0.75) and k2 (between 1-1000) are parameters. Yet another score function is the pivoted normalization weighting score function, denoted by pnw [493]: pnw(d j , q) ti 1 ln(1 ln( f ij )) dl j q,d j (1 s ) s avdl f iq ln N 1, df i (9) where s is a parameter (usually set to 0.2). Note that these are empirical functions based on intuitions and experimental evaluations. There are many variations of these functions used in practice. 6.2.3 Statistical Language Model Statistical language models (or simply language models) are based on probability and have foundations in statistical theory. The basic idea of this approach to retrieval is simple. It first estimates a language model for each document, and then ranks documents by the likelihood of the query given the language model. Similar ideas have previously been used in natural language processing and speech recognition. The formulation and discussion in this section is based on those in [595, 596]. Information retrieval using language models was first proposed by Ponte and Croft [448]. Let the query q be a sequence of terms, q = q1q2…qm and the document collection D be a set of documents, D = {d1, d2, …, dN}. In the language modeling approach, we consider the probability of a query q as being “generated” by a probabilistic model based on a document dj, i.e., Pr(q|dj). To rank documents in retrieval, we are interested in estimating the posterior probability Pr(dj|q). Using the Bayes rule, we have Pr(d j | q) Pr(q | d j ) Pr(d j ) Pr(q) (10) For ranking, Pr(q) is not needed as it is the same for every document. Pr(dj) is usually considered uniform and thus will not affect ranking. We only need to compute Pr(q|dj). The language model used in most existing work is based on unigram, 192 6 Information Retrieval and Web Search i.e., only individual terms (words) are considered. That is, the model assumes that each term (word) is generated independently, which is essentially a multinomial distribution over words. The general case is the ngram model, where the nth term is conditioned on the previous n-1 terms. Based on the multinomial distribution and the unigram model, we have |V | m Pr( q q1q 2 ...qm | d j ) Pr( qi | d j ) f Pr(t i | d j ) iq , (11) i1 i1 where fiq is the number of times that term ti occurs in q, and |V | Pr(ti | d j ) 1 . The retrieval problem is reduced to estimating Pr(ti|dj), i1 which can be the relative frequency, Pr(ti | d j ) f ij |dj | . (12) Recall that fij is the number of times that term ti occurs in document dj. |dj| denotes the total number of words in dj. However, one problem with this estimation is that a term that does not appear in dj has the probability of 0, which underestimates the probability of the unseen term in the document. This situation is similar to text classification using the naïve Bayesian model (see Sect. 3.7). A non-zero probability is typically assigned to each unseen term in the document, which is called smoothing. Smoothing adjusts the estimates of probabilities to produce more accurate probabilities. The name smoothing comes from the fact that these techniques tend to make distributions more uniform, by adjusting low probabilities such as zero probabilities upward, and high probabilities downward. Not only do smoothing methods aim to prevent zero probabilities, but they also attempt to improve the accuracy of the model as a whole. Traditional additive smoothing is Pradd (ti | d j ) fij |V | | d j | . (13) When = 1, it is the Laplace smoothing and when 0 < < 1, it is the Lidstone smoothing. Many other more sophisticated smoothing methods can be found in [97, 596]. 6.3 Relevance Feedback To improve the retrieval effectiveness, researchers have proposed many techniques. Relevance feedback is one of the effective ones. It is a process 6.3 Relevance Feedback 193 where the user identifies some relevant and irrelevant documents in the initial list of retrieved documents, and the system then creates an expanded query by extracting some additional terms from the sample relevant and irrelevant documents for a second round of retrieval. The system may also produce a classification model using the user-identified relevant and irrelevant documents to classify the documents in the document collection into relevant and irrelevant documents. The relevance feedback process may be repeated until the user is satisfied with the retrieved result. The Rocchio Method This is one of the early and effective relevance feedback algorithms. It is based on the first approach above. That is, it uses the user-identified relevant and irrelevant documents to expand the original query. The new (or expanded) query is then used to perform retrieval again. Let the original query vector be q, the set of relevant documents selected by the user be Dr, and the set of irrelevant documents be Dir. The expanded query qe is computed as follows, qe q | Dr | d r dr Dr | Dir | d ir d ir , (14) Dir where , and are parameters. Equation (14) simply augments the original query vector q with additional terms from relevant documents. The original query q is still needed because it directly reflects the user’s information need. Relevant documents are considered more important than irrelevant documents. The subtraction is used to reduce the influence of those terms that are not discriminative (i.e., they appear in both relevant and irrelevant documents), and those terms that appear in irrelevant documents only. The three parameters are set empirically. Note that a slight variation of the algorithm is one without the normalization of |Dr| and |Dir|. Both these methods are simple and efficient to compute, and usually produce good results. Machine Learning Methods Since we have a set of relevant and irrelevant documents, we can construct a classification model from them. Then the relevance feedback problem becomes a learning problem. Any supervised learning method may be used, e.g., naïve Bayesian classification and SVM. Similarity comparison with the original query is no longer needed. In fact, a variation of the Rocchio method above, called the Rocchio classification method, can be used for this purpose too. Building a Roc- 194 6 Information Retrieval and Web Search chio classifier is done by constructing a prototype vector ci for each class i, which is either relevant or irrelevant in this case (negative elements or components of the vector ci are usually set to 0): ci d | Di | d Di || d || | D Di | d D d , Di || d || (15) where Di is the set of documents of class i, and and are parameters. Using the TF-IDF term weighting scheme, = 16 and = 4 usually work quite well. In classification, cosine similarity is applied. That is, each test document dt is compared with every prototype ci based on cosine similarity. dt is assigned to the class with the highest similarity value (Fig. 6.2). 1 2 3 4 5 6 Algorithm for each class i do construct its prototype vector ci using Equation (15) endfor for each test document dt do the class of dt is arg max i cosine (d t , c i ) endfor Fig. 6.2. Training and testing of a Rocchio classifier Apart from the above classic methods, the following learning techniques are also applicable: Learning from Labeled and Unlabeled Examples (LU Learning): Since the number of user-selected relevant and irrelevant documents may be small, it can be difficult to build an accurate classifier. However, unlabeled examples, i.e., those documents that are not selected by the user, can be utilized to improve learning to produce a more accurate classifier. This fits the LU learning model exactly (see Sect. 5.1). The user-selected relevant and irrelevant documents form the small labeled training set. Learning from Positive and Unlabeled Examples (PU Learning): The two learning models mentioned above assume that the user can confidently identify both relevant and irrelevant documents. However, in some cases, the user only selects (or clicks) documents that he/she feels relevant based on the title or summary information (e.g., snippets in Web search), which are most likely to be true relevant documents, but does not indicate irrelevant documents. Those documents that are not selected by the user may not be treated as irrelevant because he/she has not seen them. Thus, they can only be regarded as unlabeled documents. This is called implicit feedback. In order to learn in this case, we can use PU learning, i.e., learning 6.4 Evaluation Measures 195 from positive and unlabeled examples (see Sect. 5.2). We regard the userselected documents as positive examples, and unselected documents as unlabeled examples. Researchers have experimented with this approach in the Web search context and obtained good results [128]. Using Ranking SVM and Language Models: In the implicit feedback setting, a technique called ranking SVM is proposed in [260] to rank the unselected documents based on the selected documents. A language model based approach is also proposed in [487]. Pseudo-Relevance Feedback Pseudo-relevance feedback is another technique used to improve retrieval effectiveness. Its basic idea is to extract some terms (usually frequent terms) from the top-ranked documents and add them to the original query to form a new query for a second round of retrieval. Again, the process can be repeated until the user is satisfied with the final results. The main difference between this method and the relevance feedback method is that in this method, the user is not involved in the process. The approach simply assumes that the top-ranked documents are likely to be relevant. Through query expansion, some relevant documents missed in the initial round can be retrieved to improve the overall performance. Clearly, the effectiveness of this method relies on the quality of the selected expansion terms. 6.4 Evaluation Measures Precision and recall measures have been described in Chap. 3 on supervised learning, where each document is classified to a specific class. In IR and Web search, usually no decision is made on whether a document is relevant or irrelevant to a query. Instead, a ranking of the documents is produced for the user. This section studies how to evaluate such rankings. Again, let the collection of documents in the database be D, and the total number of documents in D be N. Given a user query q, the retrieval algorithm first computes relevance scores for all documents in D and then produce a ranking Rq of the documents based on the relevance scores, i.e., Rq : q (16) q q d1 , d 2 ,..., d q , N q where d1 D is the most relevant document to query q and d N D is the most irrelevant document to query q. Let Dq ( D) be the set of actual relevant documents of query q in D. q We can compute the precision and recall values at each di in the ranking. 196 6 Information Retrieval and Web Search Recall at rank position i or document diq (denoted by r(i)) is the fraction of relevant documents from d1q to diq in Rq. Let the number of relevant documents from d1q to diq in Rq be si ( |Dq|) (|Dq| is the size of Dq). Then, r (i ) si . | Dq | (17) Precision at rank position i or document diq (denoted by p(i)) is the fraction of documents from d1q to diq in Rq that are relevant: s (18) p (i ) i i Example 1: We have a document collection D with 20 documents. Given a query q, we know that eight documents are relevant to q. A retrieval algorithm produces the ranking (of all documents in D) shown in Fig. 6.3. Rank i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 +/ + + + + + + + + p(i) 1/1 = 100% 2/2 = 100% 3/3 = 100% 3/4 = 75% 4/5 = 80% 4/6 = 67% 5/7 = 71% 5/8 = 63% 6/9 = 67% 7/10 = 70% 7/11 = 63% 7/12 = 58% 8/13 = 62% 8/14 = 57% 8/15 = 53% 8/16 = 50% 8/17 = 53% 8/18 = 44% 8/19 = 42% 8/20 = 40% r(i) 1/8 = 13% 2/8 = 25% 3/8 = 38% 3/8 = 38% 4/8 = 50% 4/8 = 50% 5/8 = 63% 5/8 = 63% 6/8 = 75% 7/8 = 88% 7/8 = 88% 7/8 = 88% 8/8 = 100% 8/8 = 100% 8/8 = 100% 8/8 = 100% 8/8 = 100% 8/8 = 100% 8/8 = 100% 8/8 = 100% Fig. 6.3. Precision and recall values at each rank position In column 1 of Fig. 6.3, 1 represents the highest rank and 20 represents the lowest rank. “+” and “ ” in column 2 indicate a relevant document and an irrelevant document respectively. The precision (p(i)) and recall (r(i)) values at each position i are given in columns 3 and 4. 6.4 Evaluation Measures 197 Average Precision: Sometimes we want a single precision to compare different retrieval algorithms on a query q. An average precision (pavg) can be computed based on the precision at each relevant document in the ranking, d iq Dq p avg p (i ) | Dq | (19) . For the ranking in Fig. 6.3 of Example 1, the average precision is 81%: p avg 100% 100% 100% 80% 71% 67% 70% 62% 8 81%. (20) Precision–Recall Curve: Based on the precision and recall values at each rank position, we can draw a precision–recall curve where the x-axis is the recall and the y-axis is the precision. Instead of using the precision and recall at each rank position, the curve is commonly plotted using 11 standard recall levels, 0%, 10%, 20%, …, 100%. Since we may not obtain exactly these recall levels in the ranking, interpolation is needed to obtain the precisions at these recall levels, which is done as follows: Let ri be a recall level, i {0, 1, 2, …, 10}, and p(ri) be the precision at the recall level ri. p(ri) is computed with p (ri ) max ri r r10 (21) p(r ) . That is, to interpolate precision at a particular recall level ri, we take the maximum precision of all recalls between level ri and level r10. Example 2: Following Example 1, we obtain the interpolated precisions at all 11 recall levels in the table of Fig. 6.4. The precision-recall curve is shown on the right. p(ri) ri 0 1 2 3 4 5 6 7 8 9 10 100% 100% 100% 100% 80% 80% 71% 70% 70% 62% 62% 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% Precision i 100% 90% 80% 70% 60% 50% 40% 30% 20% 10% 0% 0% Fig. 6.4. The precision-recall curve 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% Recall 198 6 Information Retrieval and Web Search Comparing Different Algorithms: Frequently, we need to compare the retrieval results of different algorithms. We can draw their precision-recall curves together in the same figure for comparison. Figure 6.5 shows the curves of two algorithms on the same query and the same document collection. We observe that the precisions of one algorithm are better than those of the other at low recall levels, but are worse at high recall levels. 100% 90% 80% Precision 70% 60% 50% 40% 30% 20% 10% 0% 0% 10% 20% 30% 40% 50% 60% Recall 70% 80% 90% 100% Fig. 6.5. Comparison of two retrieval algorithms based on their precision-recall curves Evaluation Using Multiple Queries: In most retrieval evaluations, we are interested in the performance of an algorithm on a large number of queries. The overall precision (denoted by p (ri ) ) at each recall level ri is computed as the average of individual precisions at that recall level, i.e., p (ri ) 1 |Q| |Q| p j (ri ), (22) j1 where Q is the set of all queries and pj(ri) is the precision of query j at the recall level ri. Using the average precision at each recall level, we can also draw a precision-recall curve. Although in theory precision and recall do not depend on each other, in practice a high recall is almost always achieved at the expense of precision, and a high precision is achieved at the expense of recall. Thus, precision and recall has a trade-off. Depending on the application, one may want a high precision or a high recall. One problem with precision and recall measures is that, in many applications, it can be very hard to determine the set of relevant documents Dq for each query q. For example, on the Web, Dq is almost impossible to determine because there are simply too many pages to manually inspect. Without Dq, the recall value cannot be computed. In fact, recall does not make much sense for Web search because the user seldom looks at pages 6.5 Text and Web Page Pre-Processing 199 ranked below 30. However, precision is critical, and it can be estimated for top ranked documents. Manual inspection of only the top 30 pages is reasonable. The following precision computation is commonly used. Rank Precision: We compute the precision values at some selected rank positions. For a Web search engine, we usually compute precisions for the top 5, 10, 15, 20, 25 and 30 returned pages (as the user seldom looks at more than 30 pages). We assume that the number of relevant pages is more than 30. Following Example 1, we have p(5) = 80%, p(10) = 70%, p(15) = 53%, and p(20) = 40%. We should note that precision is not the only measure for evaluating search ranking, reputation or quality of the top ranked pages are also very important as we will see later in this chapter and also in Chap. 7. F-score: Another often used evaluation measure is the F-score, which we have used in Chap. 3. Here we can compute the F-score at each rank position i. Recall that F-score is the harmonic mean of precision and recall: F (i ) 2 1 r (i ) 1 p (i ) 2 p (i )r (i ) . p (i ) r (i ) (23) Finally, the precision and recall breakeven point is also a commonly used measure, which we have discussed in Sect. 3.3.2 in Chap. 3. 6.5 Text and Web Page Pre-Processing Before the documents in a collection are used for retrieval, some preprocessing tasks are usually performed. For traditional text documents (no HTML tags), the tasks are stopword removal, stemming, and handling of digits, hyphens, punctuations, and cases of letters. For Web pages, additional tasks such as HTML tag removal and identification of main content blocks also require careful considerations. We discuss them in this section. 6.5.1 Stopword Removal Stopwords are frequently occurring and insignificant words in a language that help construct sentences but do not represent any content of the documents. Articles, prepositions and conjunctions and some pronouns are natural candidates. Common stopwords in English include: a, about, an, are, as, at, be, by, for, from, how, in, is, of, on, or, that, the, these, this, to, was, what, when, where, who, will, with 200 6 Information Retrieval and Web Search Such words should be removed before documents are indexed and stored. Stopwords in the query are also removed before retrieval is performed. 6.5.2 Stemming In many languages, a word has various syntactical forms depending on the contexts that it is used. For example, in English, nouns have plural forms, verbs have gerund forms (by adding “ing”), and verbs used in the past tense are different from the present tense. These are considered as syntactic variations of the same root form. Such variations cause low recall for a retrieval system because a relevant document may contain a variation of a query word but not the exact word itself. This problem can be partially dealt with by stemming. Stemming refers to the process of reducing words to their stems or roots. A stem is the portion of a word that is left after removing its prefixes and suffixes. In English, most variants of a word are generated by the introduction of suffixes (rather than prefixes). Thus, stemming in English usually means suffix removal, or stripping. For example, “computer”, “computing”, and “compute” are reduced to “comput”. “walks”, “walking” and “walker” are reduced to “walk”. Stemming enables different variations of the word to be considered in retrieval, which improves the recall. There are several stemming algorithms, also known as stemmers. In English, the most popular stemmer is perhaps the Martin Porter's stemming algorithm [449], which uses a set of rules for stemming. Over the years, many researchers evaluated the advantages and disadvantages of using stemming. Clearly, stemming increases the recall and reduces the size of the indexing structure. However, it can hurt precision because many irrelevant documents may be considered relevant. For example, both “cop” and “cope” are reduced to the stem “cop”. However, if one is looking for documents about police, a document that contains only “cope” is unlikely to be relevant. Although many experiments have been conducted by researchers, there is still no conclusive evidence one way or the other. In practice, one should experiment with the document collection at hand to see whether stemming helps. 6.5.3 Other Pre-Processing Tasks for Text Digits: Numbers and terms that contain digits are removed in traditional IR systems except some specific types, e.g., dates, times, and other prespecified types expressed with regular expressions. However, in search engines, they are usually indexed. 6.5 Text and Web Page Pre-Processing 201 Hyphens: Breaking hyphens are usually applied to deal with inconsistency of usage. For example, some people use “state-of-the-art”, but others use “state of the art”. If the hyphens in the first case are removed, we eliminate the inconsistency problem. However, some words may have a hyphen as an integral part of the word, e.g., “Y-21”. Thus, in general, the system can follow a general rule (e.g., removing all hyphens) and also have some exceptions. Note that there are two types of removal, i.e., (1) each hyphen is replaced with a space and (2) each hyphen is simply removed without leaving a space so that “state-of-the-art” may be replaced with “state of the art” or “stateoftheart”. In some systems both forms are indexed as it is hard to determine which is correct, e.g., if “pre-processing” is converted to “pre processing”, then some relevant pages will not be found if the query term is “preprocessing”. Punctuation Marks: Punctuation can be dealt with similarly as hyphens. Case of Letters: All the letters are usually converted to either the upper or lower case. 6.5.4 Web Page Pre-Processing We have indicated at the beginning of the section that Web pages are different from traditional text documents. Thus, additional pre-processing is needed. We describe some important ones below. 1. Identifying different text fields: In HTML, there are different text fields, e.g., title, metadata, and body. Identifying them allows the retrieval system to treat terms in different fields differently. For example, in search engines terms that appear in the title field of a page are regarded as more important than terms that appear in other fields and are assigned higher weights because the title is usually a concise description of the page. In the body text, those emphasized terms (e.g., under header tags <h1>, <h2>, …, bold tag <b>, etc.) are also given higher weights. 2. Identifying anchor text: Anchor text associated with a hyperlink is treated specially in search engines because the anchor text often represents a more accurate description of the information contained in the page pointed to by its link. In the case that the hyperlink points to an external page (not in the same site), it is especially valuable because it is a summary description of the page given by other people rather than the author/owner of the page, and is thus more trustworthy. 3. Removing HTML tags: The removal of HTML tags can be dealt with similarly to punctuation. One issue needs careful consideration, which affects proximity queries and phrase queries. HTML is inherently a vis- 202 6 Information Retrieval and Web Search Fig. 6.6. An example of a Web page from Wikipedia ual presentation language. In a typical commercial page, information is presented in many rectangular blocks (see Fig. 6.6). Simply removing HTML tags may cause problems by joining text that should not be joined. For example, in Fig. 6.6, “cite this article” at the bottom of the left column will join “Main Page” on the right, but they should not be joined. They will cause problems for phrase queries and proximity queries. This problem had not been dealt with satisfactorily by search engines at the time when this book was written. 4. Identifying main content blocks: A typical Web page, especially a commercial page, contains a large amount of information that is not part of the main content of the page. For example, it may contain banner ads, navigation bars, copyright notices, etc., which can lead to poor results for search and mining. In Fig. 6.6, the main content block of the page is the block containing “Today’s featured article.” It is not desirable to index anchor texts of the navigation links as a part of the content of this page. Several researchers have studied the problem of identifying main content blocks. They showed that search and data mining results can be 6.5 Text and Web Page Pre-Processing 203 improved significantly if only the main content blocks are used. We briefly discuss two techniques for finding such blocks in Web pages. Partitioning based on visual cues: This method uses visual information to help find main content blocks in a page. Visual or rendering information of each HTML element in a page can be obtained from the Web browser. For example, Internet Explorer provides an API that can output the X and Y coordinates of each element. A machine learning model can then be built based on the location and appearance features for identifying main content blocks of pages. Of course, a large number of training examples need to be manually labeled (see [77, 495] for details). Tree matching: This method is based on the observation that in most commercial Web sites pages are generated by using some fixed templates. The method thus aims to find such hidden templates. Since HTML has a nested structure, it is thus easy to build a tag tree for each page. Tree matching of multiple pages from the same site can be performed to find such templates. In Chap. 9, we will describe a tree matching algorithm for this purpose. Once a template is found, we can identify which blocks are likely to be the main content blocks based on the following observation: the text in main content blocks are usually quite different across different pages of the same template, but the nonmain content blocks are often quite similar in different pages. To determine the text similarity of corresponding blocks (which are sub-trees), the shingle method described in the next section can be used. 6.5.5 Duplicate Detection Duplicate documents or pages are not a problem in traditional IR. However, in the context of the Web, it is a significant issue. There are different types of duplication of pages and contents on the Web. Copying a page is usually called duplication or replication, and copying an entire site is called mirroring. Duplicate pages and mirror sites are often used to improve efficiency of browsing and file downloading worldwide due to limited bandwidth across different geographic regions and poor or unpredictable network performances. Of course, some duplicate pages are the results of plagiarism. Detecting such pages and sites can reduce the index size and improve search results. Several methods can be used to find duplicate information. The simplest method is to hash the whole document, e.g., using the MD5 algorithm, or computing an aggregated number (e.g., checksum). However, these methods are only useful for detecting exact duplicates. On the Web, one seldom 204 6 Information Retrieval and Web Search finds exact duplicates. For example, even different mirror sites may have different URLs, different Web masters, different contact information, different advertisements to suit local needs, etc. One efficient duplicate detection technique is based on n-grams (also called shingles). An n-gram is simply a consecutive sequence of words of a fixed window size n. For example, the sentence, “John went to school with his brother,” can be represented with five 3-gram phrases “John went to”, “went to school”, “to school with”, “school with his”, and “with his brother”. Note that 1-gram is simply the individual words. Let Sn(d) be the set of distinctive n-grams (or shingles) contained in document d. Each n-gram may be coded with a number or a MD5 hash (which is usually a 32-digit hexadecimal number). Given the n-gram representations of the two documents d1 and d2, Sn(d1) and Sn(d2), the Jaccard coefficient can be used to compute the similarity of the two documents, sim(d1 , d 2 ) | S n ( d1 ) | S n ( d1 ) S n (d 2 ) | . S n (d 2 ) | (24) A threshold is used to determine whether d1 and d2 are likely to be duplicates of each other. For a particular application, the window size n and the similarity threshold are chosen through experiments. 6.6 Inverted Index and Its Compression The basic method of Web search and traditional IR is to find documents that contain the terms in the user query. Given a user query, one option is to scan the document database sequentially to find the documents that contain the query terms. However, this method is obviously impractical for a large collection, such as the Web. Another option is to build some data structures (called indices) from the document collection to speed up retrieval or search. There are many index schemes for text [31]. The inverted index, which has been shown superior to most other indexing schemes, is a popular one. It is perhaps the most important index method used in search engines. This indexing scheme not only allows efficient retrieval of documents that contain query terms, but also very fast to build. 6.6.1 Inverted Index In its simplest form, the inverted index of a document collection is basically a data structure that attaches each distinctive term with a list of all documents that contains the term. Thus, in retrieval, it takes constant time 6.6 Inverted Index and Its Compression 205 to find the documents that contains a query term. Finding documents containing multiple query terms is also easy as we will see later. Given a set of documents, D = {d1, d2, …, dN}, and each document has a unique identifier (ID). An inverted index consists of two parts: a vocabulary V, containing all the distinct terms in the document set, and for each distinct term ti an inverted list of postings. Each posting stores the ID (denoted by idj) of the document dj that contains term ti and other pieces of information about term ti in document dj. Depending on the need of the retrieval or ranking algorithm, different pieces of information may be included. For example, to support phrase and proximity search, a posting for a term ti usually consists of the following, <idj, fij, [o1, o2, …, o| fij|]> where idj is the ID of document dj that contains the term ti, fij is the frequency count of ti in dj, and ok are the offsets (or positions) of term ti in dj. Postings of a term are sorted in increasing order based on the idj’s and so are the offsets in each posting (see Example 3). This facilitates compression of the inverted index as we will see in Sect. 6.6.4. Example 3: We have three documents of id1, id2, and id3: id1: Web mining is useful. 1 2 3 4 id2: Usage mining applications. 1 2 3 id3: Web structure mining studies the Web hyperlink structure. 1 2 3 4 5 6 7 8 The numbers below each document are the offset position of each word. The vocabulary is the set: {Web, mining, useful, applications, usage, structure, studies, hyperlink} Stopwords “is” and “the” have been removed, but no stemming is applied. Figure 6.7 shows two inverted indices. Applications: id2 Hyperlink: id3 Mining: id1, id2, id3 Structure: id3 Studies: id3 Usage: id2 Useful: id1 Web: id1, id3 (A) Applications: Hyperlink: Mining: Structure: Studies: Usage: Useful: Web: <id2, 1, [3]> <id3, 1, [7]> <id1, 1, [2]>, <id2, 1, [2]>, <id3, 1, [3]> <id3, 2, [2, 8]> <id3, 1, [4]> <id2, 1, [1]> <id1, 1, [4]> <id1, 1, [1]>, <id3, 2, [1, 6]> (B) Fig. 6.7. Two inverted indices: a simple version and a more complex version 206 6 Information Retrieval and Web Search Figure 6.7(A) is a simple version, where each term is attached with only an inverted list of IDs of the documents that contain the term. Each inverted list in Fig. 6.7(B) is more complex as it contains additional information, i.e., the frequency count of the term and its positions in each document. Note that we use idi as the document IDs to distinguish them from offsets. In an actual implementation, they may also be positive integers. Note also that a posting can contain other types of information depending on the need of the retrieval or search algorithm (see Sect. 6.8). 6.6.2 Search Using an Inverted Index Queries are evaluated by first fetching the inverted lists of the query terms, and then processing them to find the documents that contain all (or some) terms. Specifically, given the query terms, searching for relevant documents in the inverted index consists of three main steps: Step 1 (vocabulary search): This step finds each query term in the vocabulary, which gives the inverted list of each term. To speed up the search, the vocabulary usually resides in the main memory. Various indexing methods, e.g., hashing, tries or B-tree, can be used to speed up the search. Lexicographical ordering may also be employed due to its space efficiency. Then the binary search method can be applied. The complexity is O(log|V|), where |V| is the vocabulary size. If the query contains only a single term, this step gives all the relevant documents and the algorithm then goes to step 3. If the query contains multiple terms, the algorithm proceeds to step 2. Step 2 (results merging): After the inverted list of each term is found, merging of the lists is performed to find their intersection, i.e., the set of documents containing all query terms. Merging simply traverses all the lists in synchronization to check whether each document contains all query terms. One main heuristic is to use the shortest list as the base to merge with the other longer lists. For each posting in the shortest list, a binary search may be applied to find it in each longer list. Note that partial match (i.e., documents containing only some of the query terms) can be achieved as well in a similar way, which is more useful in practice. Usually, the whole inverted index cannot fit in memory, so part of it is cached in memory for efficiency. Determining which part to cache involves analysis of query logs to find frequent query terms. The inverted lists of these frequent query terms can be cached in memory. Step 3 (Rank score computation): This step computes a rank (or relevance) score for each document based on a relevance function (e.g., 6.6 Inverted Index and Its Compression 207 okapi or cosine), which may also consider the phrase and term proximity information. The score is then used in the final ranking. Example 4: Using the inverted index built in Fig. 6.7(B), we want to search for “web mining” (the query). In step 1, two inverted lists are found: Mining: Web: <id1, 1, [2]>, <id2, 1, [2]>, <id3, 1, [3]> <id1, 1, [1]>, <id3, 2, [1, 6]> In step 2, the algorithm traverses the two lists and finds documents containing both words (documents id1 and id3). The word positions are also retrieved. In step 3, we compute the rank scores. Considering the proximity and the sequence of words, we give id1 a higher rank (or relevance) score than id3 as “web” and “mining” are next to each other in id1 and in the same sequence as that in the query. Different search engines may use different algorithms to combine these factors. 6.6.3 Index Construction The construction of an inverted index is quite simple and can be done efficiently using a trie data structure among many others. The time complexity of the index construction is O(T), where T is the number of all terms (including duplicates) in the document collection (after pre-processing). For each document, the algorithm scans it sequentially and for each term, it finds the term in the trie. If it is found, the document ID and other information (e.g., the offset of the term) are added to the inverted list of the term. If the term is not found, a new leaf is created to represent the term. Example 5: Let us build an inverted index for the three documents in Example 3, which are reproduced below for easy reference. Figure 6.8 shows the vocabulary trie and the inverted lists for all terms. id1: Web mining is useful. 1 2 3 4 id2: Usage mining applications. 1 2 3 id3: Web structure mining studies the Web hyperlink structure 1 2 3 4 5 6 7 8 To build the index efficiently, the trie is usually stored in memory. However, in the context of the Web, the whole index will not fit in the main memory. The following technique can be applied. We follow the above algorithm to build the index until the memory is full. The partial index I1 obtained so far is written on the disk. Then, we process the subsequent documents and build the partial index I2 in memory, and so on. After all documents have been processed, we have k partial in- 208 6 Information Retrieval and Web Search dices, I1, I2, …, Ik, on disk. We then merge the partial indices in a hierarchical manner. That is, we first perform pair-wise merges of I1 and I2, I3 and I4, and so on. This gives us larger indices I1-2, I3-4 and so on. After the first level merging is complete, we proceed to the second level merging, i.e., we merge I1-2 and I3-4, I5-6 and I7-8 and so on. This process continues until all the partial indices are merged into a single index. Each merge is fairly straightforward because the vocabulary in each partial index is sorted by the trie construction. The complexity of each merge is thus linear in the number of terms in both partial indices. Since each level needs a linear process of the whole index, the complete merging process takes O(klog k) time. To reduce the disk space requirement, whenever a new partial index is generated, we can merge it with a previously merged index. That is, when we have I1 and I2, we can merge them immediately to produce I1-2, and when I3 is produced, it is merged with I1-2 to produce I1-2-3 and so on. web: <id1, 1, [1]>, <id3, 2, [1, 6]> ‘e’ ‘w’ ‘s’ ‘u’ ‘a’ useful: <id1, 1, [4]> ‘a’ usage: <id2, 1, [2]> studies: <id3, 1, [4]> ‘u’ ‘s’ ‘t’ ‘m’ structure: <id3, 2, [2, 8]> ‘r’ ‘h’ mining: <id1, 1, [2]>, <id2, 1, [2]>, <id3, 1, [3]> hyperlink: <id3, 1, [7]> applications: <id2, 1, [3]> Fig. 6.8. The vocabulary trie and the inverted lists Instead of using a trie, an alternative method is to use an in-memory hash table (or other data structures) for terms. The algorithm is quite straightforward and will not be discussed further. On the Web, an important issue is that pages are constantly added, modified or deleted. It may be quite inefficient to modify the main index because a single page change can require updates to a large number of records of the index. One simple solution is to construct two additional indices, one for added pages and one for deleted pages. Modification can be regarded as a deletion and then an addition. Given a user query, it is searched in the main index and also in the two auxiliary indices. Let the pages returned from the search in the main index be D0, the pages returned from the search in the index of added pages be D+ and the pages returned from the search in the index of deleted pages be D–. Then, the final results returned to the user is (D0 D+) – D. When the two auxiliary indices become too large, they can be merged into the main index. 6.6 Inverted Index and Its Compression 6.6.4 209 Index Compression An inverted index can be very large. In order to speed up the search, it should reside in memory as much as possible to avoid disk I/O. Because of this, reducing the index size becomes an important issue. A natural solution to this is index compression, which aims to represent the same information with fewer bits or bytes. Using compression, the size of an inverted index can be reduced dramatically. In the lossless compression, the original index can also be reconstructed exactly using the compressed version. Lossless compression methods are the focus of this section. The inverted index is quite amiable to compression. Since the main space used by an inverted index is for the storage of document IDs and offsets of each term, we thus want to reduce this space requirement. Since all the information is represented with positive integers, we only discuss integer compression techniques in this section. Without compression, on most architectures an integer has a fixed-size representation of four bytes (32 bits). However, few integers need 4 bytes to represent, so a more compact representation (compression) is clearly possible. There are generally two classes of compression schemes for inverted lists: the variable-bit scheme and the variable-byte scheme. In the variable-bit (also called bitwise) scheme, an integer is represented with an integral number of bits. Well known bitwise methods include unary coding, Elias gamma coding and delta coding [161], and Golomb coding [202]. In the variable-byte scheme, an integer is stored in an integral number of bytes, where each byte has 8 bits. A simple bytewise scheme is the variable-byte coding [547]. These coding schemes basically map integers onto self-delimiting binary codewords (bits), i.e., the start bit and the end bit of each integer can be detected with no additional delimiters or markers. An interesting feature of the inverted index makes compression even more effective. Since document IDs in each inverted list are sorted in increasing order, we can store the difference between any two adjacent document IDs, idi and idi+1, where idi+1> idi, instead of the actual IDs. This difference is called the gap between idi and idi+1. The gap is a smaller number than idi+1 and thus requires fewer bits. In search, if the algorithm linearly traverses each inverted list, document IDs can be recovered easily. Since offsets in each posting are also sorted, they can be stored similarly. For example, the sorted document IDs are: 4, 10, 300, and 305. They can be represented with gaps, 4, 6, 290 and 5. Given the gap list 4, 6, 290 and 5, it is easy to recover the original document IDs, 4, 10, 300, and 305. We note that for frequent terms (which appear in a large number of documents) the gaps are small and can be encoded with short codes (fewer 210 6 Information Retrieval and Web Search bits). For infrequent or rare terms, the gaps can be large, but they do not use up much space due to the fact that only a small number of documents contain them. Storing gaps can significantly reduce the index size. We now discuss each of the coding schemes in detail. Each scheme includes a method for coding (or compression) and a method for decoding (decompression). Unary Coding Unary coding is simple. It represents a number x with x 1 bits of zeros followed by a bit of one. For example, 5 is represented as 00001. The one bit is simply the delimitor. Decoding is also straightforward. This scheme is effective for very small numbers, but wasteful for large numbers. It is thus seldom used alone in practice. Table 6.1 shows example codes of different coding schemes for 10 decimal integers. Column 2 shows the unary code for each integer. Table 6.1: Example codes for integers of different coding schemes: Spacing in the Elias, Golomb, and variable-byte codes separates the prefix of the code from the suffix. Decimal Unary Elias Gamma Elias Delta Golomb (b = 3) Golomb Variable (b = 10) byte 1 2 3 4 5 6 7 8 9 10 1 01 001 0001 00001 000001 0000001 00000001 000000001 0000000001 1 0 10 0 11 00 100 00 101 00 110 00 111 000 1000 000 1001 000 1010 1 0 100 0 101 0 1100 0 1101 0 1110 0 1111 00 100000 00 100001 00 100010 1 10 1 11 01 0 01 10 01 11 001 0 001 10 001 11 0001 0 0001 10 1 001 1 010 1 011 1 100 1 101 1 1100 1 1101 1 1110 1 1111 01 000 0000001 0 0000010 0 0000011 0 0000100 0 0000101 0 0000110 0 0000111 0 0001000 0 0001001 0 0001010 0 Elias Gamma Coding Coding: In the Elias gamma coding, a positive integer x is represented by: 1+ log2x in unary (i.e., log2x 0-bits followed by a 1-bit), followed by the binary representation of x without its most significant bit. Note that 1+ log2x is simply the number of bits of x in binary. The coding can also be described with the following two steps: 1. Write x in binary. 2. Subtract 1 from the number of bits written in step 1 and prepend that many zeros. 6.6 Inverted Index and Its Compression 211 Example 6: The number 9 is represented by 0001001, since 1+ log29 = 4, or 0001 in unary, and 9 is 001 in binary with the most significant bit removed. Alternatively, we first write 9 in binary, which is 1001 with 4 bits, and then prepend three zeros. In this way, 1 is represented by 1 (in one bit), and 2 is represented by 010. Additional examples are shown in column 3 of Table 6.1. Decoding: We decode an Elias gamma-coded integer in two steps: 1. Read and count zeroes from the stream until we reach the first one. Call this count of zeroes K. 2. Consider the one that was reached to be the first digit of the integer, with a value of 2K, read the remaining K bits of the integer. Example 7: To decompress 0001001, we first read all zero bits from the beginning until we see a bit of 1. We have K = 3 zero bits. We then include the 1 bit with the following 3 bits, which give us 1001 (binary for 9). Gamma coding is efficient for small integers but is not suited to large integers for which the parameterized Golomb code or the Elias delta code is more suitable. Elias Delta Coding Elias delta codes are somewhat longer than gamma codes for small integers, but for larger integers such as document numbers in an index of Web pages, the situation is reversed. Coding: In the Elias delta coding, a positive integer x is stored with the gamma code representation of 1+ log2x , followed by the binary representation of x less the most significant bit. Example 8: Let us code the number 9. Since 1+ log2x = 4, we have its gamma code 00100 for 4. Since 9’s binary representation less the most significant bit is 001, we have the delta code of 00100001 for 9. Additional examples are shown in column 4 of Table 6.1. Decoding: To decode an Elias delta-coded integer x, we first decode the gamma-code part 1+ log2x as the magnitude M (the number of bits of x in binary), and then retrieve the binary representation of x less the most significant bit. Specifically, we use the following steps: 1. Read and count zeroes from the stream until you reach the first one. Call this count of zeroes L. 2. Considering the one that was reached to be the first bit of an integer, with a value of 2L, read the remaining L digits of the integer. This is the 212 6 Information Retrieval and Web Search integer M. 3. Put a one in the first place of our final output, representing the value 2M. Read and append the following M-1 bits. Example 9: We want to decode 00100001. We can see that L = 2 after step 1, and after step 2, we have read and consumed 5 bits. We also obtain M = 4 (100 in binary). Finally, we prepend 1 to the M-1 bits (which is 001) to give 1001, which is 9 in binary. While Elias codes yield acceptable compression and fast decoding, a better performance in both aspects is possible with the Golomb coding. Golomb Coding The Golomb coding is a form of parameterized coding in which integers to be coded are stored as values relative to a constant b. Several variations of the original Golomb scheme exist, which save some bits in coding compared to the original scheme. We describe one version here. Coding: A positive integer x is represented in two parts: 1. The first part is a unary representation of q+1, where q is the quotient (x/b) , and 2. The second part is a special binary representation of the remainder r = x qb. Note that there are b possible remainders. For example, if b = 3, the possible remainders will be 0, 1, and 2. The binary representation of a remainder requires log2b or log2b bits. Clearly, it is not possible to write every remainder in log2b bits in binary. To save space, we want to write the first few remainders using log2b bits and the rest using log2b bits. We must do so such that the decoder knows when log2b bits are used and when log2b bits are used. Let i = log2b . We code the first d remainders using i bits, d = 2i+1 – b. (25) It is worth noting that these d remainders are all less than d. The rest of the remainders are coded with log2b bits and are all greater than or equal to d. They are coded using a special binary code (also called a fixed prefix code) with log2b (or i+1) bits. Example 10: For b = 3, to code x = 9, we have the quotient q = 9/3 = 3. For remainder, we have i = log2 3 = 1 and d = 1. Note that for b = 3, there are three remainders, i.e., 0, 1, and 2, which are coded as 0, 10, and 11 respectively. The remainder for 9 is r = 9 3 3 = 0. The final code for 9 is 00010. Additional examples for b = 3 are shown in column 5 of Table 6.1. 6.6 Inverted Index and Its Compression 213 For b = 10, to code x = 9, we have the quotient q = 9/10 = 0. For remainder, we have i = log2 10 = 3 and d = 6. Note that for b = 10, there are 10 remainders, i.e., 0, 1, 2, …, 10, which are coded as 000, 001, 010, 011, 100, 101, 1100, 1101, 1110, 1111 respectively. The remainder of 9 is r = 9 0 5 = 9. The final code for 9 is 11111. Additional examples for b = 10 are shown in column 6 of Table 6.1. We can see that the first d remainders are standard binary codes, but the rest are not. They are generated using a tree instead. Figure 6.9 shows an example based on b = 5. The leaves are the five remainders. The first three remainders (0, 1, 2) are in the standard binary code, and the rest (3 and 4) have an additional bit. It is important to note that the first 2 bits (i = 2) of the remainder 3 (the first remainder coded in i+1 bits) is 11, which is 3 (i.e., d) in binary. This information is crucial for decoding because it enables the algorithm to know when i+1 bits are used. We also notice that d is completely determined by b, which helps decoding. 0 0 0 1 1 0 1 2 1 0 3 1 4 Fig. 6.9. The coding tree for b = 5 If b is a power of 2 (called Golomb–Rice coding), i.e., b = 2k for integer k 0, every remainder is coded with the same number of bits because log2b = log2b . This is also easy to see from Equation (25), i.e., d = 2k. Decoding: To decode a Golomb-coded integer x, we use the following steps: 1. Decode unary-coded quotient q (the relevant bits are comsumed). 2. Compute i = log2 b and d = 2i+1 – b. 3. Retrieve the next i bits and assign it to r. 4. If r d then retrieve one more bit and append it to r at the end; r = r – d. 5. Return x = qb + r. Some explanation is in order for step 4. As we discussed above, if r d we need i+1 bits to code the remainder. The first line of step 4 retrieves the additional bit and appends it to r. The second line obtains the true value of 214 6 Information Retrieval and Web Search the remainder r. Example 11: We want to decode 11111 for b = 10. We see that q = 0 because there is no zero at the beginning. The first bit is consumed. We know that i = log2 10 = 3 and d = 6. We then retrieve the next three bits, 111, which is 7 in decimal, and assign it to r (= 111). Since 7 > 6 (which is d), we retrieve one more bit, which is 1, and r is now 1111 (15 in decimal). The new r = r – d = 15 – 6 = 9. Finally, x = qb + r = 0 + 9 = 9. Now we discuss the selection of b for each term. For gap compression, Witten et al. [551] reported that a suitable b is b 0.69 N , nt (26) where N is the total number of documents and nt is the number of documents that contain term t. Variable-Byte Coding Coding: In this method, seven bits in each byte are used to code an integer, with the least significant bit set to 0 in the last byte, or to 1 if further bytes follow. In this way, small integers are represented efficiently. For example, 135 is represented in two bytes, since it lies in the range 27 and 214, as 00000011 00001110. Additional examples are shown in column 6 of Table 6.1. Decoding: Decoding is performed in two steps: 1. Read all bytes until a byte with the zero last bit is seen. 2. Remove the least significant bit from each byte read so far and concatenate the remaining bits. For example, 00000011 00001110 is decoded to 00000010000111, which is 135. Finally, experimental results in [547] show that non-parameterized Elias coding is generally not as space-efficient or as fast as parameterized Golomb coding for retrieval. Gamma coding does not work well. Variablebyte integers are often faster than variable-bit integers, despite having higher storage costs, because fewer CPU operations are required to decode variable-byte integers and they are byte-aligned on disk. A suitable compression technique can allow retrieval to be up to twice as fast than without compression, while the space requirement averages 20% – 25% of the cost of storing uncompressed integers. 6.7 Latent Semantic Indexing 215 6.7 Latent Semantic Indexing The retrieval models discussed so far are based on keyword or term matching, i.e., matching terms in the user query with those in the documents. However, many concepts or objects can be described in multiple ways (using different words) due to the context and people’s language habits. If a user query uses different words from the words used in a document, the document will not be retrieved although it may be relevant because the document uses some symonyms of the words in the user query. This causes low recall. For example, “picture”, “image” and “photo” are synonyms in the context of digital cameras. If the user query only has the word “picture”, relevant documents that contain “image” or “photo” but not “picture” will not be retrieved. Latent semantic indexing (LSI), proposed by Deerwester et al. [125], aims to deal with this problem through the identification of statistical associations of terms. It is assumed that there is some underlying latent semantic structure in the data that is partially obscured by the randomness of word choice. It then uses a statistical technique, called singular value decomposition (SVD) [203], to estimate this latent structure, and to remove the “noise”. The results of this decomposition are descriptions of terms and documents based on the latent semantic structure derived from SVD. This structure is also called the hidden “concept” space, which associates syntactically different but semantically similar terms and documents. These transformed terms and documents in the “concept” space are then used in retrieval, not the original terms or documents. Furthermore, the query is also transformed into the “concept” space before retrieval. Let D be the text collection, the number of distinctive words in D be m and the number of documents in D be n. LSI starts with an m n termdocument matrix A. Each row of A represents a term and each column represents a document. The matrix may be computed in various ways, e.g., using term frequency or TF-IDF values. We use term frequency as an example in this section. Thus, each entry or cell of the matrix A, denoted by Aij, is the number of times that term i occurs in document j. 6.7.1 Singular Value Decomposition What SVD does is to factor matrix A (a m n matrix) into the product of three matrices, i.e., A U VT, (27) 216 6 Information Retrieval and Web Search where U is a m r matrix and its columns, called right singular vectors, are eigenvectors associated with the r non-zero eigenvalues of AAT. Furthermore, the columns of U are unit orthogonal vectors, i.e., UTU = I (identity matrix). V is an n r matrix and its columns, called right singular vectors, are eigenvectors associated with the r non-zero eigenvalues of ATA. The columns of V are also unit orthogonal vectors, i.e., VTV = I. is a r r diagonal matrix, = diag( 1, 2, …, r), i > 0. 1, 2, …, and r, called singular values, are the non-negative square roots of the r (non-zero) eigenvalues of AAT. They are arranged in decreasing order, i.e., 1 … 2 r > 0. We note that initially U is in fact an m m matrix and V an n n matrix and an m n diagonal matrix. ’s diagonal consists of nonnegative eigenvalues of AAT or ATA. However, due to zero eigenvalues, has zero-valued rows and columns. Matrix multiplication tells us that those zero-valued rows and columns from can be dropped. Then, the last m r columns in U and the last n r columns in V can also be dropped. m is the number of row (terms) in A, representing the number of terms. n is the number of columns in A, representing the number of documents. r is the rank of A, r min(m, n). The singular value decomposition of A always exists and is unique up to 1. allowable permutations of columns of U and V and elements of leaving it still diagonal; that is, columns i and j of may be interchanged iff row i and j of are interchanged, and columns i and j of U and V are interchanged. 2. sign (+/ ) flip in U and V. An important feature of SVD is that we can delete some insignificant dimensions in the transformed (or “concept”) space to optimally (in the least square sense) approximate matrix A. The significance of the dimensions is indicated by the magnitudes of the singular values in , which are already sorted. In the context of information retrieval, the insignificant dimensions may represent “noisy” in the data, and should be removed. Let us use only the k largest singular values in and set the remaining small ones to zero. The approximated matrix of A is denoted by Ak. We can also reduce the size of the matrices , U and V by deleting the last r k rows and columns from , the last r k columns in U and the last r k columns in V. We then obtain 6.7 Latent Semantic Indexing Ak Uk kV k T 217 (28) , which means that we use the k-largest singular triplets to approximate the original (and somewhat “noisy”) term-document matrix A. The new space is called the k-concept space. Figure 6.10 shows the original matrices and the reduced matrices schematically. Term vectors Documents k k k VkT k Document vectors VT Terms A/Ak mn = Uk k mr U rr rn Fig. 6.10. The schematic representation of A and Ak It is critical that the LSI method does not re-construct the original termdocument matrix A perfectly. The truncated SVD captures most of the important underlying structures in the association of terms and documents, yet at the same time removes the noise or variability in word usage that plagues keyword matching retrieval methods. Intuitive Idea of LSI: The intuition of LSI is that SVD rotates the axes of m-dimensional space of A such that the first axis runs along the largest variation (variance) among the documents, the second axis runs along the second largest variation (variance) and so on. Figure 6.11 shows an example. The original x-y space is mapped to the x -y space generated by SVD. We can see that x and y are clearly correlated. In our retrieval context, each data point represents a document and each axis (x or y) in the original space represents a term. Hence, the two terms are correlated or co-occur frequently. In the SVD, the direction of x in which the data has the largest variation is represented by the first column vector of U, and the direction of y is represented by the second column vector of U. VT represents the documents in the transformed “concept” space. The singular values in are simply scaling factors. We observe that y direction is insignificant, and may represent some “noise”, so we can remove it. Then, every data point (document) is pro- 218 6 Information Retrieval and Web Search jected to x . We have an outlier document di that contains term x, but not term y. However, if it is projected to x , it becomes closer to other points. Let us see what happens if we have a query q represented with a star in Fig. 6.11, which contains only a single term “y”. Using the traditional exact term matching, di is not relevant because “y” does not appear in di. However, in the new space after projection, they are quite close or similar. y x' y’ q x di Fig. 6.11. Intuition of the LSI. 6.7.2 Query and Retrieval Given a user query q (represented by a column vector as those in A), it is first converted into a document in the k-concept space, denoted by qk. This transformation is necessary because SVD has transformed the original documents into the k-concept space and stored them in Vk. The idea is that q is treated as a new document in the original space represented as a column in A, and then mapped to qk as an additional document (or column) in VkT. From Equation (28), it is easy to see that q Uk (29) T k qk . Since the columns in U are unit orthogonal vectors, UkTUk = I. Thus, T T Uk q k qk . (30) As the inverse of a diagonal matrix is still a diagonal matrix, and each entry on the diagonal is 1/ i (1 i k), if it is multiplied on both sides of Equation (30), we obtain, -1 k T T Uk q qk . (31) Finally, we get the following (notice that the transpose of a diagonal matrix is itself), qk qT U k -1 k . (32) 6.7 Latent Semantic Indexing 219 For retrieval, we simply compare qk with each document (row) in Vk using a similarity measure, e.g., the cosine similarity. Recall that each row of Vk (or each column of VkT ) corresponds to a document (column) in A. This method has been used traditionally. Alternatively, since kVkT (not VkT) represents the documents in the transformed k-concept space, we can compare the similarity of the query document in the transformed space, which is kqkT, and each transformed document in kVkT for retrieval. The difference between the two methods is obvious. This latter method considers scaling effects of the singular values in k, but the former does not. However, it is not clear which method performs better as I know of no reported study on this alternative method. 6.7.3 An Example Example 12: We will use the example in [125] to illustrate the process. The document collection has the following nine documents. The first five documents are related to human computer interaction, and the last four documents are related to graphs. To reduce the size of the problem, only the underlined terms are used in our computation. c1: c2: c3: c4: c5: m1: m2: m3: m4: Human machine interface for Lab ABC computer applications A survey of user opinion of computer system response time The EPS user interface management system System and human system engineering testing of EPS Relation of user-perceived response time to error measurement The generation of random, binary, unordered trees The intersection graph of paths in trees Graph minors IV: Widths of trees and well-quasi-ordering Graph minors: A survey The term-document matrix A is given below, which is a 9 12 matrix. c1 A= c2 c3 c4 c5 m1 m2 m3 m4 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 2 0 0 1 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 1 1 human interface computer user system response time EPS survey trees graph minors 220 6 Information Retrieval and Web Search After performing SVD, we obtain three matrices, U, and VT, which are given below. Singular values on the diagonal of are in decreasing order. 0.22 0.20 0.24 0.40 0.64 0.27 0.27 0.30 0.21 0.01 0.04 0.03 = 0.29 0.14 -0.16 -0.34 0.36 -0.43 -0.43 0.33 -0.18 0.23 0.22 0.14 -0.41 -0.55 -0.59 0.10 0.33 0.07 0.07 0.19 -0.03 0.03 0.00 -0.01 -0.11 0.28 -0.11 0.33 -0.16 0.08 0.08 0.11 -0.54 0.59 -0.07 -0.30 -0.34 0.50 -0.25 0.38 -0.21 -0.17 -0.17 0.27 0.08 -0.39 0.11 0.28 0.52 -0.07 -0.30 0.00 -0.17 0.28 0.28 0.03 -0.47 -0.29 0.16 0.34 -0.06 -0.01 0.06 0.00 0.03 -0.02 -0.02 -0.02 -0.04 0.25 -0.68 0.68 -0.41 -0.11 0.49 0.01 0.27 -0.05 -0.05 -0.17 -0.58 -0.23 0.23 0.18 3.34 0 0 0 0 0 0 0 0 U= -0.11 -0.07 0.04 0.06 -0.17 0.11 0.11 -0.14 0.27 0.49 0.62 0.45 0 2.54 0 0 0 0 0 0 0 0 0 2.35 0 0 0 0 0 0 0 0 0 1.64 0 0 0 0 0 0 0 0 0 1.50 0 0 0 0 0 0 0 0 0 1.31 0 0 0 0 0 0 0 0 0 0.85 0 0 0 0 0 0 0 0 0 0.56 0 0 0 0 0 0 0 0 0 0.36 0.20 -0.06 0.11 -0.95 0.05 -0.08 0.18 -0.01 -0.06 0.61 0.17 -0.50 -0.03 -0.21 -0.26 -0.43 0.05 0.24 0.46 -0.13 0.21 0.04 0.38 0.72 -0.24 0.01 0.02 0.54 -0.23 0.57 0.27 -0.21 -0.37 0.26 -0.02 -0.08 0.28 0.11 -0.51 0.15 0.33 0.03 0.67 -0.06 -0.26 0.00 0.19 0.10 0.02 0.39 -0.30 -0.34 0.45 -0.62 0.01 0.44 0.19 0.02 0.35 -0.21 -0.15 -0.76 0.02 0.02 0.62 0.25 0.01 0.15 0.00 0.25 0.45 0.52 0.08 0.53 0.08 -0.03 -0.60 0.36 0.04 -0.07 -0.45 VT = Now let us choose only two largest singular values from , i.e., k = 2. Thus, the concept space has only two dimensions. The other two matrices are also truncated accordingly. We obtain the 3 matrix Uk, k and VkT: Uk 0.22 0.20 0.24 0.40 0.64 Ak = 0.27 0.27 0.30 0.21 0.01 0.04 0.03 -0.11 -0.07 0.04 0.06 -0.17 0.11 0.11 -0.14 0.27 0.49 0.62 0.45 k 3.34 0 0 2.54 Vk T 0.20 0.61 0.46 0.54 0.28 0.00 0.02 0.02 0.08 -0.06 0.17 -0.13 -0.23 0.11 0.19 0.44 0.62 0.53 6.7 Latent Semantic Indexing 221 Now we issue a search query q, “user interface”, to find relevant documents. The transformed query document qk of query q in the k-concept space is computed below using Equation (26), which is (0.179 -0.004). 0 1 0 1 0 0 qk = 0 0 0 0 0 0 T 0.22 0.20 0.24 0.40 0.64 0.27 0.27 0.30 0.21 0.01 0.04 0.03 -0.11 -0.07 0.04 0.06 -0.17 0.11 0.11 -0.14 0.27 0.49 0.62 0.45 3.34 0 0 2.54 -1 = (0.179 0.004) qk is then compared with every document vector in Vk using the cosine similarity. The similarity values are as follows: c1: c2: c3: c4: c5: m1: m2: m3: m4: 0.964 0.957 0.968 0.928 0.922 0.022 0.023 0.010 0.127 We obtain the final ranking of (c3, c1, c2, c4, c5, m4, m2, m3, m1). 6.7.4 Discussion LSI has been shown to perform better than traditional keywords based methods. The main drawback is the time complexity of the SVD, which is O(m2n). It is thus difficult to use for a large document collection such as the Web. Another drawback is that the concept space is not interpretable as its description consists of all numbers with little semantic meaning. Determining the optimal number of dimensions k of the concept space is also a major difficulty. There is no general consensus for an optimal number of dimensions. The original paper [125] of LSI suggests 50–350 dimensions. In practice, the value of k needs to be determined based on the specific document collection via trial and error, which is a very time consuming process due to the high time complexity of the SVD. To close this section, one can imagine that association rules may be able to approximate the results of LSI and avoid its shortcomings. Association 222 6 Information Retrieval and Web Search rules represent term correlations or co-occurrences. Association rule mining has two advantages. First, its mining algorithm is very efficient. Since we may only need rules with 2-3 terms, which are sufficient for practical purposes, the mining algorithm only needs to scan the document collection 2-3 times. Second, rules are easy to understand. However, little research has been done in this direction so far. 6.8 Web Search We now put it all together and describe the working of a search engine. Since it is difficult to know the internal details of a commercial search engine, most contents in this section are based on research papers, especially the early Google paper [68]. Due to the efficiency problem, latent semantic indexing is probably not used in Web search yet. Current search algorithms are still mainly based on the vector space model and term matching. A search engine starts with the crawling of pages on the Web. The crawled pages are then parsed, indexed, and stored. At the query time, the index is used for efficient retrieval. We will not discuss crawling here. Its details can be found in Chap. 8. The subsequent operations of a search engine are described below: Parsing: A parser is used to parse the input HTML page, which produces a stream of tokens or terms to be indexed. The parser can be constructed using a lexical analyzer generator such as YACC and Flex (which is from the GNU project). Some pre-processing tasks described in Sect. 6.5 may also be performed before or after parsing. Indexing: This step produces an inverted index, which can be done using any of the methods described in Sect. 6.6. For retrieval efficiency, a search engine may build multiple inverted indices. For example, since the titles and anchor texts are often very accurate descriptions of the pages, a small inverted index may be constructed based on the terms appeared in them alone. Note that here the anchor text is for indexing the page that its link points to, not the page containing it. A full index is then built based on all the text in each page, including anchor texts (a piece of anchor text is indexed both for the page that contains it, and for the page that its link points to). In searching, the algorithm may search in the small index first and then the full index. If a sufficient number of relevant pages are found in the small index, the system may not search in the full index. Searching and Ranking: Given a user query, searching involves the following steps: 6.8 Web Search 223 1. pre-processing the query terms using some of the methods described in Sect. 6.5, e.g., stopword removal and stemming; 2. finding pages that contain all (or most of) the query terms in the inverted index; 3. ranking the pages and returning them to the user. The ranking algorithm is the heart of a search engine. However, little is known about the algorithms used in commercial search engines. We give a general description based on the algorithm in the early Google system. As we discussed earlier, traditional IR uses cosine similarity values or any other related measures to rank documents. These measures only consider the content of each document. For the Web, such content based methods are not sufficient. The problem is that on the Web there are too many relevant documents for almost any query. For example, using “web mining” as the query, the search engine Google estimated that there were 46,500,000 relevant pages. Clearly, there is no way that any user will look at this huge number of pages. Therefore, the issue is how to rank the pages and present the user the “best” pages at the top. An important ranking factor on the Web is the quality of the pages, which was hardly studied in traditional IR because most documents used in IR evaluations are from reliable sources. However, on the Web, anyone can publish almost anything, so there is no quality control. Although a page may be 100% relevant, it may not be a quality page due to several reasons. For example, the author may not be an expert of the query topic, the information given in the page may be unreliable or biased, etc. However, the Web does have an important mechanism, the hyperlinks (links), that can be used to assess the quality of each page to some extent. A link from page x to page y is an implicit conveyance of authority of page x to page y. That is, the author of page x believes that page y contains quality or authoritative information. One can also regard the fact that page x points to page y as a vote of page x for page y. This democratic nature of the Web can be exploited to assess the quality of each page. In general, the more votes a page receives, the more likely it is a quality page. The actual algorithms are more involved than simply counting the number of votes or links pointing to a page (called in-links). We will describe the algorithms in the next chapter. PageRank is the most well known such algorithm (see Sect. 7.3). It makes use of the link structure of Web pages to compute a quality or reputation score for each page. Thus, a Web page can be evaluated based on both its content factors and its reputation. Content-based evaluation depends on two kinds of information: Occurrence Type: There are several types of occurrences of query terms in a page: 224 6 Information Retrieval and Web Search Title: a query term occurs in the title field of the page. Anchor text: a query term occurs in the anchor text of a page pointing to the current page being evaluated. URL: a query term occurs in the URL of the page. Many URL addresses contain some descriptions of the page. For example, a page on Web mining may have the URL http://www.domain.edu/Webmining.html. Body: a query term occurs in the body field of the page. In this case, the prominence of each term is considered. Prominence means whether the term is emphasized in the text with a large font, or bold and/or italic tags. Different prominence levels can be used in a system. Note that anchor texts in the page can be treated as plain texts for the evaluation of the page. Count: The number of occurrences of a term of each type. For example, a query term may appear in the title field of the page 2 times. Then, the title count for the term is 2. Position: This is the position of each term in each type of occurrence. The information is used in proximity evaluation involving multiple query terms. Query terms that are near to each other are better than those that are far apart. Furthermore, query teams appearing in the page in the same sequence as they are in the query are also better. For the computation of the content based score (also called the IR score), each occurrence type is given an associated weight. All type weights form a fixed vector. Each raw term count is converted to a count weight, and all count weights also form a vector. The quality or reputation of a page is usually computed based on the link structure of Web pages, which we will study in Chap. 7. Here, we assume that a reputation score has been computed for each page. Let us now look at two kinds of queries, single word queries and multi-word queries. A single word query is the simplest case with only a single term. After obtaining the pages containing the term from the inverted index, we compute the dot product of the type weight vector and the count weight vector of each page, which gives us the IR score of the page. The IR score of each page is then combined with its reputation score to produce the final score of the page. For a multi-word query, the situation is similar, but more complex since there is now the issue of considering term proximity and ordering. Let us simplify the problem by ignoring the term ordering in the page. Clearly, terms that occur close to each other in a page should be weighted higher than those that occur far apart. Thus multiple occurrences of terms need to be matched so that nearby terms are identified. For every matched set, a 6.9 Meta-Search and Combining Multiple Rankings 225 proximity value is calculated, which is based on how far apart the terms are in the page. Counts are also computed for every type and proximity. Each type and proximity pair has a type-proximity-weight. The counts are converted into count-weights. The dot product of the count-weights and the type-proximity-weights gives an IR score to the page. Term ordering can be considered similarly and included in the IR score, which is then combined with the page reputation score to produce the final rank score. 6.9 Meta-Search and Combining Multiple Rankings In the last section, we described how an individual search engine works. We now discuss how several search engines can be used together to produce a meta-search engine, which is a search system that does not have its own database of Web pages. Instead, it answers the user query by combining the results of some other search engines which normally have their databases of Web pages. Figure 6.12 shows a meta-search architecture. After receiving a query from the user through the search interface, the meta-search engine submits the query to the underlying search engines (called its component search engines). The returned results from all these search engines are then combined (fused or merged) and sent to the user. A meta-search engine has some intuitive appeals. First of all, it increases the search coverage of the Web. The Web is a huge information source, and each individual search engine may only cover a small portion of it. If we use only one search engine, we will never see those relevant pages that are not covered by the search engine. Search interface Metasearch engine Search engine 1 Search engine 2 Search engine n Fig. 6.12. A meta-search architecture Meta-search may also improve the search effectiveness. Each component search engine has its ranking algorithm to rank relevant pages, which is often biased, i.e., it works well for certain types of pages or queries but 226 6 Information Retrieval and Web Search not for others. By combining the results from multiple search engines, their biases can be reduced and thus the search precision can be improved. The key operation in meta-search is to combine the ranked results from the component search engines to produce a single ranking. The first task is to identify whether two pages from different search engines are the same, which facilitates combination and duplicate removal. Without downloading the full pages (which is too time consuming), this process is not simple due to aliases, symbolic links, redirections, etc. Typically, several heuristics are used for the purpose, e.g., comparing domain names of URLs, titles of the pages, etc. The second task is to combine the ranked results from individual search engines to produce a single ranking, i.e., to fuse individual rankings. There are two main classes of meta-search combination (or fusion) algorithms: ones that use similarity scores returned by each component system and ones that do not. Some search engines return a similarity score (with the query) for each returned page, which can be used to produce a better combined ranking. We discuss these two classes of algorithms below. It is worth noting that the first class of algorithms can also be used to combine scores from different similarity functions in a single IR system or in a single search engine. Indeed, the algorithms below were originally proposed for this purpose. It is likely that search engines already use some such techniques (or their variations) within their ranking mechanisms because a ranking algorithm needs to consider multiple factors. 6.9.1 Combination Using Similarity Scores Let the set of candidate documents to be ranked be D = {d1, d2, …, dN}. There are k underlying systems (component search engines or ranking techniques). The ranking from system or technique i gives document dj the similarity score, sij. Some popular and simple combination methods are defined by Fox and Shaw in [184]. CombMIN: The combined similarity score for each document dj is the minimum of the similarities from all underlying search engine systems: CombMIN(dj) = min(s1j, s2j, …, skj). (33) CombMAX: The combined similarity score for each document dj is the maximum of the similarities from all underlying search engine systems: CombMAX(dj) = max(s1j, s2j, …, skj). (34) CombSUM: The combined similarity score for each document dj is the sum of the similarities from all underlying search engine systems. 6.9 Meta-Search and Combining Multiple Rankings CombSUM(d j ) k i 1 ij 227 (35) s. CombANZ: It is defined as CombANZ(d j ) CombSUM(d j ) rj , (36) where rj is the number of non-zero similarities, or the number of systems that retrieved dj. CombMNZ: It is defined as CombMNZ(d j ) CombSUM(d j ) rj (37) where rj is the number of non-zero similarities, or the number of systems that retrieved dj. It is a common practice to normalize the similarity scores from each ranking using the maximum score before combination. Researchers have shown that, in general, CombSUM and CombMNZ perform better. CombMNZ outperforms CombSUM slightly in most cases. 6.9.2 Combination Using Rank Positions We now discuss some popular rank combination methods that use only rank positions of each search engine. In fact, there is a field of study called the social choice theory [273] that studies voting algorithms as techniques to make group or social decisions (choices). The algorithms discussed below are based on voting in elections. In 1770 Jean-Charles de Borda proposed “election by order of merit”. Each voter announces a (linear) preference order on the candidates. For each voter, the top candidate receives n points (if there are n candidates in the election), the second candidate receives n 1 points, and so on. The points from all voters are summed up to give the final points for each candidate. If there are candidates left unranked by a voter, the remaining points are divided evenly among the unranked candidates. The candidate with the most points wins. This method is called the Borda ranking. An alternative method was proposed by Marquis de Condorcet in 1785. The Condorcet ranking algorithm is a majoritarian method where the winner of the election is the candidate(s) that beats each of the other candidates in a pair-wise comparison. If a candidate is not ranked by a voter, the candidate loses to all other ranked candidates. All unranked candidates tie with one another. 228 6 Information Retrieval and Web Search Yet another simple method, called the reciprocal ranking, sums one over the rank of each candidate across all voters. For each voter, the top candidate has the score of 1, the second ranked candidate has the score of 1/2, and the third ranked candidate has the score of 1/3 and so on. If a candidate is not ranked by a voter, it is skipped in the computation for this voter. The candidates are then ranked according to their final total scores. This rank strategy gives much higher weight than Borda ranking to candidates that are near the top of a list. Example 13: We use an example in the context of meta-search to illustrate the working of these methods. Consider a meta-search system with five underlying search engine systems, which have ranked four candidate documents or pages, a, b, c, and d as follows: a, b, c, d b, a, d, c c, b, a, d c, b, d c, b system 1: system 2: system 3: system 4: system 5: Let us denote the score of each candidate x by Score(x). Borda Ranking: The score for each page is as follows: Score(a) = 4 + 3 + 2 + 1 + 1.5 = 11.5 Score(b) = 3 + 4 + 3 + 3 + 3 = 16 Score(c) = 2 + 1 + 4 + 4 + 4 = 15 Score(d) = 1 + 2 + 1 + 2 + 1.5 = 7.5 Thus the final ranking is: b, c, a, d. Condorcet Ranking: We first build an n n matrix for all pair-wise comparisons, where n is the number of pages. Each non-diagonal entry (i, j) of the matrix shows the number of wins, loses, and ties of page i over page j, respectively. For our example, the matrix is as follows: a b c d a 4:1:0 3:2:0 1:3:1 b 1:4:0 3:2:0 0:5:0 c 2:3:0 2:3:0 1:4:0 d 3:1:1 5:0:0 4:1:0 - Fig. 6.13. The pair-wise comparison matrix for the four candidate pages After the matrix is constructed, pair-wise winners are determined, which produces a win, lose and tie table. Each pair in Fig. 6.13 is compared, and the winner receives one point in its “win” column and the loser receives 6.10 Web Spamming 229 one point in its “lose” column. For a pair-wise tie, both receive one point in the “tie” column. For example, for page a, it only beats d because a is ranked ahead of d three times out of 5 ranks (Fig. 6.13). The win, lose and tie table for Fig. 6.13 is given in Fig. 6.14 below. a b c d win 1 2 3 0 lose 2 1 0 3 tie 0 0 0 0 Fig. 6.14. The win, lose and tie table for the comparison matrix in Fig. 6.13 To rank the pages, we use their win and lose values. If the number of wins that a page i has is higher than another page j, then i wins over j. If their win property is equal, we consider their lose scores, and the page which has a lower lose score wins. If both their win and lose scores are the same, then the pages are tied. The final ranks of the tied pages are randomly assigned. Clearly c is the Condorcet winner in our example. The final ranking is: c, b, a, d. Using Reciprocal Ranking: Score(a) = 1 + 1/2 + 1/3 = 1.83 Score(b) = 1/2 + 1 + 1/2 + 1/2 + 1/2 = 3 Score(c) = 1/3 + 1/4 + 1 + 1 + 1 = 3.55 Score(d) = 1/4 + 1/3 + 1/4 + 1/3= 1.17 The final ranking is: c, b, a, d. 6.10 Web Spamming Web search has become very important in the information age. Increased exposure of pages on the Web can result in significant financial gains and/or fames for organizations and individuals. The rank positions of Web pages in search are perhaps the single most important indicator of such exposures of pages. If a user searches for information that is relevant to your pages but your pages are ranked low by search engines, then the user may not see the pages because one seldom clicks a large number of returned pages. This is not acceptable for businesses, organizations, and even individuals. Thus, it has become very important to understand search engine ranking algorithms and to present the information in one’s pages in such a way that the pages will be ranked high when terms related to the contents 230 6 Information Retrieval and Web Search of the pages are searched. Unfortunately, this also results in spamming, which refers to human activities that deliberately mislead search engines to rank some pages higher than they deserve. There is a gray area between spamming and legitimate page optimization. It is difficult to define precisely what are justifiable and unjustifiable actions aimed at boosting the importance and consequently the rank positions of one’s pages. Assume that, given a user query, each page on the Web can be assigned an information value. All the pages are then ranked according to their information values. Spamming refers to actions that do not increase the information value of a page, but dramatically increase its rank position by misleading search algorithms to rank it high. Due to the fact that search engine algorithms do not understand the content of each page, they use syntactic or surface features to assess the information value of the page. Spammers exploit this weakness to boost the ranks of their pages. Spamming is annoying for users because it makes it harder to find truly useful information and leads to frustrating search experiences. Spamming is also bad for search engines because spam pages consume crawling bandwidth, pollute the Web, and distort search ranking. There are in fact many companies that are in the business of helping others improve their page ranking. These companies are called Search Engine Optimization (SEO) companies, and their businesses are thriving. Some SEO activities are ethical and some, which generate spam, are not. As we mentioned earlier, search algorithms consider both content based factors and reputation based factors in scoring each page. In this section, we briefly describe some spam methods that exploit these factors. The section is mainly based on [214] by Gyongyi and Garcia-Molina. 6.10.1 Content Spamming Most search engines use variations of TF-IDF based measures to assess the relevance of a page to a user query. Content-based spamming methods basically tailor the contents of the text fields in HTML pages to make spam pages more relevant to some queries. Since TF-IDF is computed based on terms, content spamming is also called term spamming. Term spamming can be placed in any text field: Title: Since search engines usually give higher weights to terms in the title of a page due to the importance of the title to a page, it is thus common to spam the title. Meta-Tags: The HTML meta-tags in the page header enable the owner to include some meta information of the page, e.g., author, abstract, key- 6.10 Web Spamming 231 words, content language, etc. However, meta-tags are very heavily spammed. Search engines now give terms within these tags very low weights or completely ignore their contents. Body: Clearly spam terms can be placed within the page body to boost the page ranking. Anchor Text: As we discussed in Sect. 6.8, the anchor text of a hyperlink is considered very important by search engines. It is indexed for the page containing it and also for the page that it points to, so anchor text spam affects the ranking of both pages. URL: Some search engines break down the URL of a page into terms and consider them in ranking. Thus, spammers can include spam terms in the URL. For example, a URL may be http://www.xxx.com/cheap-MP3player-case-battery.html There are two main term spam techniques, which simply create synthetic contents containing spam terms. 1. Repeating some important terms: This method increases the TF scores of the repeated terms in a document and thus increases the relevance of the document to these terms. Since plain repetition can be easily detected by search engines, the spam terms can be weaven into some sentences, which may be copied from some other sources. That is, the spam terms are randomly placed in these sentences. For example, if a spammer wants to repeat the word “mining”, it may add it randomly in an unrelated (or related) sentence, e.g., “the picture mining quality of this camera mining is amazing,” instead of repeating it many times consecutively (next to each other), which is easy to detect. 2. Dumping of many unrelated terms: This method is used to make the page relevant to a large number of queries. In order to create the spam content quickly, the spammer may simply copy sentences from related pages on the Web and glue them together. Advertisers may also take advantage of some frequently searched terms on the Web and put them in the target pages so that when users search for the frequently search terms, the target pages become relevant. For example, to advertise cruise liners or cruise holiday packages, spammers put “Tom Cruise” in their advertising pages as “Tom Cruise” is a popular film actor in USA and is searched very frequently. 6.10.2 Link Spamming Since hyperlinks play an important role in determining the reputation score of a page, spammers also spam on hyperlinks. 232 6 Information Retrieval and Web Search Out-Link Spamming: It is quite easy to add out-links in one’s pages pointing to some authoritative pages to boost the hub cores of one’s pages. A page is a hub page if it points to many authoritative (or quality) pages. The concepts of authority and hub will be formally studied in the next chapter (Sect. 7.4). To create massive out-links, spammers may use a technique called directory cloning. There are many directories, e.g., Yahoo!, DMOZ Open Directory, on the Web which contain a large number of links to other Web pages that are organized according to some prespecified topic hierarchies. Spammers simply replicate a large portion of a directory in the spam page to create a massive out-link structure quickly. In-Link Spamming: In-link spamming is harder to achieve because it is not easy to add hyperlinks on the Web pages of others. Spammers typically use one or more of the following techniques. 1. Creating a honey pot: If a page wants to have a high reputation/quality score, it needs quality pages pointing to it (see Sect. 7.3 in the next chapter). This method basically tries to create some important pages that contain links to target spam pages. For example, the spammer can create a set of pages that contains some very useful information, e.g., glossary of Web mining terms, or Java FAQ and help pages. The honey pots attract people pointing to them because they contain useful information, and consequently have high reputation scores (high quality pages). Such honey pots contain (hidden) links to target spam pages that the spammers want to promote. This strategy can significantly boost the spam pages. 2. Adding links to Web directories: Many Web directories allow the user to submit URLs. Spammers can submit the URLs of spam pages at multiple directory sites. Since directory pages often have high quality (or authority) and hub scores, they can boost reputation scores of spam pages significantly. 3. Posting links to the user-generated content (reviews, forum discussions, blogs, etc): There are numerous sites on the Web that allow the user to freely post messages, which are called the user-generated content. Spammers can add links pointing to their pages to the seemly innocent messages that they post. 4. Participating in link exchange: In this case, many spammers form a group and set up a link exchange scheme so that their sites point to each other in order to promote the pages of all the sites. 5. Creating own spam farm: In this case, the spammer needs to control a large number of sites. Then, any link structure can be created to boost the ranking of target spam pages. 6.10 Web Spamming 233 6.10.3 Hiding Techniques In most situations, spammer wants to conceal or to hide the spamming sentences, terms and links so that the Web users do not see them. They can use a number of techniques. Content Hiding: Spam items are made invisible. One simple method is to make the spam terms the same color as the background color. For example, one may use the following for hiding, <body background = white> <font color = white> spam items</font> … </body> To hide a hyperlink, one can also use a very small image and a blank image. For example, one may use <a href = target.html”><img src=”blank.gif”> </a> A spammer can also use scripts to hide some of the visual elements on the page, for instance, by setting the visible HTML style attribute to false. Cloaking: Spam Web servers return a HTML document to the user and a different document to a Web crawler. In this way, the spammer can present the Web user with the intended content and send a spam page to the search engine for indexing. Spam Web servers can identify Web crawlers in one of the two ways: 1. It maintains a list of IP addresses of search engines and identifies search engine crawlers by matching IP addresses. 2. It identifies Web browsers based on the user–agent field in the HTTP request message. For instance, the user–agent name of the following HTTP request message is the one used by the Microsoft Internet Explorer 6 browser: GET /pub/WWW/TheProject.html HTTP/1.1 Host: www.w3.org User–Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) User–agent names are not standard, so it is up to the requesting application what to include in the corresponding message field. However, search engine crawlers usually identify themselves by names distinct from normal Web browsers in order to allow well-intended, and legitimate optimization. For example, some sites serve search engines a version of their pages that is free of navigation links and advertisements. 234 6 Information Retrieval and Web Search Redirection: Spammers can also hide a spammed page by automatically redirecting the browser to another URL as soon as the page is loaded. Thus, the spammed page is given to the search engine for indexing (which the user will never see), and the target page is presented to the Web user through redirection. One way to achieve redirection is to use the “refresh” meta-tag, and set the refresh time to zero. Another way is to use scripts. 6.10.4 Combating Spam Some spamming activities, like redirection using refresh meta-tag, are easy to detect. However, redirections by using scripts are hard to identify because search engine crawlers do not execute scripts. To prevent cloaking, a search engine crawler may identify itself as a regular Web browser. Using the terms of anchor texts of links that point to a page to index the page is able to fight content spam to some extent because anchor texts from other pages are more trustworthy. This method was originally proposed to index pages that were not fetched by search engine crawlers [364]. It is now a general technique used by search engines as we have seen in Sect. 6.8, i.e., search engines give terms in such anchor texts higher weights. In fact, the terms near a piece of anchor text also offer good editorial judgment about the target page. The PageRank algorithm [68] is able to combat content spam to a certain degree as it is based on links that point to the target pages, and the pages that point to the target pages need to be reputable or with high PageRank scores as well (see Chap. 7). However, it does not deal with the inlink based spamming methods discussed above. Instead of combating each individual type of spam, a method (called TrustRank) is proposed in [216] to combat all kinds of spamming methods at the same time. It takes advantage of the approximate isolation of reputable and non-spam pages, i.e., reputable Web pages seldom pointing to spam pages, and spam pages often link to many reputable pages (in an attempt to improve their hub scores). Link analysis methods are used to separate reputable pages and any form of spam without dealing with each spam technique individually. Combating spam can also be seen as a classification problem, i.e., predicting whether a page is a spam page or not. One can use any supervised learning algorithm to train a spam classifier. The key issue is to design features used in learning. The following are some example features used in [417] to detect content spam. 1. Number of words in the page: A spam page tends to contain more words than a non-spam page so as to cover a large number of popular words. Bibliographic Notes 235 2. Average word length: The mean word length for English prose is about 5 letters. Average word length of synthetic content is often different. 3. Number of words in the page title: Since search engines usually give extra weights to terms appearing in page titles, spammers often put many keywords in the titles of the spam pages. 4. Fraction of visible content: Spam pages often hide spam terms by making them invisible to the user. Other features used include the amount of anchor text, compressibility, fraction of page drawn from globally popular words, independent n-gram likelihoods, conditional n-gram likelihoods, etc. Details can be found in [417]. Its spam detection classifier gave very good results. Testing on 2364 spam pages and 14806 non-spam pages (17170 pages in total), the classifier was able to correctly identify 2,037 (86.2%) of the 2364 spam pages, while misidentifying only 526 spam and non-spam pages. Another interesting technique for fighting spam is to partition each Web page into different blocks using techniques discussed in Sect. 6.5. Each block is given an importance level automatically. To combat link spam, links in less important blocks are given lower transition probabilities to be used in the PageRank computation. The original PageRank algorithm assigns every link in a page an equal transition probability (see Sect. 7.3). The non-uniform probability assignment results in lower PageRank scores for pages pointed to by links in less important blocks. This method is effective because in the link exchange scheme and the honey pot scheme, the spam links are usually placed in unimportant blocks of the page, e.g., at the bottom of the page. The technique may also be used to fight term spam in a similar way, i.e., giving terms in less important blocks much lower weights in rank score computation. This method is proposed in [78]. However, sophisticated spam is still hard to detect. Combating spam is an on-going process. Once search engines are able to detect certain types of spam, spammers invent more sophisticated spamming methods. Bibliographic Notes Information retrieval (IR) is a major research field. This chapter only gives a brief introduction to some commonly used models and techniques. There are several text books that have a comprehensive coverage of the field, e.g., those by Baeza-Yates and Ribeiro-Neto [31], Grossman and Frieder [209], Salton and McGill [471], van Rijsbergen (an online book at http://www.dcs.gla.ac.uk/Keith/Preface.html), Witten et al. [551], and Yu and Meng [581]. 236 6 Information Retrieval and Web Search A similar chapter in the book by Chakrabarti [85] also discusses many Web specific issues and has influenced the writing of this chapter. Below, we discuss some further readings related to Web search and mining. On index compression, Elias coding was introduced by Elias [161] and Golomb coding was introduced by Golomb [202]. Their applications to index compression was studied by several researchers, e.g., Witten et al. [551], Bell et al. [45], Moffat et al. [392], and Williams and Zobel [547]. Wikipedia is a great source of information on this topic as well. Latent semantic index (LSI) was introduced by Deerwester et al. [125], which uses the singular value decomposition technique (SVD) [203]. Additional information about LSI and/or SVD can be found in [48, 581, 288]. Telcordia Technologies, where LSI was developed, maintains a LSI page at http://lsi.research.telcordia.com/ with more references. On Web page pre-processing, the focus has been on identifying the main content blocks of each page because a typical Web page contains a large amount of noise, which can adversely affect the search or mining accuracy. Several researchers have attempted the task, e.g., Bar-Yossef et al. [38], Debnath et al. [124], Gibson, et al. [199], Li et al. [324], Lin and Ho [336], Ma et al. [355], Ramaswamy et al. [456], Song et al. [495], Yi et al. [576], Yin and Lee [579], etc. Although search is probably the biggest application on the Web, little is known about the actual implementation of a search engine except some principal ideas. Sect. 6.8 is largely based on the Google paper by Brin and Page [68], and bits and pieces in various other sources. Over the years, a large number of researchers have studied Web search. More recent studies on various aspects of search can be found in [37, 79, 89, 262, 289, 297, 451, 460, 508, 567, 569, 611]. For metasearch, the combination methods in Sect. 6.9.1 were proposed by Fox and Shaw [184]. Aslam and Montague [28], Montague and Aslam [394], and Nuray and Can [418] provide good descriptions of Borda ranking and Condorcet ranking. In addition to ranking, Meng et al. [378] discussed many other metasearch issues. On Web spam, Gyongyi and Garcia-Molina gave an excellent taxonomy of different types of spam [214]. The TrustRank algorithm is also due to them [216]. An improvement to TrustRank was proposed by Wu et al. [557]. General link spam detection was studied by Adali et al. [1], Amitay et al. [19], Baeza-Yates et al. [30], Gyongyi and Garcia-Molina [215], Wu and Davison [555], Zhang et al. [604], etc. Content spam detection was studied by Fetterly et al. [176, 177], and Ntoulas et al. [417]. A cloaking detection algorithm is reported in [556]. 7 Link Analysis Early search engines retrieved relevant pages for the user based primarily on the content similarity of the user query and the indexed pages of the search engines. The retrieval and ranking algorithms were simply direct implementation of those from information retrieval. Starting from 1996, it became clear that content similarity alone was no longer sufficient for search due to two reasons. First, the number of Web pages grew rapidly during the middle to late 1990s. Given any query, the number of relevant pages can be huge. For example, given the search query “classification technique”, the Google search engine estimates that there are about 10 million relevant pages. This abundance of information causes a major problem for ranking, i.e., how to choose only 30–40 pages and rank them suitably to present to the user. Second, content similarity methods are easily spammed. A page owner can repeat some important words and add many remotely related words in his/her pages to boost the rankings of the pages and/or to make the pages relevant to a large number of possible queries. Starting from around 1996, researchers in academia and search engine companies began to work on the problem. They resort to hyperlinks. Unlike text documents used in traditional information retrieval, which are often considered independent of one another (i.e., with no explicit relationships or links among them except in citation analysis), Web pages are connected through hyperlinks, which carry important information. Some hyperlinks are used to organize a large amount of information at the same Web site, and thus only point to pages in the same site. Other hyperlinks point to pages in other Web sites. Such out-going hyperlinks often indicate an implicit conveyance of authority to the pages being pointed to. Therefore, those pages that are pointed to by many other pages are likely to contain authoritative or quality information. Such linkages should obviously be used in page evaluation and ranking in search engines. During the period of 1997-1998, two most influential hyperlink based search algorithms PageRank [68, 422] and HITS [281] were designed. PageRank is the algorithm that powers the successful search engine Google. Both PageRank and HITS were originated from social network analysis [540]. They both exploit the hyperlink structure of the Web to rank pages according to their levels of “prestige” or “authority”. We will study these 238 7 Link Analysis algorithms in this chapter. We should also note that hyperlink-based page evaluation and ranking is not the only method used by search engines. As we discussed in Chap. 6, contents and many other factors are also considered in producing the final ranking presented to the user. Apart from search ranking, hyperlinks are also useful for finding Web communities. A Web community is a cluster of densely linked pages representing a group of people with a common interest. Beyond explicit hyperlinks on the Web, links in other contexts are useful too, e.g., for discovering communities of named entities (e.g., people and organizations) in free text documents, and for analyzing social phenomena in emails. This chapter will introduce some of the current algorithms. 7.1 Social Network Analysis Social network is the study of social entities (people in an organization, called actors), and their interactions and relationships. The interactions and relationships can be represented with a network or graph, where each vertex (or node) represents an actor and each link represents a relationship. From the network we can study the properties of its structure, and the role, position and prestige of each social actor. We can also find various kinds of sub-graphs, e.g., communities formed by groups of actors. Social network analysis is useful for the Web because the Web is essentially a virtual society, and thus a virtual social network, where each page can be regarded as a social actor and each hyperlink as a relationship. Many of the results from social networks can be adapted and extended for use in the Web context. The ideas from social network analysis are indeed instrumental to the success of Web search engines. In this section, we introduce two types of social network analysis, centrality and prestige, which are closely related to hyperlink analysis and search on the Web. Both centrality and prestige are measures of degree of prominence of an actor in a social network. We introduce them below. For a more complete treatment of the topics, please refer to the authoritative text by Wasserman and Faust [540]. 7.1.1 Centrality Important or prominent actors are those that are linked or involved with other actors extensively. In the context of an organization, a person with extensive contacts (links) or communications with many other people in the organization is considered more important than a person with relatively 7.1 Social Network Analysis 239 fewer contacts. The links can also be called ties. A central actor is one involved in many ties. Fig. 7.1 shows a simple example using an undirected graph. Each node in the social network is an actor and each link indicates that the actors on the two ends of the link communicate with each other. Intuitively, we see that the actor i is the most central actor because he/she can communicate with most other actors. i Fig. 7.1. An example of a social network There are different types of links or involvements between actors. Thus, several types of centrality are defined on undirected and directed graphs. We discuss three popular types below. Degree Centrality Central actors are the most active actors that have most links or ties with other actors. Let the total number of actors in the network be n. Undirected Graph: In an undirected graph, the degree centrality of an actor i (denoted by CD(i)) is simply the node degree (the number of edges) of the actor node, denoted by d(i), normalized with the maximum degree, n 1. C D (i ) d (i ) . n1 (1) The value of this measure ranges between 0 and 1 as n 1 is the maximum value of d(i). Directed Graph: In this case, we need to distinguish in-links of actor i (links pointing to i), and out-links (links pointing out from i). The degree centrality is defined based on only the out-degree (the number of out-links or edges), do(i). ' C D (i ) d o (i ) . n1 (2) 240 7 Link Analysis Closeness Centrality This view of centrality is based on the closeness or distance. The basic idea is that an actor xi is central if it can easily interact with all other actors. That is, its distance to all other actors is short. Thus, we can use the shortest distance to compute this measure. Let the shortest distance from actor i to actor j be d(i, j) (measured as the number of links in a shortest path). Undirected Graph: The closeness centrality CC(i) of actor i is defined as CC (i ) n1 n j . d (i, j ) 1 (3) The value of this measure also ranges between 0 and 1 as n 1 is the minimum value of the denominator, which is the sum of the shortest distances from i to all other actors. Note that this equation is only meaningful for a connected graph. Directed Graph: The same equation can be used for a directed graph. The distance computation needs to consider directions of links or edges. Betweenness Centrality If two non-adjacent actors j and k want to interact and actor i is on the path between j and k, then i may have some control over their interactions. Betweenness measures this control of i over other pairs of actors. Thus, if i is on the paths of many such interactions, then i is an important actor. Undirected Graph: Let pjk be the number of shortest paths between actors j and k. The betweenness of an actor i is defined as the number of shortest paths that pass i (denoted by pjk(i), j i and k i) normalized by the total number of shortest paths of all pairs of actors not including i: p jk (i ) C B (i ) jk p jk . (4) Note that there may be multiple shortest paths between actor j and actor k. Some pass i and some do not. We assume that all paths are equally likely to be used. CB(i) has a minimum of 0, attained when i falls on no shortest path. Its maximum is (n 1)(n 2)/2, which is the number of pairs of actors not including i. In the network of Fig. 7.2, actor 1 is the most central actor. It lies on all 15 shortest paths linking the other 6 actors. CB(1) has the maximum value of 15, and CB(2) = CB(3) = CB(4) = CB(5) = CB(6) = CB(7) = 0. 7.1 Social Network Analysis 241 5 6 4 1 3 7 2 Fig. 7.2. An example of a network illustrating the betweenness centrality If we are to ensure that the value range is between 0 and 1, we can normalize it with (n 1)(n 2)/2, which is the maximum value of CB(i). The standardized betweenness of actor i is defined as p jk (i ) 2 ' C B (i ) jk p jk (n 1)(n 2) (5) . Unlike the closeness measure, the betweenness can be computed even if the graph is not connected. Directed Graph: The same equation can be used but must be multiplied by 2 because there are now (n 1)(n 2) pairs considering a path from j to k is different from a path from k to j. Likewise, pjk must consider paths from both directions. 7.1.2 Prestige Prestige is a more refined measure of prominence of an actor than centrality as we will see below. We need to distinguish between ties sent (outlinks) and ties received (in-links). A prestigious actor is defined as one who is object of extensive ties as a recipient. In other words, to compute the prestige of an actor, we only look at the ties (links) directed or pointed to the actor (in-links). Hence, the prestige cannot be computed unless the relation is directional or the graph is directed. The main difference between the concepts of centrality and prestige is that centrality focuses on outlinks while prestige focuses on in-links. We define three prestige measures. The third prestige measure (i.e., rank prestige) forms the basis of most Web page link analysis algorithms, including PageRank and HITS. 242 7 Link Analysis Degree Prestige Based on the definition of the prestige, it is clear that an actor is prestigious if it receives many in-links or nominations. Thus, the simplest measure of prestige of an actor i (denoted by PD(i)) is its in-degree. d I (i ) , n1 PD (i ) (6) where dI(i) is the in-degree of i (the number of in-links of i) and n is the total number of actors in the network. As in the degree centrality, dividing by n – 1 standardizes the prestige value to the range from 0 and 1. The maximum prestige value is 1 when every other actor links to or chooses actor i. Proximity Prestige The degree index of prestige of an actor i only considers the actors that are adjacent to i. The proximity prestige generalizes it by considering both the actors directly and indirectly linked to actor i. That is, we consider every actor j that can reach i, i.e., there is a directed path from j to i. Let Ii be the set of actors that can reach actor i, which is also called the influence domain of actor i. The proximity is defined as closeness or distance of other actors to i. Let d(j, i) denote the shortest path distance from actor j to actor i. Each link has the unit distance. To compute the proximity prestige, we use the average distance, which is d ( j, i) j Ii | Ii | , (7) where |Ii| is the size of the set Ii. If we look at the ratio or proportion of actors who can reach i to the average distance that these actors are from i, we obtain the proximity prestige, which has the value range of [0, 1]: PP (i ) | I i | (n 1) , d ( j, i) | I i | (8) j Ii where |Ii|/(n 1) is the proportion of actors that can reach actor i. In one extreme, every actor can reach actor i, which gives |Ii|/(n 1) = 1. The denominator is 1 if every actor is adjacent to i. Then, PP(i) = 1. On the other extreme, no actor can reach actor i. Then |Ii| = 0, and PP(i) = 0. 7.2 Co-Citation and Bibliographic Coupling 243 Rank Prestige The above two prestige measures are based on in-degrees and distances. However, an important factor that has not been considered is the prominence of individual actors who do the “voting” or “choosing.” In the real world, a person i chosen by an important person is more prestigious than chosen by a less important person. For example, a company CEO voting for a person is much more important than a worker voting for the person. If one’s circle of influence is full of prestigious actors, then one’s own prestige is also high. Thus one’s prestige is affected by the ranks or statuses of the involved actors. Based on this intuition, the rank prestige PR(i) is defined as a linear combination of links that point to i: PR (i ) A1i PR (1) A2i PR (2) ... Ani PR (n), (9) where Aji = 1 if j points to i, and 0 otherwise. This equation says that an actor’s rank prestige is a function of the ranks of the actors who vote or choose the actor, which makes perfect sense. Since we have n equations for n actors, we can write them in the matrix notation. We use P to represent the vector that contains all the rank prestige values, i.e., P = (PR(1), PR(2), …, PR(n))T (T means matrix transpose). P is represented as a column vector. We use matrix A (where Aij = 1 if i points to j, and 0 otherwise) to represent the adjacency matrix of the network or graph. As a notational convention, we use bold italic letters to represent matrices. We then have P AT P . (10) This equation is precisely the characteristic equation used for finding the eigensystem of the matrix AT. P is an eigenvector of AT. This equation and the idea behind it turn out to be very useful in Web search. Indeed, the most well known ranking algorithms for Web search, PageRank and HITS, are directly related to this equation. Sect. 7.3 and 7.4 will focus on these two algorithms and describe how to solve the equation to obtain the prestige value of each actor (or each page on the Web). 7.2 Co-Citation and Bibliographic Coupling Another area of research concerned with links is the citation analysis of scholarly publications. A scholarly publication usually cites related prior work to acknowledge the origins of some ideas in the publication and to compare the new proposal with existing work. Citation analysis is an area 244 7 Link Analysis of bibliometric research, which studies citations to establish the relationships between authors and their work. When a publication (also called a paper) cites another publication, a relationship is established between the publications. Citation analysis uses these relationships (links) to perform various types of analysis. A citation can represent many types of links, such as links between authors, publications, journals and conferences, and fields, or even between countries. We will discuss two specific types of citation analysis, co-citation and bibliographic coupling. The HITS algorithm of Sect. 7.4 is related to these two types of analysis. 7.2.1 Co-Citation Co-citation is used to measure the similarity of two documents. If papers i and j are both cited by paper k, then they may be said to be related in some sense to one another, even they do not directly cite each other. Figure 7.3 shows that papers i and j are co-cited by paper k. If papers i and j are cited together by many papers, it means that i and j have a strong relationship or similarity. The more papers they are cited by, the stronger their relationship is. i k j Fig. 7.3. Paper i and paper j are co-cited by paper k Let L be the citation matrix. Each cell of the matrix is defined as follows: Lij = 1 if paper i cites paper j, and 0 otherwise. Co-citation (denoted by Cij) is a similarity measure defined as the number of papers that co-cite i and j, and is computed with n Cij Lki Lkj , (11) k1 where n is the total number of papers. Cii is naturally the number of papers that cite i. A square matrix C can be formed with Cij, and it is called the cocitation matrix. Co-citation is symmetric, Cij = Cji, and is commonly used as a similarity measure of two papers in clustering to group papers of similar topics together. 7.3 PageRank 7.2.2 245 Bibliographic Coupling Bibliographic coupling operates on a similar principle, but in a way it is the mirror image of co-citation. Bibliographic coupling links papers that cite the same articles so that if papers i and j both cite paper k, they may be said to be related, even though they do not directly cite each other. The more papers they both cite, the stronger their similarity is. Figure 7.4 shows both papers i and j citing (referencing) paper k. k i j Fig. 7.4. Both paper i and paper j cite paper k We use Bij to represent the number of papers that are cited by both papers i and j: n Bij Lik L jk . (12) k1 Bii is naturally the number of references (in the reference list) of paper i. A square matrix B can be formed with Bij, and it is called the bibliographic coupling matrix. Bibliographic coupling is also symmetric and is regarded as a similarity measure of two papers in clustering. We will see later that two important types of pages on the Web, hubs and authorities, found by the HITS algorithm are directly related to cocitation and bibliographic coupling matrices. 7.3 PageRank The year 1998 was an important year for Web link analysis and Web search. Both the PageRank and the HITS algorithms were reported in that year. HITS was presented by Jon Kleinberg in January, 1998 at the Ninth Annual ACM-SIAM Symposium on Discrete Algorithms. PageRank was presented by Sergey Brin and Larry Page at the Seventh International World Wide Web Conference (WWW7) in April, 1998. Based on the algorithm, they built the search engine Google. The main ideas of PageRank and HITS are really quite similar. However, it is their dissimilarity that 246 7 Link Analysis made a huge difference as we will see later. Since that year, PageRank has emerged as the dominant link analysis model for Web search, partly due to its query-independent evaluation of Web pages and its ability to combat spamming, and partly due to Google’s business success. In this section, we focus on PageRank. In the next section, we discuss HITS. A detailed study of these algorithms can also be found in [304]. PageRank relies on the democratic nature of the Web by using its vast link structure as an indicator of an individual page's quality. In essence, PageRank interprets a hyperlink from page x to page y as a vote, by page x, for page y. However, PageRank looks at more than just the sheer number of votes, or links that a page receives. It also analyzes the page that casts the vote. Votes casted by pages that are themselves “important” weigh more heavily and help to make other pages more “important.” This is exactly the idea of rank prestige in social networks (see Sect. 7.1.2). 7.3.1 PageRank Algorithm PageRank is a static ranking of Web pages in the sense that a PageRank value is computed for each page off-line and it does not depend on search queries. Since PageRank is based on the measure of prestige in social networks, the PageRank value of each page can be regarded as its prestige. We now derive the PageRank formula. Let us first state some main concepts again in the Web context. In-links of page i: These are the hyperlinks that point to page i from other pages. Usually, hyperlinks from the same site are not considered. Out-links of page i: These are the hyperlinks that point out to other pages from page i. Usually, links to pages of the same site are not considered. From the perspective of prestige, we use the following to derive the PageRank algorithm. 1. A hyperlink from a page pointing to another page is an implicit conveyance of authority to the target page. Thus, the more in-links that a page i receives, the more prestige the page i has. 2. Pages that point to page i also have their own prestige scores. A page with a higher prestige score pointing to i is more important than a page with a lower prestige score pointing to i. In other words, a page is important if it is pointed to by other important pages. According to rank prestige in social networks, the importance of page i (i’s PageRank score) is determined by summing up the PageRank scores of all pages that point to i. Since a page may point to many other pages, its pres- 7.3 PageRank 247 tige score should be shared among all the pages that it points to. Notice the difference from rank prestige, where the prestige score is not shared. To formulate the above ideas, we treat the Web as a directed graph G = (V, E), where V is the set of vertices or nodes, i.e., the set of all pages, and E is the set of directed edges in the graph, i.e., hyperlinks. Let the total number of pages on the Web be n (i.e., n = |V|). The PageRank score of the page i (denoted by P(i)) is defined by: P (i ) ( j ,i ) P( j ) , E Oj (13) where Oj is the number of out-links of page j. Mathematically, we have a system of n linear equations (13) with n unknowns. We can use a matrix to represent all the equations. Let P be a n-dimensional column vector of PageRank values, i.e., P = (P(1), P(2), …, P(n))T. Let A be the adjacency matrix of our graph with Aij 1 Oi 0 if (i, j ) E (14) otherwise We can write the system of n equations with (similar to Equation 10) P AT P . (15) This is the characteristic equation of the eigensystem, where the solution to P is an eigenvector with the corresponding eigenvalue of 1. Since this is a circular definition, an iterative algorithm is used to solve it. It turns out that if some conditions are satisfied (which will be described shortly), 1 is the largest eigenvalue and the PageRank vector P is the principal eigenvector. A well known mathematical technique called power iteration can be used to find P. However, the problem is that Equation (15) does not quite suffice because the Web graph does not meet the conditions. To introduce these conditions and the enhanced equation, let us derive the same Equation (15) based on the Markov chain [207]. In the Markov chain model, each Web page or node in the Web graph is regarded as a state. A hyperlink is a transition, which leads from one state to another state with a probability. Thus, this framework models Web surfing as a stochastic process. It models a Web surfer randomly surfing the Web as a state transition in the Markov chain. Recall that we used Oi to 248 7 Link Analysis denote the number of out-links of a node i. Each transition probability is 1/Oi if we assume the Web surfer will click the hyperlinks in the page i uniformly at random, the “back” button on the browser is not used and the surfer does not type in an URL. Let A be the state transition probability matrix, a square matrix of the following format, A11 . . A1n A22 . . . A2 n . . . . . . . . . An1 . . A21 A A12 An 2 . . . Ann Aij represents the transition probability that the surfer in state i (page i) will move to state j (page j). Aij is defined exactly as in Equation (14). Given an initial probability distribution vector that a surfer is at each state (or page) p0 = (p0(1), p0(2), …, p0(n))T (a column vector) and an n n transition probability matrix A, we have n p0 (i ) 1 (16) 1. (17) i1 n Aij j1 Equation (17) is not quite true for some Web pages because they have no out-links. If the matrix A satisfies Equation (17), we say that A is the stochastic matrix of a Markov chain. Let us assume A is a stochastic matrix for the time being and deal with it not being that later. In a Markov chain, a question of common interest is: Given the initial probability distribution p0 at the beginning, what is the probability that m steps/transitions later that the Markov chain will be at each state j? We can determine the probability that the system (or the random surfer) is in state j after 1 step (1 state transition) by using the following reasoning: n p1 ( j ) Aij (1) p0 (i ), (18) i1 where Aij(1) is the probability of going from i to j after 1 transition, and Aij(1) = Aij. We can write it with a matrix: 7.3 PageRank p1 AT p0 . 249 (19) In general, the probability distribution after k steps/transitions is: pk AT pk -1 . (20) Equation (20) looks very similar to Equation (15). We are getting there. By the Ergodic Theorem of Markov chains [207], a finite Markov chain defined by the stochastic transition matrix A has a unique stationary probability distribution if A is irreducible and aperiodic. These mathematical terms will be defined as we go along. The stationary probability distribution means that after a series of transitions pk will converge to a steady-state probability vector regardless of the choice of the initial probability vector p0, i.e., lim pk k . (21) When we reach the steady-state, we have pk = pk+1 = , and thus =AT . is the principal eigenvector of AT with eigenvalue of 1. In PageRank, is used as the PageRank vector P. Thus, we again obtain Equation (15), which is re-produced here as Equation (22): P AT P . (22) Using the stationary probability distribution as the PageRank vector is reasonable and quite intuitive because it reflects the long-run probabilities that a random surfer will visit the pages. A page has a high prestige if the probability of visiting it is high. Now let us come back to the real Web context and see whether the above conditions are satisfied, i.e., whether A is a stochastic matrix and whether it is irreducible and aperiodic. In fact, none of them is satisfied. Hence, we need to extend the ideal-case Equation (22) to produce the “actual PageRank model”. Let us look at each condition below. First of all, A is not a stochastic (transition) matrix. A stochastic matrix is the transition matrix for a finite Markov chain whose entries in each row are non-negative real numbers and sum to 1 (i.e., Equation 17). This requires that every Web page must have at least one out-link. This is not true on the Web because many pages have no out-links, which are reflected in transition matrix A by some rows of complete 0’s. Such pages are called the dangling pages (nodes). Example 1: Figure 7.5 shows an example of a hyperlink graph. 250 7 Link Analysis 1 5 3 2 4 6 Fig. 7.5. An example of a hyperlink graph If we assume that the Web surfer will click the hyperlinks in a page uniformly at random, we have the following transition probability matrix: A 0 12 12 0 0 0 12 0 12 0 0 0 0 1 0 0 0 0 . 0 0 13 0 13 13 0 0 0 0 0 0 0 0 0 12 12 0 (23) For example A12 = A13 = 1/2 because node 1 has two out-links. We can see that A is not a stochstic matrix because the fifth row is all 0’s, i.e., page 5 is a dangling page. We can fix this problem in several ways in order to convert A to a stochastic transition matrix. We describe only two ways here: 1. Remove those pages with no out-links from the system during the PageRank computation as these pages do not affect the ranking of any other page directly. Out-links from other pages pointing to these pages are also removed. After PageRanks are computed, these pages and hyperlinks pointing to them can be added in. Their PageRanks are easy to calculate based on Equation (22). Note that the transition probabilities of those pages with removed links will be slightly affected but not significantly. This method is suggested in [68]. 2. Add a complete set of outgoing links from each such page i to all the pages on the Web. Thus the transition probability of going from i to every page is 1/n assuming uniform probability distribution. That is, we replace each row containing all 0’s with e/n, where e is n-dimensional vector of all 1’s. If we use the second method to make A a stochastic matrix by adding a link from page 5 to every page, we obtain 7.3 PageRank A 0 12 12 0 0 1 0 0 16 16 0 0 12 0 0 0 12 0 0 0 0 0 0 0 . 13 0 13 13 16 16 16 16 0 12 12 0 251 (24) Below, we assume that either one of the above is done to make A a stochastic matrix. Second, A is not irreducible. Irreducible means that the Web graph G is strongly connected. Definition (strongly connected): A directed graph G = (V, E) is strongly connected if and only if, for each pair of nodes u, v V, there is a path from u to v. A general Web graph represented by A is not irreducible because for some pair of nodes u and v, there is no path from u to v. For example, in Fig. 7.5, there is no directed path from node 3 to node 4. The adjustment in Equation (24) is not enough to ensure irreducibility. That is, in A , there is still no directed path from node 3 to node 4. This problem and the next problem can be dealt with using a single strategy (to be described shortly). Finally, A is not aperiodic. A state i in a Markov chain being periodic means that there exists a directed cycle that the chain has to traverse. Definition (aperiodic): A state i is periodic with period k > 1 if k is the smallest number such that all paths leading from state i back to state i have a length that is a multiple of k. If a state is not periodic (i.e., k = 1), it is aperiodic. A Markov chain is aperiodic if all states are aperiodic. Example 2: Figure 7.6 shows a periodic Markov chain with k = 3. The transition matrix is given on the left. Each state in this chain has a period of 3. For example, if we start from state 1, to come back to state 1 the only path is 1-2-3-1 for some number of times, say h. Thus any return to state 1 will take 3h transitions. In the Web, there could be many such cases. 1 010 A 001 100 1 Fig. 7.6. A periodic Markov chain with k = 3. 1 2 1 3 252 7 Link Analysis It is easy to deal with the above two problems with a single strategy. We add a link from each page to every page and give each link a small transition probability controlled by a parameter d. The augmented transition matrix becomes irreducible because it is clearly strongly connected. It is also aperiodic because the situation in Fig. 7.6 no longer exists as we now have paths of all possible lengths from state i back to state i. That is, the random surfer does not have to traverse a fixed cycle for any state. After this augmentation, we obtain an improved PageRank model. In this model, at a page, the random surfer has two options: 1. With probability d, he randomly chooses an out-link to follow. 2. With probability 1 d, he jumps to a random page without a link. Equation (25) gives the improved model, (1 d ) P E n (25) dAT P where E is eeT (e is a column vector of all 1’s) and thus E is a n n square matrix of all 1’s. 1/n is the probability of jumping to a particular page. n is the total number of nodes in the Web graph. Note that Equation (25) assumes that A has already been made a stochastic matrix. Example 3: If we follow our example in Fig. 7.5 and Equation (24) (we use A for A here), the augmented transition matrix is (1 d ) E n dAT 1 60 7 15 7 15 1 60 1 60 1 60 7 15 1 60 7 15 1 60 1 60 1 60 1 60 1 60 1 6 11 12 1 6 0 1 6 1 6 0 19 6 0 1 6 1 60 1 60 1 6 1 6 0 19 6 0 1 6 1 6 0 19 6 0 1 6 1 60 1 60 1 60 7 15 7 15 1 60 (26) (1 d)E/n + dAT is a stochastic matrix (but transposed). It is also irreducible and aperiodic as we discussed above. Here we use d = 0.9. If we scale Equation (25) so that eTP = n, we obtain P (1 d )e dAT P . (27) Before scaling, we have eTP = 1 (i.e., P(1) + P(2) + … + P(n) = 1 if we recall that P is the stationary probability vector of the Markov chain). The scaling is equivalent to multiplying n on both sides of Equation (25). 7.3 PageRank 253 This gives us the PageRank formula for each page i as follows: n P (i ) (1 d ) d A ji P ( j ), (28) j1 which is equivalent to the formula given in the PageRank papers [68, 422]: P (i ) (1 d ) d ( j ,i ) P( j ) . E Oj (29) The parameter d is called the damping factor which can be set to between 0 and 1. d = 0.85 is used in [68, 422]. The computation of PageRank values of the Web pages can be done using the well known power iteration method [203], which produces the principal eigenvector with the eigenvalue of 1. The algorithm is simple, and is given in Fig. 7.7. One can start with any initial assignments of PageRank values. The iteration ends when the PageRank values do not change much or converge. In Fig. 7.7, the iteration ends after the 1-norm of the residual vector is less than a pre-specified threshold . Note that the 1-norm for a vector is simply the sum of all the components. PageRank-Iterate(G) P0 e/n k1 repeat Pk (1 d ) e d A T Pk -1 ; k k + 1; until ||Pk – Pk-1||1 < return Pk Fig. 7.7. The power iteration method for PageRank Since we are only interested in the ranking of the pages, the actual convergence may not be necessary. Thus, fewer iterations are needed. In [68], it is reported that on a database of 322 million links the algorithm converges to an acceptable tolerance in roughly 52 iterations. 7.3.2 Strengths and Weaknesses of PageRank The main advantage of PageRank is its ability to fight spam. A page is important if the pages pointing to it are important. Since it is not easy for Web page owner to add in-links into his/her page from other important pages, it is thus not easy to influence PageRank. Nevertheless, there are 254 7 Link Analysis reported ways to influence PageRank. Recognizing and fighting spam is an important issue in Web search. Another major advantage of PageRank is that it is a global measure and is query independent. That is, the PageRank values of all the pages on the Web are computed and saved off-line rather than at the query time. At the query time, only a lookup is needed to find the value to be integrated with other strategies to rank the pages. It is thus very efficient at query time. Both these two advantages contributed greatly to Google’s success. The main criticism is also the query-independence nature of PageRank. It could not distinguish between pages that are authoritative in general and pages that are authoritative on the query topic. Google may have other ways to deal with the problem, which we do not know due to the proprietary nature of Google. Another criticism is that PageRank does not consider time. Let us give some explanation to this. 7.3.3 Timed PageRank The Web is a dynamic environment, pages in the past may not be quality search has a temporal dimension. An given in [326, 585] adds the temporal vations are: and it changes constantly. Quality pages now or in the future. Thus, algorithm called TimedPageRank dimension to PageRank. The moti- 1. Users are often interested in the latest information. Apart from pages that contain well-established facts and classics which do not change significantly over time, most contents on the Web change constantly. New pages or contents are added, and ideally, outdated contents and pages are deleted. However, in practice many outdated pages and links are not deleted. This causes problems for Web search because such outdated pages may still be ranked very high. 2. PageRank favors pages that have many in-links. To some extent, we can say that it favors older pages because they have existed on the Web for a long time and thus have accumulated many in-links. Then the problem is that new pages which are of high quality and also give the up-to-date information will not be assigned high scores and consequently will not be ranked high because they have fewer or no in-links. It is thus difficult for users to find the latest information on the Web based on PageRank. The idea of TimedPageRank is simple. Instead of using a constant damping factor d as the parameter in PageRank, TimedPageRank uses a function of time f(t) (0 f(t) 1), where t is the difference between the current time and the time when the page was last updated. f(t) returns a probability that 7.4 HITS 255 the Web surfer will follow an actual link on the page. 1 f(t) returns the probability that the surfer will jump to a random page. Thus, at a particular page i, the Web surfer has two options: 1. With probability f(ti), he randomly chooses an out-going link to follow. 2. With probability 1 f(ti), he jumps to a random page without a link. The intuition here is that if the page was last updated (or created) a long time ago, the pages that it cites (points to) are even older and are probably out of date. Then the 1 f(t) value for such a page should be large, which means that the surfer will have a high probability of jumping to a random page. If a page is new, then its 1 f(t) value should be small, which means that the surfer will have a high probability to follow an out-link of the page and a small probability of jumping to a random page. For a complete new page in a Web site, which does not have any inlinks at all, the method given in [326] uses the average TimedPageRank value of the past pages in the Web site. Finally, we note again that the link-based ranking is not the only strategy used in a search engine. Many other information retrieval methods, heuristics and empirical parameters are also employed. However, their details are not published. We also note that PageRank is not the only linkbased static and global ranking algorithm. All major search engines, such as Yahoo! and MSN, have their own algorithms but are unpublished. 7.4 HITS HITS stands for Hypertext Induced Topic Search [281]. Unlike PageRank which is a static ranking algorithm, HITS is search query dependent. When the user issues a search query, HITS first expands the list of relevant pages returned by a search engine and then produces two rankings of the expanded set of pages, authority ranking and hub ranking. An authority is a page with many in-links. The idea is that the page may have good or authoritative content on some topic and thus many people trust it and link to it. A hub is a page with many out-links. The page serves as an organizer of the information on a particular topic and points to many good authority pages on the topic. When a user comes to this hub page, he/she will find many useful links which take him/her to good content pages on the topic. Figure 7.8 shows an authority page and a hub page. The key idea of HITS is that a good hub points to many good authorities and a good authority is pointed to by many good hubs. Thus, authorities and hubs have a mutual reinforcement relationship. Figure 7.9 shows a 256 7 Link Analysis set of densely linked authorities and hubs (a bipartite sub-graph). Below, we first present the HITS algorithm, and also make a connection between HITS and co-citation and bibliographic coupling in bibliometric research. We then discuss the strengths and weaknesses of HITS, and describe some possible ways to deal with its weaknesses. An authority A hub Fig. 7.8. An authority page and a hub page Authorities Hubs Fig. 7.9. A densely linked set of authorities and hubs 7.4.1 HITS Algorithm Before describing the HITS algorithm, let us first describe how HITS collects pages to be ranked. Given a broad search query, q, HITS collects a set of pages as follows: 1. It sends the query q to a search engine system. It then collects t (t = 200 is used in the HITS paper) highest ranked pages, which assume to be highly relevant to the search query. This set is called the root set W. 2. It then grows W by including any page pointed to by a page in W and any page that points to a page in W. This gives a larger set called S. However, this set can be very large. The algorithm restricts its size by allowing each page in W to bring at most k pages (k = 50 is used in the HITS paper) pointing to it into S. The set S is called the base set. 7.4 HITS 257 HITS then works on the pages in S, and assigns every page in S an authority score and a hub score. Let the number of pages to be studied be n. We again use G = (V, E) to denote the (directed) link graph of S. V is the set of pages (or nodes) and E is the set of directed edges (or links). We use L to denote the adjacency matrix of the graph. Lij 1 if (i, j ) E 0 otherwise (30) Let the authority score of the page i be a(i), and the hub score of page i be h(i). The mutual reinforcing relationship of the two scores is represented as follows: a (i ) h( j ) (31) ( j ,i ) E h(i ) a( j ) (32) (i , j ) E Writing them in the matrix form, we use a to denote the column vector with all the authority scores, a = (a(1), a(2), …, a(n))T, and use h to denote the column vector with all the authority scores, h = (h(1), h(2), …, h(n))T, a = LTh (33) h = La (34) The computation of authority scores and hub scores is basically the same as the computation of the PageRank scores using the power iteration method. If we use ak and hk to denote authority and hub scores at the kth iteration, the iterative processes for generating the final solutions are ak = LTLak 1 (35) hk = LLThk 1 (36) starting with a0 = h0 = (1, 1, …, 1). (37) Note that Equation (35) (or Equation 36) does not use the hub (or authority) vector due to substitutions of Equation (33) and Equation (34). After each iteration, the values are also normalized (to keep them small) so that 258 7 Link Analysis HITS-Iterate(G) a0 h0 (1, 1, …, 1); k1 Repeat ak LT Lak 1 ; hk ak hk LLT hk 1 ; ak /||ak||1; hk /||hk||1; k k + 1; until ||ak – ak-1||1 < return ak and hk // normalization // normalization a and ||hk – hk-1||1 < h; Fig. 7.10. The HITS algorithm based on power iteration n a (i ) 1 (38) h(i ) 1 (39) i1 n i1 The power iteration algorithm for HITS is given in Fig. 7.10. The iteration ends after the 1-norms of the residual vectors are less than some thresholds a and h. Hence, the algorithm finds the principal eigenvectors at “equilibrium” as in PageRank. The pages with large authority and hub scores are better authorities and hubs respectively. HITS will select a few top ranked pages as authorities and hubs, and return them to the user. Although HITS will always converge, there is a problem with uniqueness of limiting (converged) authority and hub vectors. It is shown that for certain types of graphs, different initializations to the power method produce different final authority and hub vectors. Some results can be inconsistent or wrong. Farahat et al. [171] gave several examples. The heart of the problem is that there are repeated dominant (principal) eigenvalues (several eigenvalues are the same and are dominant eigenvalues), which are caused by the problem that LTL (respectively LLT) is reducible [303]. The first PageRank solution (Equation 22) has the same problem. However, the PageRank inventors found a way to get around the problem. A modification similar to PageRank may be applied to HITS. 7.4 HITS 7.4.2 259 Finding Other Eigenvectors The HITS algorithm given in Fig. 7.10 finds the principal eigenvectors, which in a sense represent the most densely connected authorities and hubs in the graph G defined by a query. However, in some cases, we may also be interested in finding several densely linked collections of hubs and authorities among the same base set of pages. Each of such collections could potentially be relevant to the query topic, but they could be well-separated from one another in the graph G for a variety of reasons. For example, 1. The query string may be ambiguous with several very different meanings, e.g., “jaguar”, which could be a cat or a car. 2. The query string may represent a topic that may arise as a term in the multiple communities, e.g. “classification”. 3. The query string may refer to a highly polarized issue, involving groups that are not likely to link to one another, e.g. “abortion”. In each of these examples, the relevant pages can be naturally grouped into several clusters, also called communities. In general, the top ranked authorities and hubs represent the major cluster (or community). The smaller clusters (or communities), which are also represented by bipartite subgraphs as that in Fig. 7.9, can be found by computing non-principal eigenvectors. Non-principal eigenvectors are calculated in a similar way to power iteration using methods such as orthogonal iteration and QR iteration. We will not discuss the details of these methods. Interested readers can refer to the book by Golub and Van Loan [203]. 7.4.3 Relationships with Co-Citation and Bibliographic Coupling Authority pages and hub pages have their matches in the bibliometric citation context. An authority page is like an influential research paper (publication) which is cited by many subsequent papers. A hub page is like a survey paper which cites many other papers (including those influential papers). It is no surprise that there is a connection between authority and hub, and co-citation and bibliographic coupling. Recall that co-citation of pages i and j, denoted by Cij, is computed as n Cij Lki Lkj ( LT L) ij . (40) k1 This shows that the authority matrix (LTL) of HITS is in fact the cocitation matrix C in the Web context. Likewise, recall that bibliographic 260 7 Link Analysis coupling of two pages i and j, denoted by Bij, is computed as n Bij Lik L jk ( LLT )ij , (41) k1 which shows that the hub matrix (LLT) of HITS is the bibliographic coupling matrix B in the Web context. 7.4.4 Strengths and Weaknesses of HITS The main strength of HITS [281] is its ability to rank pages according to the query topic, which may be able to provide more relevant authority and hub pages. The ranking may also be combined with information retrieval based rankings. However, HITS has several disadvantages. First of all, it does not have the anti-spam capability of PageRank. It is quite easy to influence HITS by adding out-links from one’s own page to point to many good authorities. This boosts the hub score of the page. Because hub and authority scores are interdependent, it in turn also increases the authority score of the page. Another problem of HITS is topic drift. In expanding the root set, it can easily collect many pages (including authority pages and hub pages) which have nothing to do the search topic because out-links of a page may not point to pages that are relevant to the topic and in-links to pages in the root set may be irrelevant as well because people put hyperlinks for all kinds of reasons, including spamming. The query time evaluation is also a major drawback. Getting the root set, expanding it and then performing eigenvector computation are all time consuming operations. Over the years, many researchers tried to deal with these problems. We briefly discuss some of them below. It was reported by several researchers in [52, 310, 405] that small changes to the Web graph topology can significantly change the final authority and hub vectors. Minor perturbations have little effect on PageRank, which is more stable than HITS. This is essentially due to the random jump step of PageRank. Ng et al. [405] proposed a method by introducing the same random jump step to HITS (by jumping to the base set uniformly at random with probability d), and showed that it could improve the stability of HITS significantly. Lempel and Moran [310] proposed SALSA, a stochastic algorithm for link structure analysis. SALSA combines some features of both PageRank and HITS to improve the authority and hub computation. It casts the problem as two Markov chains, an authority 7.5 Community Discovery 261 Markov chain and a hub Markov chain. SALSA is less susceptible to spam since the coupling between hub and authority scores is much less strict. Bharat and Henzinger [52] proposed a simple method to fight two site nepotistic links. That means that a set of pages on one host points to a single page on a second host. This drives up the hub scores of the pages on the first host and the authority score of the page on the second host. A similar thing can be done for hubs. These links may be authored by the same person and thus are regarded as “nepotistic” links to drive up the ranking of the target pages. [52] suggests weighting the links to deal with this problem. That is, if there are k edges from documents on a first host to a single document on a second host we give each edge an authority weight of 1/k. If there are l edges from a single page on a first host to a set of pages on a second host, we give each edge a hub weight of 1/l. These weights are used in the authority and hub computation. There are much more sophisticated spam techniques now involving more than two sites. Regarding the topic drifting of HITS, existing fixes are mainly based on content similarity comparison during the expansion of the root set. In [88], if an expanded page is too different from the pages in the root set in terms of content similarity (based on cosine similarity), it is discarded. The remaining links are also weighted according to similarity. [88] proposes a method that uses the similarity between the anchor text of a link and the search topic to weight the link (instead of giving each link 1 as in HITS). [84] goes further to segment the page based on the DOM (Document Object Model) tree structure to identify the blocks or subtrees that are more related to the query topic instead of regarding the whole page as relevant to the search query. This is a good way to deal with multi-topic pages, which are abundant on the Web. A recent work on this is block-based link analysis [78], which segments each Web page into different blocks. Each block is given a different importance value according to its location in the page and other information. The importance value is then used to weight the links in the HITS (and also PageRank) computation. This will reduce the impact of unimportant links, which usually cause topic drifting and may even be a link spam. 7.5 Community Discovery Intuitively, a community is simply a group of entities (e.g., people or organizations) that shares a common interest or is involved in an activity or event. In Sect. 7.4.2, we showed that the HITS algorithm can be used to find communities. The communities are represented by dense bipartite subgraphs. We now describe several other community finding algorithms. 262 7 Link Analysis Apart from the Web, communities also exist in emails and text documents. This section describes two community finding algorithms for the Web, one community finding algorithm for emails, and one community finding algorithm for text documents. There are many reasons for discovering communities. For example, in the context of the Web, Kumar et al. [293] listed three reasons: 1. Communities provide valuable and possibly the most reliable, timely, and up-to-date information resources for a user interested in them. 2. They represent the sociology of the Web: studying them gives insights into the evolution of the Web. 3. They enable target advertising at a very precise level. 7.5.1 Problem Definition Definition (community): Given a finite set of entities S = {s1, s2, …, sn} of the same type, a community is a pair C = (T, G), where T is the community theme and G S is the set of all entities in S that shares the theme T. If si G, si is said to be a member of the community C. Some remarks about this definition are in order: A theme defines a community. That is, given a theme T, the set of members of the community is uniquely determined. Thus, two communities are equal if they have the same theme. A theme can be defined arbitrarily. For example, it can be an event (e.g., a sport event or a scandal) or a concept (e.g., Web mining). An entity si in S can be in any number of communities. That is, communities may overlap, or multiple communities may share members. The entities in S are of the same type. For example, this definition does not allow people and organizations to be in the same community. By no means does this definition cover every aspect of communities in the real world. For example, it does not consider the temporal dimension of communities. Usually a community exists within a specific period of time. Similarly, an entity may belong to a community during some time periods. This is a conceptual definition. In practice, different community mining algorithms have their own operational definitions which usually depend on how communities manifest themselves in the given data (which we will discuss shortly). Furthermore, the algorithms may not be able to discover all the members of a community or its precise theme. Communities may also have hierarchical structures. 7.5 Community Discovery 263 Definition (sub-community, super-community, and sub-theme): A community (T, G) may have a set of sub-communities {(T1, G1), …, (Tm, Gm)}, where Ti is a sub-theme of T and Gi G. (T, G) is also called a super-community of (Ti, Gi). In the same way, each sub-community (Ti, Gi) can be further decomposed, which gives us a community hierarchy. Community Manifestation in Data: Given a data set, which can be a set of Web pages, a collection of emails, or a set of text documents, we want to find communities of entities in the data. However, the data itself usually does not explicitly give us the themes or the entities (community members) associated with the themes. The system needs to discover the hidden community structures. Thus, the first issue that we need to know is how communities manifest themselves. From such manifested evidences, the system can discover possible communities. Different types of data may have different forms of manifestation. We give three examples. Web Pages: 1. Hyperlinks: A group of content creators sharing a common interest is usually inter-connected through hyperlinks. That is, members in a community are more likely to be connected among themselves than outside the community. 2. Content words: Web pages of a community usually contain words that are related to the community theme. Emails: 1. Email exchange between entities: Members of a community are more likely to communicate with one another. 2. Content words: Email contents of a community also contain words related to the theme of the community. Text documents: 1. Co-occurrence of entities: Members of a community are more likely to appear together in the same sentence and/or the same document. 2. Content words: Words in sentences indicate the community theme. Clearly, the key form of manifestation of a community is that its members are linked in some way. The associated text often contains words that are indicative of the community theme. Objective of Community Discovery: Given a data set containing entities, we want to discover hidden communities of the entities. For each community, we want to find the theme and its members. The theme is usually represented with a set of keywords. 264 7.5.2 7 Link Analysis Bipartite Core Communities HITS finds dense bipartite graph communities based on broad topic queries. The question is whether it is possible to find all such communities efficiently from the crawl of the whole Web without using eigenvector computation which is relatively inefficient. Kumar et al. [293] presented a technique for finding bipartite cores, which are defined as follows. Recall that the node set of a bipartite graph can be partitioned into two subsets, which we denote as set F and set C. A bipartite core is a complete bipartite sub-graph with at least i nodes in F and at least j nodes in C. A complete bipartite graph on node sets F and C contains all possible edges between the vertices of F and the vertices of C. Note that edges within F or within C are allowed here to suit the Web context, which deviate from the traditional definition of a complete bipartite graph. Intuitively, the core is a small (i, j)-sized complete bipartite sub-graph of the community, which contains some core members of the community but not all. The cores that we seek are directed, i.e., there is a set of i pages all of which link to a set of j pages, while no assumption is made of links out of the latter set of j pages. Intuitively, the former is the set of pages created by members of the community, pointing to what they believe are the most valuable pages for that community. For this reason we will refer to the i pages that contain the links as fans, and the j pages that are referenced as centers (as in community centers). Fans are like specialized hubs, and centers are like authorities. Figure 7.11 shows an example of a bipartite core. 4 Fans 3 Centers Fig. 7.11. A (4, 3) bipartite core In Fig. 7.11, each fan page links to every center page. Since there are four fans and three centers, this is called a (4, 3) bipartite core. Such a core almost certainly represents a Web community, but a community may have multiple bipartite cores. Given a large number of pages crawled from the Web, which is represented as a graph, the procedure for finding bipartite cores consists of two major steps: pruning and core generation. 7.5 Community Discovery 265 Step 1: Pruning We describe two types of pruning to remove those unqualified pages to be fans or centers. There are also other pruning methods given in [293]. 1. Pruning by in-degree: we can delete all pages that are very highly referenced (linked) on the Web, such as homepages of Web portals (e.g., Yahoo!, AOL, etc). These pages are referenced for a variety of reasons, having little to do with any single emerging community, and they can be safely deleted. That is, we delete pages with the number of in-links great than k, which is determined empirically (k = 50 in [293]). 2. Iterative pruning of fans and centers: If we are interested in finding (i, j) cores, clearly any potential fan with an out-degree smaller than j can be pruned and the associated edges deleted from the graph. Similarly, any potential center with an in-degree smaller than i can be pruned and the corresponding edges deleted from the graph. This process can be done iteratively: when a fan gets pruned, some of the centers that it points to may have their in-degrees fall below the threshold i and qualify for pruning as a result. Similarly, when a center gets pruned, a fan that points to it could have its out-degree fall below its threshold of j and qualify for pruning. Step 2: Generating all (i, j) Cores After pruning, the remaining pages are used to discover cores. The method works as follows: Fixing j, we start with all (1, j) cores. This is simply the set of all vertices with out-degree at least j. We then construct all (2, j) cores by checking every fan which also points to any center in a (1, j) core. All (3, j) cores can be found in the same fashion by checking every fan which points to any center in a (2, j) core, and so on. The idea is similar to the Apriori algorithm for association rule mining (see Chap. 2) as every proper subset of the fans in any (i, j) core forms a core of smaller size. Based on the algorithm, Kumar et al. found a large number of topic coherent cores from a crawl of the Web [293]. We note that this algorithm only finds the core pages of the communities, not all members (pages). It also does not find the themes of the communities or their hierarchical organizations. 7.5.3 Maximum Flow Communities Bipartite cores are usually very small and do not represent full communities. In this section, we define and find maximum flow communities based on the work of Flake et al. [180]. The algorithm requires the user to give a 266 7 Link Analysis set of seed pages, which are examples of the community that the user wishes to find. Given a Web link graph G = (V, E), a maximum flow community is defined as a collection C V of Web pages such that each member page u C has more hyperlinks (in either direction) within the community C than outside of the community V-C. Identifying such a community is intractable in the general case because it can be mapped into a family of NP-complete graph partition problems. Thus, we need to approximate and recast it into a framework with less stringent conditions based on the network flow model from operations research, specifically the maximum flow model. The maximum flow model can be stated as follows: We are given a graph G = (V, E), where each edge (u, v) is thought of as having a positive capacity c(u, v) that limits the quantity of a product that may be shipped through the edge. In such a situation, it is often desirable to have the maximum amount of flow from a starting point s (called the source) and a terminal point t (called the sink). Intuitively, the maximum flow of the graph is determined by the bottleneck edges. For example, given the graph in Fig. 7.12 with the source s and the sink t, if every edge has the unit capacity, the bottleneck edges are W-X and Y-Z. s Y Z t W X Fig. 7.12. A simple flow network. The Max Flow-Min Cut theorem of Ford and Fulkerson [181] proves that the maximum flow of a network is identical to the minimum cut that separates s and t. Many polynomial time algorithms exist for solving the s-t maximum flow problem. If Fig. 7.12 is a Web link graph, it is natural to cut the edges W-X and Y-Z to produce two Web communities. The basic idea of the approach in [180] is as follows: It starts with a set S of seed pages, which are example pages of the community that the user wishes to find. The system then crawls the Web to find more pages using the seed pages. A maximum flow algorithm is then applied to separate the community C involving the seed pages and the other pages. These steps may need to be repeated in order to find the desired community. Figure 7.13 gives the algorithm. 7.5 Community Discovery 267 Algorithm Find-Community (S) while number of iteration is less than desired do build G = (V, E) by doing a fixed depth crawl starting from S; k = |S|; C = Max-Flow-Community(G, S, k); rank all v C by the number of edges in C; add the highest ranked non-seed vertices to S end-while return all v V still connected to the source s Procedure Max-Flow-Community(G, S, k) create artificial vertices, s and t and add to V; for all v S do add (s, v) to E with c(s, v) = endfor for all (u, v) E, u s do c(u, v) = k; if (v, u) E then add (v, u) to E with c(v, u) = k endif endfor for all v V, v S {s, t} do add (v, t) to E with c(v, t) = 1 endfor Max-Flow(G, s, t); return all v V still connected to s. // V is the vertex set of G. // E is the edge set of G. Fig. 7.13. The algorithm for mining maximum flow communities The algorithm Find-Community is the control program. It takes a set S of seed Web pages as input, and crawls to a fixed depth including in-links as well as out-links (with in-links found by querying a search engine). It then applies the procedure Max-Flow-Community to the induced graph G from the crawl. After a community C is found, it ranks the pages in the community by the number of edges that each has inside of the community. Some highest ranked non-seed pages are added to the seed set. This is to create a big seed set for the next iteration in order to crawl more pages. The algorithm then iterates the procedure. Note that the first iteration may only identify a very small community. However, when new seeds are added, increasingly larger communities are identified. Heuristics are used to decide when to stop. The procedure Max-Flow-Community finds the actual community from G. Since a Web graph has no source and sink, it first augments the web 268 7 Link Analysis graph by adding an artificial source, s, with infinite capacity edges routed to all seed vertices in S; making each pre-existing edge bidirectional and assigning each edge a constant capacity k. It then adds an artificial sink t and routes all vertices except the source, the sink, and the seed vertices to t with unit capacity. After augmenting the web graph, a residual flow graph is produced by a maximum flow procedure (Max-Flow()). All vertices accessible from s through non-zero positive edges form the desired result. The value k is heuristically chosen to be the size of the set S to ensure that after the artificial source and sink are added to the original graph, the same cuts will be produced as the original graph (see the proof in [179]). Figure 7.14 shows the community finding process. Finally, we note that this algorithm does not find the theme of the community or the community hierarchy (i.e., sub-communities and so on). Artificial sink Artificial source Outside of the community Community S Cut set Fig. 7.14. Schematic representation of the community finding process 7.5.4 Email Communities Based on Betweenness Email has become the predominant means of communication in the information age. It has been established as an indicator of collaboration and knowledge (or information) exchange. Email exchanges provide plenty of data on personal communication for the discovery of shared interests and relationships between people, which were hard to discover previously. It is fairly straightforward to construct a graph based on email data. People are the vertices and the edges are added between people who corresponded through email. Usually, the edge between two people is added if a minimum number of messages passed between them. The minimum number is controlled by a threshold, which can be tuned. To analyze an email graph or network, one can make use of all the centrality measures and prestige measures discussed in Sect. 7.1. We now focus on community finding only. 7.5 Community Discovery 269 We are interested in people communities, which are subsets of vertices that are related. One way to identify communities is by partitioning the graph into discrete clusters such that there are few edges lying between the clusters. This definition is similar to that of the maximum flow community. Betweenness in social networks is a natural measure for identifying those edges in between clusters or communities [523]. The idea is that inter-community links, which are few, have high betweenness values, while the intra-community edges have low betweenness values. However, the betweenness discussed in Sect. 7.1 is evaluated on each person in the network. Here, we need to evaluate the betweenness of each edge. The idea is basically the same and Equation (4) can be used here without normalization because we only find communities in a single graph. The betweenness of an edge is simply the number of shortest paths that pass it. If the graph is not connected, we identify communities from each connected component. Given a connected graph, the method works iteratively in two steps (Fig. 7.15): repeat Compute the betweenness of each edge in the remaining graph; Remove the edge with the highest betweenness until the graph is suitably partitioned. Fig. 7.15. Community finding using the betweenness measure. Since the removal of an edge can strongly affect the betweenness of many other edges, we need to repeatedly re-compute the betweenness of all edges. The idea of the method is very similar to the minimum-cut method discussed in Sect. 7.5.3. The stopping criteria can be designed according to applications. In general, we consider that the smallest community is a triangle. The algorithm should stop producing more unconnected components if there is no way to generate triangle communities. A component of five or fewer vertices cannot consist of two viable communities. The smallest such component is six, which has two triangles connected by one edge, see Fig. 7.16. If any discovered community does not have a triangle, it may not be considered as a community. Clearly, other stopping criteria can be used. Fig. 7.16. The smallest possible graph of two viable communities. 270 7.5.5 7 Link Analysis Overlapping Communities of Named Entities Most community discovery algorithms are based on graph partitioning, which means that an entity can belong to only a single community. However, in real life, a person can be in multiple communities (see the definition in Sect. 7.5.1). For example, he/she can be in the community of his/her family, the community of his/her colleagues and the community of his/her friends. A heuristic technique is presented in [325] for finding overlapping communities of entities in text documents. In the Web or email context, there are explicit links connecting entities and forming communities. In free text documents, no explicit links exist. Then the question is: what constitutes a link between two entities in text documents? As we indicated earlier, one simple technique is to regard two entities as being linked if they co-occur in the same sentence. This method is reasonable because if two people are mentioned in a sentence there is usually a relationship between them. The objective is to find entity communities from a text corpus, which could be a set of given documents or the returned pages from a search engine using a given entity as the search query. An entity here refers to the name of a person or an organization. The algorithm in [325] consists of four steps: 1. Building a link graph: The algorithm first parses each document. For each sentence, it identifies named entities contained in the sentence. If a sentence has more than one named entities, these entities are pair-wise linked. The keywords in the sentence are attached to the linked pairs to form their textual contents. All the other sentences are discarded. 2. Finding all triangles: The algorithm then finds all triangles, which are the basic building blocks of communities. A triangle consists of three entities bound together. The reason for using triangles is that it has been observed by researchers that a community expands predominantly by triangles sharing a common edge. 3. Finding community cores: It next finds community cores. A community core is a group of tightly bound triangles, which are relaxed complete sub-graphs (or cliques). Intuitively, a core consists of a set of tightly connected members of a community. 4. Clustering around community cores: For those triangles and also entity pairs that are not in any core, they are assigned to cores according to their textual content similarities with the discovered cores. It is clear that in this algorithm a single entity can appear in multiple communities because an entity can appear in multiple triangles. To finish off, the algorithm also ranks the entities in each community according to de- Bibliographic Notes 271 gree centrality. Keywords associated with the edges of each community are also ranked. The top keywords are assumed to represent the theme of the community. The technique has been applied to find communities of political figures and celebrities from Web documents with promising results. Bibliographic Notes Social network analysis has a relative long history. A large number of interesting problems and algorithms were studied in the past 60 years. The book by Wasserman and Faust [540] is an authoritative text of the field. Co-citation [494] and bibliographic coupling [275] are from bibliometrics, which is a type of research method used in library and information science. The book edited by Borgman [58] is a good source of information on both the research and applications of bibliometrics. The use of social network analysis in the Web context (also called link analysis) started with the PageRank algorithm proposed by Brin and Page [68] and Page et al. [422], and the HITS algorithm proposed by Kleinberg [281]. PageRank is also the algorithm that powers the Google search engine. Due to several weaknesses of HITS, many researchers have tried to improve it. Various enhancements were reported by Lempel and Moran [310], Bharat and Henzinger [52], Chakrabarti et al. [88], Cai et al. [78], etc. The book by Langville and Meyer [304] contains in-depth analyses of PageRank, HITS and many enhancements to HITS. Other works related to Web link analysis include those in [98, 226, 266, 368] on improving the PageRank computation, in [168] on searching workspace Web, in [103, 182, 183, 416] on the evolution of the Web and the search engine influence on the Web, in [140, 142, 410, 516] on other link based models, in [34, 440, 370, 371] on Web graph and its characteristics, in [37, 51, 235] on sampling of Web pages, and in [32, 425, 585] on the temporal dimension of Web search. On community discovery, HITS can find some communities by computing non-principal eigenvectors [198, 281]. Kumar et al. [293] proposed the algorithm for finding bipartite cores. Flake et al. [179] introduced the maximum flow community mining. Ino et al. [249] presented a more strict definition of communities. Tyler et al. [523] gave the method for finding email communities based on betweenness. The algorithm for finding overlapping communities of named entities from texts was given by Li et al. [325]. More recent developments on communities and social networks on the Web can be found in [16, 21, 137, 158, 200, 518, 519, 561, 618]. 8 Web Crawling By Filippo Menczer Web crawlers, also known as spiders or robots, are programs that automatically download Web pages. Since information on the Web is scattered among billions of pages served by millions of servers around the globe, users who browse the Web can follow hyperlinks to access information, virtually moving from one page to the next. A crawler can visit many sites to collect information that can be analyzed and mined in a central location, either online (as it is downloaded) or off-line (after it is stored). Were the Web a static collection of pages, we would have little long term use for crawling. Once all the pages are fetched and saved in a repository, we are done. However, the Web is a dynamic entity evolving at rapid rates. Hence there is a continuous need for crawlers to help applications stay current as pages and links are added, deleted, moved or modified. There are many applications for Web crawlers. One is business intelligence, whereby organizations collect information about their competitors and potential collaborators. Another use is to monitor Web sites and pages of interest, so that a user or community can be notified when new information appears in certain places. There are also malicious applications of crawlers, for example, that harvest email addresses to be used by spammers or collect personal information to be used in phishing and other identity theft attacks. The most widespread use of crawlers is, however, in support of search engines. In fact, crawlers are the main consumers of Internet bandwidth. They collect pages for search engines to build their indexes. Well known search engines such as Google, Yahoo! and MSN run very efficient universal crawlers designed to gather all pages irrespective of their content. Other crawlers, sometimes called preferential crawlers, are more targeted. They attempt to download only pages of certain types or topics. This chapter introduces the main concepts, algorithms and data structures behind Web crawlers. After discussing the implementation issues that all crawlers have to address, we describe different types of crawlers: universal, focused, and topical. We also discuss some of the ethical issues around crawlers. Finally, we peek at possible future uses of crawlers in support of alternative models where crawling and searching activities are distributed among a large community of users connected by a dynamic and adaptive peer network. 274 8 Web Crawling 8.1 A Basic Crawler Algorithm In its simplest form, a crawler starts from a set of seed pages (URLs) and then uses the links within them to fetch other pages. The links in these pages are, in turn, extracted and the corresponding pages are visited. The process repeats until a sufficient number of pages are visited or some other objective is achieved. This simple description hides many delicate issues related to network connections, spider traps, URL canonicalization, page parsing, and crawling ethics. In fact, Google founders Sergey Brin and Lawrence Page, in their seminal paper, identified the Web crawler as the most sophisticated yet fragile component of a search engine [68]. Figure 8.1 shows the flow of a basic sequential crawler. Such a crawler fetches one page at a time, making inefficient use of its resources. Later in the chapter we discuss how efficiency can be improved by the use of multiple processes, threads, and asynchronous access to resources. The crawler maintains a list of unvisited URLs called the frontier. The list is initialized with seed URLs which may be provided by the user or another program. In each iteration of its main loop, the crawler picks the next URL from the frontier, fetches the page corresponding to the URL through HTTP, parses the retrieved page to extract its URLs, adds newly discovered URLs to the frontier, and stores the page (or other extracted information, possibly index terms) in a local disk repository. The crawling process may be terminated when a certain number of pages have been crawled. The crawler may also be forced to stop if the frontier becomes empty, although this rarely happens in practice due to the high average number of links (on the order of ten out-links per page across the Web). A crawler is, in essence, a graph search algorithm. The Web can be seen as a large graph with pages as its nodes and hyperlinks as its edges. A crawler starts from a few of the nodes (seeds) and then follows the edges to reach other nodes. The process of fetching a page and extracting the links within it is analogous to expanding a node in graph search. The frontier is the main data structure, which contains the URLs of unvisited pages. Typical crawlers attempt to store the frontier in the main memory for efficiency. Based on the declining price of memory and the spread of 64-bit processors, quite a large frontier size is feasible. Yet the crawler designer must decide which URLs have low priority and thus get discarded when the frontier is filled up. Note that given some maximum size, the frontier will fill up quickly due to the high fan-out of pages. Even more importantly, the crawler algorithm must specify the order in which new URLs are extracted from the frontier to be visited. These mechanisms determine the graph search algorithm implemented by the crawler. 8.1 A Basic Crawler Algorithm 275 Fig. 8.1. Flow chart of a basic sequential crawler. The main data operations are shown on the left, with dashed arrows. 8.1.1 Breadth-First Crawlers The frontier may be implemented as a first-in-first-out (FIFO) queue, corresponding to a breadth-first crawler. The URL to crawl next comes from the head of the queue and new URLs are added to the tail of the queue. Once the frontier reaches its maximum size, the breadth-first crawler can add to the queue only one unvisited URL from each new page crawled. 276 8 Web Crawling The breadth-first strategy does not imply that pages are visited in “random” order. To understand why, we have to consider the highly skewed, long-tailed distribution of indegree in the Web graph. Some pages have a number of links pointing to them that are orders of magnitude larger than the mean. Indeed, the mean indegree is not statistically significant when the indegree k is distributed according to a power law Pr(k) k with exponent < 3 [437]. For the Web graph, this is the case, with 2.1 [69]. This means that the fluctuations of indegree are unbounded, i.e., the standard deviation is bounded only by the finite size of the graph. Intuitively, popular pages have so many incoming links that they act like attractors for breadth-first crawlers. It is therefore not surprising that the order in which pages are visited by a breadth-first crawler is highly correlated with their PageRank or indegree values. An important implication of this phenomenon is an intrinsic bias of search engines to index well connected pages. Another reason that breadth-first crawlers are not “random” is that they are greatly affected by the choice of seed pages. Topical locality measures indicate that pages in the link neighborhood of a seed page are much more likely to be related to the seed pages than randomly selected pages. These and other types of bias are important to universal crawlers (Sect. 8.3). As mentioned earlier, only unvisited URLs are to be added to the frontier. This requires some data structure to be maintained with visited URLs. The crawl history is a time-stamped list of URLs fetched by the crawler tracking its path through the Web. A URL is entered into the history only after the corresponding page is fetched. This history may be used for postcrawl analysis and evaluation. For example, we want to see if the most relevant or important resources are found early in the crawl process. While history may be stored on disk, it is also maintained as an in-memory data structure for fast look-up, to check whether a page has been crawled or not. This check is required to avoid revisiting pages or wasting space in the limited-size frontier. Typically a hash table is appropriate to obtain quick URL insertion and look-up times (O(1)). The look-up process assumes that one can identify two URLs effectively pointing to the same page. This introduces the need for canonical URLs (see Sect. 8.2). Another important detail is the need to prevent duplicate URLs from being added to the frontier. A separate hash table can be maintained to store the frontier URLs for fast look-up to check whether a URL is already in it. 8.1.2 Preferential Crawlers A different crawling strategy is obtained if the frontier is implemented as a priority queue rather than a FIFO queue. Typically, preferential crawl- 8.2 Implementation Issues 277 ers assign each unvisited link a priority based on an estimate of the value of the linked page. The estimate can be based on topological properties (e.g., the indegree of the target page), content properties (e.g., the similarity between a user query and the source page), or any other combination of measurable features. For example, the goal of a topical crawler is to follow edges that are expected to lead to portions of the Web graph that are relevant to a user-selected topic. The choice of seeds is even more important in this case than for breadth-first crawlers. We will discuss various preferential crawling algorithms in Sects. 8.4 and 8.5. For now let us simply assume that some function exists to assign a priority value or score to each unvisited URL. If pages are visited in the order specified by the priority values in the frontier, then we have a best-first crawler. The priority queue may be a dynamic array that is always kept sorted by URL scores. At each step, the best URL is picked from the head of the queue. Once the corresponding page is fetched, the URLs extracted from it must, in turn, be scored. They are then added to the frontier in such a manner that the sorting order of the priority queue is maintained. As for breadth-first, best-first crawlers also need to avoid duplicate URLs in the frontier. Keeping a separate hash table for look-up is an efficient way to achieve this. The time complexity of inserting a URL into the priority queue is O(logF), where F is the frontier size (looking up the hash requires constant time). To dequeue a URL, it must first be removed from the priority queue (O(logF)) and then from the hash table (again O(1)). Thus the parallel use of the two data structures yields a logarithmic total cost per URL. Once the frontier’s maximum size is reached, only the best URLs are kept; the frontier must be pruned after each new set of links is added. 8.2 Implementation Issues 8.2.1 Fetching To fetch pages, a crawler acts as a Web client; it sends an HTTP request to the server hosting the page and reads the response. The client needs to timeout connections to prevent spending unnecessary time waiting for responses from slow servers or reading huge pages. In fact, it is typical to restrict downloads to only the first 10-100 KB of data for each page. The client parses the response headers for status codes and redirections. Redirect loops are to be detected and broken by storing URLs from a redirection chain in a hash table and halting if the same URL is encountered twice. One may also parse and store the last-modified header to determine the age of the document, although this information is known to be unreliable. Er- 278 8 Web Crawling ror-checking and exception handling is important during the page fetching process since the same code must deal with potentially millions of remote servers. In addition, it may be beneficial to collect statistics on timeouts and status codes to identify problems or automatically adjust timeout values. Programming languages such as Java, Python and Perl provide simple programmatic interfaces for fetching pages from the Web. However, one must be careful in using high-level interfaces where it may be harder to detect lower-level problems. For example, a robust crawler in Perl should use the Socket module to send HTTP requests rather than the higher-level LWP library (the World-Wide Web library for Perl). The latter does not allow fine control of connection timeouts. 8.2.2 Parsing Once (or while) a page is downloaded, the crawler parses its content, i.e., the HTTP payload, and extracts information both to support the crawler’s master application (e.g., indexing the page if the crawler supports a search engine) and to allow the crawler to keep running (extracting links to be added to the frontier). Parsing may imply simple URL extraction from hyperlinks, or more involved analysis of the HTML code. The Document Object Model (DOM) establishes the structure of an HTML page as a tag tree, as illustrated in Fig. 8.2. HTML parsers build the tree in a depth-first manner, as the HTML source code of a page is scanned linearly. Unlike program code, which must compile correctly or else will fail with a syntax error, correctness of HTML code tends to be laxly enforced by browsers. Even when HTML standards call for strict interpretation, de facto standards imposed by browser implementations are very forgiving. This, together with the huge population of non-expert authors generating Web pages, imposes significant complexity on a crawler's HTML parser. Many pages are published with missing required tags, tags improperly nested, missing close tags, misspelled or missing attribute names and values, missing quotes around attribute values, unescaped special characters, and so on. As an example, the double quotes character in HTML is reserved for tag syntax and thus is forbidden in text. The special HTML entity &quot; is to be used in its place. However, only a small number of authors are aware of this, and a large fraction of Web pages contains this illegal character. Just like browsers, crawlers must be forgiving in these cases; they cannot afford to discard many important pages as a strict parser would do. A wise preprocessing step taken by robust crawlers is to apply a tool such as tidy (www.w3.org/People/Raggett/tidy) to clean up the HTML content prior to parsing. To add to the complexity, there are many coexist- 8.2 Implementation Issues 279 ing HTML and XHTML reference versions. However, if the crawler only needs to extract links within a page and/or the text in the page, simpler parsers may suffice. The HTML parsers available in high-level languages such as Java and Perl are becoming increasingly sophisticated and robust. Fig. 8.2. Illustration of the DOM (or tag) tree built from a simple HTML page. Internal nodes (shown as ovals) represent HTML tags, with the <html> tag as the root. Leaf nodes (shown as rectangles) correspond to text chunks. A growing portion of Web pages are written in formats other than HTML. Crawlers supporting large-scale search engines routinely parse and index documents in many open and proprietary formats such as plain text, PDF, Microsoft Word and Microsoft PowerPoint. Depending on the appli- 280 8 Web Crawling cation of the crawler, this may or may not be required. Some formats present particular difficulties as they are written exclusively for human interaction and thus are especially unfriendly to crawlers. For instance, some commercial sites use graphic animations in Flash; these are difficult for a crawler to parse in order to extract links and their textual content. Other examples include image maps and pages making heavy use of Javascript for interaction. New challenges are going to come as new standards such as Scalable Vector Graphics (SVG), Asynchronous Javascript and XML (AJAX), and other XML-based languages gain popularity. 8.2.3 Stopword Removal and Stemming When parsing a Web page to extract the content or to score new URLs suggested by the page, it is often helpful to remove so-called stopwords, i.e., terms such as articles and conjunctions, which are so common that they hinder the discrimination of pages on the basis of content. Another useful technique is stemming, by which morphological variants of terms are conflated into common roots (stems). In a topical crawler where a link is scored based on the similarity between its source page and the query, stemming both the page and the query helps improve the matches between the two sets and the accuracy of the scoring function. Both stop-word removal and stemming are standard techniques in information retrieval, and are discussed in greater detail in Chap. 6. 8.2.4 Link Extraction and Canonicalization HTML parsers provide the functionality to identify tags and associated attribute-value pairs in a given Web page. In order to extract hyperlink URLs from a page, we can use a parser to find anchor (<a>) tags and grab the values of the associated href attributes. However, the URLs thus obtained need to be further processed. First, filtering may be necessary to exclude certain file types that are not to be crawled. This can be achieved with white lists (e.g., only follow links to text/html content pages) or black lists (e.g., discard links to PDF files). The identification of a file type may rely on file extensions. However, they are often unreliable and sometimes missing altogether. We cannot afford to download a document and then decide whether we want it or not. A compromise is to send an HTTP HEAD request and inspect the content-type response header, which is usually a more reliable label. Another type of filtering has to do with the static or dynamic nature of pages. A dynamic page (e.g., generated by a CGI script) may indicate a 8.2 Implementation Issues 281 query interface for a database or some other application in which a crawler may not be interested. In the early days of the Web, such pages were few and easily recognizable, e.g., by matching URLs against the /cgi-bin/ directory name for CGI scripts, or against the special characters [?=&] used in CGI query strings. However, the use of dynamic content has become much more common; it is used in a variety of sites for content that is perfectly indexable. Most importantly, its dynamic nature is very difficult to recognize via URL inspection. For these reasons, most crawlers no longer make such distinction between static and dynamic content. While a crawler normally would not create query URLs autonomously (unless it is designed to probe the so-called deep or hidden Web, which contain databases with query interfaces), it will happily crawl URLs hard-coded in HTML source of parsed pages. In other words, if a URL is found in a Web page, it is fair game. There is one important exception to this strategy, the spider trap, which is discussed below. Before links can be added to the frontier, relative URLs must be converted to absolute URLs. For example, the relative URL news/today.html in the page http://www.somehost.com/index.html is to be transformed into the absolute form http://www.somehost.com/news/today.html. There are various rules to convert relative URLs into absolute ones. A relative URL can be expressed as a relative or absolute path relative to the Web server’s document root directory. The base URL may be specified by an HTTP header or a meta-tag within an HTML page, or not at all–in the latter case the directory of the hyperlink’s source page is used as a base URL. Converting relative URLs is just one of many steps that make up the canonicalization process, i.e., the conversion of a URL into a canonical form. The definition of canonical form is somewhat arbitrary, so that different crawlers may specify different rules. For example, one crawler may always specify the port number within the URL (e.g., http://www.somehost.com:80/), while another may specify the port number only when it is not 80 (the default HTTP port). As long as the canonical form is applied consistently by a crawler, such distinctions are inconsequential. Some programming languages such as Perl provide modules to manage URLs, including methods for absolute/relative conversion and canonicalization. However, several canonicalization steps require the application of heuristic rules, and off-the-shelf tools typically do not provide such functionalities. A crawler may also need to use heuristics to detect when two URLs point to the same page in order to minimize the likelihood that the same page is fetched multiple times. Table 8.1 lists the steps typically employed to canonicalize a URL. 282 8 Web Crawling Table 8.1. Some transformations to convert URLs to canonical forms. Stars indicate heuristic rules, where there is a tradeoff between the risk of altering the semantics of the URL (if a wrong guess is made) and the risk of missing duplicate URLs (if no transformation is applied) for the same target Description and transformation Example and canonical form Default port number Remove Root directory Add trailing slash Guessed directory* Add trailing slash Fragment Remove Current or parent directory Resolve path Default filename* Remove Needlessly encoded characters Decode Disallowed characters Encode Mixed/upper-case host names Lower-case http://cs.indiana.edu:80/ http://cs.indiana.edu/ http://cs.indiana.edu http://cs.indiana.edu/ http://cs.indiana.edu/People http://cs.indiana.edu/People/ http://cs.indiana.edu/faq.html#3 http://cs.indiana.edu/faq.html http://cs.indiana.edu/a/./../b/ http://cs.indiana.edu/b/ http://cs.indiana.edu/index.html http://cs.indiana.edu/ http://cs.indiana.edu/%7Efil/ http://cs.indiana.edu/~fil/ http://cs.indiana.edu/My File.htm http://cs.indiana.edu/My%20File.htm http://CS.INDIANA.EDU/People/ http://cs.indiana.edu/People/ 8.2.5 Spider Traps A crawler must be aware of spider traps. These are Web sites where the URLs of dynamically created links are modified based on the sequence of actions taken by the browsing user (or crawler). Some e-commerce sites such as Amazon.com may use URLs to encode which sequence of products each user views. This way, each time a user clicks a link, the server can log detailed information on the user's shopping behavior for later analysis. As an illustration, consider a dynamic page for product x, whose URL path is /x and that contains a link to product y. The URL path for this link would be /x/y to indicate that the user is going from page x to page y. Now suppose the page for y has a link back to product x. The dynamically created URL path for this link would be /x/y/x, so that the crawler would think this is a new page when in fact it is an already visited page with a new URL. As a side effect of a spider trap, the server may create an entry in a database every time the user (or crawler) clicks on certain dynamic links. An example might be a blog or message board where users can post comments. These situations create sites that appear infinite to a crawler, because the more links are followed, the more new URLs are created. How- 8.2 Implementation Issues 283 ever these new “dummy” links do not lead to existing or new content, but simply to dynamically created form pages, or to pages that have already been visited. Thus a crawler could go on crawling inside the spider trap forever without actually fetching any new content. In practice spider traps are not only harmful to the crawler, which wastes bandwidth and disk space to download and store duplicate or useless data. They may be equally harmful to the server sites. Not only does the server waste its bandwidth, the side effect of a crawler caught in a spider trap may also be filling a server-side database with bogus entries. The database may eventually become filled to capacity, and the site may be disabled as a result. This is a type of denial of service attack carried out unwittingly by the crawler. In some cases a spider trap needs the client to send a cookie set by the server for the dynamic URLs to be generated. So the problem is prevented if the crawler avoids accepting or sending any cookies. However, in most cases a more proactive approach is necessary to defend a crawler against spider traps. Since the dummy URLs often become larger and larger in size as the crawler becomes entangled in a spider trap, one common heuristic approach to tackle such traps is by limiting the URL sizes to some maximum number of characters, say 256. If a longer URL is encountered, the crawler should simply ignore it. Another way is by limiting the number of pages that the crawler requests from a given domain. The code associated with the frontier can make sure that every consecutive sequence of, say, 100 URLs fetched by the crawler contains at most one URL from each fully qualified host name. This approach is also germane to the issue of crawler etiquette, discussed later. 8.2.6 Page Repository Once a page is fetched, it may be stored/indexed for the master application (e.g., a search engine). In its simplest form a page repository may store the crawled pages as separate files. In this case each page must map to a unique file name. One way to do this is to map each page's URL to a compact string using some hashing function with low probability of collisions, e.g., MD5. The resulting hash value is used as a (hopefully) unique file name. The shortcoming of this approach is that a large scale crawler would incur significant time and disk space overhead from the operating system to manage a very large number of small individual files. A more efficient solution is to combine many pages into one file. A naïve approach is to simply concatenate some number of pages (say 1,000) into each file, with some special markup to separate and identify the pages 284 8 Web Crawling within the file. This requires a separate look-up table to map URLs to file names and IDs within each file. A better method is to use a database to store the pages, indexed by (canonical) URLs. Since traditional RDBMSs impose high overhead, embedded databases such as the open-source Berkeley DB are typically preferred for fast access. Many high-level languages such as Java and Perl provide simple APIs to manage Berkeley DB files, for example as tied associative arrays. This way the storage management operations become nearly transparent to the crawler code, which can treat the page repository as an in-memory data structure. 8.2.7 Concurrency A crawler consumes three main resources: network, CPU, and disk. Each is a bottleneck with limits imposed by bandwidth, CPU speed, and disk seek/transfer times. The simple sequential crawler described in Sect. 8.1 makes a very inefficient use of these resources because at any given time two of them are idle while the crawler attends to the third. The most straightforward way to speed-up a crawler is through concurrent processes or threads. Multiprocessing may be somewhat easier to implement than multithreading depending on the programming language and platform, but it may also incur a higher overhead due to the involvement of the operating system in the management (creation and destruction) of child processes. Whether threads or processes are used, a concurrent crawler may follow a standard parallel computing model [292] as illustrated in Fig. 8.3. Basically each thread or process works as an independent crawler, except for the fact that access to the shared data structures (mainly the frontier, and possibly the page repository) must be synchronized. In particular a frontier manager is responsible for locking and unlocking the frontier data structures so that only one process or thread can write to them at one time. Note that both enqueueing and dequeuing are write operations. Additionally, the frontier manager would maintain and synchronize access to other shared data structures such as the crawl history for fast look-up of visited URLs. It is a bit more complicated for a concurrent crawler to deal with an empty frontier than for a sequential crawler. An empty frontier no longer implies that the crawler has reached a dead-end, because other processes may be fetching pages and adding new URLs in the near future. The process or thread manager may deal with such a situation by sending a temporary sleep signal to processes that report an empty frontier. The process manager needs to keep track of the number of sleeping processes; when all the processes are asleep, the crawler must halt. 8.3 Universal Crawlers 285 Fig. 8.3. Architecture of a concurrent crawler The concurrent design can easily speed-up a crawler by a factor of 5 or 10. The concurrent architecture however does not scale up to the performance needs of a commercial search engine. We discuss in Sect. 8.3 further steps that can be taken to achieve more scalable crawlers. 8.3 Universal Crawlers General purpose search engines use Web crawlers to maintain their indices [25], amortizing the cost of crawling and indexing over the millions of queries received between successive index updates (though indexers are 286 8 Web Crawling designed for incremental updates [101]. These large-scale universal crawlers differ from the concurrent breadth-first crawlers described above along two major dimensions: 1. Performance: They need to scale up to fetching and processing hundreds of thousands of pages per second. This calls for several architectural improvements. 2. Policy: They strive to cover as much as possible of the most important pages on the Web, while maintaining their index as fresh as possible. These goals are, of course, conflicting so that the crawlers must be designed to achieve good tradeoffs between their objectives. Next we discuss the main issues in meeting these requirements. 8.3.1 Scalability Figure 8.4 illustrates the architecture of a large-scale crawler, based on the accounts in the literature [68, 85, 238]. The most important change from the concurrent model discussed earlier is the use of asynchronous sockets in place of threads or processes with synchronous sockets. Asynchronous sockets are non-blocking, so that a single process or thread can keep hundreds of network connections open simultaneously and make efficient use of network bandwidth. Not only does this eliminate the overhead due to managing threads or processes, it also makes locking access to shared data structures unnecessary. Instead, the sockets are polled to monitor their states. When an entire page has been fetched into memory, it is processed for link extraction and indexing. This “pull” model eliminates contention for resources and the need for locks. The frontier manager can improve the efficiency of the crawler by maintaining several parallel queues, where the URLs in each queue refer to a single server. In addition to spreading the load across many servers within any short time interval, this approach allows to keep connections with servers alive over many page requests, thus minimizing the overhead of TCP opening and closing handshakes. The crawler needs to resolve host names in URLs to IP addresses. The connections to the Domain Name System (DNS) servers for this purpose are one of the major bottlenecks of a naïve crawler, which opens a new TCP connection to the DNS server for each URL. To address this bottleneck, the crawler can take several steps. First, it can use UDP instead of TCP as the transport protocol for DNS requests. While UDP does not guarantee delivery of packets and a request can occasionally be dropped, this is rare. On the other hand, UDP incurs no connection overhead with a 8.3 Universal Crawlers 287 Fig. 8.4. High-level architecture of a scalable universal crawler significant speed-up over TCP. Second, the DNS server should employ a large, persistent, and fast (in-memory) cache. Finally, the pre-fetching of DNS requests can be carried out when links are extracted from a page. In addition to being added to the frontier, the URLs can be scanned for host names to be sent to the DNS server. This way, when a URL is later ready to be fetched, the host IP address is likely to be found in the DNS cache, obviating the need to propagate the request through the DNS tree. 288 8 Web Crawling In addition to making more efficient use of network bandwidth through asynchronous sockets, large-scale crawlers can increase network bandwidth by using multiple network connections switched to multiple routers, thus utilizing the networks of multiple Internet service providers. Similarly, disk I/O throughput can be boosted via a storage area network connected to a storage pool through a fibre channel switch. 8.3.2 Coverage vs. Freshness vs. Importance Given the size of the Web, it is not feasible even for the largest-scale crawlers employed by commercial search engines to index all of the content that could be accessed. Instead, search engines aim to focus on the most “important” pages, where importance is assessed based on various factors such as link popularity measures (indegree or PageRank) [102, 234]. At the time of this writing the three major commercial search engines report index sizes in the order of 1010 pages, while the indexable Web may be at least an order of magnitude larger. The simplest strategy to bias the crawler in favor of popular pages is to do nothing – given the long-tailed distribution of indegree discussed in Sect. 8.1, a simple breadth-first crawling algorithm will tend to fetch the pages with the highest PageRank by definition, as confirmed empirically [401]. In fact, one would have to apply a reverse bias to obtain a fair sample of the Web. Suppose that starting with a random Web walk, we wanted a random sample of pages drawn with uniform probability distribution across all pages. We can write the posterior probability of adding a page p to the sample as Pr(accept(p)|crawl(p)) Pr(crawl(p)) where the first factor is the conditional probability of accepting the page into the sample given that it was crawled, and the second factor is the prior probability of crawling the page in the random walk. We can find the acceptance strategy to obtain a uniform sample by setting the product to a constant, yielding Pr(accept(p)|crawl(p)) 1/Pr(crawl(p)). The prior Pr(crawl(p)) is given by the PageRank of p, and can be approximated during the random walk by the frequency f(p) that the crawler has encountered a link to p. So therefore, each visited page p should be accepted with probability proportional to 1/f(p). Empirical tests on a simulated Web graph validate that this strategy yields a sample of the graph that is statistically representative of the original [235]. The goal to cover as many pages as possible (among the most important ones) is in conflict with the need to maintain a fresh index. Because of the highly dynamic nature of the Web, with pages being added, deleted, and modified all the time, it is necessary for a crawler to revisit pages already 8.4 Focused Crawlers 289 in the index in order to keep the index up-to-date. Many studies have been conducted to analyze the dynamics of the Web, i.e., the statistical properties of the processes leading to change in Web structure and content 66, 101, 152, 177, 416]. They all indicate that the Web changes at very rapid rates. While early studies relied on the values reported by Web servers in the last-modified HTTP header, recently there is consensus that this information has little reliability. The most recent and exhaustive study at the time of this writing [416] reports that while new pages are created at a rate of about 8% per week, only about 62% of the content of these pages is really new because pages are often copied from existing ones. The link structure of the Web is more dynamic, with about 25% new links created per week. Once created, pages tend to change little so that most of the changes observed in the Web are due to additions and deletions rather than modifications. Finally, there is an agreement on the observation that the degree of change of a page is a better predictor of future change than the frequency of change [177, 416]. This suggests that crawler revisit strategies based on frequency of change [25, 101] may not be the most appropriate for achieving a good tradeoff between coverage and freshness. 8.4 Focused Crawlers Rather than crawling pages from the entire Web, we may want to crawl only pages in certain categories. One applications of such a preferential crawler would be to maintain a Web taxonomy such as the Yahoo! Directory (dir.yahoo.com) or the volunteer-based Open Directory Project (ODP, dmoz.org). Suppose you are the ODP editor for a certain category; you may wish to launch such a crawler from an initial seed set of pages relevant to that category, and see if any new pages discovered should be added to the directory, either directly under the category in question or one of its subcategories. A focused crawler attempts to bias the crawler towards pages in certain categories in which the user is interested. Chakrabarti et al. [87] proposed a focused crawler based on a classifier. The idea is to first build a text classifier using labeled example pages from, say, the ODP. Then the classifier would guide the crawler by preferentially selecting from the frontier those pages that appear most likely to belong to the categories of interest, according to the classifier's prediction. To train the classifier, example pages are drawn from various categories in the taxonomy as shown in Fig. 8.5. The classification algorithm used was the naïve Bayesian method (see Chap. 3). For each category c in the taxonomy we can build a Bayesian classifier to compute the probability Pr(c|p) that a crawled page p belongs to c (by definition, Pr(top|p) = 1 for the top or root 290 8 Web Crawling Fig. 8.5. Left: A taxonomy supporting a focused crawler. The areas in gray represent the categories of interest c*. A crawler with hard focus would add to the fronˆ tier the links extracted from a page classified in the leaf category c1 because its ancestor category c' is of interest to the user, while the links from a page classiˆ fied in c2 would be discarded. Right: A context graph with L = 3 layers constructed to train a context focused crawler from the target set in layer = 0. category). The user can select a set c* of categories of interest. Each crawled page is assigned a relevance score. R( p) c c* Pr(c | p ). (1) 3 Two strategies were explored. In the “soft” focused strategy, the crawler uses the score R(p) of each crawled page p as a priority value for all unvisited URLs extracted from p. The URLs are then added to the frontier, which is treated as a priority queue (see Sect. 8.1.2). In the “hard” focused strategy, for a crawled page p, the classifier first finds the leaf category ˆ c( p) in the taxonomy most likely to include p: ˆ c ( p) arg max Pr(c | p). c: c' c (2) ˆ ˆ If an ancestor of c( p ) is a focus category, i.e., c’: c( p ) c’ c’ c*, then the URLs from the crawled page p are added to the frontier. Otherwise they are discarded. The idea is illustrated in Fig. 8.5 (left). For example, imagine a crawler focused on soccer (c' = soccer c*) visits a page p in the FIFA World Cup Germany 2006 site. If the classifier correctly asˆ signs p to the leaf category c =Sports/Soccer/Competitions/World_Cup/2006, 8.4 Focused Crawlers 291 the links extracted from p are added to the frontier because 2006 is a subˆ category of Sports/Soccer ( c soccer). The soft and hard focus strategies worked equally well in experiments. Another element of the focused crawler is the use of a distiller. The distiller applies a modified version of the HITS algorithm [282] to find topical hubs. These hubs provide links to authoritative sources on a focus category. The distiller is activated at various times during the crawl and some of the top hubs are added to the frontier. Context-Focused Crawlers are another type of focused crawlers. They also use naïve Bayesian classifiers as a guide, but in this case the classifiers are trained to estimate the link distance between a crawled page and a set of relevant target pages [139]. To see why this might work, imagine looking for information on “machine learning.” One might go to the home pages of computer science departments and from there to faculty pages, which may then lead to relevant pages and papers. A department home page, however, may not contain the keywords “machine learning.” A typical focused or best-first crawler would give such a page a low priority and possibly never follow its links. However, if the crawler could estimate that pages about machine learning are only two links away from a page containing the keywords “computer science department,” then it would give the department home page a higher priority. The context-focused crawler is trained using a context graph with L layers (Fig. 8.5 right). The seed (target) pages form the layer 0 of the graph. The pages corresponding to the in-links to the seed pages are in layer 1. The in-links to the layer 1 pages make up the layer 2, and so on. The in-links to any page can be obtained by submitting a link: query to a search engine. The seed pages in layer 0 (and possibly those in layer 1) are then concatenated into a single large document, and the top few terms according to the TF-IDF weighting scheme (see Chap. 6) are selected as the vocabulary (feature space) to be used for classification. A naïve Bayesian classifier is built for each layer in the context graph. A prior probability Pr( ) = 1/L is assigned to each layer. All the pages in a layer are used to compute Pr(t| ), the probability of occurrence of a term t given the layer (class) . At the crawling time, these are used to compute Pr(p| ) for each crawled page p. The posterior probability Pr( |p) of p belonging to layer can then be computed for each layer from Bayes’ rule. The layer * with highest posterior probability wins: * ( p ) arg max Pr( | p ). (3) 292 8 Web Crawling If Pr( *|p) is less than a threshold, p is classified into the “other” class, which represents pages that do not have a good fit with any of the layers in the context graph. If Pr( *|p) exceeds the threshold, p is classified into *. The set of classifiers corresponding to the context graph provides a mechanism to estimate the link distance of a crawled page from a relevant page. If the mechanism works, the computer science department page in our example will get classified into layer 2. The crawler maintains a separate priority queue for each layer, containing the links extracted from visited pages classified in that layer. Each queue is sorted by the scores Pr( |p). The next URL to crawl is taken from the non-empty queue with the smallest . So the crawler gives precedence to links that appear to be closest to relevant targets. It is shown in [139] that the context-focused crawler outperforms the standard focused crawler in experiments. While the majority of focused crawlers in the literature have employed the naïve Bayesian method as the classification algorithm to score unvisited URLs, an extensive study with hundreds of topics has provided strong evidence that classifiers based on SVM or neural networks can yield significant improvements in the quality of the crawled pages [433]. 8.5 Topical Crawlers For many preferential crawling tasks, labeled (positive and negative) examples of pages are not available in sufficient numbers to train a focused crawler before the crawl starts. Instead, we typically have a small set of seed pages and a description of a topic of interest to a user or user community. The topic can consist of one or more example pages (possibly the seeds) or even a short query. Preferential crawlers that start with only such information are often called topical crawlers [85, 102, 377]. They do not have text classifiers to guide crawling. Even without the luxury of a text classifier, a topical crawler can be smart about preferentially exploring regions of the Web that appear relevant to the target topic by comparing features collected from visited pages with cues in the topic description. To illustrate a topical crawler with its advantages and limitations, let us consider the MySpiders applet (myspiders.informatics.indiana.edu). Figure 8.6 shows a screenshot of this application. The applet is designed to demonstrate two topical crawling algorithms, best-N-first and InfoSpiders, both discussed below [431]. MySpiders is interactive in that a user submits a query just like one would do with a search engine, and the results are then shown in a win- 8.5 Topical Crawlers 293 dow. However, unlike a search engine, this application has no index to search for results. Instead the Web is crawled in real time. As pages deemed relevant are crawled, they are displayed in a list that is kept sorted by a user-selected criterion: score or recency. The score is simply the content (cosine) similarity between a page and the query (see Chap. 6); the recency of a page is estimated by the last-modified header, if returned by the server (as noted earlier this is not a very reliable estimate). Fig. 8.6. Screenshot of the MySpiders applet in action. In this example the user has launched a population of crawlers with the query “search censorship in france” using the InfoSpiders algorithm. The crawler reports some seed pages obtained from a search engine, but also a relevant blog page (bottom left) that was not returned by the search engine. This page was found by one of the agents, called Spider2, crawling autonomously from one of the seeds. We can see that Spider2 spawned a new agent, Spider13, who started crawling for pages also containing the term “italy.” Another agent, Spider5, spawned two agents one of which, Spider11, identified and internalized the relevant term “engine.” One of the advantages of topic crawling is that all hits are fresh by definition. No stale results are returned by the crawler because the pages are visited at query time. This makes this type of crawlers suitable for applica- 294 8 Web Crawling tions that look for very recently posted documents, which a search engine may not have indexed yet. On the down side, the search is slow compared to a traditional search engine because the user has to wait while the crawler fetches and analyzes pages. If the user's client machine (where the applet runs) has limited bandwidth, e.g., a dial-up Internet connection, the wait is likely infeasible. Another disadvantage is that the ranking algorithms cannot take advantage of global prestige measures, such as PageRank, available to a traditional search engine. Several research issues around topical crawlers have received attention. One key question is how to identify the environmental signals to which crawlers should attend in order to determine the best links to follow. Rich cues such as the markup and lexical (text) signals within Web pages, as well as features of the link graph built from pages already seen, are all reasonable sources of evidence to exploit. Crawlers can use the evidence available to them in different ways, for example more or less greedily. The goals of the application also provide crucial context. For example the desired properties of the pages to be fetched (similar pages, popular pages, authoritative pages, recent pages, and so on) can lead to significant differences in crawler design and implementation. The task could be constrained by parameters like the maximum number of pages to be fetched (long crawls vs. short crawls) or the memory available. A crawling task can thus be viewed as a constrained multiobjective search problem. The wide variety of objective functions, coupled with the lack of appropriate knowledge about the search space, make such a problem challenging. In the remainder of this section we briefly discuss the theoretical conditions necessary for topical crawlers to function, and the empirical evidence supporting the existence of such conditions. Then we review some of the machine learning techniques that have been successfully applied to identify and exploit useful cues for topical crawlers. 8.5.1 Topical Locality and Cues The central assumption behind topical crawlers is that Web pages contain reliable cues about each other’s content. This is a necessary condition for designing a crawler that has a better-than-random chance to preferentially visit pages relevant with respect to a given topic. Indeed, if no estimates could be made about unvisited pages, then all we could do is a random walk through the Web graph, or an exhaustive search (using breadth-first or depth-first search algorithms). Fortunately, crawling algorithms can use cues from words and hyperlinks, associated respectively with a lexical and 8.5 Topical Crawlers 295 a link topology. In the former, two pages are close to each other if they have similar textual content; in the latter, if there is a short path between them (we will see what “short” means). Lexical metrics are text similarity measures derived from the vector space model (see Chap. 6). The cluster hypothesis behind this model is that a document lexically close to a relevant document (with respect to the given query) is also relevant with high probability [461]. Link metrics typically look at hyperlinks as directed edges in a graph, but a path can also be defined in an undirected sense, in which case two pages have a short link distance between them if they are co-cited or coreferenced, even if there is no directed path between them. Links are a very rich source of topical information about Web pages. From a crawler's perspective, there are two central questions: 1. link-content conjecture: whether two pages that link to each other are more likely to be lexically similar to each other, compared to two randomly selected pages; 2. link-cluster conjecture: whether two pages that link to each other are more likely to be semantically related to each other, compared to two randomly selected pages. A first answer to the link-content conjecture was obtained by computing the cosine similarity between linked and random pairs of pages, showing that the similarity is an order of magnitude higher in the former case [123]. The same study also showed that the anchor text tends to be a good (similar) description of the target page. The link-content conjecture can be generalized by looking at the decay in content similarity as a function of link distance from a source page. This decay was measured by launching an exhaustive breadth-first crawl from seed sets of 100 topics in the Yahoo! directory [372]. Let us use the cosine similarity measure (p1, p2) between pages p1 and p2 (see Chap. 6). We can measure the link distance 1(p1, p2) along the shortest directed path from p1 and p2, revealed by the breadth-first crawl. Both distances 1(q, p) and similarities (q, p) were averaged for each topic q over all pages p in the crawl set Pdq for each depth d: ( q, d ) ( q, d ) 1 ( q, p ) 1 q Nd Pdp ( q, p ) Pdp d i ( N iq N iq 1 ) (4) i1 1 q Nd ( q, p ) p Pdp (5) 296 8 Web Crawling where Ndq is the size of the cumulative page set Pdq = {p | 1(q, p) The crawlers were stopped at depth d = 3, yielding 3000 data points d}. {(p, d): q {1, …, 100}, d {1, 2, 3}}. These points were then used for fitting an exponential decay model: () (1 )e 1 2 (6) is the noise level in similarity, measured empirically by averagwhere ing across random pairs of pages. The parameters 1 and 2 are set by fitting the data. This was done for pages in various top-level domains, and the resulting similarity decay curves are plotted in Fig. 8.7. Fig. 8.7. Illustration of the link-content conjecture. The curves plot, for each toplevel domain, the decay in mean cosine similarity between pages as a function of their mean directed link distance, obtained by fitting data from 100 exhaustive breadth-first crawls starting from the 100 Yahoo! directory topics [372]. The curves provide us with a rough estimate of how far in link space one can make inferences about lexical content. We see that a weak signal is still present three links away from the starting pages for all but the .com domain, and even further for the .edu domain. Such heterogeneity is not surprising – academic pages are written carefully to convey information and proper pointers, while business sites often do not link to related sites because of competition. Therefore a topical crawler in the commercial domain would have a harder task, other things being equal. A solution may 8.5 Topical Crawlers 297 be to use undirected links. More specifically, if a crawler can obtain inlinks to good pages (by querying a search engine), it can use co-citation to detect hubs. If a page links to several good pages, it is probably a good hub and all its out-links should be given high priority. This strategy, related to the so-called sibling locality [3], has been used in focused crawlers [87] and in topical crawlers for business intelligence [432]. In addition to cocitation, one could look at bibliographic coupling: if several good pages link to a certain page, that target is likely to be a good authority so it and its in-links should be given high priority. Fig. 8.8 illustrates various ways in which crawlers can exploit co-citation and bibliographic coupling. Fig. 8.8. Crawling techniques exploiting co-citation (top) and bibliographic coupling (bottom). Dashed edges represent in-links, which require access to a search engine or connectivity server. Page A is a good hub, so it should be given high priority; once fetched, page B linked by it can be discovered and placed in the frontier with high priority since it is likely to be a good authority. Page C is also a good hub, so D should be given high priority. Page E is a good authority, so it should be given high priority. Its URL can also be used to discover F, which may be a good hub and should be placed in the frontier. G is also a good authority, so H should be given high priority and I should be placed in the frontier. 298 8 Web Crawling The link-cluster conjecture, also known as linkage locality [87], states that one can infer the meaning of a page by looking at its neighbors. This is actually more important than inferring lexical content, since the latter is only relevant insofar as it is correlated with the semantic content of pages. The same exhaustive crawl data used to validate the link-content conjecture can also be used to explore the link-cluster conjecture, namely the extent to which relevance is preserved within link space neighborhoods and the decay in expected relevance as one browses away from a relevant page [372]. The link-cluster conjecture can be simply formulated in terms of the conditional probability that a page p is relevant with respect to some query q, given that page r is relevant and that p is within d links from r: Rq ( d) Pr(relq ( p) | relq (r) 1 (r, p) d ] (7) where relq() is a binary relevance assessment with respect to q. In other words a page has a higher than random probability of being about a certain topic if it is in the neighborhood of other pages about that topic. Rq(d) is the posterior relevance probability given the evidence of a relevant page nearby. The conjecture is then represented by the likelihood ratio (q, d) between Rq(d) and the prior relevance probability Gq Pr(relq(p)), also known as the generality of the query. If semantic inferences are possible within a link radius d, then the following condition must hold: ( q, d ) Rq (d ) Gq (8) 1. To illustrate the meaning of the link-cluster conjecture, consider a random crawler searching for pages about a topic q. Call q(t) the probability that the crawler hits a relevant page at time t. Solving the recursion q for q(t+1) = q(t) ( t 1) q ( t )Rq (1) (1 q ( t ))Gq (9) yields the stationary hit rate * q Gq 1 Gq Rq (1) . (10) The link-cluster conjecture is a necessary and sufficient condition for such a crawler to have a better than chance hit rate: * q Gq (q,1) 1. (11) Figure 8.9 plots the mean likelihood ratio (q, d) versus the mean link distance (q, d) obtained by fitting an exponential decay function 8.5 Topical Crawlers () 1 3 e 4 5 299 (12) to the same 300 data points {(q, d)}. Note that this three-parameter model is more complex than the one used to validate the link-content conjecture, because ( = 0) must also be estimated from the data ( (q, 0) = 1/Gq). The fitted curve reveals that being within a radius of three links from a relevant page increases the relevance probability by a factor (q, d) >>1. This is very reassuring for the design of topical crawlers. It also suggests that crawlers should attempt to remain within a few links from some relevant source. In this range hyperlinks create detectable signals about lexical and semantic content, despite the Web's apparent lack of structure. Fig. 8.9. Illustration of the link-cluster conjecture. The curve plots the decay in mean likelihood ratio as a function of mean directed link distance from a relevant page, obtained by fitting data from 100 exhaustive breadth-first crawls starting from as many Yahoo! directory topics [372]. The link-content and link-cluster conjectures can be further developed by looking at the correlation between content-based, link-based, and semantic-based similarity measures. Using the ODP as a ground truth, we can express the semantic similarity between any two pages in the taxonomy [373, 359] and see how it can be approximated by content and link similarity measures. For content one can consider for example cosine similarity based on TF or TF-IDF term weights. For link similarity one can similarly represent a page as a bag of links (in-links, out-links, or 300 8 Web Crawling both/undirected) and then apply a Jaccard coefficient or a cosine similarity. Figure 8.10 shows, for various topical domains from the ODP, the correlation between semantic similarity and two representative content and link similarity measures. We observe significant heterogeneity in the correlations, suggesting that topical crawlers have an easier job in some topics (e.g., “news”) than others (e.g., “games”). Another observation is that in some topical domains (e.g., “home”) textual content is a more reliable signal, while in others (e.g., “computers”) links are more helpful. Fig. 8.10. Pearson correlation coefficients between the semantic similarity extracted from ODP [359] and two representative content and link similarity measures. The correlations are measured using a stratified sample of 150,000 URLs from the ODP, for a total of 4 billion pairs [373]. Content similarity is cosine with TF weights, and link similarity is the Jaccard coefficient with undirected links. 8.5.2 Best-First Variations The majority of crawling algorithms in the literature are variations of the best-first scheme described in Sect. 8.1.2. The difference is in the heuristics that they use to score unvisited URLs. A very simple instance is the case where each URL is queued into the frontier with priority given by the content similarity between the topic description and the page from which the URL was extracted. Content similarity can be measured with the stan- 8.5 Topical Crawlers 301 Fig. 8.11. Link context from distance-weighted window (top) and from the DOM tree (bottom). dard cosine similarity, using TF or TFIDF term weights (in the latter case the crawler must have global or topic-contextual term frequency information available). This simple crawler is also known as naïve best-first. Many variations of the naïve best-first crawlers are possible. Some give more importance to certain HTML markups, such as the title, or to text segments marked by special tags, such as headers. Other techniques focus on determining the most appropriate textual context to score a link. One alternative to using the entire page or just the anchor text as context, used by 302 8 Web Crawling InfoSpiders [375] and Clever [86], is a weighted window where topic keywords occurrences near the anchor count more toward the link score than those farther away, as shown in Fig. 8.11. Another approach is to consider the tag (DOM) tree of the HTML page [85]. The idea is to walk up the tree from the link anchor toward the root, stopping at an appropriate aggregation node. The link context is then obtained by the text in the tag subtree rooted at the aggregation node (Fig. 8.11). SharkSearch [237] is an improved version of the earlier FishSearch crawler [60]. It uses a similarity measure like the one used in the naïve best-first crawler as a first step for scoring unvisited URLs. The similarity is computed for anchor text, a fixed-width link context, the entire source page, and ancestor pages. The ancestors of a URL are the pages that appear on the crawl path to the URL. SharkSearch, like its predecessor FishSearch, maintains a depth bound. That is, if the crawler finds unimportant pages on a crawl path it stops crawling further along that path. To this end, each URL in the frontier is associated with a depth and a potential score. The score of an unvisited URL is obtained from a linear combination of anchor text similarity, window context similarity, and an inherited score. The inherited score is the similarity of the source page to the topic, unless it is zero, in which case it is inherited from the source's parent (and recursively from its ancestors). The implementation of SharkSearch requires to preset three similarity coefficients in addition to the depth bound. This crawler does not perform as well as others described below. Rather than (or in addition to) improving the way we assign priority scores to unvisited URLs, we can also improve on a naïve best-first crawler by altering the priority scheme. A classic trade-off in machine learning is that between exploration and exploitation of information. A crawler is no different: it can greedily pursue the best-looking leads based on noisy quality estimates, or be more explorative and visit some pages that seem less promising, but might lead to better pages. The latter approach is taken in many optimization algorithms in order to escape local optima and reach a global optimum with some probability. As it turns out, the same strategy is also advantageous for topical crawlers. Visiting some URLs with lower priority leads to a better overall quality of the crawler pages than strictly following the best-first order. This is demonstrated by best-N-first, a crawling algorithm that picks N URLs at a time from the frontier (the top N by priority score) and fetches them all. Once all N pages are visited, the newly extracted URLs are merge-sorted into the priority queue, and the cycle is repeated. The best-N-first crawler with N = 256 is a very strong competitor, outperforming most of the other topical crawlers in the literature [434, 377]. Figure 8.12 shows a comparison with two crawlers dis- 8.5 Topical Crawlers 303 Fig. 8.12. Performance of best-N-first crawler with N = 256 (BFS256) compared with a naïve best-first crawler (BFS1) and a breadth-first crawler. Recall refers to sets of relevant pages that the crawlers are supposed to discover; averages and error bars are computed across 100 crawls from as many ODP topics. cussed thus far. Note that a concurrent implementation of a best-first crawler with N threads or processes is equivalent to a best-N-first crawler. 8.5.3 Adaptation All the crawlers discussed thus far use a static strategy both to evaluate unvisited URLs and to manage the frontier. Thus they do not learn from experience or adapt to the context of a particular topic in the course of the crawl. In this section we describe a number of machine learning techniques that have been incorporated into adaptive topical crawlers. The intelligent crawler uses a statistical model for learning to assign priorities to the URLs in the frontier, considering Bayesian interest factors derived from different features [3]. For example, imagine that the crawler is supposed to find pages about soccer and that 40% of links with the keyword football in the anchor text lead to relevant pages, versus a background or prior frequency of only 2% of crawled pages being relevant. Then the crawler assigns an interest factor 304 8 Web Crawling ( soccer, football anchor ) Pr[relsoccer ( p) | football anchor ( p)] Pr[relsoccer ( p)] (13) 20 to the feature “keyword football in anchor.” Recall relsoccer(p) is the binary relevance score (0 or 1) of page p to soccer. The interest factors are treated as independent sources of evidence, or likelihoods. They are combined by a linear combination of log-likelihoods, with user-defined weight parameters. The features employed by the intelligent crawler may be diverse, depending on the particular crawling task. They may include tokens extracted from candidate URLs, source page content and links, co-citation (sibling) relationships, and/or other characteristics of the visited and unvisited URLs. As more evidence is accumulated and stored throughout the crawl, the interest factors are recalculated and the priorities updated, so that the frontier is always sorted according to the most recent estimates. Thus intelligent crawlers adapt to the content and link structure of the Web neighborhoods being explored. The original focused crawlers described earlier also use machine learning, in particular a classifier that guides the crawler. However the classifier is trained before the crawl is launched, and no learning occurs during the crawl. Therefore we do not consider it an adaptive crawler. However, in a later “accelerated” version of the focused crawler [85], an online learning apprentice was added to the system; the original (baseline) classifier then acts as a critic, providing the apprentice with training examples for learning to classify outgoing links from the features of the pages from which they are extracted. Suppose page p1 is fetched and contains a link to page p2. Later, p2 is fetched and the baseline classifier assigns it to a relevant class. This information is passed to the apprentice, which uses the labeled example (“the link from p1 to p2 is good”) to learn to classify the link to p2 as good based on the textual features in the context of the anchor within p1. Future links with a similar context should be given high priority. Conversely, if p2 is deemed irrelevant by the baseline classifier, the apprentice learns to predict (“bad link”) when it encounters a link with a similar context in the future. The features used to train the apprentice were textual tokens associated with a link context based on the DOM tree, and the learning algorithm used by the apprentice was a naïve Bayesian classifier. This approach led to a significant reduction in the number of irrelevant pages fetched by the focused crawler. While the accelerated focused crawler is not a topical crawler because it still needs labeled examples to train the baseline classifier prior to the crawl, the idea of training an apprentice online during the crawl can be ap- 8.5 Topical Crawlers 305 plied in topical crawlers as well. Indeed this is a type of reinforcement learning technique employed in several crawlers, using different features and/or different learning algorithms for the apprentice. In reinforcement learning [263] we have a network where nodes are states and directed links are actions. An action a A (think “anchor”) moves an agent from a state p P (think “page”) to another state according to a transition function L: P A P. Thus an adaptive crawler is seen as an agent moving from page to page. Actions are rewarded according to a function r: P A . We want to learn a policy mapping states to actions, : P A, that maximizes future reward discounted over time: t V ( p0 ) r ( pt , at ) (14) t0 where we follow action (link) at= (pt) from state (page) pt at each time step t. The parameter determines how future rewards are discounted (0 < 1). If = 0, the reinforcement learning policy is the greedy one employed by the naïve best-first crawler. To learn an optimal policy, we define the value of selecting action a from state p, and following the optimal policy thereafter: Q ( p, a ) r ( p, a ) V * [ L( p, a)] (15) where V* is the value function of the optimal policy *(p) = argmaxaQ(p, a). The question then becomes how to estimate the function Q, i.e., to assign a value to a link a based on the context information in page p from which the link is extracted. However, the actions available to the crawler are not limited to the links from the last page visited; any of the actions corresponding to the URLs in the frontier are available. Furthermore, there is no reason why the Q value of a link should be a function of a particular source page; if links to the same target page are extracted from multiple source pages, the estimated values of the anchors can be combined, for example Q(u) = max{(p, a): L(p, a) = u}Q(p, a). This way Q values can be computed not for links (anchors), but for target pages (URLs); the state and action spaces are thus greatly reduced, basically collapsing all visited pages into a single degenerate state and all links to their target URLs. The policy reduces to the simple selection of the URL in the frontier with the maximum Q value. One way to calculate Q values is via a naïve Bayesian classifier. This method was found to work well compared to a breadth-first crawler for the tasks of crawling computer science research papers and company directory information [366, 459]. In this case, the classifier was trained off-line 306 8 Web Crawling rather than online while crawling, using labeled examples as in the focused crawler. Training the classifier to predict future reward ( > 0) was better than only using immediate reward ( = 0). For future reward the authors use a heavy discount = 0.5, arguing that it is optimal to be greedy in selecting URLs from the frontier, so that one can crawl toward the nearest relevant page. This assumes that all relevant targets are within reach. So there is no reason to delay reward. However, as discussed earlier, a crawler typically deals with noisy data, so the classifier’s Q estimates are not entirely reliable; more importantly, a typical crawler cannot possibly cover the entire search space. These factors suggest that it may be advantageous to occasionally give up some immediate reward in order to explore other directions, potentially leading to pockets or relevant pages unreachable by a greedy crawler (see Fig. 8.12). Using a previously trained classifier to compute Q values for URLs in the frontier means that supervised learning is combined with reinforcement learning. As for focused crawlers, labeled examples must be available prior to the start of the crawl. This may be possible in tasks such as the collection of research articles, but is not a realistic assumption for typical topical crawlers. An adaptive crawling algorithm that actually uses reinforcement learning while crawling online, without any supervised learning, is InfoSpiders. This crawler employs various machine learning techniques to exploit various types of Web regularities. InfoSpiders are inspired by artificial life models in which a population of agents lives, learn, evolve, and die in an environment. Individual agents learn during their lifetimes based on their experiences, with the environment playing the role of a critic, providing rewards and penalties for actions. Agents may also reproduce, giving rise to new agents similar to them, and die. The environment is the Web, the actions consist of following links and visiting pages and the text and link features of pages are the signals that agents can internalize into their learned and evolved behaviors. Feedback from the environment consists of a finite energy resource, necessary for survival. Each action has an energy cost, which may be fixed or proportional to, say, the size of a fetched page or the latency of a page download [126]. Energy is gained from visiting new pages relevant to the query topic. A cache prevents an agent from accumulating energy by visiting the same page multiple times. In the recent version of InfoSpiders, each agent maintains its own frontier of unvisited URLs [377]. The agents can be implemented as concurrent processes/threads, with non-contentious access to their local frontiers. Fig. 8.13 illustrates the representation and flow of an individual agent. The adaptive representation of each InfoSpiders agent consists of a list of keywords (initialized with the topic description) and a neural net used to evaluate new links. Each input unit of the neural net receives a count of the 8.5 Topical Crawlers 307 frequency with which the keyword occurs in the vicinity of each link, weighted to give more importance to keywords occurring near the anchor and maximum for the anchor text (Fig. 8.11). The neural net has a single output unit whose activation is used as a Q value (score) for each link u in input. The agent’s neural net learns to predict the Q value of the link’s target URL u given the inputs from the link's source page p. The reward function r(u) is the cosine similarity between the agent’s terms and the target page u. The future discounted optimal value V*(u) is approximated using the highest neural net prediction among the links subsequently extracted from u. This procedure is similar to the reinforcement learning algorithm described above, except that the neural net replaces the naïve Bayesian classifier. The neural net is trained by the back-propagation algorithm [469]. This mechanism is called connectionist reinforcement learning [335]. While the neural net can in principle model nonlinear relationships between term frequencies and pages, in practice we have used a simple perceptron whose prediction is a linear combination of the keyword weights. Such a learning technique provides each InfoSpiders agent with the capability to adapt its own link-following behavior in the course of a crawl by associating relevance estimates with particular patterns of keyword frequencies around links. Fig. 8.13. A single InfoSpiders agent. The link context is the weighted window as shown in Fig. 8.11: for each newly extracted URL and for each term in the agent's term list, this produces a weight that is fed into the neural network, whose output is stored as the link's priority score in the frontier. 308 8 Web Crawling The neural net's link scores are combined with estimates based on the cosine similarity between the agent's keyword list and the entire source page. A parameter (0 1) regulates the relative importance given to the estimates based on the neural net versus the source page. Based on the combined score the agent uses a stochastic selector to pick one of the links in the frontier with probability Pr(u ) e u' (u ) e (16) ( u ') where u is a URL in the local frontier . Parameter regulates the greediness of the link selector. Its value can be fixed or evolved with the agent. After a new page u has been fetched, the agent receives an energy payoff proportional to the difference between the reward r(u) and the cost charged for the download. An agent dies when it runs out of energy. The energy level is also used to determine whether or not the agent should reproduce after visiting a page. An agent reproduces when the energy level passes a fixed threshold. The reproduction is meant to bias the search toward areas with pages relevant to the topic. Topical locality suggests that if an agent visits a few relevant pages in rapid sequence, more relevant pages are likely to be nearby (in the frontier). To exploit this, the accumulated energy results in a short-term doubling of the frequency with which the crawler explores this agent’s frontier. At reproduction, the agent’s energy and frontier are split in half with the offspring (new agent or thread). According to ecological theory, this way the agent population is supposed to move toward an optimal cover of the Web graph in proportion to the local density of resources, or relevant pages. In addition to the individual's reinforcement learning and the population’s evolutionary bias, InfoSpiders employ a third adaptive mechanism. At reproduction, the offspring’s keyword vector is mutated (expanded) by adding a new term. The chosen term/keyword is the one that is most frequent in the parent’s last visited page, i.e., the page that triggered the reproduction. This selective query expansion strategy, illustrated in Fig. 8.6, is designed to allow the population to diversify and expand its focus according to each agent’s local context. An InfoSpiders crawler incorporating all of these adaptive techniques has been shown to outperform various versions of naïve best-first crawlers (Fig. 8.14) when visiting a sufficiently large number of pages (more than 10,000) so that the agents have time to adapt [377, 504]. 8.5 Topical Crawlers 309 Fig. 8.14. Performance plots [377]: average target recall RT(t) (top) and average precision PD(t) (similarity to topic description, bottom). The averages are calculated over 10 ODP topics. After 50,000 pages crawled, one tailed t-tests reveal that both BFS256 and InfoSpiders outperform the breadth-first crawler on both performance metrics. InfoSpiders outperform BFS256 on recall, while the difference in precision is not statistically significant. 310 8 Web Crawling 8.6 Evaluation Given the goal of building a “good” crawler, a critical question is how to evaluate crawlers so that one can reliably compare two crawling algorithms and conclude that one is “better” than the other. Since a crawler is usually designed to support some application, e.g., a search engine, it can be indirectly evaluated through the application it supports. However, attribution is problematic; if a search engine works better than another (assuming that were easy to determine!), how can we attribute this difference in performance to the underlying crawling algorithms as opposed to the ranking or indexing schemes? Thus it is desirable to evaluate crawlers directly. Often crawler evaluation has been carried out by comparing a few crawling algorithms on a limited number of queries/tasks without considering the statistical significance. Such anecdotal results, while important, do not suffice for thorough performance comparisons. As the Web crawling field has matured, a need has emerged for evaluating and comparing disparate crawling strategies on common tasks through well-defined performance measures. Let us review the elements of such an evaluation framework, which can be applied to topical as well as focused crawlers. A comparison between crawlers must be unbiased and must allow one to measure statistically significant differences. This requires a sufficient number of crawl runs over different topics, as well as sound methodologies that consider the temporal nature of crawler outputs. Significant challenges in evaluation include the general unavailability of relevant sets for particular topics or queries. Unfortunately, meaningful experiments involving real users for assessing the relevance of pages as they are crawled are extremely problematic. In order to obtain a reasonable notion of crawl effectiveness one would have to recruit a very large number of subjects, each of whom would have to judge a very large number of pages. Furthermore, crawls against the live Web pose serious time constraints and would be overly burdensome to the subjects. To circumvent these problems, crawler evaluation typically relies on defining measures for automatically estimating page relevance and quality. The crawler literature reveals several performance measures used for these purposes. A page may be considered relevant if it contains some or all of the keywords in the topic/query. The frequency with which the keywords appear on the page may also be considered [102]. While the topic of interest to the user is often expressed as a short query, a longer description may be available in some cases. Similarity between the short or long description and each crawled page may be used to judge the page's relevance [237, 376, 504]. The pages used as the crawl's seed URLs may be combined together into a single document, and the cosine similarity between this 8.6 Evaluation 311 document and a crawled page may serve as the page’s relevance score [18]. A classifier may be trained to identify relevant pages. The training may be done using seed pages or other pre-specified relevant pages as positive examples. The trained classifier then provides boolean or continuous relevance scores for each of the crawled pages [87, 139]. Note that if the same classifier, or a classifier trained on the same labeled examples, is used both to guide a (focused) crawler and to evaluate it, the evaluation is not unbiased. Clearly the evaluating classifier would be biased in favor of crawled pages. To partially address this issue, an evaluation classifier may be trained on a different set than the crawling classifier. Ideally the training sets should be disjoint. At a minimum the training set used for evaluation must be extended with examples not available to the crawler [433]. Another approach is to start N different crawlers from the same seeds and let them run until each crawler gathers P pages. All of the N P pages collected from the crawlers are ranked against the topic query/description using a retrieval algorithm such as cosine. The rank provided by the retrieval system for each page is then used as a relevance score. Finally, one may use algorithms, such as PageRank or HITS, that provide authority or popularity estimates for each crawled page. A simpler method would be to use just the number of in-links to the crawled page to derive similar information [18, 102]. Many variations of link-based methods using topical weights may be applied to measure the topical quality of pages [52, 88]. Once each page is assessed, a method is needed to summarize the performance of a crawler across a set of crawled pages. Given a particular measure of page relevance and/or importance we can summarize the performance of the crawler with metrics that are analogous to the information retrieval notions of precision and recall (see Chap. 6). Lacking welldefined relevant sets, the classic boolean relevance is replaced by one of the scores outlined above. A few precision-like measures are found in the literature. In case we have boolean relevance scores, we could measure the rate at which “good” pages are found; if 100 relevant pages are found in the first 500 pages crawled, we have an acquisition rate or harvest rate of 20% at 500 pages [3]. If the relevance scores are continuous (e.g., from cosine similarity or a trained classifier) they can be averaged over the crawled pages. The average relevance, as shown in Fig. 8.14, may be computed over the progress of the crawl [376]. Sometimes running averages are calculated over a window of a number of pages, e.g., the last 50 pages from a current crawl point [87]. Another measure from information retrieval that has been applied to crawler evaluation is search length [375], defined as the number of pages (or the number of irrelevant pages) crawled before a certain percentage of the relevant pages are found. Search length is akin to the reciprocal of precision for a preset level of recall. 312 8 Web Crawling Recall-like measures would require normalization by the number of relevant pages. Since this number is unknown for Web crawling tasks, it might appear that recall cannot be applied to crawlers. However, even if unknown, the size of the relevant set is a constant. Therefore, it can be disregarded as a scaling factor when comparing two crawling algorithms on the same topical query. One can simply sum the quality or relevance estimates (obtained by one of the methods outlined above) over the course of a crawl, and obtain a total relevance as shown in Fig. 8.14. It is possible to design crawling experiments so that a set of relevant target pages is known by the experimenter. Then precision and recall can be calculated from the fraction of these relevant targets that are discovered by the crawler, rather than based on relevance estimates. One way to obtain a set of relevant pages is from a public directory such as the ODP. This way one can leverage the classification already carried out by the volunteer editors of the directory. The experimenter can select as topics a set of categories from the ODP, whose distance from the root of the ODP taxonomy can be determined so as to obtain topics with generality/specificity appropriate for the crawling task [377, 504]. Figure 8.5 (left) illustrates how subtrees rooted at a chosen category can be used to harvest a set of relevant target pages. If a page is classified in a subtopic of a target topic, it can be considered relevant with respect to the target topic. If a set of known relevant target pages is used to measure the performance of a topical crawler, these same pages cannot be used as seeds for the crawl. Two approaches have been proposed to obtain suitable seed pages. One is to perform a back-crawl from the target pages [504]. By submitting link: queries to a search engine API, one can obtain a list of pages linking to each given target; the process can be repeated from these parent pages to find “grandparent” pages, and so on until a desired link distance is reached. The greater the link distance, the harder the task is for the crawler to locate the relevant targets from these ancestor seed pages. The procedure has the desired property that directed paths are guaranteed to exist from any seed page to some relevant targets. Given the potentially large fan-in of pages, sampling is likely required at each stage of the back-crawl to obtain a suitable number of seeds. The process is similar to the construction of a context graph, as shown in Fig. 8.5 (right). A second approach is to split the set of known relevant pages into two sets; one set can be used as seeds, the other as targets. While there is no guarantee that the targets are reachable from the seeds, this approach is significantly simpler because no backcrawl is necessary. Another advantage is that each of the two relevant subsets can be used in turn as seeds and targets. In this way, one can measure the overlap between the pages crawled starting from the two disjoint sets. 8.6 Evaluation 313 Fig. 8.15. Illustration of precision and recall measures based on known relevant target pages and underlying independence assumption/requirement. A large overlap is interpreted as robustness of the crawler in covering relevant portions of the Web [87, 85]. The use of known relevant pages as proxies for unknown relevant sets implies an important assumption, which we can illustrate by the Venn diagram in Fig. 8.15. Here S is a set of crawled pages and T is the set of known relevant target pages, a subset of the relevant set R. Let us consider the measure of recall. Using T as if it were the relevant set means that we are estimating the recall |R S| / |R| by |T S| / |T|. This approximation only holds if T is a representative, unbiased sample of R independent of the crawl process. While the crawler attempts to cover as much as possible of R, it should not have any information about how pages in T are sampled from R. If T and S are not independent, the measure is biased and unreliable. For example if a page had a higher chance of being selected in T because it was in S, or vice versa, then the recall would be overestimated. The same independence assumption holds for precision-like measures, where we estimate |R S| / |S| by |T S| / |S|. A consequence of the independence requirement is that if the ODP is used to obtain T, the experimenter must prevent the crawler from accessing the ODP. This would bias the results because, once a relevant ODP category page is found, all of the relevant target pages can be reached by the crawler in a short breadth-first sweep. Preventing access to the ODP may pose a challenge because so many ODP mirrors exist on the Web. They may not be known by the experimenter, and not trivial to detect. To summarize, crawler performance measures [504] can be characterized along two dimensions: the source of relevance assessments (target pages vs. similarity to their descriptions) and the normalization factor (av- 314 8 Web Crawling erage relevance, or precision, vs. total relevance, or recall). Using target pages as the relevant sets we can define crawler precision and recall as follows: RT (t , ) | St T | |T | (17) PT (t , ) | St T | | St | (18) where St is the set of pages crawled at time t (t can be wall clock time, network latency, number of pages visited, number of bytes downloaded, and so on). T is the relevant target set, where represents the parameters used to select the relevant target pages. This could include for example the depth of ODP category subtrees used to extract topic-relevant pages. Analogously we can define crawler precision and recall based on similarity to target descriptions: RD (t , ) p St PD (t , ) p St ( p, D ) (19) |T | ( p, D ) (20) | St | where D is the textual description of the target pages, selected with parameters , and is a text-based similarity function, e.g., cosine similarity (see Chap. 6). Figure 8.14 shows two examples of performance plots for three different crawlers discussed earlier in this chapter. The two plots depict RT and PD as a function of pages crawled. InfoSpiders and the BFS256 crawler are found to outperform the breadth-first crawler. InfoSpiders gain a slight edge in recall once the agents have had an opportunity to adapt. This evaluation involves each of the three crawlers visiting 50,000 pages for each of 10 topics, for a total of 1.5 million pages. Another set of evaluation criteria can be obtained by scaling or normalizing any of the above performance measures by the critical resources used by a crawler. This way, one can compare crawling algorithms by way of performance/cost analysis. For example, with limited network bandwidth one may see latency as a major bottleneck for a crawling task. The time spent by a crawler on network I/O can be monitored and applied as a scaling factor to normalize precision or recall. Using such a measure, a crawler 8.7 Crawler Ethics and Conflicts 315 designed to preferentially visit short pages, or pages from fast servers [126], would outperform one that can locate pages of equal or even better quality but less efficiently. 8.7 Crawler Ethics and Conflicts Crawlers, especially when efficient, can put a significant strain on the resources of Web servers, mainly on their network bandwidth. A crawler that sends many page requests to a server in rapid succession, say ten or more per second, is considered impolite. The reason is that the server would be so busy responding to the crawler that its service to other requests, including those from human browsing interactively, would deteriorate. In the extreme case a server inundated with requests from an aggressive crawler would become unable to respond to other requests, resulting in an effective denial of service attack by the crawler. To prevent such incidents, it is essential for a crawler to put in place measures to distribute its requests across many servers, and to prevent any one server (fully qualified host name) from receiving requests at more than some reasonably set maximum rate (say, one request every few seconds). In a concurrent crawler, this task can be carried out by the frontier manager, when URLs are dequeued and passed to individual threads or processes. This practice not only is required by politeness toward servers, but also has the additional benefits of limiting the impact of spider traps and not overloading the server, which will respond slowly. Preventing server overload is just one of a number of policies required of ethical Web agents [160]. Such policies are often collectively referred to as crawler etiquette. Another requirement is to disclose the nature of the crawler using the User-Agent HTTP header. The value of this header should include not only a name and version number of the crawler, but also a pointer to where Web administrators may find information about the crawler. Often a Web site is created for this purpose and its URL is included in the User-Agent field. Another piece of useful information is the email contact to be specified in the From header. Finally, crawler etiquette requires compliance with the Robot Exclusion Protocol. This is a de facto standard providing a way for Web server administrators to communicate which files may not be accessed by a crawler. This is accomplished via an optional file named robots.txt in the root directory of the Web server (e.g., http://www.somehost.com/robots.txt). The file provides access policies for different crawlers, identified by the User-agent field. For any user-agent value (or the default “*”) a number of Disallow entries identify directory subtrees to be avoided. Compliant crawlers must 316 8 Web Crawling fetch and parse a server's robots.txt file before sending requests to that server. For example, the following policy in robots.txt: User-agent: * Disallow: / directs any crawler to stay away from the entire server. Some high-level languages such as Perl provide modules to parse robots.txt files. It is wise for a crawler to cache the access policies of recently visited servers, so that the robots.txt file need not be fetched and parsed every time a request is sent to the same server. Additionally, Web authors can indicate if a page may or may not be indexed, cached, or mined by a crawler using a special HTML meta-tag. Crawlers need to fetch a page in order to parse this tag, therefore this approach is not widely used. More details on the robot exclusion protocols can be found at http://www.robotstxt.org/wc/robots.html. When discussing the interactions between information providers and search engines or other applications that rely on Web crawlers, confusion sometime arises between the ethical, technical, and legal ramifications of the Robot Exclusion Protocol. Compliance with the protocol is an ethical issue, and non-compliant crawlers can justifiably be shunned by the Web community. However, compliance is voluntary, and a robots.txt file cannot enforce it. Servers can, however, block access to a client based on its IP address. Thus it is likely that a crawler which does not comply with the Exclusion Protocol and does not follow proper etiquette will be quickly blocked by many servers. Crawlers may disguise themselves as browsers by sending a browser's identifying string in the User-Agent header. This way a server administrator may not immediately detect lack of compliance with the Exclusion Protocol, but an aggressive request profile is likely to reveal the true nature of the crawler. To avoid detection, some mischievous crawlers send requests at low and randomized rates. While such behaviors may be reprehensible, they are not illegal – at least not at the time of this writing. Nonetheless, there have been cases of businesses bringing lawsuits against search organizations for not complying with the Robot Exclusion Protocol. In a recent lawsuit involving the Internet Archive's WayBack Machine (www.archive.org), a plaintiff not only attributed legal weight to the Exclusion Protocol, but also expected that a newly added robots.txt policy should have retroactive value! Deception does not occur only by crawlers against servers. Some servers also attempt to deceive crawlers. For example, Web administrators may attempt to improve the ranking of their pages in a search engine by providing different content depending on whether a request originates from a browser or a search engine crawler, as determined by inspecting the request's User-Agent header. This technique, called cloaking, is frowned 8.7 Crawler Ethics and Conflicts 317 upon by search engines, which remove sites from their indices when such abuses are detected. For more information about Web spam, see Chap. 6. One of the most serious challenges for crawlers originates from the rising popularity of pay-per-click advertising. If a crawler is not to follow advertising links, it needs to have a robust detection algorithm to discriminate ads from other links. A bad crawler may also pretend to be a genuine user who clicks on the advertising links in order to collect more money from merchants for the hosts of advertising links. The above examples suggest a view of the Web as a new playground for artificial intelligence (AI). Crawlers need to become increasingly sophisticated to prevent insidious forms of spam from polluting and exploiting the Web environment. Malicious crawlers are also becoming smarter in their efforts, not only to spam but also to steal personal information and in general to deceive people and crawlers for illicit gains. One chapter of this arms race has been the development of CAPTCHAs [14], graphics-based inverse Turing tests automatically generated by server sites to keep out malicious crawlers. Maybe a stronger AI will be a positive outcome of crawler evolution; maybe a less usable Web will be a hefty price to pay. Interestingly, the gap between humans and crawlers may be narrowing from both sides. While crawlers become smarter, some humans are dumbing down their content to make it more accessible to crawlers. For example some online news providers use simpler titles than can be easily classified and interpreted by a crawler as opposed or in addition to witty titles that can only be understood by humans. Another gap that is getting narrower is the distinction between browsers and crawlers, with a growing gray area between the two. A business may wish to disallow crawlers from its site if it provides a service by which it wants to entice human users to visit the site, say to make a profit via ads on the site. A competitor crawling the information and mirroring it on its own site, with different ads, is a clear violator not only of the Robot Exclusion Protocol but also possibly of copyright law. What about an individual user who wants to access the information but automatically hide the ads? There are many browser extensions that allow users to perform all kinds of tasks that deviate from the classic browsing activity, including hiding ads, altering the appearance and content of pages, adding and deleting links, adding functionality to pages, pre-fetching pages, and so on. Such extensions have some of the functionalities of crawlers. Should they identify themselves through the User-Agent header as distinct from the browser with which they are integrated? Should a server be allowed to exclude them? And should they comply with such exclusion policies? These too are questions about ethical crawler behaviors that remain open for the moment. 318 8 Web Crawling 8.8 Some New Developments The typical use of (universal) crawlers thus far has been for creating and maintaining indexes for general purpose search engines. However a more diverse use of (topical) crawlers is emerging both for client and server based applications. Topical crawlers are becoming important tools to support applications such as specialized Web portals (a.k.a. “vertical” search engines), live crawling, and competitive intelligence. Another characteristic of the way in which crawlers have been used by search engines up to now is the one-directional relationship between users, search engines, and crawlers. Users are consumers of information provided by search engines, search engines are consumers of information provided by crawlers, and crawlers are consumers of information provided by users (authors). This one-directional loop does not allow, for example, information to flow from a search engine (say, the queries submitted by users) to a crawler. It is likely that commercial search engines will soon leverage the huge amounts of data collected from their users to focus their crawlers on the topics most important to the searching public. To investigate this idea in the context of a vertical search engine, a system was built in which the crawler and the search engine engage in a symbiotic relationship [430]. The crawler feeds the search engine which in turn helps the crawler. It was found that such a symbiosis can help the system learn about a community's interests and serve such a community with better focus. As discussed in Sect. 8.3, universal crawlers have to somehow focus on the most “important” pages given the impossibility to cover the entire Web and keep a fresh index of it. This has led to the use of global prestige measures such as PageRank to bias universal crawlers, either explicitly [102, 234] or implicitly through the long-tailed structure of the Web graph [401]. An important problem with these approaches is that the focus is dictated by popularity among “average” users and disregards the heterogeneity of user interests. A page about a mathematical theorem may appear quite uninteresting to the average user, if one compares it to a page about a pop star using indegree or PageRank as a popularity measure. Yet the math page may be highly relevant and important to a small community of users (mathematicians). Future crawlers will have to learn to discriminate between low-quality pages and high-quality pages that are relevant to very small communities. Social networks have recently received much attention among Web users as vehicles to capture commonalities of interests and to share relevant information. We are witnessing an explosion of social and collaborative engines in which user recommendations, opinions, and annotations are aggregated and shared. Mechanisms include tagging (e.g., del.icio.us and 8.8 Some New Developments 319 flickr.com), ratings (e.g., stumbleupon.com), voting (e.g., digg.com), and hierarchical similarity (GiveALink.org) [363]. One key advantage of social systems is that they empower humans rather than depending on crawlers to discover relevant resources. Further, the aggregation of user recommendations gives rise to a natural notion of trust. Crawlers could be designed to expand the utility of information collected through social systems. For example it would be straightforward to obtain seed URLs relevant to specific communities of all sizes. Crawlers would then explore the Web for other resources in the neighborhood of these seed pages, exploiting topical locality to locate and index other pages relevant to those communities. Social networks can emerge not only by mining a central repository of user-provided resources, but also by connecting hosts associated with individual users or communities scattered across the Internet. Imagine a user creating its own micro-search engine by employing a personalized topical crawler, seeded for example with a set of bookmarked pages. Desktop search applications make it easy to also share certain local files, if so desired. Can federations of such micro-engine agents emerge on the basis of mutual interests? Peer-to-peer (P2P) networks are beginning to be seen as robust architectures ideal for brokering among individual needs and catering to communities [354]. Adaptive peer-based search systems driven by simple distributed adaptive query routing algorithms can spontaneously organize into networks with efficient communication and with emerging clusters capturing semantic locality. Specifically, in a P2P search application called 6Search (6S), each peer crawls the Web in a focused way, guided by its user’s information context. Each peer submits and responds to queries to/from its neighbors. This search process has no centralized control. Peers depend on local adaptive routing algorithms to dynamically change the topology of the peer network and search for the best neighbors to answer their queries. Machine learning techniques are being explored to improve local adaptive routing. Validation of the 6S framework and network via simulations with 70 500 model users based on actual Web crawls has yielded encouraging preliminary results. The network topology rapidly converges from a random network to a small-world network, with clusters emerging to match user communities with shared interests [15]. Additionally the quality of the results is significantly better than obtained by centralized search engines built with equivalent resources, and comparable with the results from much larger search engines such as Google [553, 554]. The integration of effective personalized/topical crawlers with adaptive query routing algorithms is the key to the success of peer-based social search systems. Many synergies may be exploited in this integration by leveraging contextual information about the local peer that is readily avail- 320 8 Web Crawling able to the crawler, as well as information about the peer's neighbors that can be mined through the stream of queries and results routed through the local peer. An open-source prototype of 6S enabling sharing of bookmarks, one-click crawling, and distributed collaborative search is available (http://homer.informatics.indiana.edu/~nan/6S/). If successful, this kind of application could create a new paradigm for crawling and searching where universal crawlers and search engines are complemented with swarms of personal crawlers and micro-engines tuned to the specialized information needs of individual users and dynamic self-organized social networks. Bibliographic Notes General ideas and techniques about crawling can be found in [68, 85, 263], little is known about implementation details of commercial crawlers. Focused crawling discussed in this chapter is based on [87, 85, 139]. Literature on topical crawling algorithms is extensive [e.g., 3, 60, 102, 126, 237, 366, 369, 375, 377, 432, 434, 459]. Topical crawlers have been used for building focused repositories, automating resource discovery, and supporting software agents. For example, topical crawlers are used to collect papers for building scientific literature digital libraries such as CiteSeer and Google Scholar [308, 366, 550]. Applications of topical crawlers to business and competitive intelligence are discussed in [432], and biomedical applications in [503]. Controversial applications to harvest personal information for spam and phishing purposes are illustrated in [251]. On best-first crawlers, various methods have been used to determine an appropriate textual context in which to evaluate and score unvisited links. Using the anchor text is one strategy [123]. Another strategy is to use windows of a fixed size, e.g., 50 words around the anchor, in place of/in addition to the anchor text [237]. The weighted window used by InfoSpiders [375] yields a weight for each link, which is then fed to a neural network to score each link. In the tag (DOM) tree approach [85], using the parent node of the anchor as aggregation node worked well in a business intelligence crawling task [432]. There is a tradeoff analogous to that between precision and recall when we consider the optimal size of a link context: small contexts (e.g., anchor text) have the highest average similarities to the target page, but also highest chance to miss important cues about the target. Larger contexts (e.g., parent or grand-parent aggregator node) have lower average similarities to the target, but lower chance to miss all the keywords in the target. This suggests a greedy optimization scheme: climb the DOM tree from the anchor until sufficient terms are present in the link context [429]. This approach outperformed both the fixed-window method Bibliographic Notes 321 (with optimal window size) and the DOM tree method with a fixed aggregator depth (anchor, parent, or grandparent). Early versions of InfoSpiders were described in [369, 374, 375, 376]. Certain aspects of evolutionary computation have also been used in other topical crawlers such as the itsy bitsy spider [96]. Another adaptive mechanism for topical crawlers inspired by natural processes is ant colony optimization [194]. The idea is that a population of agents leaves a trail of pheromone along the paths that lead to relevant pages, gradually biasing the crawl toward promising portions of the Web graph. A more extensive review of adaptive topical crawling algorithms can be found in [380]. 9 Structured Data Extraction: Wrapper Generation Web information extraction is the problem of extracting target information items from Web pages. There are two general problems: extracting information from natural language text and extracting structured data from Web pages. This chapter focuses on extracting structured data. A program for extracting such data is usually called a wrapper. Extracting information from text is studied mainly in the natural language processing community. Structured data on the Web are typically data records retrieved from underlying databases and displayed in Web pages following some fixed templates. In this chapter, we still call them data records. Extracting such data records is useful because it enables us to obtain and integrate data from multiple sources (Web sites and pages) to provide value-added services, e.g., customizable Web information gathering, comparative shopping, meta-search, etc. With more and more companies and organizations disseminating information on the Web, the ability to extract such data from Web pages is becoming increasingly important. At the time of writing this book, there are several companies working on extracting products sold online, product reviews, job postings, research publications, forum discussions, statistics data tables, news articles, search results, etc. Researchers and Internet companies started to work on the extraction problem from the middle of 1990s. There are three main approaches: 1. Manual approach: By observing a Web page and its source code, the human programmer finds some patterns and then writes a program to extract the target data. To make the process simpler for programmers, several pattern specification languages and user interfaces have been built. However, this approach is not scalable to a large number of sites. 2. Wrapper induction: This is the supervised learning approach, and is semi-automatic. The work started around 1995-1996. In this approach, a set of extraction rules is learned from a collection of manually labeled pages or data records. The rules are then employed to extract target data items from other similarly formatted pages. 3. Automatic extraction: This is the unsupervised approach started around 1998. Given a single or multiple pages, it automatically finds 324 9 Structured Data Extraction: Wrapper Generation patterns or grammars from them for data extraction. Since this approach eliminates the manual labeling effort, it can scale up data extraction to a huge number of sites and pages. The first approach will not be discussed further. This chapter focuses on the last two approaches. Sects. 9.2 and 9.3 study supervised wrapper learning, and the rest of the chapter studies automatic extraction. 9.1 Preliminaries To start our discussion, let us see some real pages that contain structured data that we want to extract. We then develop a Web data model and a HTML mark-up encoding scheme for the data model. Data extraction is simply the reverse engineering task. That is, given the HTML mark-up encoded data (i.e., Web pages), the extraction system recovers the original data model and extracts data from the encoded data records. 9.1.1 Two Types of Data Rich Pages There are mainly two types of data rich pages. Data in such pages are usually retrieved from underlying databases and displayed on the Web following some fixed templates. This task is often done by computer programs. 1. List pages: Each of such pages contains several lists of objects. Figure 9.1 shows such a page, which has two lists of products. From a layout point of view, we see two data regions (one horizontal and one vertical). Within each region, the data records are formatted using the same template. The templates used in the two regions are different. 2. Detail pages: Such a page focuses on a single object. For example, in Fig. 9.2, the page focuses on the product “iPod Video 30GB, Black”. That is, it contains all the details of the product, name, image, price and other purchasing information, product description, customer rating, etc. Note that when we say that a page focuses on a particular object (or lists of objects), we do not mean that the page contains no other information. In fact, it almost certainly contains other information. For example, in the page for “iPod Video 30GB, Black” (Fig. 9.2), there are some related products on the right-hand side, company information at the top, and copyright notices, terms and conditions, privacy statements at the bottom, etc. They are not shown in Fig. 9.2 as we want the main part of the product clearly eligible. For list pages, it is often easy to use some heuristics to identify the main data regions, but for detail pages, it is harder. 9.1 Preliminaries Fig. 9.1. A segment of a list page with two data regions Fig. 9.2. A segment of a detail page 325 326 9 Structured Data Extraction: Wrapper Generation (A) An example of a nested data record image 1 Cabinet Organizers by Copco 9-in. Round Turntable: White image 1 Cabinet Organizers by Copco 12-in. Round Turntable: White image 2 Cabinet Organizers 14.75x9 Cabinet Organizer (Nonskid): White image 3 Cabinet Organizers 22x6 Cookware Lid Rack ***** $4.95 ***** $7.95 ***** $7.95 **** $19.95 (B) Extraction results Fig. 9.3. An example input page and output data table In Fig. 9.1, the description of each product is called a data record. Notice that the data records in this page are all flat with no nesting. Figure 9.3(A) contains some nested data records, which makes the problem more interesting and also harder. The first product, “Cabinet Organizers by Copco,” has two sizes (9-in. and 12-in.) with different prices. These two organizers are not at the same level as “Cabinet Organizers by Copco”. Our objective: We want to extract the data and produce the data table given in Fig. 9.3(B). “image 1” and “Cabinet Organizers by Copco” are repeated for the first two rows due to the nesting. 9.1.2 Data Model We now describe a data model commonly used for structured data on the Web. In the next sub-section, we present a HTML mark-up encoding of the model and the data, which helps extraction. Most Web data can be modeled as nested relations, which are typed objects allowing nested sets and tuples. The types are defined as follows: 9.1 Preliminaries 327 There is a set of basic types, B = {B1, B2, …, Bk}. Each Bi is an atomic type, and its domain, denoted by dom(Bi), is a set of constants; If T1, T2, …, Tn are basic or set types, then [T1, T2, …, Tn] is a tuple type with the domain dom([T1, T2, …, Tn]) = {[v1, v2, …, vn] | vi dom(Ti)}; If T is a tuple type, then {T} is a set type with the domain dom({T}) being the power set of dom(T). A basic type Bi is analogous to the type of an attribute in relational databases, e.g., string and int. In the context of the Web, Bi is usually a text string, image-file, etc. The example in Fig. 9.4 shows a nested tuple type product, with attributes name (of type string), image (of type image-file), and differentSizes (a set type), consisting of a set of tuples with attributes: size (of type string), and price (of type string). product [ name: image: differentSizes: string; image-file; { [ size: price: string; string; ] } ] Fig. 9.4. An example nested type We can also define flat tuple and set types: If T1, T2, , Tn are basic types, then [T1, T2, …, Tn] is a flat tuple type; If T is a flat tuple type, then {T} is a flat set type. Classic flat relations are of flat set types. Nested relations are of arbitrary set types. Types can be represented as trees. A basic type Bi is a leaf tree or node; A tuple type [T1, T2, …, Tn] is a tree rooted at a tuple node with n subtrees, one for each Ti; A set type {T} is a tree rooted at a set node with one sub-tree. An instance of a type T is simply an element of dom(T). Clearly, instances can be represented as trees as well: An instance (constant) of a basic type is a leaf tree; A tuple instance [v1, v2, …, vn] forms a tree rooted at a tuple node with n children or sub-trees representing attribute values v1, v2, …, vn; A set instance {e1, e2, …, en} forms a set node with n children or subtrees representing the set elements e1, e2, …, and en. 328 9 Structured Data Extraction: Wrapper Generation An instance of a tuple type (also known as a tuple instance) is usually called a data record in the data extraction research. An instance of a set type (also known as a set instance) is usually called a list as in an actual Web page the data records in the set are presented in a particular order. An instance of a flat tuple type is called a flat data record (no nested lists), and an instance of a flat set type is called a list of flat data records. We note that attribute names are not included in the type tree. We next introduce a labeling of a type tree, which is defined recursively: If a set node is labeled , then its child is labeled .0, a tuple node; If a tuple node is labeled , then its n children are labeled .1, …, .n. We can think of labels as abstract names for types or attributes. For example, in Fig. 9.4 the top level tuple type is “product”, its three children are attributes: product.name, product.image, and product.differentSizes. .0 labels a tuple node without a name of two attributes, “size” and “price”. 9.1.3 HTML Mark-Up Encoding of Data Instances In a Web page, the data is encoded or formatted with HTML mark-up tags. This sub-section discusses the encoding of data instances in the above abstract data model using HTML tags. Web pages are written in HTML consisting of plain texts, tags and links to image, audio and video files, and other pages. Most HTML tags work in pairs. Each pair consists of an open tag and a close tag indicated by < > and </> respectively. Within each corresponding tag-pair, there can be other pairs of tags, resulting in nested structures. Thus, HTML tags can naturally encode nested data. We note the following: 1. There are no designated tags for each type as HTML was not designed as a data encoding language. Any HTML tag can be used for any type. 2. For a tuple type, values (data items) of different attributes are usually encoded differently to distinguish them and to highlight important items. 3. A tuple may be partitioned into several groups or sub-tuples. Each group covers a disjoint subset of attributes and may be encoded differently. Based on these characteristics of the HTML language, the HTML mark-up encoding of instances is defined recursively below. We encode based on the type tree, where each node of the tree is associated with an encoding function, which will encode (or mark-up) all the instances of the type in the same way. We will use the tuple type and its attributes explicitly because values of different attributes in the tuple type are typically encoded differently. We use T.i to represent a value instance of the tuple type T and 9.1 Preliminaries 329 attribute i. We use enc to denote an abstract encoding function. For a leaf node of a basic type labeled , an instance c is encoded with enc( :c) = OPEN-TAGS c CLOSE-TAGS where OPEN-TAGS is a sequence of open HTML tags, and CLOSETAGS is the sequence of corresponding close HTML tags. The number of tags is greater than or equal to 0. For a tuple node labeled of n children or attributes, [ .1, …, .n], the attributes are first partitioned into h ( 1) groups < .1, …, .e>, < .(e+1),…, .g> … < .(k+1), …, .n> and an instance [v1, …, vn] of the tuple node is encoded with enc( :[v1, …, vn]) = OPEN-TAGS1 enc(v1) … enc(ve) CLOSE-TAGS1 OPEN-TAGS2 enc(ve+1)…enc(vg) CLOSE-TAGS2 … OPEN-TAGSh enc(vk+1)…enc(vn) CLOSE-TAGSh where OPEN-TAGSi is a sequence of open HTML tags, and CLOSETAGSi is the sequence of corresponding close tags. The number of tags is greater than or equal to 0. For a set node labeled , an non-empty set instance {e1, e2, …, en} is encoded with enc( :{e1, …, en}) = OPEN-TAGS enc(ej1)…enc(ejn) CLOSE-TAGS, where OPEN-TAGS is a sequence of open HTML tags, and CLOSETAGS is the sequence of corresponding close HTML tags. The number of tags is greater than or equal to 0. The set elements are ordered based on an ordering function <. With ordering, a set instance is called a list. An empty set instance is encoded with OPEN-TAGS CLOSE-TAGS. By no means does this mark-up encoding cover all cases in Web pages. In fact, each group of a tuple type can be further divided. Anyway, you get the idea. We should also note that in an actual Web page the encoding is usually done not only by HTML tags, but also by words and punctuation marks. For example, in Fig. 9.5, if we are interested in extracting the addresses and the area codes, the punctuation marks are useful. Fig. 9.5. Words and punctuation marks are also used in data encoding 330 9 Structured Data Extraction: Wrapper Generation 9.2 Wrapper Induction We are now ready to study the first approach to data extraction, namely wrapper induction, which is based on supervised learning. A wrapper induction system learns data extraction rules from a set of labeled training examples. Labeling is usually done manually, which simply involves marking the data items in the training pages/examples that the user wants to extract. The learned rules are then applied to extract target data from other pages with the same mark-up encoding or the same template. The algorithm discussed in this section is based on the Stalker system [399]. Related work includes WIEN [296], Softmealy [244], WL2 [108], the system in [250], etc. The next section describes a different learning approach, which is based on the IDE system given in [599]. Stalker models the Web data as nested relations. Let us model the restaurant page in Fig. 9.5. It has four addresses in four different cities. The type tree of the data is given in Fig. 9.6 (the country code is omitted). For each type, we also added an intuitive label. The wrapper uses a tree structure based on this to facilitate extraction rule learning and data extraction. Tuple: Restaurant Set: Addresses String: Name Tuple: Address String: Street String: Integer: City Area-Code String: Phone-No. Fig. 9.6. Type tree of the restaurant page in Fig. 9.5 Below, we first introduce the data extraction process, and then describe the learning algorithm for generating extraction rules. 9.2.1 Extraction from a Page A Web page can be seen as a sequence of tokens S (e.g., words, numbers and HTML tags). The extraction is done using a tree structure called the EC tree (embedded catalog tree), which models the data embedding in a HTML page. The EC tree is based on the type tree above. The root of the tree is the document containing the whole token sequence S of the page, and the content of each child node is a subsequence of the sequence of its parent node. To extract a node of interest, the wrapper uses the EC description of the page and a set of extraction rules. Figure 9.7 shows the EC tree of the page in Fig. 9.5. Note that we use LIST here because the set of ad- 9.2 Wrapper Induction 331 dresses are already ordered in a page. For an extraction task, the EC tree for a data source is specified by the user (not discovered by the system). Page Name LIST (Addresses) Address Street City Area-Code Phone-No. Fig. 9.7. The EC tree of the HTML page in Fig. 9.5 For each node in the tree, the wrapper identifies or extracts the content of the node from its parent, which contains the sequence of tokens of all its children. Each extraction is done using two rules, the start rule and the end rule. The start rule identifies the beginning of the node and the end rule identifies the end of the node. This strategy is applicable to both leaf nodes (which represent data items) and list nodes. For a list node, list iteration rules are needed to break the list into individual data records (tuple instances). To extract items from the data records, data extraction rules are applied to each record. All the rules are learned during wrapper induction, which will be discussed in Sect. 9.2.2. Given the EC tree and the rules, any node can be extracted by following the tree path P from the root to the node by extracting each node in P from its parent. The extraction rules are based on the idea of landmarks. Each landmark is a sequence of consecutive tokens and is used to locate the beginning or the end of a target item. Let us use the example in Fig. 9.5 to introduce extraction rules and the extraction process based on the EC tree (Fig. 9.7). Figure 9.8 shows the HTML source code of the page in Fig. 9.5. 1: 2: 3: 4: 5: <p> Restaurant Name: <b>Good Noodles</b><br><br> <li> 205 Willow, <i>Glen</i>, Phone 1-<i>773</i>-366-1987</li> <li> 25 Oak, <i>Forest</i>, Phone (800) 234-7903 </li> <li> 324 Halsted St., <i>Chicago</i>, Phone 1-<i>800</i>-996-5023 </li> <li> 700 Lake St., <i>Oak Park</i>, Phone: (708) 798-0008 </li> </p> Fig. 9.8. The HTML source of the page in Fig. 9.5 Let us try to extract the restaurant name “Good Noodles”. The following rule can be used to identify the beginning of the name: R1: SkipTo(<b>) This rule means that the system should start from the beginning of the page and skip all the tokens until it sees the first <b> tag. <b> is a landmark. 332 9 Structured Data Extraction: Wrapper Generation Obviously, SkipTo(:) and SkipTo(<i>) will not work. According to the EC tree in Fig. 9.7, R1 is applied to the parent of node name, which is the root node. The root node contains the token sequence of the whole page. Similarly, to identify the end of the restaurant name, we can use: R2: SkipTo(</b>) R2 is applied from the end of the page toward the beginning. R1 is called the start rule and R2 is called the end rule. Note that a rule may not be unique. For example, we can also use the following rules (and many more) to identify the beginning of the name: R3: SkiptTo(Name _Punctuation_ _HtmlTag_) or R4: SkiptTo(Name) SkipTo(<b>) R3 means that we skip everything till the word “Name” followed by a punctuation symbol and then a HTML tag. In this case, “Name _Punctuation_ _HtmlTag_” together is a landmark. _Punctuation_ and _HtmlTag_ are called wildcards. A wildcard represents a class of tokens. For example, _HtmlTag_ represents any HTML tag, i.e., any HTML tag matches the wildcard _HtmlTag_. R4 means that we skip everything till the word “Name” and then again skip everything till the tag <b>. Since wrapper induction algorithms find simple rules first, R1 will be produced. Now, suppose that we also want to extract each area code. The wrapper needs to perform the following steps: 1. Identify the entire list of addresses. We can use the start rule SkipTo(<br><br>), and the end rule SkipTo(</p>). 2. Iterate through the list (lines 2-5 in Fig. 9.8) to break it into four individual records. To identify the beginning of each address, the wrapper can start from the first token of the parent and repeatedly apply the start rule SkipTo(<li>) to the content of the list. Each successive identification of the beginning of an address starts from where the previous one ends. Similarly, to identify the end of each address, it starts from the last token of its parent and repeatedly apply the end rule SkipTo(</li>). Once each address record is identified or extracted, we can extract the area code in it. Due to variations in the format of area codes (some are in italic and some are not), we need to use disjunctions. In this case, the disjunctive start and the end rules are respectively R5 and R6: R5: either SkipTo( ( ) or SkipTo(-<i>) R6: either SkipTo( ) ) or SkipTo(</i>) In a disjunctive rule, the disjuncts are applied sequentially until a disjunct can identify the target node. 9.2 Wrapper Induction E1: E2: E3: E4: 333 205 Willow, <i>Glen</i>, Phone 1-<i>773</i>-366-1987 25 Oak, <i>Forest</i>, Phone (800) 234-7903 324 Halsted St., <i>Chicago</i>, Phone 1-<i>800</i>-996-5023 700 Lake St., <i>Oak Park</i>, Phone: (708) 798-0008 Fig. 9.9. Training examples: four addresses with labeled area codes Finally, we summarize the data extraction features of Stalker. 1. Extraction is done hierarchically based on the EC tree, which enables extraction of items at any level of the hierarchy. 2. The extraction of each node is independent of its siblings. No contextual or ordering information of siblings is used in extraction or rule learning. 3. Each extraction is done using two rules, the start rule and the end rule. Each rule consists of an ordered list of disjuncts (could be one). 9.2.2 Learning Extraction Rules We now present the wrapper learning algorithm for generating extraction rules. The basic idea is as follows: To generate the start rule for a node in the EC tree, some prefix tokens or their wildcards of the node are identified as the landmarks that can uniquely identify the beginning of the node. To generate the end rule for a node, some suffix tokens or their wildcards of the node are identified as the landmarks. The rule generation process for the start rule and the end rule is basically the same. Their applications are also similar except that to apply a start rule the system starts by consuming the first token in the sequence of the parent and goes towards the last token, while for an end rule the system starts from the last token in the sequence of the parent and goes towards the first. Without loss of generality, in this section, we will discuss only the generation of start rules. For rule learning, the user first marks or labels the target items that need to be extracted in a few training examples. For instance, we have the examples in Fig. 9.8, which are addresses from the page in Fig. 9.5. Suppose we want to generate rules to extract the area code from each address. The area codes are labeled (marked) as in Fig. 9.9. A graphic user interface can make the labeling process very easy. Given a set of labeled training examples E, the learning algorithm should generate extraction rules that extract all the target items (also called positive items) without extracting any other items (called negative items). Learning is done based on the machine learning method, sequential covering (see Sect. 3.4.1). The algorithm is given in Fig. 9.10. In each iteration, the algorithm LearnRule() (Fig. 9.10) generates a perfect dis- 334 9 Structured Data Extraction: Wrapper Generation junct that covers as many positive items as possible and does not cover any negative item in E (Examples). Then, all the examples whose positive items are covered by the rule are removed. The next iteration starts. The input to LearnRule() is E. Once all the positive items are covered, the rule is returned (line 6), which consists of an ordered list of learned disjuncts. The function LearnDisjunct() performs the actual generation of perfect disjuncts (Fig. 9.11). It works as follows: It first chooses a Seed example (line 1), which is the shortest example. In the case of Fig. 9.9, it is E2. It then generates the initial candidate disjuncts. Let us explain using a generic Seed, which can be represented as follows: t1 t2 … tk <target item> tk+1 tk+2 … tn, where ti is a token and <target item> is a labeled target item. We call t1 t2 … tk the prefix sequence of the target item, and tk+1tk+2…tn the suffix sequence of the target item. The initial candidate disjuncts for the start rule are tk, and its matching wildcards. Let us use seven wildcards, _Numeric_, _AlphaNum_, _Alphabetic_, _Capitalized_, _AllCaps_, _HtmlTag_, and _Punctuation_. Their meanings are self-explanatory. For the example E2 of Fig. 9.9, the following candidate disjuncts are generated: D1: SkipTo( ( ) D2: SkipTo(_Punctuation_) In line 4 of LearnDisjunct(), the function BestDisjunct() selects the best disjunct using a set of heuristics given in Fig. 9.13. In this case, D1 is selected as the best disjunct. D1 is a perfect disjunct, i.e., it only covers positive items in E2 and E4 but not any negative items in E. D1 is returned from LearnDisjunct(), which also ends the first iteration of LearnRule(). E2 and E4 are removed (line 4) of Fig. 9.10. The next iteration of LearnRule() is left with E1 and E3. LearnDisjunct() will select E1 as Seed as it is shorter. Two candidates are then generated: D3: SkipTo( <i> ) D4: SkipTo( _HtmlTag_ ) Both these two candidates match early in the uncovered examples, E1 and E3. Thus, they cannot uniquely locate the positive items. Even worse, they can match to negative items in the two already covered examples, E2 and E4. Refinement is thus needed, which aims to specialize a disjunct by adding more terminals (a token or one of its matching wildcards) to it. We hope the refined version will be able to uniquely identify the positive items in some examples without matching any negative item in any example in E. Two refinement strategies are used: 9.2 Wrapper Induction 335 Algorithm LearnRule(Examples) // Examples: training examples // Rule: the returned rule 1 Rule 2 while Examples do 3 Disjunct LearnDisjunct(Examples); 4 remove all examples in Examples covered by Disjunct; 5 add Disjunct to Rule 6 return Rule Fig. 9.10. The main learning algorithm based on sequential covering Function LearnDisjunct(Examples) 1 let Seed Examples be the shortest example; 2 Candidates GetInitialCandidates(Seed); 3 while Candidates do 4 D BestDisjunct(Candidates); 5 if D is a perfect disjunct then 6 return D 7 Candidates Candidates Refine(D, Seed); 8 remove D from Candidates; 9 return D Fig. 9.11. Learning disjuncts Function Refine(D, Seed) 1 D is a consecutive landmarks (l0, l1, …, ln); // li is in fact SkipTo(li) 2 TopologyRefs LandmarkRefs ; // t0 or t1 below may be null 3 for i = 1 to n do 4 for each sequence s = t0 li t1 before the target item in Seed do 5 LandmarkRefs LandmarkRefs {(l0, …, li 1, t0 li, …, ln)} {(l0, …, li 1, x li, …, ln) | x is a wildcard that matches t0} {(l0, …, li t1, li+1, …, ln)} {(l0, …, li x, li+1, …, ln) | x is a wildcard that matches t1} 6 for each token t between li 1 and li before the target item in Seed do 7 ToplogyRefs TopologyRefs {(l0, …, li, t, li+1, …, ln)} {(l0, …, li, x, li+1, …, ln)} | x is a wildcard that matches t} 8 return TopologyRefs LandmarkRefs Fig. 9.12. Refining a disjunct to generate more specialized candidates BestDisjunct () prefer candidates that have: - more correct matches - accepts fewer false positives - fewer wildcards - longer end-landmarks Fig. 9.13. Choosing the best disjunct 336 9 Structured Data Extraction: Wrapper Generation D7: SkipTo(205) SkipTo(<i>) D8: SkipTo(Willow) SkipTo(<i>) D9: SkipTo(,) SkipTo(<i>) D10: SkipTo(<i>) SkipTo(<i>) D11: SkipTo(Glen) SkipTo(<i>) D12: SkipTo(1) SkipTo(<i>) D13: SkipTo(-) SkipTo(<i>) D14: SkipTo(Phone) SkipTo(<i>) D15: SkipTo(_Numeric_) SkipTo(<i>) D16: SkipTo(_Alphabetic_) SkipTo(<i>) D17: SkipTo(_Punctuation_) SkipTo(<i>) D18: SkipTo(_HtmlTag_) SkipTo(<i>) D19: SkipTo(_Capitalized_) SkipTo(<i>) D20: SkipTo(_AlphaNum_) SkipTo(<i>) D21: SkipTo(</i>) SkipTo(<i>) Fig. 9.14. All 15 topology refinements of D3 1. Landmark refinement (lines 4-5 in Fig. 9.12): Increase the size of a landmark li by concatenating a terminal (a token t0 or t1, and its matching wildcards) at the beginning or at the end of li. If t0 or t1 does not exist, it will not be considered. We note that each landmark li in the algorithm in Fig. 9.12 actually represents the SkipTo(li). 2. Topology refinement (lines 6-7 in Fig. 9.12): Increase the number of landmarks by adding 1-terminal landmarks, i.e., t and its matching wildcards. Note that l0 is not a landmark, which is used to simplify the algorithm presentation. It represents the beginning of the Seed example. Let us go back to our running example. D3 is selected as the best disjunct (line 4 of Fig. 9.11). Clearly, D3 is not a perfect disjunct. Then, refinement is carried out. Landmark refinement produces the following candidates: D5: SkipTo( - <i>) D6: SkipTo( _Punctuation_ <i>) Topology refinement produces the 15 candidates in Fig. 9.14. We can already see that D5, D10, D12, D13, D14, D15, D18 and D21 match correctly with E1 and E3 and fail to match on E2 and E4. Using the heuristics in Fig. 9.13, D5 is selected as the final solution as it has longest last landmark (- <i>). D5 is then returned by LearnDisjunct(). It is possible that no perfect disjunct can be found after all possible refinements have been tried. In this case, an imperfect best disjunct will be returned (line 9 in Fig. 9.11). Since all the examples are covered, LearnRule() ends and returns the disjunctive (start) rule “either D1 or D5”, i.e., R7: either SkipTo( ( ) or SkipTo(- <i>) In summary, we note the following: 1. The algorithm presented in this section is by no mean the only possible algorithm. Many variations are possible. Of course, there are also many other entirely different algorithms for wrapper induction. 9.2 Wrapper Induction 337 2. In our discussion above, we used only the SkipTo() function in extraction rules. However, in some situations it may not be sufficiently expressive. Therefore, other functions may be added. For example, Stalker also has the SkipUntil() function. Its argument is a part of the target item to be extracted, and is not consumed when the rule is applied. That is, the rule stops right before its occurrence. 9.2.3 Identifying Informative Examples One of the important issues in wrapper learning is the manual labeling of training examples. To ensure accurate learning, a large number of training examples are needed. To manually label them is labor intensive and time consuming. The question is: is it possible to automatically select (unlabelled) examples that are informative for the user to label? Clearly, examples of the same format are of limited use. Examples that represent exceptions are informative as they are different from already labeled examples. Active learning is an approach that helps identify informative unlabeled examples automatically. Given a set of unlabeled examples U, the approach works as follows in the wrapper induction context: 1. Randomly select a small subset L of unlabeled examples from U 2. Manually label the examples in L, and U = U L; 3. Learn a wrapper W based on the labeled set L; 4. Apply W to U to find a set of informative examples L; 5. Stop if L = , otherwise go to step 2. The key is to find informative examples in step 4. In [400], Muslea et al. proposed a method, called co-testing, to identify informative examples. The idea of co-testing is simple. It exploits the fact that there are often multiple ways of extracting the same item. Thus, the system can learn different rules, forward and backward rules, to locate the same item. Let us use learning of start rules as an example. The rules learned in Sect. 9.2.2 are called forward rules because they consume tokens from the beginning of the example to the end. In a similar way, we can also learn backward rules that consume tokens from the end of the example to the beginning. Given an unlabeled example, both the forward rule and backward rule are applied. If the two rules disagree on the beginning of a target item in the example, this example is given to the user to label. The intuition behind is simple. When the two rules agree, the extraction is very likely to be correct. When the two rules do not agree on the example, one of them must be wrong. By giving the user the example to label, we obtain a labeled informative training example. 338 9.2.4 9 Structured Data Extraction: Wrapper Generation Wrapper Maintenance Once a wrapper is generated, it is applied to other Web pages that contain similar data and are formatted in the same ways as the training examples. This introduces new problems. 1. If the site changes, does the wrapper know the change? This is called the wrapper verification problem. 2. If the change is correctly detected, how to automatically repair the wrapper? This is called the wrapper repair problem. One way to deal with both problems is to learn the characteristic patterns of the target items, which are then used to monitor the extraction to check whether the extracted items are correct. If they are incorrect, the same patterns can be used to locate the correct items assuming that the page changes are minor formatting changes. This is called re-labeling. After relabeling, re-learning is performed to produce a new wrapper. These two tasks are very difficult because contextual and/or semantic information is often needed to detect changes and to find the new locations of the target items. Wrapper maintenance is still an active research area. 9.3. Instance-Based Wrapper Learning The wrapper induction method discussed in the previous section requires a set of labeled examples to learn extraction rules. Active learning may be applied to identify informative examples for labeling to reduce the manual labeling effort. In this section, we introduce an instance-based learning approach to wrapper building, which does not learn extraction rules. Instead, it extracts target items in a new instance/page by comparing their prefix and suffix token strings with those of the corresponding items in the labeled examples. At the beginning, the user needs to label only a single example, which is then used to identify target items from unlabeled examples. If some item in an unlabeled example cannot be identified, it is sent for labeling, which is active learning but with no additional mechanism. Thus, in this approach the user labels only a minimum number of training examples. The method described here is based on the IDE algorithm in [599], which is given in Fig. 9.15. It consists of three steps: 1. A random example p from a set of unlabeled training examples S is selected for labeling (line 1). The examples here can be a set of detail pages or a set of data records identified from list pages. We will see in Sect. 9.8 that data records in list pages can be identified automatically. 9.3. Instance-Based Wrapper Learning 1. 2. 3. 4. 5. 6. 7. 8. 9. 339 Algorithm IDE(S) // S is the set of unlabeled examples. p randomSelect(S); // Randomly select a page p from S // the user labels the page p Tp labeling(p); Templates Tp ; // initialization for each remaining unlabeled example d in S do if (extract(Templates, d)) then Td labeling(d); insert Td into Templates end-if end-for Fig. 9.15. The IDE algorithm 2. The user labels/marks the target items in the selected example p (line 2). The system also stores a sequence of k consecutive tokens right before each labeled item (called the prefix string of the item) and a sequence of k consecutive tokens right after the labeled item (called the suffix string of the item). The prefix and suffix strings of all target items form a template. Storing the prefix and suffix strings is to avoid keeping the whole page in memory. The value of k does not affect the extraction result. If it is too small, the algorithm can always get more tokens from the original page. In practice, we can give k a large number, say 30, so that the system does not have to refer back to the original page during extraction. The variable Templates keeps all templates (line 3). 3. The algorithm then starts to extract items from unlabeled examples (line 4–9) using the function extract() (line 5). For each unlabeled example d, it compares the stored prefix and suffix strings of each target item with the token string of d to identify its corresponding item. If some item from d cannot be identified, d is passed to the user for labeling (line 6) (which is active learning), i.e., d is an informative example. Let us use an example to show what a template looks like. For example, in the page of Fig. 9.1, we are interested in extracting three items from each product, namely, name, image, and price. The template (Tj) for a labeled example j is represented with: Tj = patname, patimage, patprice Each pati in Tj consists of a prefix string and a suffix string of the item i. For example, if the product image is embedded in the following source: … <table><tr><td> <img> </td><td></td> … then we have (we use k = 3 and regard each HTML tag as a token) patimg = (img, prefix: <table><tr><td> , suffix: </td><td></td> ). 340 9 Structured Data Extraction: Wrapper Generation prefix: <table> <tr> <td> <td>(23) <i>(17) <i>(24) <i>(67) <b> price <b>(10) <b>(18) <b>(25) <b>(68) 1 HTML source of the <i> 3 4 …<td> <font> <b>……<td> <font> <i> <b> … 8 9 10 15 16 17 18 … <tr> <td> <i> <b> $25.00 …... 22 23 24 25 ... <br> <font> <i> <b> 65 66 67 68 Fig. 9.16. The price is found uniquely. Extract(Templates, d) function: For each unlabeled example d, extract() tries to use each saved template T ( Templates) to match with the token string of d to identify every target item in d. If a sequence of prefix (and respectively suffix) tokens of a target item g in T matches a sequence of prefix (and suffix) tokens of an item f in d that uniquely identifies f in d, f is regarded as g’s corresponding item in d. By “uniquely identifies”, we mean that only item f in d matches g based on their prefix and suffix strings. An example is given below. After item f, which corresponds to item g in T, is identified and extracted from d, we use the token strings of d before f and after f to find the remaining target items using the same template T. This process continues until all the corresponding items of those items in T are identified from d. If the corresponding item of an item in T cannot be uniquely identified from d, then the extraction using T fails on d. The next template in Templates is tried. If every template in Templates fails on d, d is sent to the user for labeling (line 6 of Fig. 9.15). The algorithm is fairly straightforward, and thus is omitted. See [599] for more details, which also discusses how to deal with some additional issues, e.g., missing items in a page. Fig. 9.16 gives an example to show how a target item is uniquely identified. Assume that 5 tokens <table><tr><td><i><b> are saved in the prefix string of item price from a labeled example. Given an unlabeled example, after scanning through its token string, we obtain the match situation in Fig. 9.16. That is, we find 4 <b>’s, three <i><b> together, and only one <td><i><b> together, which can match some prefix tokens of price. These are shown in four rows below the saved prefix string. The number within each “( )” is the sequence id of the token (the tag) in the unlabeled example. “ ” means no match. The HTML source is given in the box of Fig. 9.16 with sequence id’s attached. We observe that the beginning of price is 9.4 Automatic Wrapper Generation: Problems 341 uniquely identified because the sequence of prefix tokens of price, <td><i><b>, has only one match. Note that we do not need to use all the saved tokens in the prefix string of price. This technique is thus called sufficient match. We see that <b> is not unique because there are 4 <b>’s. <i><b> is not unique because there are 3 matches. Once the beginning of item price is found, the algorithm tried to locate the ending of item price in the same way by comparing suffix strings in the opposite direction. After item price is identified and extracted, the algorithm goes to identify other items if they are not extracted. The final set of templates and the extract() function together form a wrapper, which can be used to extract target items from future examples. Apart from performing active learning automatically, there are two other interesting features about IDE. Firstly, there is no pre-specified sequence of items to be extracted. For example, the order of items in the HTML source may be: name, price, and image. If at the beginning we can identify item price uniquely in the unlabeled example, we can then start from price and search forward to find item image and search backward to find item name. The final extraction sequence of items may be price, image and name. Secondly, the method exploits local contexts in extraction. It may be the case that from the whole page/data record we are unable to identify a particular item. However, within a local area, it is easy to identify the item. For instance, in the above example, after identifying item price, we only need to search for item image in the rest of the input. Even a similar item appears before price, it will not be considered. Evaluation results in [599] show that this simple technique works very well. 9.4 Automatic Wrapper Generation: Problems Wrapper generation using supervised learning has two main shortcomings: 1. It is not suitable for a large number of sites due to the manual labeling effort. For example, if a shopping site wants to extract all the products sold on the Web, manual labeling becomes almost an impossible task. 2. Wrapper maintenance is very costly. The Web is a dynamic environment. Sites change constantly. Since wrapper learning systems mainly rely on HTML formatting tags, if a site changes its formatting templates, the existing wrapper for the site will become invalid. As we discussed earlier, automatic verification and repair are still difficult. Doing them manually is very costly if the number of sites involved is large. Due to these problems, automatic (or unsupervised) extraction has been studied by researchers. Automatic extraction is possible because data re- 342 9 Structured Data Extraction: Wrapper Generation cords (tuple instances) in a Web site are usually encoded using a very small number of fixed templates. It is possible to find these templates by mining repeated patterns in multiple data records. The rest of the chapter focuses on automatic extraction. Note that in general we use the term “templates” to refer to hidden templates employed by Web page designers. We use the term “patterns” to refer to regular structures that the system has discovered. 9.4.1 Two Extraction Problems In Sects. 9.1.2 and 9.1.3, we described an abstract model of structured data on the Web (i.e., nested relations), and a HTML mark-up encoding of the data model respectively. The general goal of data extraction is to recover the hidden schema from the HTML mark-up encoded data. In the rest of the chapter, we focus on two problems, which are really quite similar. Problem 1: Extraction Based on a Single List Page Input: A single HTML string S, which contains k non-overlapping substrings s1, s2, …, sk with each si encoding an instance of a set type. That is, each si contains a collection Wi of mi ( 2) non-overlapping subsubstrings encoding mi instances of a tuple type. Output: k tuple types 1, 2, …, k, and k collections C1, C2, …, Ck of instances of the tuple types such that for each collection Ci there is a HTML encoding function enci such that enci: Ci Wi is a bijection. We use the example in Fig. 9.1 to explain. The input string S is the full Web page (only part of it is shown in Fig. 9.1). In this page, there are two substrings s1 and s2 that encode two set instances, i.e., the two sets of data records. s1 consists of four encodings (displayed horizontally) enc1(I1), enc1(I2), enc1(I3), enc1(I4) of four product instances I1, I2, I3, I4 of a tuple type 1, according to some mark-up encoding function enc1. Similarly, s2 consists of encodings of some other products (displayed vertically). One important note is that S often contains some other information (not shown in Fig. 9.1) apart from the encoded data. An algorithm needs to work on the string S to find each substring and construct the tuple type by generating a pattern from each substring representing the mark-up encoding function enci. The pattern may be represented as a regular expression. Data extraction can be done using the regular expression or the original pattern as we will see in Sect. 9.11.1. 9.4 Automatic Wrapper Generation: Problems 343 Problem 2: Extraction Based on Multiple Pages Input: A collection W of k HTML strings, which encodes k instances of the same type. Output: A type , and a collection C of instances of type , such that there is a HTML encoding enc such that enc: C W is a bijection. The input consists of a collection of k encodings enc(I1), enc(I2), …, enc(Ik) of instances I1, I2, …, Ik of a nested type , according to some mark-up encoding function enc. An algorithm works on the encoded instances and constructs the type by generating a pattern (the encoding function enc), which again may be represented as a regular expression and used to extract data from other pages. Note that, for this problem, the input may be a set of detail pages (of a tuple type) or list pages (of a set type). The next few sections describe several techniques to solve the two problems. As we will see in Sect. 9.10, most techniques for solving problem 1 can also be used for solving problem 2. 9.4.2 Patterns as Regular Expressions A regular expression can be naturally used to model the HTML encoded version of a nested type. Given an alphabet of symbols and a special token “#text” that is not in , a regular expression over is a string over {#text, *, ?, |, (, )} defined as follows: The empty string and all elements of {#text} are regular expressions. If A and B are regular expressions, then AB, (A|B) and (A)? are regular expressions, where (A|B) stands for A or B and (A)? stands for (A| ). If A is a regular expression, (A)* is a regular expression, where (A)* stands for or A or AA or ... We also use (A)+ as a shortcut for A(A)*, which can be used to model the set type of a list of tuples. (A)? indicates that A is optional. (A|B) represents a disjunction. If a regular expression does not include (A|B), it is called a union-free regular expression. Regular expressions are often employed to represent extraction patterns (or encoding functions). However, extraction patterns do not have to be regular expressions, as we will see later. Given a regular expression, a nondeterministic finite-state automaton can be constructed and employed to match its occurrences in string sequences representing Web pages. In the process, data items can be extracted, which are text strings represented by #text. 344 9 Structured Data Extraction: Wrapper Generation 9.5 String Matching and Tree Matching As we can see from both problems in Sect. 9.4.1, the key is to find the encoding template from a collection of encoded instances of the same type. A natural way to do this is to detect repeated patterns from HTML encoding strings. String matching and tree matching are obvious techniques for the task. Tree matching is useful because HTML encoding strings also form nested structures due to their nested HTML tags. Such nested structures can be modeled as trees, commonly known as DOM (tag) trees. DOM stands for Document Object Model (http://www.w3.org/DOM/). Below we describe some string matching and tree matching algorithms. 9.5.1 String Edit Distance String edit distance (also known as Levenshtein distance) is perhaps the most widely used string matching/comparison technique. The edit distance of two strings, s1 and s2, is defined as the minimum number of point mutations required to change s1 into s2, where a point mutation is one of: (1) change a character, (2) insert a character, and (3) delete a character. Assume we are given two strings s1 and s2. The following recurrence relations define the edit distance, d(s1, s2), of two strings s1 and s2: d( , ) = 0 // represents an empty string d(s, ) = d( , s) = |s| // |s| is the length of string s d(s1–+c1, s2–+c2) = min(d(s1–, s2–) + p(c1, c2), d(s1–+c1, s2–) + 1, d(s1–, s2–+c2) + 1), where c1 and c2 are the last characters of s1 (= s1–+c1) and s2 (= s2–+c2) respectively, and p(c1, c2) = 0 if c1 = c2; p(c1, c2) = 1, otherwise. The first two rules are obvious. Let us examine the last one. Since neither string is empty, each has a last character, c1 and c2 respectively. c1 and c2 have to be explained in an edit of s1–+c1 into s2–+c2. If c1 = c2, they match with no penalty, i.e., p(c1, c2) = 0, and the overall edit distance is d(s1–, s2–). If c1 c2, then c1 could be changed into c2, giving p(c1, c2) = 1 and an overall cost d(s1–, s2–)+1. Another possibility is to edit s1–+c1 into s2– and then insert c2, giving d(s1–+c1, s2–)+1. The last possibility is to delete c1 and edit s1– into s2–+c2, giving d(s1–, s2–+c2)+1. There are no other alternatives. We take the least expensive, i.e., min. of these alternatives. From the relations, we can see that d(s1, s2) depends only on d(s1 , s2 ) where s1 is a shorter string than s1, or s2 is a shorter string than s2, or both. Thus, the dynamic programming technique can be applied to compute the edit distance of two strings. 9.5 String Matching and Tree Matching 345 We can use a two-dimensional matrix, m[0..|s1|, 0..|s2|], to hold the edit distances. The low right corner cell m(|s1|, |s2|) will furnish the required value of the edit distance d(s1, s2). We have m[0, 0] = 0 m[i, 0] = i, i =1, 2, ..., |s1| m[0, j] = j, j =1, 2, ..., |s2| m[i, j] = min(m[i 1, j 1] + p(s1[i], s2[j]), m[i 1, j] + 1, m[i, j 1] + 1 ), where i = 1, 2, ..., |s1|, j = 1, 2, ..., |s2|, and p(s1[i], s2[j]) = 0 if s1[i] = s2[j]; p(s1[i], s2[j]) = 1, otherwise. Once the edit distance computation is completed, we can find the alignment of characters that give the final distance. For this, we need to record which case in the above recursive rule minimizes the distance, and then trace back the path that corresponds to the best alignment. Note that, in many cases, the minimal choice is not unique, and different paths could have been drawn, which indicate alternative optimal alignments. Example 1: We want to compute the edit distance and find the alignment of the following two strings: s1 : s2: XGYXYXYX XYXYXYTX The edit distance matrix is given in Fig. 9.17. The final edit distance value is 2, which is the value in the bottom right corner cell. Figure 9.17 also shows the trace back path. Notice that a diagonal line means match or change, a vertical line means insertion, and a horizontal line means deletion. Thus, the final alignment of our two strings is: s1 : s2: XGYXYXY X X YXYXYTX s2 X Y X Y X Y T X s1 X G Y X Y X Y X 0 12345678 101234567 211123456 322212345 433221234 544322123 655432212 766543322 877654332 Fig. 9.17. The edit distance matrix and back trace path 346 9 Structured Data Extraction: Wrapper Generation The time-complexity of the algorithm is O(|s1||s2|) (to fill the matrix). The space complexity is also O(|s1||s2|). Back trace takes O(|s1|+|s2|) time. The normalized edit distance ND(s1, s2) is defined as the edit distance divided by the mean length of the two strings: ND ( s1, s 2 ) d ( s1, s 2 ) (| s1 | . (1) | s 2 |) / 2 Another commonly used denominator is max(|s1|, |s2|). Finally, in data extraction, “change a character” may be undesirable (which represents a disjunction in regular expressions). A large distance may be used to disallow it. We will discuss this issue again in Sect. 9.11.2. 9.5.2 Tree Matching Like string edit distance, tree edit distance between two trees A and B (labeled ordered rooted trees) is the cost associated with the minimum set of operations needed to transform A into B. In the classic formulation, the set of operations used to define tree edit distance includes, node removal, node insertion, and node replacement. A cost is assigned to each operation. Solving the tree edit distance problem is to find a minimum-cost mapping between two trees. The concept of mapping is formally defined as: Let X be a tree and let X[i] be the ith node of tree X in a preorder walk of the tree. A mapping M between a tree A of size n1 and a tree B of size n2 is a set of ordered pairs (i, j), one from each tree, satisfying the following conditions for all (i1, j1), (i2, j2) M: (1) i1 = i2 iff j1 = j2; (2) A[i1] is on the left of A[i2] iff B[j1] is on the left B[j2]; (3) A[i1] is an ancestor of A[i2] iff B[j1] is an ancestor of B[j2]. Intuitively, the definition requires that each node appears no more than once in a mapping and the order among siblings and the hierarchical relation among nodes are preserved. Figure 9.18 shows a mapping example. Several algorithms have been proposed to address the problem of finding the minimum set of operations (i.e., the one with the minimum cost) to transform one tree into another. All the formulations have complexities above quadratic. In [509], a solution based on dynamic programming is presented with the complexity of O(n1n2h1h2), where n1 and n2 are the sizes of the trees and h1 and h2 are the heights of the trees. In [95, 532], two improved algorithms are presented, and in [605], it is shown that if the trees are not ordered, the problem is NP-complete. 9.5 String Matching and Tree Matching A p a p e b c d a 347 B h c d Fig. 9.18. A general tree mapping example In the above general setting, mapping can cross levels, e.g., node a in tree A and node a in tree B (Fig. 9.18). Replacements are also allowed, e.g., node b in A and node h in B. We now define a restricted tree mapping [572], called simple tree matching (STM), in which no node replacement and no level crossing are allowed. In STM, the aim is to find the maximum matching between two trees (not the edit distance of two trees). This restricted model has been found quite effective for Web data extraction. Let A and B be two trees, and i A and j B be two nodes in A and B respectively. A matching between two trees is defined to be a mapping M such that, for every pair (i, j) M where i and j are non-root nodes, (parent(i), parent(j)) M. A maximum matching is a matching with the maximum number of pairs. Let A = RA: A1, …, Ak and B = RB: B1,…, Bn be two trees, where RA and RB are the roots of A and B, and Ai and Bj are the ith and jth first-level subtrees of A and B respectively. Let W(A, B) be the number of pairs in the maximum matching of trees A and B. If RA and RB contain identical symbols, the maximum matching between A and B (i.e., W(A, B)) is m( A1, …, Ak , B1, …, Bn ) + 1, where m( A1, …, Ak , B1, …, Bn ) is the number of pairs in the maximum matching of A1, …, Ak and B1, …, Bn . If RA RB, W(A, B)) = 0. Formally, W(A, B) is defined as follows: W ( A, B ) 0 if R A RB ; m( A1 ,..., Ak , B1 ,..., Bn ) 1 otherwise m( , ) = 0 // represents an empty sub-tree list. m(s, ) = m( , s) = 0 // s matches any non-empty sub-tree list m( A1, …, Ak , B1, …, Bn ) = max(m( A1, …, Ak-1 , B1, …, Bn-1 ) + W(Ak, Bn), m( A1, …, Ak , B1, …, Bn-1 ), m( A1, …, Ak-1 , B1, …, Bn )). This definition of m is similar to that of the string edit distance except that here we compute the maximum matching rather than the distance and 348 9 Structured Data Extraction: Wrapper Generation Algorithm: STM(A, B) 1. if the roots of the two trees A and B contain distinct symbols then 2. return (0) 3. else k the number of first-level sub-trees of A; 4. n the number of first-level sub-trees of B; 5. Initialization: m[i, 0] 0 for i = 0, …, k; m[0, j] 0 for j = 0, …, n; 6. for i = 1 to k do 7. for j = 1 to n do 8. m[i, j] max(m[i, j 1], m[i 1, j], m[i 1, j 1]+W[i, j]), where W[i, j] STM(Ai, Bj) 9. end-for 10. end-for 11. return (m[k, n]+1) 12. end-if Fig. 9.19. The simple tree matching (STM) algorithm that W(Ak, Bn) needs to be computed recursively since Ak and Bn are subtrees. Clearly, the dynamic programming technique is again applicable. We now give an algorithm for simple tree matching (STM), which computes W(A, B). The algorithm is also called STM (Fig. 9.19). STM is a topdown algorithm. It evaluates the similarity by producing the maximum matching through dynamic programming. The algorithm has the complexity of O(n1n2), where n1 and n2 are the sizes of trees A and B respectively. In line 1, the roots of A and B are compared first. If the roots contain distinct symbols, then the two trees do not match at all. If the roots contain identical symbols, then the algorithm recursively finds the maximum matching between first-level sub-trees of A and B and save it in a W matrix (line 8). Based on the W matrix, a dynamic programming scheme is applied to find the number of pairs in a maximum matching between two trees A and B. We use an example (Fig. 9.20) to explain the algorithm. To find the maximum matching between trees A and B, their roots (N1 and N15) are compared first. Since N1 and N15 contain identical symbols, m1,15[4, 2]+1 is returned as the maximum matching value between trees A and B (line 11). The m1,15 matrix is computed based on the W1,15 matrix. Each entry in W1,15, e.g., W1,15[i, j], is the maximum matching between the ith and jth first-level sub-trees of A and B, which is computed recursively based on its m matrix. For example, W1,15[4, 2] is computed recursively by building the matrices (E)-(H). All the relevant cells are shaded. The zero column and row in m matrices are initializations. Note that we use subscripts for both m and W to indicate the nodes that they are working on. 9.5 String Matching and Tree Matching (A) (B) N1 p N2 b N3 c e e N5 c N4 b d N6 N7 f N8 N11 d d N9 N10 h N15 p N17 c N16 b g g e N18 N19 i N20 N22 (D) W1,15 0 1 (N16) 2 (N16-N17) 0 0 0 0 3 3 0 3 5 0 3 5 0 3 6 0 1 (N2) 2 (N2-N3) 3 (N2-N4) 4 (N2-N5) 0 0 0 1 (N20) 2 (N20-N21) 0 0 2 2 (G) m11,20 0 1 (N12) 2 (N12-N13) 3 (N12-N14) 1 (N2) 2 (N3) 3 (N4) 4 (N5) 1 (N16) 2 (N17) 3 0 0 2 2 0 0 3 (F) W5,17 (E) m5,17 0 1 (N11) f N21 h j N12 N13 N14 (C) m1,15 349 1 (N11) 1(N20) 2(N21) 2 0 (H) W11,20 0 0 0 0 0 1 (N22) 0 1 1 1 1 (N12) 2 (N13) 3 (N14) 1 (N22) 1 0 0 Fig. 9.20. (A) Tree A; (B) Tree B; (C) m matrix for the first level sub-trees of N1 and N15; (D) W matrix for the first level sub-trees of N1 and N15; (E)-(H) m matrixes and W matrixes for the lower level sub-trees. The normalized simple tree matching NSTM(A, B) is obtained by dividing the matching score by the mean number of nodes in the two trees: NSTM ( A, B ) STM ( A, B) (nodes( A) nodes( B )) / 2 . (2) We may also use max(nodes(A), nodes(B)) as the denominator. nodes(X) denotes the number of nodes in tree X. Similar to string edit distance, after matching computation, we can trace back in the m matrices to find the aligned nodes from the two trees. 350 9 Structured Data Extraction: Wrapper Generation 9.6 Multiple Alignment In order to find repeated patterns from HTML strings based on string edit distance or tree matching, we need alignments of strings and trees. We have discussed how to obtain the alignment of two strings or trees. However, a Web page usually contains more than two data records, thus more than two strings or trees need to be aligned. Producing a global alignment of all the strings or trees is crucial. The task is called multiple alignment. In [82], Carrillo and Lipman proposed an optimal multiple alignment based on multidimensional dynamic programming. However, its time complexity is exponential, and is thus not suitable for practical use. Many heuristic methods exist. We describe two of them: the center star method and the partial tree alignment method in [600]). 9.6.1 Center Star Method This is a classic technique [213]. It is commonly used for multiple string alignments, but can be adopted for trees. The method is applied to data extraction based on alignments of HTML strings in [91]. Let the set of strings to be aligned be S. In the method, a string sc that minimizes si S d ( s c , si ) (3) is first selected as the center string. d(sc, si) is the distance of two strings. The algorithm then iteratively computes the alignment of rest of the strings with sc. Spaces are added when needed. The algorithm is in Fig. 9.21. CenterStar(S) 1. choose the center star sc using Equation (3); 2. initialize the multiple sequence alignment M that contains only sc; 4. for each s in S-{sc} do 5. let c* be the aligned version of sc in M; 6. let s and c* be the optimally aligned strings of s and c*; 7. add aligned strings s and c* into the multiple alignment M; 8. add spaces to each string in M, except, s and c* , at locations where new spaces are added to c* 9. endfor 10. return multiple string alignment M Fig. 9.21. The center star algorithm Example 2: We have three strings, i.e., S = {ABC, XBC, XAB}. ABC is selected as the center string sc. Let us align the other strings with ABC. 9.6 Multiple Alignment 351 Iteration 1: Align c* (= sc) with s =XBC: c* : ABC | s: Update M: A B C Iteration 2: Align c* with s = XAB: c* : ABC | s: Update M: A B C XBC | XBC ABC XBC | XAB ABC XBC XAB Assume there are k strings in S and all strings have length n, finding the center takes O(k2n2) time and the iterative pair-wise alignment takes O(kn2) time. Thus, the overall time complexity is O(k2n2). For our data extraction task, this method has two shortcomings: 1. the algorithm runs slowly for pages containing many data records and/or data records containing many tags (i.e., long strings) because finding the center string needs O(k2n2) time. 2. if the center string (or tree) does not have a particular data item, other data records that contain the same data item may not be aligned properly. For example, the letter X’s in the last two strings (in bold) are not aligned in the final result, but they should. Let us discuss the second point further. As we mentioned in Sect. 9.5.1, giving the cost of 1 for “changing a letter” in edit distance is problematic (e.g., A and X in the first and second strings in the final result) because of optional data items in data records. The problem can be partially dealt with by disallowing “changing a letter” (e.g., giving it a very large cost). However, this introduces another problem. For example, if we align only ABC and XBC, it is not clear which of the following alignment is better. (1) A– BC – XBC (2) – ABC X– BC If we also consider the string XAB, then (2) is better. However, the center star method does not have this global view. The partial tree alignment algorithm described below deals with both problems nicely. 9.6.2 Partial Tree Alignment This method is proposed in [600] for multiple tree alignment in the context of data extraction. It can also be used for aligning multiple strings. For 352 9 Structured Data Extraction: Wrapper Generation simplicity, we describe the method in the context of trees. The main idea is as follows: The algorithm aligns multiple trees by progressively growing a seed tree. The seed tree, denoted by Ts, is initially picked to be the tree with the maximum number of data fields, which is similar to the center string but without the O(k2n2) pair-wise tree matching to choose it. The reason for choosing this seed tree is clear as it is more likely for this tree to have a good alignment with data fields in other data records. Then, for each Ti (i s), the algorithm finds for each node in Ti a matching node in Ts. If no match can be found for a node vi, then the algorithm attempts to expand the seed tree by inserting vi into Ts. The expanded seed tree Ts is then used in subsequent matching. The insertion is done only if a position for vi can be uniquely determined in Ts. Otherwise, it is left unmatched. Thus the alignment is partial. It represents a least commitment approach. Early uncertain commitments can result in undesirable effects for later matches. Note that although the method was designed originally for aligning multiple trees, it can also be adapted for aligning multiple strings. Partial Alignment of Two Trees Before presenting the full algorithm for aligning multiple trees, let us first look at the partial alignment of two trees. As indicated above, after Ts and Ti are matched, some nodes in Ti can be aligned with their corresponding nodes of Ts because they match one another. For those nodes in Ti that are not matched, we want to insert them into Ts as they may contain optional data items. There are two possible situations when inserting a new node vi from Ti into Ts, depending on whether a location in Ts can be uniquely determined to insert vi. Instead of considering a single node vi, we can consider each set of unmatched consecutive sibling nodes vj…vm from Ti together. Without loss of generality, we assume that the parent of vj…vm has a match in Ts and we want to insert vj…vm into Ts under the same parent node. We only insert vj…vm into Ts if a position for inserting vj…vm can be uniquely determined in Ts. Otherwise, they will not be inserted into Ts and left unaligned. The location for inserting vj…vm can be uniquely decided: 1. If vj…vm have two neighboring siblings in Ti, one on the right and one on the left, that are matched with two consecutive siblings in Ts. Figure 9.22(A) shows such a situation, which gives one part of Ts and one part of Ti. We can see that node c in Ti can be inserted into Ts between node b and node e in Ts because node b and node e in Ts and Ti match. The new (extended) Ts is also shown in Fig. 9.22(A). We note that nodes a, b, c and e may also have their own children. We did not draw them to save space. This applies to all the cases below. 9.6 Multiple Alignment 353 2. If vj…vm has only one left neighboring sibling x in Ti and x matches the right most node x in Ts, then vj…vm can be inserted after node x in Ts. Figure 9.22(B) illustrates this case. 3. If vj…vm has only one right neighboring sibling x in Ti and it matches the left most node x in Ts, then vj…vm can be inserted before node x in Ts. This case is similar to the second case above. Otherwise, we cannot uniquely decide a location for unmatched nodes in Ti to be inserted into Ts. This is shown in Fig. 9.22(C). The unmatched node x in Ti could be inserted into Ts in two positions, between nodes a and b, or between node b and e in Ts. In this situation, we will not insert it into Ts. Ti Ts p a e b Ts p e c b p e b a f e g New Ts p New Ts p a Ti p c b e b a e f g (B) (A) Ti p Ts p a b e a x e (C) Fig. 9.22. Expand the seed: (A) and (B) unique insertion; (C) insertion ambiguity Partial Alignment of Multiple Trees Figure 9.23 gives the full algorithm for multiple tree alignment based on partial alignment of two trees. S is the set of input trees. We use a simple example in Fig. 9.24 to explain the algorithm. S has three example trees. Lines 1–2 (Fig. 9.23) find the tree with the most data items. It is used as the seed tree Ts. In Fig. 9.24, the seed tree is the first tree (we omitted many nodes on the left of T1). Line 3 initializes R, which is used to store those trees that are not completely aligned with Ts in each iteration. Line 4 starts the while loop to align every other tree against Ts. Line 5 picks the next unaligned tree, and line 6 does the tree matching. Line 7 finds all the matched pairs by tracing the matrix results of line 6. This function is similar to aligning two strings using edit distance. In Fig. 9.24, Ts and T2 pro- 354 9 Structured Data Extraction: Wrapper Generation Algorithm PartialTreeAlignment(S) 1. Sort trees in S in descending order of the number of unaligned data items; 2. Ts the first tree (which is the largest) and delete it from S; 3. R ; 4. while (S ) do 5. Ti select and delete next tree from S; // follow the sorted order 6. STM(Ts, Ti); // tree matching 7. AlignTrees(Ts, Ti); // based on the result from line 6 8. if Ti is not completely aligned with Ts then 9. if InsertIntoSeed(Ts, Ti) then // True: some insertions are done 10. S S R; 11. R 12 endif; 13. if there are still unaligned items in Ti that are not inserted into Ts then 14. R R {Ti} 15. endif; 16. endif; 17. endwhile; 18. Output data fields from each Ti to a data table based on the alignment results. Fig. 9.23. The partial tree alignment algorithm duce one match, node b. Nodes n, c, k and g are not matched to Ts. Lines 8 and 9 attempt to insert the unmatched nodes into Ts. This is the partial tree alignment discussed above. In Fig. 9.24, none of the nodes w, c, k and g in T2 can be inserted into Ts because no unique location can be found. Thus, it will not pass the if-statement (InsertIntoSeed() returns false in line 9 of Fig. 9.23). Lines 13–14 inserts T2 into R, which is a list of trees that need to be re-matched since some data items are not aligned and not inserted into Ts. In Fig. 9.24, when matching T3 with Ts in the next iteration, all unmatched nodes c, h and k can be inserted into Ts (line 9). Since there are some insertions, we re-match those trees in R. Line 10 and line 11 put the trees in R into S and reinitializes R. T3 will not be inserted into R (line 13). In Fig. 9.24, T2 is the only tree in R, which will be matched to the new Ts in the next round. Now, every node in T2 can be matched or inserted, and the process completes. Line 18 of Fig. 9.23 outputs the data items from each tree according to the alignment produced. Note that if there are still un-matched nodes with data after the algorithm completes (e.g., R ), each un-matched data will occupy a single column by itself. Table 1 shows the data table for the trees in Fig. 9.24. We use “1” to indicate a data item. The complexity of the algorithm is O(k2n2), where k is the number of trees in S and n is the size of each tree (we assume that all the trees are of similar size). However, as reported in [600], in practice, the algorithm almost always goes through S only once (i.e., R = ). 9.6 Multiple Alignment Initial set S Ts = T1 p …x bwc d Ts p …x g k b c d h k No node inserted b d New Ts p S R. R contains only T2 c, h, and k are inserted …x T2 p bwc T3 p T2 p b 355 b c h k g d h k kg p …x bwc d Fig. 9.24. Iterative tree alignment with two iterations Table 1. Final data table (“1” indicates a data item) T1 T2 T3 … … x 1 b 1 1 1 w c 1 1 1 d 1 1 h k g 1 1 1 1 In fact, to make the algorithm complete, a recursive call should be added after line 17 in Fig. 9.23 to handle the case when R , i.e., to further align only those trees in R. The following three lines can be added: 18. if R then 19. PartialTreeAlignment(R) 20. endif This takes care of the situation where some items are not aligned and not inserted. However, it is shown in [600] that this part is usually not needed for data extraction. We make two remarks about this complete algorithm. First, the recursion will terminate even if no alignment and/or no insertion is made to the seed tree because the seed tree is deleted in each recursion and thus 356 9 Structured Data Extraction: Wrapper Generation R becomes smaller and smaller. Second, the algorithm can found multiple templates in the data. The seed tree from each recursion represents a different template. 9.7 Building DOM Trees DOM (Document Object Model) tree building from input pages is a necessary step for many data extraction algorithms. We describe two methods for building DOM trees, which are also commonly called tag trees (we will use them interchangeably in this chapter). Using Tags Alone: Most HTML tags work in pairs. Each pair consists of an open tag and a close tag (indicated by < > and </> respectively). Within each corresponding tag-pair, there can be other pairs of tags, resulting in a nested structure. Building a DOM tree from a page using its HTML code is thus natural. In the tree, each pair of tags is a node, and the nested pairs of tags within it are the children of the node. Two tasks need to be performed: 1. HTML code cleaning: Some tags do not require close tags (e.g., <li>, <hr> and <p>) although they have close tags. Hence, additional close tags should be inserted to ensure all tags are balanced. Ill-formatted tags also need to be fixed. Such error tags are usually close tags that cross different nested blocks, e.g., <tr> … <td> … </tr> … </td>, which can be hard to fix if multiple levels of nesting exist. There are open source programs that can be used to clean up HTML pages. One popular program is called tidy (available at http://tidy.sourceforge. net/). 2. Tree building: We can follow the nested blocks of the HTML tags in the page to build the DOM tree. It is fairly straightforward. We will not discuss it further. This method works for most pages. However, for some ill-formatted tags, even the tidy program cannot fix. Then, the constructed DOM trees may be wrong, which makes it difficult for subsequent data extraction. Using Tags and Visual Cues: Instead of analyzing the HTML code to fix errors, rendering or visual information (i.e., the locations on the screen at which tags are rendered) can be used to infer the structural relationship among tags and to construct a DOM tree. This method leads to more robust tree construction due to the high error tolerance of the rendering engines of Web browsers (e.g., Internet Explorer). As long as the browser is able to render a page correctly, its tag tree can be built correctly. In a Web browser, each HTML element (consisting of an open tag, optional attributes, optional embedded HTML content, and a close tag that 9.8 Extraction Based on a Single List Page: Flat Data Records 357 may be omitted) is rendered as a rectangle. The visual information can be obtained after the HTML code is rendered by a Web browser. A DOM tree can then be constructed based on the nested rectangles (resulted from nested tags). The details are as follows: 1. Find the four boundaries of the rectangle of each HTML element by calling the rendering engine of a browser, e.g., Internet Explorer. 2. Follow the sequence of open tags and perform containment checks to build the tree. Containment check means checking if one rectangle is contained in another. Let us use an example to illustrate the process. Assume we have the HTML code on the left of Fig. 9.25. However, there are three errors in the code. The close tag </td> for line 3 is put after the open tag for line 4. Also, the close tags </tr> in line 5 and </td> in line 7 are missing. However, this HTML segment can be rendered correctly in a browser, with the boundary coordinates for each HTML element shown in the middle of Fig. 9.25. Using this visual information, it is easy to build the tree on the right. left 1 <table> 2 <tr> 3 <td> data1 <td> 4 </td>data2 </td> 5 <tr> 6 <td> data3 </td> 7 <td> data4 8 </tr> 9 </table> right top bottom 100 100 100 200 100 100 200 300 300 200 300 300 200 300 400 300 300 300 400 400 400 200 200 200 200 300 300 300 table tr td tr td td td Fig. 9.25. A HTML code segment, boundary coordinates and the resulting tree 9.8 Extraction Based on a Single List Page: Flat Data Records We are now ready to perform the data extraction task. In this and the next sections, we study the first extraction problem in Sect. 9.4.1, i.e., extraction based on a single list page. This section focuses on a simpler case, i.e., a list (a data region) containing only flat data records (no nesting). We assume that the DOM tree has been built for the page. In Sect. 9.10, we will study the second extraction problem based on multiple input pages. The techniques studied in this section are based on the work in [341, 600]. 358 9 Structured Data Extraction: Wrapper Generation Given a list page containing multiple lists and each list contains multiple data records (at least two), the following tasks are performed: 1. Identify each list (also called a data region), i.e., mine all data regions, 2. Segment data records in each list or data region, and 3. Align data items in the data records to produce a data table for each data region and also a regular expression pattern. 9.8.1 Two Observations about Data Records Data records in each list (or data region) are encoded using the same HTML mark-up encoding. Finding the data records and its hidden schema means to find repeated patterns and align them. String or tree matching/comparison are natural techniques. The problem, however, is the efficiency because a data record can start from anywhere in a page and has any length. It is prohibitive to try all possibilities. If all data records have exactly the same tag string, then the problem is easier. However, in practice, a set of data records typically does not have exactly the same tag string or data items due to missing or optional items (see Fig. 9.26). The two important observations below help to solve the problem, which are based on the DOM tree structure [341]. Observation 1: A group of data records that contains descriptions of a set of similar objects is typically presented in a contiguous region of a page and is formatted using similar HTML tags. Such a region represents a list or a data region. For example, in Fig. 9.26 two books are presented in one contiguous region. Observation 2: A list of data records in a region is formed by some child sub-trees of the same parent node. It is unlikely that a data record starts in the middle of a child sub-tree and ends in the middle of another child sub-tree. Instead, it starts from the beginning of a child sub-tree and ends at the end of the same or a later child sub-tree. For example, Fig. 9.27 shows the DOM tree of the page in Fig. 9.26 (with some parts omitted). In this tree, each data record is wrapped in five TR nodes with their sub-trees under the same parent TBODY. The two data records are in the two dash-lined boxes. It is unlikely that a data record starts from TD* and ends at TD# (Fig. 9.27). The second observation makes it possible to design an efficient algorithm to identify data records because it limits the tags from which a data record may start and end in a DOM tree. 9.8 Extraction Based on a Single List Page: Flat Data Records 359 Fig. 9.26. An example of a page segment HTML BODY HEAD TABLE P TABLE TBODY TR | TD TR | TD* TD TD TR TR TD TD TD TD TR | TD data record 1 TR | TD TR TR | TD TD TD TR TR | TD TD TD TD# TD data record 2 Fig. 9.27. The DOM tree of the page segment in Fig. 9.26 9.8.2 Mining Data Regions This first step mines every data region in a Web page that contains a list of data records (a set instance). Finding data regions (or individual data re- 360 9 Structured Data Extraction: Wrapper Generation cords in them) directly is, however, hard. We first mine generalized nodes (defined below). A sequence of adjacent generalized nodes forms a data region. From each data region, we identify the actual data records (discussed in Sect. 9.8.3). Below, we define generalized nodes and data regions using the DOM (tag) tree: Definition: A generalized node (a node combination) of length r consists of r (r 1) nodes in the DOM tree with the following two properties: (1) the nodes all have the same parent; (2) the nodes are adjacent. We introduce the generalized node to capture the situation that a data record is contained in several sibling HTML tag nodes rather than one. For example, in Fig. 9.27, we see that each notebook is contained in five table rows (or five TR nodes). We call each node in the HTML tag tree a tag node to distinguish it from a generalized node. Definition: A data region is a collection of two or more generalized nodes with the following properties: (1) the generalized nodes all have the same parent; (2) the generalized nodes all have the same length; (3) the generalized nodes are all adjacent; (4) the similarity between adjacent generalized nodes is greater than a fixed threshold. For example, in Fig. 9.27, we can form two generalized nodes. The first one consists of the first five children TR nodes of TBODY, and the second one consists of the next five children TR nodes of TBODY. We should note that although the generalized nodes in a data region have the same length (the same number of children nodes of a parent node in the tag tree), their lower level nodes in their sub-trees can be quite different. Thus, they can capture a wide variety of regularly structured objects. We also note that a generalized node may not represent a final data record (see Sect. 9.8.3), but will be used to find the final data records. To further explain different kinds of generalized nodes and data regions, we make use of an artificial DOM/tag tree in Fig. 9.28. For notational convenience, we do not use actual HTML tag names but ID numbers to denote tag nodes in a tree. The shaded areas are generalized nodes. Nodes 5 and 6 are generalized nodes of length 1 and they together define the data region labeled 1 if the similarity condition (4) is satisfied. Nodes 8, 9 and 10 are also generalized nodes of length 1 and they together define the data region labeled 2 if the similarity condition (4) is satisfied. The pairs of nodes (14, 15), (16, 17) and (18, 19) are generalized nodes of length 2. They together define the data region labeled 3 if the similarity condition (4) is satisfied. It 9.8 Extraction Based on a Single List Page: Flat Data Records 361 should be emphasized that a data region includes the sub-trees of the component nodes, not just the component nodes alone. 1 3 2 5 6 Region 1 7 8 9 4 11 10 Region 2 13 14 15 12 16 17 18 19 20 Region 3 Fig. 9.28. An illustration of generalized nodes and data regions Comparing Generalized Nodes In order to find each data region in a Web page, the mining algorithm needs to find the following: (1) Where does the first generalized node of a data region start? For example, in Region 2 of Fig. 9.28, it starts at node 8. (2) How many tag nodes or components does a generalized node in each data region have? For example, in Region 2 of Fig. 9.28, each generalized node has one tag node (or one component). Let the maximum number of tag nodes that a generalized node can have be K, which is normally a small number (< 10). In order to answer (1), we can try to find a data region starting from each node sequentially. To answer (2), we can try one node, two node combination, …, K node combination. That is, we start from each node and perform all 1-node comparisons, all 2-node comparisons, and so on (see the example below). We then use the comparison results to identify each data region. The number of comparisons is actually not very large because: Due to the two observations in Sect. 9.8.1, we only need to perform comparisons among the children nodes of a parent node. For example, in Fig. 9.28, we do not compare node 8 with node 13. Some comparisons done for earlier nodes are the same as for later nodes (see the example below). We use Fig. 9.29 to illustrate the comparison. There are 10 nodes below the parent node p. We start from each node and perform string (or tree) comparison of all possible combinations of component nodes. Let the maximum number of components that a generalized node can have be 3. 362 9 Structured Data Extraction: Wrapper Generation p 1 2 3 4 5 6 7 8 9 10 Fig. 9.29. Combination and comparison Start from node 1: We compute the following string or tree comparisons. (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10) (1-2, 3-4), (3-4, 5-6), (5-6, 7-8), (7-8, 9-10) (1-2-3, 4-5-6), (4-5-6, 7-8-9). (1, 2) means that the tag string of node 1 is compared with the tag string of node 2. The tag string of a node includes all the tags of the sub-tree of the node. (1-2, 3-4) means that the combined tag string of nodes 1 and 2 is compared with the combined tag string of nodes 3 and 4. Start from node 2: We only compute: (2-3, 4-5), (4-5, 6-7), (6-7, 8-9) (2-3-4, 5-6-7), (5-6-7, 8-9-10). We do not need to do 1-node comparisons because they have been done when we started from node 1 above. Start from node 3: We only need to compute: (3-4-5, 6-7-8). Again, we do not need to do 1-node comparisons. Also, 2-node comparisons are not necessary as they were done when we started at node 1. We do not need to start from any other node after node 3 because all the computations have been done. The Overall Algorithm The overall algorithm (called MDR) is given in Fig. 9.30. It traverses the tag tree from the root downward in a depth-first fashion (lines 5 and 6). Node is any tree node. K is the maximum number of tag nodes in a generalized node (10 is sufficient). is the similarity threshold. The node comparison can be done either using string edit distance or tree matching (e.g., STM). The similarity threshold can be set empirically. Line 1 says that the algorithm will not search for data regions if the depth of the sub-tree at Node is 2 or 1 as it is unlikely that a data region is formed with only a single level of tag(s). 9.8 Extraction Based on a Single List Page: Flat Data Records 363 Algorithm MDR(Node, K, ) 1 if TreeDepth(Node) >= 3 then 2 CombComp(Node.Children, K); 3 DataRegions IdenDRs(Node, K, ); 4 if (UncoveredNodes Node.Children UDR DataRegionsDR) then 5 for each ChildNode UncoveredNodes do 6 DataRegions DataRegions MDR(ChildNode, K, ); 7 return DataRegions 8 else return Fig. 9.30. The MDR algorithm At each internal node, the function CombComp() (line 2) performs string (tree) comparisons of various combinations of the children sub-trees, which have been discussed above. The function IdenDRs() (line 3) uses the comparison results to find similar children node combinations (using the similarity threshold ) to obtain generalized nodes and data regions (DataRegions) under Node (i.e., among the children of Node). That is, it decides which combinations represent generalized nodes and where the beginning and end are for each data region. DataRegions consists of a set of data regions, and each data region contains a list of tag nodes organized as generalized nodes of the region. IdenDRs() is discussed further below. Line 4 says that if some nodes are not covered by discovered data regions, the algorithm will go down the tree further from these nodes to see whether they contain data regions (lines 5 and 6). We note that a generalized node may not be a data record, but may contain more than one data record. Figure 9.31 illustrates the point. This data region has eight data records. Each row has two. However, each row will be reported as a generalized node because rows 1 4 are similar. We will explain how to find data records from each generalized node shortly. row 1 1 2 3 4 5 6 7 8 row 2 row 3 row 4 Fig. 9.31. A possible configuration of data records 364 9 Structured Data Extraction: Wrapper Generation Let us come back to the function IdenDRs(), which is not hard to design and it is omitted. Interested readers can refer to [341]. We only describe two issues that the function needs to consider. 1. It is clear from Fig. 9.28 that there may be several data regions under a single parent node Node. Generalized nodes in different data regions may have different number of tag node components. 2. A property about similar strings (or trees) is that if a set of strings (trees), s1, s2, s3, …., sn, is similar to one another, then a combination of any number of them is also similar to another combination of them of the same number. IdenDRs should only report generalized nodes of the smallest length that cover a data region. For Fig. 9.31, it only reports each row as a generalized node rather than a combination of two rows (rows 1-2, and rows 3-4). The computation of the algorithm is dominated by string (or tree) comparison. Assume that the total number of nodes in the tag tree is N, the number of comparisons is in the order of O(NK2). Since K is normally very small, the computation requirement of the algorithm is low. Visual information (see Sect. 9.8.5) and simple heuristics can be applied to reduce the number of string (tree) comparisons substantially. 9.8.3 Identifying Data Records in Data Regions As we have discussed above, a generalized node may consist of multiple data records. Figure 9.31 shows an example, where each row is a generalized node that contains two data records. To find data records from each generalized node in a data region, the following observation is useful: If a generalized node contains two or more data records, these data records must be similar in terms of their tag strings. This is clear because we assume that a data region contains descriptions of similar data records. Identifying data records from each generalized node in a data region is relatively easy because they are nodes (together with their sub-trees) at the same level as the generalized node, or nodes at a lower level of the DOM/tag tree. This can be done in two steps: 1. Produce one rooted tree for each generalized node: An artificial root node is created first, and all the components (which are sub-trees) of the generalized node are then put as its children. 2. Call the MDR algorithm using the tree built in step 1: Due to the observation above, this step will find the data records if exist. Otherwise, the generalized node is a data record. Two issues needs to be considered. 9.8 Extraction Based on a Single List Page: Flat Data Records 365 The discovered data records should covered all the data items in the original generalized node. Each data record should not be too small, e.g., a single number or a piece of text, which is likely to be an entry in a spreadsheet table. Further details can be found in [341, 601], where handling noncontiguous data records is also discussed. For example, two books are described in two table rows. One row lists the names of the two books in two cells, and the next row lists the other pieces of information about the books also in two cells. This results in the following sequence in the HTML code: name 1, name 2, description 1, description 2. 9.8.4 Data Item Alignment and Extraction Once data records in each data region are discovered, they are aligned to produce an extraction pattern that can be used to extract data from the current page and also other pages that use the same encoding template. We use the partial tree alignment algorithm to perform the task in two steps: 1. Produce a rooted tree for each data record: An artificial root node is created first. The sub-trees of the data record are then put as its children. 2. Align the resulting trees: The trees of all the data records in each data region are aligned using the partial tree alignment method in Sect. 9.6.2. After alignments are done, the final seed tree can be used as the extraction pattern, or be turned into a regular expression. Conflict Resolution: In tree matching and alignment, it is possible that multiple matches can give the same maximum score, but only one match is correct. Then, we need to decide which one. For example, in Fig. 9.32, node c in tree A can match either the first or the last node c in tree B. A a B p b c p a b c g c Fig. 9.32. Two trees with more than one possible match: which is correct? To deal with this problem, we can use data content similarity. Data items that share some common substrings are more likely to match. In many cases, a data item contains both the attribute name and the attribute value. For example, “shopping in 24 hours” and “shopping within a week”. 366 9 Structured Data Extraction: Wrapper Generation The data content similarity can in fact be considered in the simple tree matching (STM) algorithm in Fig. 9.19 with minor changes to line 1 and line 11. Data contents (data items) should be included as leaves of the DOM trees. When data items are matched, their match score is computed. In [601], the longest common subsequence (LCS) is used, but cosine similarity should work too. Let q be the number of words in the LCS of the two data items, and m be the maximal number of words contained in them. Their matching score is computed with q/m. 9.8.5 Making Use of Visual Information It is quite clear that many visual features that are designed to help people locate and understand information in a Web page can help data extraction. We have already shown that visual cues can be used to construct DOM trees. In fact, they can be exploited everywhere. Here are some examples: Reduce the number of string or tree comparisons. If two sub-trees are visual too different, they do not need to be compared. Confirm the boundary of data records using the space gap between data records. It is usually the case that gaps between data records are larger than gaps between items within a data record. Determine item alignment. Visual alignment (left, right or center) of items can help determine whether two data items should match. Relative positions of the data items in each data record are very helpful too. Identify data records based on their contour shapes. This method was exploited to segment data records from search engine results [612]. 9.8.6 Some Other Techniques Both string comparison and tree comparison based methods have been used to solve the data extraction problem. The task described in Sect. 9.8.2 can be done either based on strings or trees. A pure string based method called was proposed in [91]. It finds patterns from the HTML tag string, and then uses the patterns to extract data. The center star method is used to align multiple strings. A more sophisticated string based method, which also deals with nested records, was proposed in [530]. The main problem with string based methods is that they can find many patterns and it is hard to determine which one is correct. Some patterns may cross boundaries of data records. In [91], the user needs to choose the right pattern for extraction. In [530], multiple pages containing similar data records and other methods are used to choose the right pattern. This problem is not a major 9.9 Extraction Based on a Single List Page: Nested Data Records 367 issue for tree based methods because of tree structures, which eliminate most alternatives. The observations made in Sect. 9.8.1 were also very helpful. In [312], a method based on constraints and the EM algorithm is proposed, which needs to use some information from detail pages to segment data records. Note that a data record usually (not always) has a link to its detail page, which contains the detail description of the object (e.g., a product) represented by the data record. Visual information is extensively used in [612] to segment snippets (data records) of returned pages from search engines. It uses the contour shape on the left of each snippet, distance from the left boundary of the page, and also the type of each line in a snippet (e.g., text line, link line, empty line, etc.) to determine the similarity of candidate data records. 9.9 Extraction Based on a Single List Page: Nested Data Records The problem with the method in Sect. 9.8 is that it is not suitable for nested data records, i.e., data records containing nested lists. Since the number of elements in a list of each data record can be different, using a fixed threshold to determine the similarity of data records will not work. The problem, however, can be dealt with as follows. Instead of traversing the DOM tree top down, we can traverse it post-order. This ensures that nested lists at lower levels are found first based on repeated patterns before going to higher levels. When a nested list is found, its records are collapsed to produce a single pattern which replaces the list of data records. When comparisons are made at a higher level, the algorithm only sees the pattern. Thus it is treated as a flat data record. This solves the fixed threshold problem above. We introduce an algorithm below, which is based on the NET system in [351]. A running example is also given to illustrate the process. The NET algorithm is given in Fig. 9.33. It is basically a post-order tree traversal algorithm. The observations in Sect. 9.8.1 are still applicable here. The function TraverseAndMatch() performs the post-order traversal. During the process, each nested list is collapsed. The function PutDataInTables() (line 3) outputs the extracted data to the user in relational tables (a page may have multiple data regions, and data in each region are put in a separate table). Line 3 can be easily done if the function TraverseAndMatch() saves the nodes whose children form data records. Line 1 of TraverseAndMatch() says that the algorithm will not search for data records if the depth of the sub-tree from Node is 2 or 1 as it is unlikely that a data record is formed with only a single level of tag(s). This 368 9 Structured Data Extraction: Wrapper Generation Algorithm NET(Root, ) 1 TraverseAndMatch(Root, ); 2 for each top level node Node whose children have aligned data records do 3 PutDataInTables(Node); 4 endfor Function TraverseAndMatch (Node, ) 1 if Depth(Node) 3 then 2 for each Child Node.Children do 3 TraverseAndMatch(Child, ); 4 endfor 5 Match(Node, ); 6 endif Fig. 9.33. The NET algorithm Function Match(Node, ) 1 Children Node.Children; 2 while Children do 3 ChildFirst select and remove the first child from Children; 4 for each ChildR in Children do 5 if TreeMatch(ChildFirst, ChildR) > then 6 AlignAndLink(); 7 Children Children – {ChildR} 8 endfor 9 if some alignments (or links) have been made with ChildFirst then 10 GenNodePattern(ChildFirst) 11 endwhile 12 If consecutive child nodes in Children are aligned then 13 GenRecordPattern(Node) Fig. 9.34. The Match function parameter can be changed. The Match() function performs tree matching on child sub-trees of Node and pattern generation. is the threshold for a match of two trees that are considered sufficiently similar. Match(): The Match() function is given in Fig. 9.34. Figure 9.35 shows a running example. In this figure, Ni represents an internal node, and tj represents a terminal (leaf) node with a data item. We use the same shape or shading to indicate matching nodes. We explain the algorithm below. Given the input node Node, line 1 obtains all its children to be matched. In our example, Children of p are t1, N1, N2, N3, N4, t2, N5, N6, N7, N8, and N9 (with their sub-trees). Lines 2–4 set every pair of child nodes to be matched. The matching is done by TreeMatch(), which uses algorithm 9.9 Extraction Based on a Single List Page: Nested Data Records 369 p t1 N1 t3 t4 N2 t5 N3 t6 t7 N4 t8 t2 N5 t9 N6 t10 t11 N7 N8 N9 t12 t13 t14 t15 t16 Fig. 9.35. A running example: All matched items are linked STM() in Fig. 9.19. AlignAndLink() (line 6) aligns and links all matched data items (leaf nodes) in ChildFirst and ChildR. The links are directional, i.e., from earlier data items to later (matched) data items. If ChildR matches ChildFirst, ChildR is removed from Chirdren so that it will not be matched again later (line 7). For our example, after lines 4 11, the resulting matches and links (dashed lines) are given in Fig. 9.35. Assume they all satisfy the match condition in line 5. In lines 9 10, if some alignments (or links) have been made, the GenNodePattern() function generates a node pattern for all the nodes (including their sub-trees) that match ChildFirst. This function first gets the set of matched nodes ChildR’s, and then calls PartialTreeAlignment() in Fig. 9.23 to produce a pattern which is the final seed tree. Note that PartialTreeAlignment() can be simplified here because most alignments have been done. Only insertions and matching of unaligned items are needed. A node pattern can also be represented as a regular expression. In lines 12 13, it collapses the sub-trees to produce a global pattern for the data records (which are still unknown). Notice that lines 9 10 already produced the pattern for each child sub-tree. The GenRecordPattern() function simply produces a regular expression pattern for the list of data records. This is essentially a grammar induction problem [243]. Grammar induction in our context is to infer a regular expression given a finite set of positive and negative example strings. However, we only have a single positive example (a list of hidden data records). Fortunately, structured data in Web pages are usually highly regular which enables heuristic methods to generate “simple” regular expressions. Here, we introduce such a simple method, which depends on three assumptions: 1. The nodes in the first data record at each level must be complete, e.g., in Fig. 9.35, nodes t1, N1 and N2 must all be present. 370 9 Structured Data Extraction: Wrapper Generation Function GenRecordPattern(Node) 1 String Assign a distinctive symbol to each set of matched children of Node; 2 Initilize a data structure for NFA N = (Q, , , q0, F), where Q is the set of states, is the symbol set containing all symbols appeared in String, is the { }) to Q, and F is transition relation that is a partial function from Q ( and F ; the set of accept states, Q {q0} (q0 is the start state), // qc is the current state 3 qc q0; 4 for each symbol s in String in sequence do 5 if a transition (qc, s) = qn then // transit to the next state; 6 qc qn (qi, s) = qj, where qi, qj Q then // s appeared before 7 else if (qf, ) = qi, where (qi, s) = qj and f c then 8 if 9 TransitTo(qc, qf) 10 else TransitTo(qc, qi) 11 qc qj 12 else create a new state qc+1 and a transition (qc, s) = qc+1, {((qc, ), qc+1)} i.e., 13 Q Q {qc+1}; 14 qc qc+1 15 if s is the last symbol in String then 16 Assign the state with the largest subscript the accept state qr, F = {qr}; 17 TransitTo(qc, qr); 18 endfor 19 generate a regular expression based on the NFA N; 20 Substitute all the node patterns into the regular expression. Function TransitTo(qc, qs) 1 while qc qs do (qc, ) = qk and k>c then 2 if 3 qc qk 4 else create a transition (qc, ) = qc+1, i.e., 5 qc qc+1 6 endwhile {((qc, ), qc+1)}; Fig. 9.36. Generating regular expressions 2. The first node of every data record at each level must be present, e.g., at the level of t1 and t2, they both must be present, and at the next level, N1, N3, N5, N7 and N8 must be present. Note that the level here is in the hierarchical data organizational sense (not the HTML code sense). 3. Nodes within a single flat data record (no nesting) do not match one another, e.g., N1 and N3 do not appear in the same data record. The GenRecordPattern() function is given in Fig. 9.36. It generates a regular expression pattern. 9.9 Extraction Based on a Single List Page: Nested Data Records 371 Line 1 in Fig. 9.36 simply produces a string for generating a regular expression. For our example, we obtain the following: t1 N1 N2 N3 N4 t2 N5 N6 N7 N8 N9 String: a b c b c a b c b b c Lines 2 3 initialize a NFA (non-deterministic finite automaton). Lines 4 18 traverses String from left to right to construct the NFA. For our example, we obtain the final NFA in Fig. 9.37. q0 a q1 b q3 c q4 (a (b c?)+ )+ Fig. 9.37. The generated NFA and its regular expression Line 19 produces a regular expression from the NFA, which is shown in Fig. 9.37 on the right. Line 20 produces the final pattern (Fig. 9.38) by substituting the node patterns into the regular expression. Here, we use node t1 as the pattern (the seed tree) for nodes t1 and t2, the N1 sub-tree as the pattern for all the linked sub-trees rooted at N1, N3, N5, N7 and N8. The N2 sub-tree is the pattern of the sub-trees rooted at N2, N4, N6 and N9. N1 t1 t3 t4 N2 ? + + t5 Fig. 9.38. The regular expression produced from Fig. 9.35 Some additional notes about the algorithm are in order: Each child node here represents a sub-tree (e.g., N1, N2, etc). Assumption 1 does not require lower level nodes of each sub-tree in the first data record to be complete (no missing items). We will see an example in Fig. 9.40 and Fig. 9.41. Regular expressions produced by the algorithm do not allow disjunctions (i.e., A|B) except (A| ), which means that A is optional. Such regular expresses are called union-free regular expressions. However, disjunctions are possible at lower level matches of the sub-trees. We will discuss the issue of disjunction again in Sect. 9.11.2. Function GenRecordPattern() in Fig. 9.37 assumes that under Node there is only one data region, which may not be true. The algorithm can 372 9 Structured Data Extraction: Wrapper Generation be easily extended to take care of multiple data regions under Node. In fact, the NET() algorithm here is a simplified version to present the main ideas. For practical use, it can be significantly enhanced to remove most assumptions if not all. Finally, the function PutDataInTables() (line 3 of NET() in Fig. 9.33) simply outputs data items in a table, which is straightforward after the data record patterns are found. For the example in Fig. 9.35, the following data table is produced (only terminal nodes contain data): t1 t1 t2 t2 t2 t3 t6 t9 t12 t14 t4 t7 t10 t13 t15 t5 t8 t11 t16 Fig. 9.39. The output data table for the example in Fig. 9.35 N1 N1 N2 t1 N3 N5 N4 t2 N2 N6 t1 N3 t2 N5 + N6 ? t3 t4 t5 t6 t7 t8 t9 t5 t3 t5 t8 t7 t8 t9 Fig. 9.41. Alignment after collapsing Fig. 9.40. Aligned data nodes are linked t1 t1 t2 t6 t4 t6 t9 t7 Fig. 9.42. The output data table for the example in Fig. 9.40 Let us use a smaller but more complete example (Fig. 9.40) to show that generating a pattern of a lower level list makes it possible for a higher level matching. At the level of N4 N5 (which has the parent N2), t3 t5 and t4 t6 are matched (assume they satisfy the match condition, line 5 of Fig. 9.34). They are aligned and linked (dash lines). N4 and N5 are data records at this level (nested in N2 in this case), in which t7 is optional. N4 and N5 9.10 Extraction Based on Multiple Pages 373 are then collapsed to produce a pattern data record using GenNodePattern() first and then GenRecordPattern(), which does not do anything in this case. t7 is marked with a “?”, indicating that it is optional. The pattern data record is N5 (selected based on the PartialTreeAlignment() function). The sub-tree at N4 is then omitted in Fig. 9.41. N5 is marked with a “+” indicating that there is one or more such data records and that the sub-tree of N5 is the pattern. We can see in Fig. 9.41 that the sub-trees rooted at N2 and N3 can now match. The final output data table is given in Fig. 9.42. 9.10 Extraction Based on Multiple Pages We now discuss the second extraction problem described in Sect. 9.4.1. Given multiple pages with the same encoding template, the system finds patterns from them to be used to extract data from other similar pages. The collection of input pages can be a set of list pages or detail pages. Below, we first see how the techniques described so far can be applied in this setting, and then describe a technique specifically designed for this setting. 9.10.1 Using Techniques in Previous Sections We discuss extraction of list pages and detail pages separately. Given a Set of List Pages Since the techniques described in previous sections are for a single list page, they can obviously be applied to multiple list pages. The pattern discovered from a single page can be used to extract data from the rest of the pages. Multiple list pages may also help improve the extraction. For example, patterns from all input pages may be found separately and merged to produce a single refined pattern. This can deal with the problem that a single page may not contain the complete information. Given a Set of Detail Pages In some applications, one needs to extract data from detail pages as they contain more information. For example, in a list page, the information on each product is usually quite brief, e.g., containing only the name, image, and price. However, if an application also needs the product description and customer reviews, one has to extract them from detail pages. For extraction from detail pages, we can treat each page as a data record and apply the algorithms described in Sect. 9.8 and/or Sect. 9.9. For in- 374 9 Structured Data Extraction: Wrapper Generation stance, to apply the NET algorithm, we can simply construct a rooted tree as input to NET as follows: (1) create an artificial root node, and (2) make the DOM tree of each page as a child sub-tree of the artificial root. 9.10.2 RoadRunner Algorithm We now describe the RoadRunner algorithm [117], which is designed specifically for problem 2. Given a set of pages, each containing one or more data records (i.e., the pages can be list pages or detail pages), the algorithm compares the pages to find similarities and differences, and in the process generating a union-free regular expression (i.e., a regular expression without disjunctions) extractor/wrapper. The approach works as follows: To start, it takes a random page as the regular expression wrapper W. The wrapper W is then refined by matching it sequentially with the HTML code of each remaining page pi. It generalizes W by solving mismatches between the wrapper W and the page pi. A mismatch occurs when some token in pi does not match the grammar of the wrapper. There are two types of mismatches: 1. Text string mismatches: They indicate data fields or items. 2. Tag mismatches: They indicate optional items, or iterators (a list of repeated patterns): In this case, a mismatch occurs at the beginning of a repeated pattern and the end of a list. The system finds the last token of the mismatch position and identifies some candidate repeated patterns from the wrapper and the page pi by searching forward. It then compares the candidates with the upward portion of the page pi to confirm. The algorithm is best explained with an example, which is given in Fig. 9.43. In this figure, page 1 on the left (in HTML code) is the initial wrapper. Page 2 on the right is a new page to be matched with page 1. Let us look at some matches and mismatches. Lines 1 3 of both pages are the same and thus match. Lines 4 of both pages are text strings and are different. They are thus data items to be extracted. We go down further. Lines 6 of the pages do not match. Line 6 of page 1 matches line 7 of page 2. Thus, <IMG src=…/> is likely to be optional. Line 11 of page 1 and line 12 of page 2 give another mismatch. Since they are text strings, they are thus data items to be extracted. Line 17 of page 1 and line 18 of page 2 are also data items. Another mismatch occurs at line 19 of page 1 and line 20 9.11 Some Other Issues 375 of page 2. Further analysis will find that we have a list here. The final refined regular expression wrapper is given at the bottom of Fig. 9.43. - Wrapper (initially Page 1): 1: 2: 3: 4: 5: 6: <HTML> Books of: <B> Paul Smith </B> <UL> 7: <LI> 8-10: <I>Title:</I> 11: Web Mining 12: </LI> 13: <LI> 14-16: <I>Title:</I> 17: Data Mining 18: </LI> 19: </UL> 20: </HTML> - Sample (page 2) parsing string mismatch tag mismatch (?) string mismatch (#text) string mismatch (#text) tag mismatch (+) terminal tag search and square matching - Wrapper after solving mismatches: 1: <HTML> 2: Books of: 3: <B> 4: Mike Jones 5: </B> 6: <IMG src=…/> 7: <UL> 8: <LI> 9-11: <I>Title:</I> 12: Databases 13: </LI> 14: <LI> 15-17: <I>Title:</I> 18: HTML Premier 19: </LI> 20: <LI> 21-23: <I>Title:</I> 24: Javascript 25: </LI> 26: </UL> 27: </HTML> <HTML>Books of:<B>#text</B> (<IMG src=…/>)? <UL> (<LI><I>Title:</I>#text</LI>)+ </UL><HTML> Fig. 9.43. A wrapper generation example The match algorithm is exponential in the input string length as it has to explore all possibilities. A set of heuristics is introduced to lower the complexity by limiting the space to explore and to backtrack. In [26], a more efficient method is given based on sophisticated tag path analysis. 9.11 Some Other Issues We now briefly discuss a few other issues that are important to automatic extraction techniques. 9.11.1 Extraction from Other Pages Once the encoding template pattern is found, it can be used to extract data from other pages that contain data encoded in the same way. There are three ways to perform the extraction: 376 9 Structured Data Extraction: Wrapper Generation Finite-state machines: An encoding template pattern is usually represented as a regular expression. A nondeterministic finite-state automaton can be constructed to match occurrences of the pattern in the input string representing a Web page. In the process, data items are extracted. Pattern matching: It is also possible to directly match the string or tree pattern against the input to extract data. This approach is more flexible than finite-state machines because pattern matching allows partial matching. For example, in the page where the pattern is discovered, an optional item does not occur, but it occurs in some other pages. Pattern matching can deal with this easily. In the process, the pattern can be enhanced as well by inserting the new optional item in it. Extracting each page independently: The above two approaches can be problematic if the Web site use many different templates to encode its data. If we start to extract after only finding one pattern, then the data encoded using other templates will not be extracted. One solution to this is to find patterns from each page and extract the page using only the discovered patterns from the page. However, handling each page individually is inefficient. Detecting new templates: To detect new templates without sacrificing efficiency of mining extraction patterns from each page, a pre-screening strategy may be applied. In most applications, the user is interested in only a particular kind of data, e.g., products, research publications, or job postings. It is usually possible to design some simple and efficient heuristics to check whether a page contains such data. If so, a full blown extraction is performed using already generated patterns. If no data is extracted from the page, it is an indication that the page is encoded with a different template. A new mining process can be initiated to discover the new template. 9.11.2 Disjunction or Optional In automatic extraction, it can be difficult to recognize disjunctions. For example, for the three digital cameras in Fig. 9.44, it is easy to know that “On Sale” is an optional item. However, for the prices (including “Out of stock”), it is hard to decide whether they are optional items or disjuncts of a disjunction. The HTML codes for the three fields are given below, (1) <b> $250.00 </b> (2) <b><i> $300.00 </i></b> (3) <i> Out of stock </i>. If they are treated as optional items, they are put in three different columns in the output table, but if they are disjuncts, they are put in the same 9.11 Some Other Issues 377 column. In this example, it is easy for a human user to see that they are disjuncts, i.e., (<b> #text </b>) | (<b><i> #text </i></b>) | (<i> #text</i>). Digital camera 4mp Digital camera 5mp $250.00 (1) Digital camera 3mp On Sale $300.00 (2) Out of stock (3 ) Fig. 9.44. Disjuncts or optional items There are two main pieces of information that can be used to determine whether they are optional or disjuncts: 1. Visual information: If they are in the same relative location with respect to their objects, then they are more likely to be disjuncts. In the above example, the three items are all at the same relative location. 2. Data type information: If the data items are of the same type, they are more likely to be disjuncts. “$250.00” and “$300.00” are of the same type, but “Out of stock” is not. In many cases, it can be hard to decide. Fortunately, disjunctive cases are rare on the Web. Even if an extraction system does not deal with disjunction, it does not cause a major problem. For example, if “Out of stock” is identified as optional, it is probably acceptable. 9.11.3 A Set Type or a Tuple Type Sometimes it can also be difficult to determine whether a list is a tuple type or a set type. For example, if all the lists of a set type have the same number of elements, it is hard to know if they are in fact attributes of a tuple. For instance, the following are three colors of a jacket with different prices. Clearly, they represent a set instance with a list of three tuples: <tr><td><b>Blue:<b></td> <td> $5.00 </td></tr> <tr><td><b>Yellow:<b></td> <td> $6.50 </td></tr> <tr><td><b>Pink:<b></td> <td> $10.99 </td></tr>. However, the following specifications of a particular product are obviously the attributes of the product. Without knowing the semantics of the encoded data, it is difficult to know that the above three are a set instance and the following two are attributes of a tuple: <tr><td><b>weight:<b></td> <td> 30 kg </td></tr> <tr><td><b>height:<b></td> <td> 5 m </td></tr>. 378 9 Structured Data Extraction: Wrapper Generation If multiple lists of the same type are available, we may have some additional information to make the decision. For instance, one pair of shoes has three colors, and another has four colors. We can be fairly confident that different sets of colors represent set instances (or lists). In the second example, if all products have both height and width, it is more likely that they are attributes. However, these heuristics do not always hold. In some cases, it is hard to decide without understanding of the data semantics. 9.11.4 Labeling and Integration Once the data is extracted from a page/site and put in tables, it is desirable to label each column (assigning an attribute name to it). Some preliminary studies have been reported in [27, 530]. However, the problem is still very much open. Furthermore, the extracted data from multiple sites may need to be integrated. There are two main integration problems. The first one is schema matching, which matches columns of data tables. The second one is data value/instance match. For example, in one site, Coca Cola is called “Coke”, but in another site it is called “Coca Cola”. The problem is: how does the system know that they are the same semantically? In Chap. 10, we will study some data integration techniques. 9.11.5 Domain Specific Extraction In most applications, the user is only interested in some specific data objects, e.g., products sold online, and for each object, only some specific items are needed, e.g., product name, image, and price. Domain specific information can be exploited to simplify and also to speed up the extraction dramatically. Such information can be utilized in at least two ways. 1. Quickly identify pages that may contain required data. For example, it is fairly easy to design some domain heuristics to determine whether a page contains a list of products (a list page). One heuristic is to detect repeated images and repeated prices in some fixed order and interval. Such heuristics are usually very efficient to execute and can be used to filter out those pages that are unlikely to contain required data. The extraction algorithm, which is slower, will only run on those pages that are very likely to contain target data. 2. Identifying target items in a data record. Based on the characteristics of target items, it may be easy to identify and label the target items. For example, it is often easy to find product names and product images based on simple heuristics. If heuristics are not reliable, machine learn- Bibliographic Notes 379 ing methods may be applied to learn models to identify target items. For example, in [620], an extended conditional random fields method is used to learn an extraction model, which is then used to extract target items from new data records. 9.12 Discussion Finally, we discuss the main advantages and disadvantages of wrapper induction and automatic data extraction. The key advantage of wrapper induction is that it extracts only the data that the user is interested in. Due to manual labeling, there is no schema matching problem. However, data value or instance matching is still needed. The main disadvantages are that it is not scalable to a large number of sites due to significant manual efforts, and that maintenance is very costly if sites change frequently. The main advantages of automatic extraction are that it is scalable to a huge number of sites, and that there is little maintenance cost. The main disadvantage is that it can extract a large amount of unwanted data because the system does not know what is interesting to the user. Also, in some applications, the extracted data from multiple sites need integration, i.e., their schemas as well as values need to be matched, which are difficult tasks. However, if the application domain is narrow, domain heuristics may be sufficient to filter out unwanted data and to perform the integration tasks. In terms of extraction accuracy, it is reasonable to assume that wrapper induction is more accurate than automatic extraction, although there is no reported large scale study comparing the two approaches. Bibliographic Notes Web data extraction techniques can be classified into three main categories: (1) wrapper programming languages and visual platforms, (2) wrapper induction, and (3) automatic data extraction. The first approach provides some specialized pattern specification languages and visual platforms to help the user construct extraction programs. Systems that follow this approach include WICCAP [613], Wargo [457], Lixto [41], etc. The second approach is wrapper induction, which uses supervised learning to learn data extraction rules from a set of manually labeled positive and negative examples. A theoretical work on wrapper learning based on the PAC learning framework was done by Kushmerick [295]. Example wrapper induction systems include WIEN [296], Softmealy [244], Stalker 380 9 Structured Data Extraction: Wrapper Generation [399], WL2 [108], Thresher [241], IDE [599], [250], etc. Most existing systems are based on inductive learning from a set of labeled examples. IDE [599] employs a simple instance-based learning technique, which performs active learning at the same time so that the user only needs to label a very small number of pages. Related ideas are also used in [90] and [241]. Most existing wrapper induction systems built wrappers based on similar pages from the same site. Zhu et al. [620, 621] reported a system that learns from labeled pages from multiple sites in a specific domain. The resulting wrapper can be used to extract data from other sites. This avoids the labor intensive work of building a wrapper for each site. The third approach is automatic extraction. In [163], Embley et al. studied the automatic identification of data record boundaries given a list page. The technique uses a set of heuristic rules and domain ontologies. In [75], Buttler et al. proposed additional heuristics to perform the task without using domain ontologies. The MDR algorithm discussed in this chapter was proposed by Liu et al. [341]. It uses string edit distance in pattern finding (incidentally, Lloyd Allison has a great page on string edit distance). An algorithm based on the visual information was given by Zhao et al. [612] for extracting search engine results. These systems, however, do not align or extract data items from data records. Chang et al. [91] reported a semiautomatic system called IEPAD to find extraction patterns from a list page to extract data items. The DeLa system by Wang et al. [530] works in the same framework. The DEPTA system by Zhai and Liu [600] works in a different way. It first segments data records, and then aligns and extracts data items in the data records using the partial tree alignment algorithm. Both DEPTA and IEPAD do not deal with nested data records, which are dealt with in NET [351] and DeLa [530]. The RoadRunner system, which needs multiple pages as input, was proposed by Crescenzi et al. [117]. Its theoretical foundation was given by Grumbach and Mecca [210]. Sects. 9.1 and 9.4 of this chapter are influenced by this paper. The work of RoadRunner was improved by Arasu and Garcia-Molina in their EXALG system [26]. Both systems need multiple input pages with a common schema/template and assume that these pages are given. The pages can be either detail pages or list pages. The method proposed in [312] works in a similar setting. 10 Information Integration In Chap. 9, we studied data extraction from Web pages. The extracted data is put in tables. For an application, it is, however, often not sufficient to extract data from only a single site. Instead, data from a large number of sites are gathered in order to provide value-added services. In such cases, extraction is only part of the story. The other part is the integration of the extracted data to produce a consistent and coherent database because different sites typically use different data formats. Intuitively, integration means to match columns in different data tables that contain the same type of information (e.g., product names) and to match values that are semantically identical but represented differently in different Web sites (e.g., “Coke” and “Coca Cola”). Unfortunately, limited integration research has been done so far in this specific context. Much of the Web information integration research has been focused on the integration of Web query interfaces. This chapter will have several sections on their integration. However, many ideas developed are also applicable to the integration of the extracted data because the problems are similar. Web query interfaces are used to formulate queries to retrieve needed data from Web databases (called the deep Web). Figure 10.1 shows two query interfaces from two travel sites, expedia.com and vacation.com. The user who wants to buy an air ticket typically tries many sites to find the cheapest ticket. Given a large number of alternative sites, he/she has to access each individually in order to find the best price, which is tedious. To reduce the manual effort, we can construct a global query interface that allows uniform access to disparate relevant sources. The user can then fill in his/her requirements in this single global interface and all the underlying sources (or databases) will be automatically filled and searched. The retrieved results from multiple sources also need to be integrated. Both integration problems, i.e., integration of query interfaces and integration of returned results, are very challenging due to the heterogeneity of Web sites. Clearly, integration is not peculiar only to the Web. It was, in fact, first studied in the context of relational databases and data warehouse. Hence, this chapter first introduces most integration related concepts using traditional data models (e.g., relational) and then shows how the concepts are tailored to Web applications and how Web specific problems are handled. 382 10 Information Integration Fig. 10.1. Two examples of Web query interfaces 10.1 Introduction to Schema Matching Information/data integration has been studied in the database community since the early 1980s [40, 146, 455]. The fundamental problem is schema matching, which takes two (or more) database schemas to produce a mapping between elements (or attributes) of the two (or more) schemas that correspond semantically to each other. The objective is to merge the schemas into a single global schema. This problem arises in building a global database that comprises several distinct but related databases. One application scenario in a company is that each department has its database about customers and products that are related to the operations of the department. Each database is typically designed independently and possibly by different people to optimize database operations required by the functions of the department. This results in different database schemas in different departments. However, to consolidate the data about customers or company operations across the organization in order to have a more complete understanding of its customers and to better serve them, integration of databases is needed. The integration problem is clearly also important on the Web as we discussed above, where the task is to integrate data from multiple sites. There is a large body of literature on the topic. Most techniques have been proposed to achieve semi-automatic matching in specific domains (see the surveys in [146, 265, 455, 491]). Unfortunately, the criteria and methods used in match operations are almost all based on domain heuristics which are not easily formulated mathematically. Thus, to build a schema matching system, we need to produce mapping heuristics which reflect our understanding of what the user considers to be a good match. Schema matching is challenging for many reasons. First of all, schemas of identical concepts may have structural and naming differences. Schemas may model similar but not identical contents, and may use different data models. They may also use similar words for different meanings. 10.1 Introduction to Schema Matching 383 Although it may be possible for some specific applications, in general, it is not possible to fully automate all matches between two schemas because some semantic information that determines the matches between two schemas may not be formally specified or even documented. Thus, any automatic algorithm can only generate candidate matches that the user needs to verify, i.e., accept, reject or change. Furthermore, the user should also be allowed to specify matches for elements that the system is not able to find satisfactory match candidates. Let us see a simple example. Example 1: Consider two schemas, S1 and S2, representing two customer relations, Cust and Customer. S1 S2 Cust CNo CompName FirstName LastName Customer CustID Company Contact Phone We can represent the mapping with a similarity relation, , over the power sets of S1 and S2, where each pair in represents one element of the mapping. For our example schemas, we may obtain Cust.CNo Customer.CustID Cust.CompName Customer.Company {Cust.FirstName, Cust.LastName} Customer.Contact There are various types of matching based on the input information [455]. 1. Schema-level only matching: In this type of matching, only the schema information (e.g. names and data types) is considered. No data instance is available. 2. Domain and instance-level only matching: In this type of match, only instance data and possibly the domain of each attribute are provided. No schema is available. Such cases occur quite frequently on the Web, where we need to match corresponding columns of the hidden schemas. 3. Integrated matching of schema, domain and instance data: In this type of match, both schemas and instance data (possibly domain information) are available. The match algorithm can exploit clues from all of them to perform matching. There are existing approaches to all above types of matching. We will focus on the first two types. The third type usually combines the results of techniques from the first two, which we discuss in Sect. 10.5. Before going to the details, we first discuss some pre-processing tasks that usually need to be done before matching. 384 10 Information Integration 10.2 Pre-Processing for Schema Matching For pre-processing, issues such as concatenated words, abbreviations, and acronyms are dealt with. That is, they need to be normalized before being used in matching [227, 358, 559]. Prep 1 (Tokenization): This process breaks an item, which can be a schema element (attribute) or attribute value, into atomic words. Such items are usually concatenated words. Delimiters (such as “-”, “_”, etc.) and case changes of letters are used to suggest the breakdown. For example, we can break “fromCity” into “from City”, and “first-name” into “first name”. A domain dictionary of words is typically maintained to help the breakdown. Note that if “from”, “city”, “first” and “name” are not in the dictionary, they will be added to the dictionary. Existing dictionary words are also utilized to suggest the breakdown. For example, “deptcity” will be split into “dept” and “city” if “city” is a word. The dictionary may be constructed automatically, which consists of all the individual words appeared in the given input used in matching, e.g., schemas, instance data and domains. The dictionary is updated as the processing progresses. However, the tokenization step has to be done with care. For example, we have “Baths” and “Bathrooms” if we split “Bath” with “Room” it could be a mistake because “Rooms” could have a very different meaning (the number of rooms in the house). To be sure, we need to ensure that “Bathroom” is not an English word, for which an online English dictionary may be employed. Prep 2 (Expansion): It expands abbreviations and acronyms to their full words, e.g., from “dept” to “departure”. The expansion is usually done based on the auxiliary information provided by the user or collected from other sources. Constraints may be imposed to ensure that the expansion is likely to be correct. For example, we may require that the word to be expanded is not in the English dictionary, with at least three letters, and having the same first letter as the expanding word. For example, “CompName” is first converted to (Comp, Name) in tokenization, and then “Comp” is expanded to “Company”. Prep 3 (Stopword removal and stemming): These are information retrieval pre-processing methods (see Chap. 6). They can be performed to attribute names and domain values. A domain specific stopword list may also be constructed manually. This step is useful especially in linguistic based matching methods discussed below. Prep 4 (Standardization of words): Irregular words are standardized to a single form (e.g., using WordNet [175]), “colour” “color”, “Children” “Child”. 10.3 Schema-Level Matching 385 10.3 Schema-Level Matching A schema level matching algorithm relies on information about schema elements, such as name, description, data type and relationship types (such as part-of, is-a, etc.), constraints and schema structures. Before introducing some matching methods using such information, let us introduce the notion of match cardinality, which describes the number of elements in one schema that match the number of elements in the other schema. In general, given two schemas, S1 and S2, within a single match in the match relation one or more elements of S1 can match one or more elements of S2. We thus have 1:1, 1:m, m:1 and m:n matches. 1:1 match means that one element of S1 corresponds to one element of S2, and 1:m means that one element of S1 corresponds to a set of m (m > 1) elements of S2. Example 2: Consider the following schemas: S1 Cust CustomID Name Phone S2 Customer CustID FirstName LastName We can find the following 1:1 and 1:m matches: 1:1 CustomID 1:m Name CustID FirstName, LastName m:1 match is similar to 1:m match; m:n match is considerably more complex. An example of an m:n match is to match Cartesian coordinates with polar coordinates. There is little work on such complex matches. Most existing approaches are for 1:1 and 1:m matches. We now describe some general matching approaches that employ various types of information available in schemas. There are two main types of information in schemas, natural language words and constraints. Thus, there are two main types of approaches to matching. 10.3.1 Linguistic Approaches They are used to derive match candidates based on the names, comments or descriptions of schema elements [107, 133, 144, 145, 227, 358, 559]. Name Match N1 Equality of names: The same name in different schemas often has the same semantics. 386 N2 N3 N4 N5 N6 10 Information Integration Synonyms: The names of two elements from different schemas are synonyms, e.g., Customer Client. This requires the use of thesaurus and/or dictionaries such as WordNet. In many cases, domain dependent or enterprise specific thesaurus and dictionaries are required. Equality of hypernyms: A is a hypernym of B if B is a kind of A. If X and Y have the same hypernym, they are likely to match. For example, “Car” is-a “vehicle” and “automobile” is-a “vehicle”. Thus, we have Car vehicle, automobile vehicle, and Car automobile. Common substrings: Edit distance and similar pronunciation may be used. For example, CustomerID CustID, and ShipTo Ship2. Cosine similarity: Some names are natural language words or phrases (after pre-processing). Then, text similarity measures are useful. Cosine similarity is a popular similarity measure used in information retrieval (see Chap. 6). This method is also very useful for Web query interface integration since the labels of the schema elements are natural language words or phrases (see the query interfaces in Fig. 10.1) User provided name matches: The user may provide a domain dependent match dictionary (or table), a thesaurus, and/or an ontology. Description Match In many databases, there are comments to schema elements, e.g., S1: CNo S2: CustID // customer unique number // id number of a customer These comments can be compared based on the cosine similarity as well. D1 – Use the cosine similarity to compare comments after stemming and stopword removal. 10.3.2 Constraint Based Approaches Constraints such as data types, value ranges, uniqueness, relationship types and cardinalities, etc., can be exploited in determining candidate matches [327, 358, 382, 424]. C1: An equivalence or compatibility table for data types and keys that specifies compatibility constraints for two schema elements to match can be provided, e.g., string varchar, and (primary key) unique. 10.4 Domain and Instance-Level Matching 387 Example 3: Consider the following two schemas: S1 Cust CNo: int, primary key CompName: varchar (60) CTname: varchar (15) StartDate: date S2 Customer CustID: int, unique Company: string Contact: string Date: date Constraints can suggest that “CNo” matches “CustID”, and “StartDate” may match “Date”. “CompName” in S1 may match “Company” in S2 or “Contact” in S2. Likewise, “CTname” in S1 may match “Company” or “Contact” in S2. In both cases, the types match. Although in these two cases, we are unable to find a unique match, the approach helps limit the number of match candidates and may be combined with other matchers (e.g., name and instance matchers). For structured schemas, hierarchical relationships such as is-a and part-of relationships may be utilized to help match. In the context of the Web, the constraint information above is often not explicitly available because Web databases are for general public who are unlikely to know what an int, string or varchar is. Thus, these types are never shown in Web pages. However, some information may be inferred from the domain or instance information, which we discuss next. 10.4 Domain and Instance-Level Matching In this type of matching, value characteristics are exploited to match schema elements [53, 145, 327, 531, 558]. For example, the two attribute names may match according to the linguistic similarity, but they may have different domain value characteristics. Then, they may not be the same but homonyms. For example, Location in a real estate sell may mean the address, but could also mean some specific locations, e.g., lakefront property, hillside property, etc. In many applications, data instances are available, which is often the case in the Web database context. In some applications, although the instance information is not available, the domain information of each attribute may be obtained. This is the case for Web query interfaces. Some attributes in the query interface contain a list of possible values (the domain) for the user to choose from. No type information is explicitly given, but it can often be inferred. We note that the set of value instances of an attribute can be treated in the similar way as a domain. Thus, we will only deal with domains below. 388 10 Information Integration Let us look at two types of domains or types of values: simple domains and composite domains. The domain similarity of two attributes, A and B, is the similarity of their domains: dom(A) and dom(B). Definition (Simple Domain): A simple domain is a domain in which each value has only a single component, i.e., the value cannot be decomposed. A simple domain can be of any type, e.g., year, time, money, area, month, integer, real, string, etc. Data Type: If there is no type specification at the schema level, we identify the data type from the domain values. Even if there is a type specification at the schema level for each attribute, we can still refine the type to find more characteristic patterns. For example, the ISBN number of a book may be specified as a string type in a given schema. However, due to its fixed format, it is easy to generate a characteristic pattern from a set of ISBN numbers, e.g., a regular expression. Other examples include phone numbers, post codes, money, etc. Such specialized patterns are more useful in matching compatible attribute types. We describe two approaches for type identification: semi-automatic [559, 563] and automatic [145, 327] approaches. Semi-automatic approach: This is done via pattern matching. The pattern for each type may be expressed as a regular expression, which is defined by a human expert. For example, the regular expression for the time type can be defined as “[0 9]{2}:[0 9]{2}" or “dd:dd” (d for digit from 0-9) which recognizes time of the form “03:15”. One can use such regular expressions to recognize integer, real, string, month, weekday, date, time, datetime (combination of date and time), etc. To identify the data type, we can simply apply all the regular expression patterns to determine the type. In some cases, the values themselves may contain some information on the type. For example, values that contain “$” or “US$” indicate the monetary type. For all values that we cannot infer their types, we can assume their domains are of string type with an infinite cardinality. Automated approach: Machine learning techniques, e.g., grammar induction, may be used to learn the underlying grammar/pattern of the values of an attribute, and then use the grammar to match attribute values of the other schemas. This method is particularly useful for value of fixed format, e.g., zip codes, phone numbers, zip codes, ISBNs, date entries, or moneyrelated entries, if their regular expressions are not specified by the user. 10.4 Domain and Instance-Level Matching 389 The following methods may be used in matching: DI 1 – Data types are used as constraints. The method C1 above is applicable here. If the data/domain types of two attributes are not compatible, they should not be matched. We can use a table specifying the degree of compatibility between a set of predefined generic data types, to which data types of schema elements are mapped in order to determine their similarity. DI 2 – For numerical data, value ranges, averages and variances can be computed to access the level of similarity. DI 3 – For categorical data, we can extract and compare the set of values in the two domains to check whether the two attributes from different schemas share some common values. For example, if an attribute from S1 contains many “Microsoft” entries and an attribute in S2 also contains some “Microsoft”’s, then we can propose them as a match candidate. DI 4 – For alphanumeric data, string-lengths and alphabetic/non-alphabetic ratios are also helpful. DI 5 – For textual data, information retrieval methods such as the cosine measure may be used to compare the similarity of all data values in the two attributes. DI 6 – Schema element name as value is another match indicator, which characterizes the cases where matches relate some data instances of a schema with a set of elements (attributes) in another schema. For example, in the airfare domain one schema uses “Economy” and “Business” as instances (values) of the attribute “Ticket Class”, while in another interface, “Economy” and “Business” are attributes with the Boolean domain (i.e., “Yes” and “No”). This kind of match can be detected if the words used in one schema as attribute names are among the values of attributes in another schema [133, 563]. Definition (Composite Domain and Attribute): A composite domain d of arity k is a set of ordered k-tuples, where the ith component of each tuple is a value from the ith sub-domain of d, denoted as di. Each di is a simple domain. The arity of domain d is denoted as rity(d) (= k). An attribute is composite if its domain is composite. A composite domain is usually indicated by its values that contained delimiters of various forms. The delimiters can be punctuation marks (such as “,”, “-”, “/”, “_”, etc) and white spaces and some special words such as “to”. To detect a composite domain, we can use these delimiters to split a composite domain into simple sub-domains. In order to ensure correctness, we may also want to require that a majority of (composite) values can be consistently split into the same number of components. For example, the date can be expressed as a composite domain with MM/DD/YY. 390 10 Information Integration DI 7 – The similarity of a simple domain and a composite domain is determined by comparing the simple domain with each sub-domain of the composite domain. The similarity of composite domains is established by comparing their component sub-domains. We note that splitting a composite domain can be quite difficult in the Web context. For example, without sufficient auxiliary information (e.g., information from other sites) it is not easy to split the following: “Dell desktop PC 1.5GHz 1GB RAM 30GB disk space” 10.5 Combining Similarities Let us call a program that assesses the similarity of a pair of elements from two different schemas based on a particular match criterion a matcher. It is typically the case that the more indicators we have the better results we can achieve, because different matchers have their own advantages and also shortcomings. Combining schema-level and instance-level approach will produce better results than each type of approaches alone. This combination can be done in various ways. Given the set of similarity values, sim1(u, v), sim2(u, v), …, simn(u, v), of a set of n matchers that compared two schema elements u (from S1) and v (from S2), one of the following strategies can be used to combine their similarity values. 1. Max: This strategy returns the maximal similarity value of any matcher. It is thus optimistic. Let the combined similarity be CSim. Then CSim(u, v) = max{sim1(u, v), sim2(u, v), …, simn(u, v)} (1) 2. Weighted Sum: This strategy computes a weighted sum of similarity values of the individual matchers. It needs relative weights which correspond to the expected importance of the matchers: CSim(u, v) = 1*sim1(u, v) + 2sim2(u, v) + … + n*simn(u, v), (2) where i is a weight coefficient, and usually determined empirically. 3. Weighted Average: This strategy computes a weighted average of similarity values of the individual matchers. It also needs relative weights that correspond to the expected importance of the matchers. CSim(u , v) where i is 1 Sim1 (u, v) 2 Sim2 (u , v) ... n n (3) Simn (u , v) , a weight coefficient and is determined experimentally. 10.6 1:m Match 391 4. Machine Learning: This approach uses a classification algorithm, e.g., a decision tree, a naïve Bayesian classifier, or SVM, to determine whether two schema elements match each other. In this case, the user needs to label a set of training examples, which is described by a set of attributes and a class. The attributes can be the similarities. Each training example thus represents the similarity values of a pair of schema elements. The class of the example is either Yes or No, which indicates whether the two elements match or not as decided by the user. There are many other possible approaches. In practice, which method to use involves a significant amount of experimentation and parameter tuning. Note that the combination can also be done in stages for different types of matches. For example, we can combine the instance based similarities first using one method, e.g., Max, and then combine schema based similarities using another method, e.g., Weighted Average. After that, the final combined similarity computation may use Weighted Sum. 10.6 1:m Match The approaches presented above are for 1:1 matches. For 1:m match, other techniques are needed [133, 563, 559]. There are mainly two types of 1:m matches. Part-of Type: Each relevant schema element on the many side is a part of the element on the one side. For example, in one schema, we may have an attribute called “Address”, while in another schema, we may have three attributes, “Street”, “City” and “State”. In this case, “Street”, “City” or “State” is a part of “Address”. That is, the combination of “Street”, “City” or “State” forms “Address”. Thus, it is a 1:m match. Is-a Type: Each relevant schema element on the many side is a specialization of the schema element on the one side. The content of the attribute on the one side is the union or sum of the contents of the attributes on the many side. For example, “HomePhone” and “CellPhone” in S2 are specializations of “Phone” in S1. Another example is the (number of) “Passengers” in Fig. 10.3 (page 397), and the (number of) “Adults”, the (number of) “Seniors”, and the (number of) “Children” in Fig. 10.1 in the airline ticket domain. Identifying Part-of 1:m Matches: For each attribute A in interface S1, we first check if it is a composite attribute as described above. If A is a composite attribute, we find a subset of schema elements in S2 that has a 1:1 correspondence with the sub-attributes of A. For a real application, we may need additional conditions to make the decision (see Sect. 10.8.1). 392 10 Information Integration Identify Is-a 1:m Matches: In the case of part-of 1:m mappings, the domains of the sub-attributes are typically different. In contrast, the identification of is-a 1:m mappings of attributes requires that the domain of each corresponding sub-attribute be similar to that of the general attribute. Name matching of schema elements is useful here. For example, in the case of “Phone” in S1 and “HomePhone” and “CellPhone” in S2, the name similarity can help decide 1:m mapping. However, this strategy alone is usually not sufficient, e.g., “Passengers” in S1 and “Adults”, “Seniors” and “Children” in S2 have no name similarity. Additional information is needed. We will show an example in Sect. 10.8.1. Using the auxiliary information provided by the user is also a possibility. It is not unreasonable to ask the user to provide some information about the domain. For example, a domain ontology that includes a set of concepts and their relationships such as the following (Fig. 10.2) will be of great help: Part-of(“street”, “address”) Is-a(“home phone”, “phone”) Part-of(“city”, “address”) Is-a(“cell phone”, “phone”) Part-of(“state”, “address”) Is-a(“office phone”, “phone”) Part-of(“country”, “address”) Is-a(“day phone”, “phone”) Fig. 10.2. Part-of(X, Y) 10.7 X is a part of Y, and Is-a(X, Y) X is a Y. Some Other Issues 10.7.1 Reuse of Previous Match Results We have mentioned in several places that auxiliary information in addition to the input schemas and data instances, such as dictionaries, thesauri, and user-provided ontology information are very useful in schema matching. The past matching results can also be stored and reused for future matches [356, 455]. Reuse is important because many schemas are very similar to each other and to previously matched schemas. Given a new schema S to be matched with a set of existing schemas E, we may not need to match S with every existing schema in E. There are two slightly different scenarios: 1. Matching of a large number of schemas: If we have a large number of schemas to match, we may not need to perform all pair-wise matches, which have n(n+1)/2 of them with n being the number of input schemas. Since most schemas are very similar, the n(n+1)/2 number of matches are not necessary. 10.7 Some Other Issues 393 2. Incremental schema matching: In this scenario, given a set of schemas that has already been matched, when a new schema S needs to be matched with existing matched schemas E, we may not want to use S to match every schema in E using pair-wise matching. This is the same situation as the first case above. If the original match algorithm is not based on pair-wise match, we may not want to run the original algorithm on all the schemas to just match this single schema with them. For both cases, we want to use old matches to facilitate the discovery of new matches. The key idea is to exploit the transitive property of similarity relationship. For example, “Cname” in S1 matches “CustName” in S2 as they are both customer names. If “CTname” in the new schema S matches “Cname” in S1, we may conclude that “CTname” matches “CustName” in S2. The transitive property has also been used to deal with some difficult matching cases. For example, it may be difficult to map a schema element A directly to a schema element B, but easy to map both A and B to the schema element C in another schema. This helps us decide that A corresponds to B [144, 559, 563]. In the incremental case, we can also use a clustering-based method. For example, if we already have a large number of matches, we can group them into clusters and find a centroid to represent each cluster, in term of schema names and domains. When a new schema needs to be matched, the schema is compared with the centroid rather than with each individual schema in the cluster. 10.7.2 Matching a Large Number of Schemas The techniques discussed so far are mainly for pair-wise matching of schemas. However, in many cases, we may have a large number of schemas. This is the case for many Web applications because there are many Web databases in any domain or application. With a large number of schemas, new techniques can be applied. We do not need to depend solely on pair-wise matches. Instead, we can use statistical approaches such as data mining to find patterns, correlations and clusters to match the schemas. In the next section, we will see two examples in which clustering and correlation methods are applied. 10.7.3 Schema Match Results In pair-wise matching, for each element v in S2, the set of matching elements in S1 can be decided by one of the following methods [144]. 394 10 Information Integration 1. Top N candidates: The top N elements of S1 that have the highest similarities are chosen as match candidates. In most cases, N = 1 is the natural choice for 1:1 correspondences. Generally, N > 1 is useful in interactive mode, i.e., the user can select among several match candidates. 2. MaxDelta: The S1 element with the maximal similarity is determined as match candidate plus all S1 elements with a similarity differing at most by a tolerance value t, which can be specified either as an absolute or relative value. The idea is to return multiple match candidates when there are several S1 elements with almost the same similarity values. 3. Threshold: All S1 elements with the final combined similarity values exceeding a given threshold t are selected. 10.7.4 User Interactions Due to the difficulty of schema matching, extensive user interaction is often needed in building an accurate matching system for both parameter tuning and resolving uncertainties Building the Match System: There are typically many parameters and thresholds in an integration system, e.g., similarity values, weight coefficients, and decision thresholds, which are usually domain-specific or even attribute specific. Before the system is used to match other schemas, interactive experiments are needed to tune the parameters by trial-and-errors. After Matching: Although the parameters are fixed in the system building, their values may not be perfect. Matching mistakes and failures will still occur: (1) some matched attributes may be wrong (false positive); (2) some true matches may not be found (false negative). User interactions are needed to correct the situations and to confirm the correct matches. 10.8 Integration of Web Query Interfaces The preceding discussions are generic to database integration and Web data integration. In this and the next sections, we focus on integration in the Web context. The Web consists of the surface Web and the deep Web. The surface Web can be browsed using any Web browser, while the deep Web consists of databases that can only be accessed through parameterized query interfaces. With the rapid expansion of the Web, there are now a huge number of deep web data sources. In almost any domain, one can find a large number of them, which are hosted by e-commerce sites. Each of such sources usually has a keyword based search engine or a query 10.8 Integration of Web Query Interfaces 395 interface that allows the user to fill in some information in order to retrieve the needed data. We have seen two query interfaces in Fig. 10.1 for finding airline tickets. We want to integrate multiple interfaces in order to provide the user a global query interface [153, 227] so that he/she does not need to manually query each individual source to obtain more complete information. Only the global interface needs to be filled with the required information. The individual interfaces are filled and searched automatically. We focus on query interface integration mainly because there is extensive research in this area, although the returned instance data integration is also of great importance and perhaps even more important due to the fact that the number of sites that provide such structured data is huge and most of them do not have query interfaces but only keyword search or can only be browsed by users (see Chap. 9). Since query interfaces are different from traditional database schemas, we first define a schema model. Schema Model of Query Interfaces: In each domain, there is a set of concepts C = {c1, c2, …, cn} that represents the essential information of the domain. These concepts are used in query interfaces to enable the user to restrict the search for some specific instances or objects of the domain. A particular query interface uses a subset of the concepts S C. A concept i in S may be represented in the interface with a set of attributes (or fields) fi1, fi2, ..., fik. In most cases, each concept is only represented with a single attribute. Each attribute is labeled with a word or phrase, called the label of the attribute, which is visible to the user. Each attribute may also have a set of possible values that the user can use in search, which is its domain. All the attributes with their labels in a query interface are called the schema of the query interface [227, 608]. Each attribute also has a name in the HTML code. The name is attached to a TEXTBOX (which takes the user input). However, this name is not visible to the user. It is attached to the input value of the attribute and returned to the server as the attribute of the input value. The name is often an acronym that is less useful than the label for schema matching. For practical schema integration, we are not concerned with the set of concepts but only the label and name of each attribute and its domain. Most ideas for schema matching in traditional databases are applicable to Web query interfaces as the schema of a query interface is similar to a schema in databases. However, there are also some important differences [67, 92]. 1. Limited use of acronyms and abbreviations: Data displayed in Web pages are for the general public to view and must be easy to understand. Hence, the use of acronyms and abbreviations is limited to those very 396 10 Information Integration obvious ones. Enterprise-specific acronyms and abbreviations seldom appear. In the case of a company database, abbreviations are frequently used, which are often hard to understand by human users and difficult to analyze by automated systems. To a certain extent, this feature makes information integration on the Web easier. 2. Limited vocabulary: In the same domain, there are usually a limited number of essential attributes that describe each object in the domain. For example, in the book domain, we have the title, author, publisher, ISBN number, etc. For each attribute, there is usually limited ways to express the attribute. The chosen label (describing a data attribute, e.g., “departure city”) needs to be short, and easily understood by the general public. Therefore, there are not many ways to express the same attributes. Limited vocabulary also makes statistical approaches possible. 3. A large number of similar databases: There are often a large number of sites that offer the same services or sell the same products, which result in a large number of query interfaces and make it possible to use statistical methods. This is not the case in a company because the number of related databases is small. Integration of databases from multiple companies seldom happens. 4. Additional structure: The attributes of a Web interface are usually organized in some meaningful ways. For example, related attributes are grouped and put together physically (e.g., “first name” and “last name” are usually next to each other), and there may also be a hierarchical organization of attributes. Such structures also help integration as we will see later. In the case of databases, attributes usually have no structure. Due to these differences, schema matching of query interfaces can exploit new methods. For example, data mining techniques can be employed as we will see in the next few sub-sections. Traditional schema matching approaches in the database context are usually based on pair-wise matching. Similar to schema integration, query interface integration also requires mapping of corresponding attributes of all the query interfaces. Example 4: For the two query interfaces in Fig. 10.3, the attribute correspondences are: Interface 1 (S1) Leaving from Going to Departure date Return date Passengers: Time Preferred cabin Interface 2 (S2) From To Departure date Return date Number of tickets 10.8 Integration of Web Query Interfaces 397 Fig. 10.3. Two query interfaces from the domain of airline ticket reservation The last two attributes from Interface 1 do not have matching attributes in Interface 2. The problem of generating the mapping is basically the problem of identifying synonyms in the application domain. However, it is important to note that the synonyms here are domain dependent. A general-purpose semantic lexicon such as WordNet or any thesaurus is not sufficient for the identification of most domain-specific synonyms. For example, it is difficult to infer from WordNet or any thesaurus that “Passengers” is synonymous to “Number of tickets” in the context of airline ticket reservation. Domain-specific lexicons are not generally available as they are expensive to build. In this section, we discuss three query interface matching techniques. We also describe a method for building a global interface. 10.8.1 A Clustering Based Approach This technique is a simplified version of the work in [559]. Given a large set of schemas from query interfaces in the same application domain, this technique utilizes a data mining method, clustering, to find attribute matches of all interfaces. Three types of information are employed, namely, attribute labels, attribute names and value domains. Let the set of interface schemas be {S1, S2, …, Sn}. The technique works in five steps: 1. Pre-processing the data. It uses the methods given in Sect. 10.2. 2. Computing all pair-wise attribute similarities of u ( Si) and v ( Sj), i j. This produces a similarity matrix. 3. Identify initial 1:m matches. 4. Cluster schema elements based on the similarity matrix. This step discovers 1:1 matches. 5. Generate the final 1:m matches of attributes. We now discuss each step in turn except the first step. 398 10 Information Integration Computing all Pair-Wise Attribute Similarities: Let u be an attribute of Si and v be an attribute of Sj (i j). This step computes all linguistic similarities (denoted by LingSim(u, v)) and domain similarities (denoted DomSim(u, v)). The aggregated similarity (denoted by AS(u, v)) is: AS (u , v) ls LingSim(u , v) ds DomSim(u , v), (4) where ls and ds are weight coefficients reflecting the relative importance of each component similarity. The linguistic similarity is based on both attribute labels and attribute names, which give two similarity values, lSim(u, v) and nSim(u, v), representing label and name similarities respectively. Both similarities are computed using the cosine measure as discussed in N5 of Sect. 10.3.1. The two similarities are then combined through a linear combination method similar to Equation (4) above. Domain similarity of two simple domains dv and du is computed based on the data type similarity (denoted by typeSim(dv, du) and values similarity (denoted by valueSim(dv, du)). The final DomSim is again a linear combination of the two values. For the type similarity computation, if the types of domains dv and du are the same, typeSim(dv, du) = 1 and 0 otherwise. If typeSim(dv, du) = 0, then valueSim(dv, du) = 0. For two domains dv and du of the same type, the algorithm further evaluates their value similarity. Let us consider two character string domains. Let the set of values in dv be {t1, t2, …, tn} and the set of values in du be {q1, q2, …, qk}. valueSim(dv, du) is computed as follows: 1. Calculate all pair-wise value (i.e., (ti, qj)) similarities using the cosine measure with one value from each domain. 2. Choose the pair with the maximum similarity among all pairs and delete the corresponding two values from dv and du. For a pair to be considered, its similarity must be greater than a threshold value . 3. Repeat step 2 on all remaining values in the domains until no pair of values has a similarity greater than . Let the pairs of values chosen be P. valueSim(dv, du) is then computed using the Dice function [136]: valueSim( d v , d u ) 2| P| . | dv | | du | (5) For two numeric domains, their value similarity is the proportion of the overlapping range of the domains. For an attribute whose domain is unknown, it is assumed that its domain is dissimilar to the domain of any other attribute, be it finite or infinite. 10.8 Integration of Web Query Interfaces 399 Identify a Preliminary Set of 1:m Mappings: To identify 1:m mappings, the technique exploits the hierarchical organization of the interfaces. The hierarchical organization is determined using the layout and the proximity of attributes as they are likely to be physically close to each other. Part-of type: To identify the initial set of aggregate 1:m mappings of attributes, it first finds all composite attributes in all interfaces as discussed in Sect. 10.4. For each composite attribute e in S, in every interface other than S, denoted by X, it looks for a set of attributes f = {f1, f2, … fr} (r > 1) with the same parent p, such that the following conditions hold: 1. fi's are siblings, i.e., they share the same parent p. The sibling information is derived from the physical proximity in the interface. 2. The label of the parent p of fi's is highly similar to the label of e. 3. The domains of fi’s have a 1-to-1 mapping with a subset of the subdomains of e based on the high domain similarities. If there exists such a f in interface X, a 1:m mapping of the part-of type is identified between e and attributes in f. Is-a type: The identification of is-a 1:m attribute mappings requires that the domain of each corresponding sub-attribute on the m side be similar to that of the general attribute on the one side. More precisely, for each noncomposite attribute h in an interface, we look for a set of attributes f = {f1, f2, … fr} (r > 1) in another interface X, that meets the following conditions: 1. fi's are siblings of the same parent p, and p does not have any children other than fi's. 2. The label of the parent p is highly similar to the label of h. 3. The domain of each fi is highly similar to the domain of h. If the conditions are met, a 1:m mapping of the is-a type is identified between h and attributes in f. Cluster the Schema Elements based on the Similarity Matrix: Step 2 produces a similarity matrix M. Let the total number of simple domains in the set of all given query interfaces S be w. We then have a w w symmetric similarity matrix. M[i, j] is the aggregated similarity of two attributes i and j. For attributes in the same interface, M[i, j] is infinite, which indicate that they should not be put together into a cluster. The clustering algorithm used is the hierarchical agglomerative clustering algorithm. The stopping criterion is a similarity threshold. That is, when there is no pair of clusters has the similarity greater than the threshold, the algorithm stops. Each output cluster contains a set of 1:1 attribute mappings from different interfaces. 400 10 Information Integration Obtain Additional 1:m Mapping: The preliminary set of 1:m correspondences may not have found all such mappings. The clustering results may suggest additional 1:m mappings. The transitivity property can be used here. For example, assume that a composite attribute e maps to two attributes f1 and f2 in another interface in step 3 and the clustering results suggest that f1 and f2 map to h1 and h2 in yet another interface. Then, e also matches h1 and h2. 10.8.2 A Correlation Based Approach This technique also makes use of a large number of interfaces. It is based on the technique in [229]. For pre-processing, the methods discussed in Sect. 10.2 are applied. The approach is based on co-occurrences of schema attributes and the following observations: 1. In an interface, some attributes may be grouped together to form a bigger concept. For example, “first name” and “last name” compose the name of a person. This is called the grouping relationship, denoted by a set, e.g., {first name, last name}. Attributes in such a group often cooccur in schemas, i.e., they are positively correlated. 2. An attribute group rarely co-occurs in schemas with their synonym attribute groups. For example, “first name” and “last name” rarely cooccur with name in the same query interface. Thus, {first name, last name} and {name} are negatively correlated. They represent 2:1 match. Note that a group may contain only one attribute. Based on the two observations, a correlation-based method to schema matching is in order. Negatively correlated groups represent synonym groups or matching groups. Given a set of input schemas S = {S1, S2, …, Sn} in the same application domain, where each schema Si is a transaction of attributes, we want to find all the matches M = {m1, …, mv}. Each mj is a complex matching gj1 = gj2 = … = gjw, where each gjk is an positively correlated attribute group and n . Each mj represents the synonym relationship of attribute g jk i 1 Si groups gj1 ,..., gjw. The approach for finding M consists of three steps: 1. Group discovery: This step mines co-occurring or positively correlated attribute groups. It is done by first finding the set of all 2-attribute groups (i.e., each group contains only two attributes), denoted by L2, that are positively correlated according to the input schema set S (one data scan is needed). A 2-attribute group {a, b} is considered positively correlated if cp(a, b) is greater than a threshold value p, where cp is a positive correlation measure. The algorithm then extends 2-attribute 10.8 Integration of Web Query Interfaces 401 groups to 3-attribute groups L3. A 3-attribute group g is considered positively correlated if every 2-attribute subset of g is in L2. In general, a kattribute group g is in Lk if every (k 1)-attribute sub-group of g is in Lk-1. This is similar to candidate generation in the Apriori algorithm for association rule mining (see Chap. 2). However, the method here does not scan the data after all 2-attribute groups have been generated. Example 5: Let L2 = {{a, b}, {b, c}, {a, c}, {c, d}, {d, f}}, which contains all 2-attribute groups that are discovered from the data. {a, b, c} is in L3, but {a, c, d} is not because {a, d} is not in L2. 2. Match discovery: This step mines negatively correlated groups including those singleton groups. Each discovered positively correlated group is first added into those transactions in S that contain some attributes of the group. That is, for a schema Si and a group g, if Si g , then Si = Si {g}. The final augmented transaction set S is then used to mine negatively correlated groups; which are potential matching groups. The procedure for finding all negatively correlated groups is exactly the same as the above procedure for finding positively correlated groups. The only difference is that a different measure is used to determine negative correlations, which will be discussed shortly. A 2-attribute group {a, b} is considered negatively correlated if cn(a, b) is greater than a threshold value n, where cn is a negative correlation measure. 3. Matching selection: The discovered negative correlations may contain conflicts due to the idiosyncrasy of the data. Some correlations may also subsume others. For instance, in the book domain, the mining result may contain both {author} = {first name, last name}, denoted by m1 and {subject} = {first name, last name}, denoted by m2. Clearly, m1 is correct, but m2 is not. Since {subject} = {author} is not discovered, which should be due to transitivity of synonyms, m1 and m2 cannot be both correct. This causes a conflict. A match mj semantically subsumes a match mk, denoted by mj mk, if all the semantic relationships in mk are contained in mj. For instance, {arrival city} = {destination} = {to} {arrival city} = {destination} because the synonym relationship in the second match is subsumed by the first one. Also, {author} = {first name, last name} {author} = {first name} because the second match is part of the first. We now present a method to choose the most confident and consistent matches and to remove possibly false ones. Between conflicting matches, we want to select the most negatively correlated one because it is more likely to be a group of genuine synonyms. Thus, a score function is needed, which is defined as the maximum negative correlation values of all 2-attribute groups in the match: 402 10 Information Integration score(mj, cn) = max cn(gjr, gjt), gjr, gjt mj, jr jt. (6) Combining the score function and semantic subsumption, the matches are ranked based on the following rules: 1. If score(mj, cn) > score(mk, cn), mj is ranked higher than mk. 2. If score(mj, cn) = score(mk, cn) and mj mk, mj is ranked higher than mk. 3. Otherwise, mj and mk are ranked arbitrarily. Figure 10.4 gives the MatchingSelection() function. After the highest ranked match mt in an iteration is selected, the inconsistent parts in the remaining matches are removed (lines 6 10). The final output is the selected n-ary complex matches with no conflict. Note that ranking is redone in each iteration instead of sorting all the matches in the beginning, because after removing some conflicting parts, the ranking may change. Function MatchingSelection(M, cn) 1R // R stores the selected n-ary complex matches 2 while M do 4 Let mt be the highest ranked match in M //select the top ranked match 5 R R {mt} 6 for each mj M do // remove the conflicting part 7 mj mj – mt; 8 if |mj | < 2 then 9 M M – {mj} // delete mj if it contains no matching 10 endfor 11 endwhile 12 return R Fig. 10.4. The MatchingSelection function Correlation Measures: There are many existing correlation tests in statistic, e.g., 2 test and lift, etc. However, it was found that these methods were not suitable for this application. Hence, a new negative correlation measure corrn for two attributes Ap and Aq was proposed, which is called the Hmeasure. Let us use a contingency table (Fig. 10.5) to define it. fij in the figure is the co-occurrence frequency count of the corresponding cell: Aq Aq Ap f11 f01 f+1 Ap f10 f00 f+0 Fig. 10.5. Contingency table for test of correlation f1+ f0+ f++ 10.8 Integration of Web Query Interfaces 403 f 01 f10 . f 1 f1 (7) corrn ( Ap , Aq ) H ( Ap , Aq ) The positive correlation measure corrp is defined as ( corrp ( Ap , Aq ) 1 H ( Ap , Aq ) 0 d is a threshold): f11 d f otherwise. (8) 10.8.3 An Instance Based Approach This method is based on the technique given in [531]. It matches query interfaces and also the query results. It assumes that: 1. a global schema (GS) for the application domain is given, which represents the key attributes of the domain, and 2. a number of sample data instances under the domain global schema are also available. This technique only finds 1:1 attribute matches. We use IS to denote the query interface schema and RS the returned result schema. Let us use an example to introduce the key observation exploited in this technique. Figure 10.6 shows an example of an online bookstore. The part labeled Data Attributes is the global schema with six attributes {Title, Author, Publisher, ISBN, Publication Date, Format}. The part labeled Interface is the query interface with five input elements/attributes. When the keyword query “Harry Potter” is submitted through the Title attribute in the interface, a result page is returned which contains the answer to the query (labeled Result Page), which shows three book instances. Three types of semantic correspondence represented by different lines (dotted, dashed and solid) are also shown in Fig. 10.6. They are respectively, the correspondence between attributes of the global schema and those of the query interface, the correspondence between the attributes of the global schema and those of the instance values in the result page, and the correspondence between attributes in the query interface and those of the instance values in the result pages. Observation: When a proper query is submitted to the right element of the query interface, the query words are very likely to reappear in the corresponding attribute of the returned results. However, if an improper query is submitted to the Web database there are often few or no returned results. 404 10 Information Integration Title Author Publisher Publish Date ISBN Refine Search Your Search: Format … Search Results A Comprehensive Guide to Harry Potter Paperback | Jan 2001|Carson Dellosa Publishing company, Incorporated Harry Potter Beatrix Potter to Harry Potter: Portraits of Children’s Writers Author: God, Devil and Harry Potter Title: Data Attributes Julia Eccleshare John Killinger Hardcover | Sep 2002 | National Portrait Gallery Hardcover | Dec 2002 | St. Martin’s Press, LLC Format: any Result Page ISBN: Search Query Interface Fig. 10.6. An example of a Web database with its query interface and a result page In the example shown in Fig. 10.6, the site retrieves only three matches for the query “Harry Potter” when submitted through the “Author” attribute, while it retrieves 228 matches for the same query when submitted to the Title attribute. If “Harry Potter” is submitted to the “ISBN” attribute, there is no returned result. Intuitively, the number of times that query words reappear in the returned results gives us a good indication what attributes match in the interface schema, the global schema, and the result schema. To obtain the number of reappearing occurrences, each value from the given instances can be submitted to each interface element while keeping default values for the other elements. For each TEXTBOX element in the query interface, all attribute values from the given instances are tried exhaustively. For each SELECT element, its domain values are limited to a set of fixed options. Then, an option similar to a value in the given instances is found and submitted. Here, “similar” means that the attribute value and the option value have at least one common word. Note that this approach assumes that a data extraction system is available to produce a table from a returned result page (see Chap. 9). Each column has a hidden attribute (i.e., of the result schema). By counting the number of times that the query words re-occur in each column of the result table, a 3-dimensional occurrence matrix (OM) can be constructed. The three dimensions are: global schema (GS) attributes, query interface schema (IS) attributes and result schema (RS) attributes. Each cell OM[i, j, k] contains the sum of the occurrence counts obtained from kth attribute of RS of all the sample query words from the ith attribute of GS when the query words are submitted to the jth attribute of IS. 10.8 Integration of Web Query Interfaces 405 Intra-Site Schema Matching: We now briefly describe how to match attributes in IS and GS, IS and RS, and GS and RS based on the projected matrices of OM, i.e., OMIG(M×N), OMIR(M×L), and OMGR(N×L), where N is the number of attributes in the global schema, M is the number of elements in the interface schema, and L is the number of columns in the result table. An example OMIG(5×4) matrix is shown in Fig. 10.7 with the correct matching highlighted, GS = {TitleGS, AuthorGS, PublisherGS, ISBNGS} and IS = {AuthorIS, TitleIS, PublisherIS, KeywordIS, ISBNIS}. We observe from Fig. 10.7 that the highest occurrence count may not represent a correct match. For example, the cell for AuthorIS and PublisherGS (534) has the highest value in the matrix but AuthorIS and PublisherGS do not correspond to each other. In general, for a cell mij, its value in comparison with those of other cells in its row i and its column j is more important than its absolute count. TitleGS AuthorIS TitleIS PublisherIS KeywordIS ISBNIS 93 451 62 120 0 AuthorGS PublisherGS ISBNGS 498 345 184 248 0 534 501 468 143 0 0 0 2 275 258 Fig. 10.7. An example of a OMIG(M×N) matrix with all matches highlighted The algorithm in [531] uses the mutual information measure (MI) to determine correct matches. The mutual information, which measures the mutual dependence of two variables, is defined as follows: MI ( x , y ) Pr( x , y ) log 2 Pr( x , y ) . Pr( x ) Pr( y ) (9) In our context, x and y are attributes from IS and GS respectively. The probabilities, Pr(x, y), Pr(x) and Pr(y), can be easily computed using the OMIG(M×N) matrix. The algorithm simply computes the mutual information of every pair of attributes based on the counts in the matrix such as the one in Fig. 10.7. A corresponding mutual information matrix (called MI matrix) is then constructed (not shown here). To find 1-1 matches of the two schemas, the algorithm chooses each cell in the MI matrix whose value is the largest among all the values in the same row and the same column. The corresponding attributes of the cell forms a final match. The paper also has a similar method for finding matches from multiple Web databases, which is called inter-site schema matching. 406 10.9 10 Information Integration Constructing a Unified Global Query Interface Once a set of query interfaces in the same domain is matched, we can automatically construct a well-designed global query interface that contains all the (or the most significant) distinct attributes of all source interfaces. To build a “good” global interface, three requirements are identified in [154]. 1. Structural appropriateness: As noted earlier, elements of query interfaces are usually organized in groups (logical units) of related attributes so that semantically related attributes are placed in close vicinity. For example, “Adults”, “Seniors”, and “Children” of the interfaces shown in Fig. 10.1 are placed together. In addition, multiple related groups of attributes are organized as super-groups (e.g., “Where and when do you want to go?” in Fig. 10.1). This leads to a hierarchical structure for interfaces (see Fig. 10.8), where a leaf in the tree corresponds to an attribute in the interface, an internal node corresponds to a (super)group of attributes and the order among the sibling nodes within the tree resembles the order of attributes in the interface (from left to right). The global query interface should reflect this hierarchical structure of the domain. 2. Lexical appropriateness: Labels of elements should be chosen so as to convey the meaning of each individual element and to underline the hierarchical organization of attributes (e.g., the three attributes together with the parent attribute “Number of Passengers” in Fig. 10.1). 3. Instance appropriateness: The domain values for each attribute in the global interface must contain the values of the source interfaces. We will give a high level description of the algorithms in [153, 154] that build the global interface by merging given interfaces based on the above three requirements. The input to the algorithms consists of (1) a set of query interfaces and (2) a global mapping of corresponding attributes in the query interfaces. It is assumed that mapping is organized in clusters as discussed in Sect. 10.8.1. Each cluster contains all the matched attributes from different interfaces. We note that the domain model discovery idea in [227] can be seen as another approach to building global interfaces. 10.9.1 Structural Appropriateness and the Merge Algorithm Structural appropriateness means to satisfy grouping constraints and ancestor-descendant relationship constraints of the attributes in individual interfaces. These constraints guide the merging algorithm to produce the global interface, which has one attribute for each cluster. 10.9 Constructing a Unified Global Query Interface S1 Where … travel? Departing From Going To When Do You Want to Go? Departure Date depMonth depDay depTime Return Date retMonth retDay retTime Number of Passengers Adults Seniors Children S2 GS S3 From To Depart leaveMonth leaveDay Return retMonth retDay Passengers Adults Children 407 From Depart dep_Month dep_Day dep_Year To Return ret_Month ret_Day ret_Year Cabin Travelers Adult Child Infant Where … travel? Departing From Going To When Do You Want to Go? Departure Date depMonth depDay depTime dep_Year Return Date retMonth retDay retTime ret_Year Travelers Adults Seniors Children Infant Cabin Fig. 10.8. Three input query interfaces (S1, S2, and S3) and the derived global query interface (GS). Grouping Constraints: Recall that semantically related attributes within an interface are usually grouped together. Grouping constraints require that these attributes should also appear together in the global interface. As the global interface has an attribute for each cluster, the problem is to partition all the clusters into semantically meaningful subsets (or groups), which are employed to organize attributes in the global interface. For instance, for the example in Fig. 10.8, the following sets of clusters are produced, {c_deptCity, c_destCity}, {c_deptYear, c_deptTime, c_deptDay, c_depMonth}, and {c_Senior, c_Adult, c_Child, c_Infant}, where c_X is a cluster representing X (e.g., c_deptCity and c_destCity are clusters representing departure cities and destination cities, respectively). The partition is determined by considering each maximal set of adjacent sibling leaves in the schema tree of each source interface whose parent is not the root. The leaves whose parent is the root are not considered because no reliable information can be derived. These structural constraints are collected from all source interfaces in order to infer the way that attributes are organized in the global interface. All those sets (or groups) of clusters whose intersection is not empty are merged to form the final groups, which are sequences of attribute clusters that preserve adjacency constraints in all interfaces. For example, {c_Adult, c_Senior, c_Child}, {c_Adult, c_Child}, {c_Adult, c_Child, c_Infant} are merged to produce the final group, [c_Senior, c_Adult, c_Child, c_Infant], which preserves all 408 10 Information Integration adjacency constraints. Such a sequence does not always exist. In such a case, a sequence that accommodates most adjacency constraints is sought. Ancestor-Descendant Relationships: In hierarchical modeling of data the same information can be represented in various ways. For instance, the relationship between “Authors” and “Books” can be captured as either ”Authors” having “Books”, which makes “Books” a descendant of “Authors”, or “Books” having “Authors”, which makes “Books” an ancestor of “Authors”. This, however, was not found to be a problem [153]. No such conflicting cases were found from a study of 300 query interfaces in eight application domains. Merge Algorithm: The merge algorithm merges two interfaces at a time to produce the final global interface schema. One of them is the current global interface G. At the beginning, the schema tree with the most levels is chosen as the initial global schema G. Then each other interface is sequentially merged with G. During each merge, G is refined and expanded. The algorithm works in a bottom-up fashion. The merging between leaves is produced based on the clusters. The mapping between internal nodes is based on mappings of their children, which may be either leaf nodes or internal nodes. To meaningfully insert leaves without a match in the correct position, the algorithm relies on groups computed above to infer each leaf position. In our example, we start by merging S1 and S3. S1 is the initial global interface G. Within each group, it is easy to see the position of “Infant”, “ret_Year” and “dep_Year” (see Fig. 10.8 on the right). “Cabin” is inserted at the end since leaf children of the root are discarded before merging and then added as children of the root of the integrated schema tree. Additional details can be found in [153]. 10.9.2 Lexical Appropriateness After the interfaces are merged, the attributes in the integrated interface need to be labeled so that (1) the labels of the attributes within a group are consistent and (2) the labels of the internal nodes are consistent with respect to themselves and to the leaf nodes [154]. It can be observed in the query interface of Fig. 10.1 that between the labels of the attributes grouped together there are certain commonalities. For instance, “Adults”, “Seniors” and “Children” are all plurals, whereas “Leaving” and “Returning” are gerunds. Ideally, the groups within the global interface should have the same uniformity property. Since the attributes may be from different interfaces, a group of attributes within the unified interface might not correspond to any group in a single interface, 10.9 Constructing a Unified Global Query Interface 409 which makes it hard to assign consistent labels. To deal with the problem, a strategy called intersect-and-union is used, which finds groups with non-empty intersection from different interfaces and then unions them. Example 6: Consider the example of the three interfaces in Fig. 10.8 with their passenger related groups organized as the table below. It is easy to see a systematic way of building a consistent solution. Cluster/Interface S1 S2 S3 c_Adult c_Senior c_Child c_Infant Adults Adults Adult Seniors Children Children Child Infant Notice that by combining the labels given by S1 and S2 a consistent naming assignment, namely, “Seniors”, “Adults” and “Children”, can be achieved because the two sets share labels (i.e., “Adults” and “Children”) that are consistent with the labels in both sets. This strategy can be iteratively applied until a label is assigned to each attribute in the group. To deal with minor variations, more relaxed rules for combining attribute labels can be used, e.g., requiring that the set of tokens of the labels to be equal after removal of stopwords (e.g., “Number of Adults” has the same set of tokens as “Adults Number”, i.e. {Number, Adults}) and stemming. If a consistent solution for the entire group cannot be found, consistent solutions for subsets of attributes are constructed. The assignment of consistent labels to the internal nodes uses a set of rules [154] that tries to select a label for each node in such a way that it is generic enough to semantically cover the set of its descendant leaf nodes. For example, the label “Travelers” is obtained in the integrated interface in Fig. 10.8 as follows. First, we know that “Passengers” is more generic than “Number of Passengers” and thus semantically covers both {Seniors, Adults, Children} and {Adults, Children}. Then, “Travelers” is found to be a hypernym of “Passengers“ (using WordNet) and thus semantically covers the union of {Seniors, Adults, Children} and {Adults, Children, Infant} which is the desired set {Seniors, Adults, Children, Infant} 10.9.3 Instance Appropriateness Finally, we discuss how to determine the domain for each attribute in the global schema (interface). A domain has two aspects: the type and the set of values. To determine the domain type of a global attribute, compatibility rules are needed [230]. For instance, if all attributes in a cluster have a finite (infinite) domain then the global attribute will have a finite (infinite) domain. If in the cluster there are both finite and infinite domains, then the 410 10 Information Integration domain of the global attribute will be hybrid (i.e., users can either select from a list of pre-compiled values or fill in a new value). As a case in point, the “Adults” attribute on the global interface derived from the two interface in Fig. 10.1 will have a finite domain, whereas the attribute “Going to” will have a hybrid domain. The set of domain values of a global attribute is given by the union of the domains of the attributes in the cluster. Computing the union is not always easy. For example, the values of the domains may have different scale/unit (e.g., the price may be in US$ or in Euro). Moreover, the same value may be specified in various ways (e.g., “Chicago O’Hare” vs. “ORD”). Currently, the problem is dealt with using user-provided auxiliary thesauruses [230]. Bibliographic Notes Schema integration has been studied in the database community since the early 1980s. The main contributions are described in the surveys by Batini et al [40], Doan and Halevy [146], Kalfoglou and Schorlemmer [265], Larson et al. [306], Kashyap and Sheth [269], Rahm and Bernstein [455], Sheth and Larson [488], and Shvaiko and Euzenat [491]. The database integration aspects of this chapter are mainly based on the survey paper by Rahm and Bernstein [455]. Many ideas are also taken from Clifton et al. [105], Cohen [107], Do and Rahm [144], Dhamankar et al. [133], Embley et al. [162], Madhavan et al. [358], Xu and Embley [563], and Yan et al. [566]. Web data integration is considerably more recent. Various ideas on Web information integration in the early part of the chapter are taken from papers by He and Chang [227, 229], and Wu et al. [559]. On Web query interface integration, which perhaps received the most attention in the research community, several methods have been studied in the chapter, which are based on the works of Dragut et al. [153, 154], He and Chang [227, 229], He et al. [230], Wang et al. [531], and Wu et al. [559]. Before matching can be performed, the Web interfaces have to be found and extracted first. This extraction task was investigated by Zhang et al. [609] and He et al. [231]. Another area of research is the ontology, taxonomy or catalog integration. Ontologies (taxonomies or catalogs) are tree structured schemas. They are similar to query interfaces as most interfaces have some hierarchical structures. More focused works on ontology integration include those by Agrawal and Srikant [13], Doan et al. [147], Gal et al. [190], Zhang and Lee [602]. Wache et al. gave a survey of the area in [527]. 11 Opinion Mining In Chap. 9, we studied structured data extraction from Web pages. Such data are usually records retrieved from underlying databases and displayed in Web pages following some fixed templates. The Web also contains a huge amount of information in unstructured texts. Analyzing these texts is of great importance and perhaps even more important than extracting structured data because of the sheer volume of valuable information of almost any imaginable types contained in them. In this chapter, we only focus on mining of opinions on the Web. The task is not only technically challenging because of the need for natural language processing, but also very useful in practice. For example, businesses always want to find public or consumer opinions on their products and services. Potential customers also want to know the opinions of existing users before they use a service or purchase a product. Moreover, opinion mining can also provide valuable information for placing advertisements in Web pages. If in a page people express positive opinions or sentiments on a product, it may be a good idea to place an ad of the product. However, if people express negative opinions about the product, it is probably not wise to place an ad of the product. A better idea may be to place an ad of a competitor’s product. The Web has dramatically changed the way that people express their opinions. They can now post reviews of products at merchant sites and express their views on almost anything in Internet forums, discussion groups, blogs, etc., which are commonly called the user generated content or user generated media. This online word-of-mouth behavior represents new and measurable sources of information with many practical applications. Techniques are now being developed to exploit these sources to help businesses and individuals gain such information effectively and easily. The first part of this chapter focuses on three mining tasks of evaluative texts (which are documents expressing opinions): 1. Sentiment classification: This task treats opinion mining as a text classification problem. It classifies an evaluative text as being positive or negative. For example, given a product review, the system determines whether the review expresses a positive or a negative sentiment of the reviewer. The classification is usually at the document-level. No details are discovered about what people liked or didn’t like. 412 11 Opinion Mining 1. Featured-based opinion mining and summarization: This task goes to the sentence level to discover details, i.e., what aspects of an object that people liked or disliked. The object could be a product, a service, a topic, an individual, an organization, etc. For example, in a product review, this task identifies product features that have been commented on by reviewers and determines whether the comments are positive or negative. In the sentence, “the battery life of this camera is too short,” the comment is on the “battery life” and the opinion is negative. A structured summary will also be produced from the mining results. 2. Comparative sentence and relation mining: Comparison is another type of evaluation, which directly compares one object against one or more other similar objects. For example, the following sentence compares two cameras: “the battery life of camera A is much shorter than that of camera B.” We want to identify such sentences and extract comparative relations expressed in them. The second part of the chapter discusses opinion search and opinion spam. Since our focus is on opinions on the Web, opinion search is naturally relevant, and so is opinion spam. An opinion search system enables users to search for opinions on any object. Opinion spam refers to dishonest or malicious opinions aimed at promoting one’s own products and services, and/or at damaging the reputations of those of one’s competitors. Detecting opinion spam is a challenging problem because for opinions expressed on the Web, the true identities of their authors are often unknown. The research in opinion mining only began recently. Hence, this chapter should be treated as statements of problems and descriptions of current research rather than a report of mature techniques for solving the problems. We expect major progresses to be made in the coming years. 11.1 Sentiment Classification Given a set of evaluative texts D, a sentiment classifier classifies each document d D into one of the two classes, positive and negative. Positive means that d expresses a positive opinion. Negative means that d expresses a negative opinion. For example, given some reviews of a movie, the system classifies them into positive reviews and negative reviews. The main application of sentiment classification is to give a quick determination of the prevailing opinion on an object. The task is similar but also different from classic topic-based text classification, which classifies documents into predefined topic classes, e.g., politics, science, sports, etc. In topic-based classification, topic related words are important. However, 11.1 Sentiment Classification 413 in sentiment classification, topic-related words are unimportant. Instead, sentiment words that indicate positive or negative opinions are important, e.g., great, excellent, amazing, horrible, bad, worst, etc. The existing research in this area is mainly at the document-level, i.e., to classify each whole document as positive or negative (in some cases, the neutral class is used as well). One can also extend such classification to the sentence-level, i.e., to classify each sentence as expressing a positive, negative or neutral opinion. We discuss several approaches below. 11.1.1 Classification Based on Sentiment Phrases This method performs classification based on positive and negative sentiment words and phrases contained in each evaluative text. The algorithm described here is based on the work of Turney [521], which is designed to classify customer reviews. This algorithm makes use of a natural language processing technique called part-of-speech (POS) tagging. The part-of-speech of a word is a linguistic category that is defined by its syntactic or morphological behavior. Common POS categories in English grammar are: noun, verb, adjective, adverb, pronoun, preposition, conjunction and interjection. Then, there are many categories which arise from different forms of these categories. For example, a verb can be a verb in its base form, in its past tense, etc. In this book, we use the standard Penn Treebank POS Tags as shown in Table 11.1. POS tagging is the task of labeling (or tagging) each word in a sentence with its appropriate part of speech. For details on part-of-speech tagging, please refer to the report by Santorini [472]. The Penn Treebank site is at http://www.cis.upenn.edu/~treebank/home.html. The algorithm given in [521] consists of three steps: Step 1: It extracts phrases containing adjectives or adverbs. The reason for doing this is that research has shown that adjectives and adverbs are good indicators of subjectivity and opinions. However, although an isolated adjective may indicate subjectivity, there may be an insufficient context to determine its semantic (or opinion) orientation. For example, the adjective “unpredictable” may have a negative orientation in an automotive review, in such a phrase as “unpredictable steering”, but it could have a positive orientation in a movie review, in a phrase such as “unpredictable plot”. Therefore, the algorithm extracts two consecutive words, where one member of the pair is an adjective/adverb and the other is a context word. Two consecutive words are extracted if their POS tags conform to any of the patterns in Table 11.2. For example, the pattern in line 2 means 414 . 11 Opinion Mining Table 11.1. Penn Treebank part-of-speech tags (excluding punctuation) Tag CC CD DT EX FW IN Tag PRP$ RB RBR RBS RP SYM Description Possessive pronoun Adverb Adverb, comparative Adverb, superlative Particle Symbol JJ JJR JJS LS MD NN NNS Description Coordinating conjunction Cardinal number Determiner Existential there Foreign word Preposition or subordinating conjunction Adjective Adjective, comparative Adjective, superlative List item marker Modal Noun, singular or mass Noun, plural TO UH VB VBD VBG VBN VBP NNP NNPS PDT POS PRP Proper noun, singular Proper noun, plural Predeterminer Possessive ending Personal pronoun VBZ WDT WP WP$ WRB to Interjection Verb, base form Verb, past tense Verb, gerund or present participle Verb, past participle Verb, non-3rd person singular present Verb, 3rd person singular present Wh-determiner Wh-pronoun Possessive wh-pronoun Wh-adverb that two consecutive words are extracted if the first word is an adverb and the second word is an adjective, but the third word (which is not extracted) cannot be a noun. NNP and NNPS are avoided so that the names of the objects in the review cannot influence the classification. Table 11.2. Patterns of tags for extracting two-word phrases from reviews First word 1. 2. 3. 4. 5. Second word JJ RB, RBR, or RBS JJ NN or NNS RB, RBR, or RBS NN or NNS JJ JJ JJ VB, VBD, VBN, or VBG Third word (Not Extracted) anything not NN nor NNS not NN nor NNS not NN nor NNS anything Example 1: In the sentence “this camera produces beautiful pictures”, “beautiful pictures” will be extracted as it satisfies the first pattern. Step 2: It estimates the semantic orientation of the extracted phrases using the pointwise mutual information measure given in Equation 1: 11.1 Sentiment Classification PMI (term1 , term2 ) log 2 Pr(term1 term2 ) . Pr(term1 ) Pr(term2 ) 415 (1) Here, Pr(term1 term2) is the co-occurrence probability of term1 and term2, and Pr(term1)Pr(term2) gives the probability that the two terms cooccur if they are statistically independent. The ratio between Pr(term1 term2) and Pr(term1)Pr(term2) is thus a measure of the degree of statistical dependence between them. The log of this ratio is the amount of information that we acquire about the presence of one of the words when we observe the other. The semantic/opinion orientation (SO) of a phrase is computed based on its association with the positive reference word “excellent” and its association with the negative reference word “poor”: SO(phrase) = PMI(phrase, “excellent”) PMI(phrase, “poor”). (2) The probabilities are calculated by issuing queries to a search engine and collecting the number of hits. For each search query, a search engine usually gives the number of relevant documents to the query, which is the number of hits. Thus, by searching the two terms together and separately, we can estimate the probabilities in Equation 1. Turney [521] used the AltaVista search engine because it has a NEAR operator, which constrains the search to documents that contain the words within ten words of one another, in either order. Let hits(query) be the number of hits returned. Equation 2 can be rewritten as: SO( phrase) log 2 hits (phrase NEAR " excellent") hits (" poor" ) . hits (phrase NEAR " poor" )hits (" excellent") (3) To avoid division by zero, 0.01 is added to the hits. Step 3: Given a review, the algorithm computes the average SO of all phrases in the review, and classifies the review as recommended if the average SO is positive, not recommended otherwise. Final classification accuracies on reviews from various domains range from 84% for automobile reviews to 66% for movie reviews. 11.1.2 Classification Using Text Classification Methods The simplest approach to sentiment classification is to treat the problem as a topic-based text classification problem. Then, any text classification algorithm can be employed, e.g., naïve Bayesian, SVM, kNN, etc. 416 11 Opinion Mining The approach was experimented by Pang et al. [428] using movie reviews of two classes, positive and negative. It was shown that using a unigram (a bag of individual words) in classification performed well using either naïve Bayesian or SVM. Test results using 700 positive reviews and 700 negative reviews showed that these two classification algorithms achieved 81% and 82.9% accuracy respectively with 3-fold cross validation. However, neutral reviews were not used in this work, which made the problem easier. No stemming or stopword removal was applied. 11.1.3 Classification Using a Score Function A custom score function for review sentiment classification was given by Dave et al. [122]. The algorithm consists of two steps: Step 1: It scores each term in the training set using the following equation, score(ti ) Pr(ti | C ) Pr(ti | C ' ) , Pr(ti | C ) Pr(ti | C ' ) (4) where ti is a term and C is a class and C is its complement, i.e., not C, and Pr(ti|C) is the conditional probability of term ti in class C. It is computed by taking the number of times that a term ti occurs in class C reviews and dividing it by the total number of terms in the reviews of class C. A term’s score is thus a measure of bias towards either class ranging from 1 and 1. Step 2: To classify a new document di = t1…tn, the algorithm sums up the scores of all terms and uses the sign of the total to determine the class. That is, it uses the following equation for classification, class (d i ) C eval (d i ) 0 C' otherwise, (5) where score(t j ) . eval (d i ) j (6) Experiments were conducted based on a large number of reviews (more than 13000) of seven types of products. The results showed that the bigrams (consecutive two words) and trigrams (consecutive three words) as terms gave (similar) best accuracies (84.6% 88.3%), on two different review data sets. No stemming or stopword removal was applied. In this paper, the authors experimented with many alternative classification techniques, e.g., naïve Bayesian, SVM, and several algorithms based 11.2 Feature-Based Opinion Mining and Summarization 417 on other score functions. They also tried some word substitution strategies to improve generalization, e.g., replace product names with a token (“_productname”); replace rare words with a token (“_unique”); replace category-specific words with a token (“_producttypeword”); replace numeric tokens with NUMBER. Some linguistic modifications using WordNet, stemming, negation, and collocation were tested too. However, they were not helpful, and usually degraded the classification accuracy. In summary, the main advantage of document level sentiment classification is that it provides a prevailing opinion on an object, topic or event. The main shortcomings of the document-level classification are: It does not give details on what people liked or disliked. In a typical evaluative text such as a review, the author usually writes specific aspects of an object that he/she likes or dislikes. The ability to extract such details is useful in practice. It is not easily applicable to non-reviews, e.g., forum and blog postings, because although their main focus may not be evaluation or reviewing of a product, they may still contain a few opinion sentences. In such cases, we need to identify and extract opinion sentences. There are several variations of the algorithms discussed in this section (see Bibliographic Notes). Apart from these learning based methods, there are also manual approaches for specific applications. For example, Tong [517] reported a system that generates sentiment timelines. The system tracks online discussions about movies and displays a plot of the number of positive sentiment and negative sentiment messages (Y-axis) over time (X-axis). Messages are classified by matching specific phrases that indicate sentiments of the author towards the movie (e.g., “great acting”, “wonderful visuals”, “uneven editing”, “highly recommend it”, and “it sucks”). The phrases were manually compiled and tagged as indicating positive or negative sentiments to form a lexicon. The lexicon is specific to the domain (e.g., movies) and must be built anew for each new domain. 11.2 Feature-Based Opinion Mining and Summarization Although studying evaluative texts at the document level is useful in many cases, it leaves much to be desired. A positive evaluative text on a particular object does not mean that the author has positive opinions on every aspect of the object. Likewise, a negative evaluative text does not mean that 418 11 Opinion Mining the author dislikes everything about the object. For example, in a product review, the reviewer usually writes both positive and negative aspects of the product, although the general sentiment on the product could be positive or negative. To obtain such detailed aspects, we need to go to the sentence level. Two tasks are apparent [245]: 1. Identifying and extracting features of the product that the reviewers have expressed their opinions on, called product features. For instance, in the sentence “the picture quality of this camera is amazing,” the product feature is “picture quality”. 2. Determining whether the opinions on the features are positive, negative or neutral. In the above sentence, the opinion on the feature “picture quality” is positive. 11.2.1 Problem Definition In general, the opinions can be expressed on anything, e.g., a product, an individual, an organization, an event, a topic, etc. We use the general term “object” to denote the entity that has been commented on. The object has a set of components (or parts) and also a set of attributes (or properties). Thus the object can be hierarchically decomposed according to the part-of relationship, i.e., each component may also have its sub-components and so on. For example, a product (e.g., a car, a digital camera) can have different components, an event can have sub-events, a topic can have subtopics, etc. Formally, we have the following definition: Definition (object): An object O is an entity which can be a product, person, event, organization, or topic. It is associated with a pair, O: (T, A), where T is a hierarchy or taxonomy of components (or parts), subcomponents, and so on, and A is a set of attributes of O. Each component has its own set of sub-components and attributes. Example 2: A particular brand of digital camera is an object. It has a set of components, e.g., lens, battery, view-finder, etc., and also a set of attributes, e.g., picture quality, size, weight, etc. The battery component also has its set of attributes, e.g., battery life, battery size, battery weight, etc. Essentially, an object is represented as a tree. The root is the object itself. Each non-root node is a component or sub-component of the object. Each link represents a part-of relationship. Each node is also associated with a set of attributes. An opinion can be expressed on any node and any attribute of the node. 11.2 Feature-Based Opinion Mining and Summarization 419 Example 3: Following Example 2, one can express an opinion on the camera (the root node), e.g., “I do not like this camera”, or on one of its attributes, e.g., “the picture quality of this camera is poor”. Likewise, one can also express an opinion on one of the camera’s components, e.g., “the battery of this camera is bad”, or an opinion on the attribute of the component, “the battery life of this camera is too short.” To simplify our discussion, we use the word “features” to represent both components and attributes, which allows us to omit the hierarchy. Using features for products is also quite common in practice. For an ordinary user, it is probably too complex to use a hierarchical representation of product features and opinions. We note that in this framework the object itself is also treated as a feature. Let the evaluative text (e.g., a product review) be r. In the most general case, r consists of a sequence of sentences r = s1, s2, …, sm . Definition (explicit and implicit feature): If a feature f appears in evaluative text r, it is called an explicit feature in r. If f does not appear in r but is implied, it is called an implicit feature in r. Example 4: “battery life” in the following sentence is an explicit feature: “The battery life of this camera is too short”. “Size” is an implicit feature in the following sentence as it does not appear in the sentence but it is implied: “This camera is too large”. Definition (opinion passage on a feature): The opinion passage on feature f of an object evaluated in r is a group of consecutive sentences in r that expresses a positive or negative opinion on f. It is common that a sequence of sentences (at least one) in an evaluative text together expresses an opinion on an object or a feature of the object. Also, it is possible that a single sentence expresses opinions on more than one feature: “The picture quality is good, but the battery life is short”. Most current research focuses on sentences, i.e., each passage consisting of a single sentence. Thus, in our subsequent discussion, we use sentences and passages interchangeably. Definition (explicit and implicit opinion): An explicit opinion on feature f is a subjective sentence that directly expresses a positive or negative opinion. An implicit opinion on feature f is an objective sentence that implies a positive or negative opinion. 420 11 Opinion Mining Example 5: The following sentence expresses an explicit positive opinion: “The picture quality of this camera is amazing.” The following sentence expresses an implicit negative opinion: “The earphone broke in two days.” Although this sentence states an objective fact (assume it is true), it implicitly expresses a negative opinion on the earphone. Definition (opinion holder): The holder of a particular opinion is a person or an organization that holds the opinion. In the case of product reviews, forum postings and blogs, opinion holders are usually the authors of the postings, although occasionally some authors cite or repeat the opinions of others. Opinion holders are more important in news articles because they often explicitly state the person or organization that holds a particular view. For example, the opinion holder in the sentence “John expressed his disagreement on the treaty” is “John”. We now put things together to define a model of an object and a set of opinions on the object. An object is represented with a finite set of features, F = {f1, f2, …, fn}. Each feature fi in F can be expressed with a finite set of words or phrases Wi, which are synonyms. That is, we have a set of corresponding synonym sets W = {W1, W2, …, Wn} for the n features. Since each feature fi in F has a name (denoted by fi), then fi Wi. Each author or opinion holder j comments on a subset of the features Sj F. For each feature fk Sj that opinion holder j comments on, he/she chooses a word or phrase from Wk to describe the feature, and then expresses a positive or negative opinion on it. This simple model covers most but not all cases. For example, it does not cover the situation described in the following sentence: “the viewfinder and the lens of this camera are too close”, which expresses a negative opinion on the distance of the two components. We will follow this simplified model in the rest of this chapter. This model introduces three main practical problems. Given a collection of evaluative texts D as input, we have: Problem 1: Both F and W are unknown. Then, in opinion mining, we need to perform three tasks: Task 1: Identifying and extracting object features that have been commented on in each evaluative text d D. Task 2: Determining whether the opinions on the features are positive, negative or neutral. 11.2 Feature-Based Opinion Mining and Summarization 421 Task 3: Grouping synonyms of features, as different people may use different words or phrases to express the same feature. Problem 2: F is known but W is unknown. This is similar to Problem 1, but slightly easier. All the three tasks for Problem 1 still need to be performed, but Task 3 becomes the problem of matching discovered features with the set of given features F. Problem 3: W is known (then F is also known). We only need to perform Task 2 above, namely, determining whether the opinions on the known features are positive, negative or neutral after all the sentences that contain them are extracted (which is simple). Clearly, the first problem is the most difficult to solve. Problem 2 is slightly easier. Problem 3 is the easiest, but still realistic. Example 6: A cellular phone company wants to mine customer reviews on a few models of its phones. It is quite realistic to produce the feature set F that the company is interested in and also the set of synonyms of each feature (although the set might not be complete). Then there is no need to perform Tasks 1 and 3 (which are very challenging problems). Output: The final output for each evaluative text d is a set of pairs. Each pair is denoted by (f, SO), where f is a feature and SO is the semantic or opinion orientation (positive or negative) expressed in d on feature f. We ignore neutral opinions in the output as they are not usually useful. Note that this model does not consider the strength of each opinion, i.e., whether the opinion is strongly negative (or positive) or weakly negative (or positive), but it can be added easily (see [548] for a related work). There are many ways to use the results. A simple way is to produce a feature-based summary of opinions on the object. We use an example to illustrate what that means. Example 7: Assume we summarize the reviews of a particular digital camera, digital_camera_1. The summary looks like that in Fig. 11.1. In Fig. 11.1, “picture quality” and (camera) “size” are the product features. There are 123 reviews that express positive opinions about the picture quality, and only 6 that express negative opinions. The <individual review sentences> link points to the specific sentences and/or the whole reviews that give positive or negative comments about the feature. With such a summary, the user can easily see how the existing customers feel about the digital camera. If he/she is very interested in a particular feature, he/she can drill down by following the <individual review sentences> link to see why existing customers like it and/or what they are not 422 11 Opinion Mining Digital_camera_1: Feature: picture quality Positive: 123 <individual review sentences> Negative: 6 <individual review sentences> Feature: size Positive: 82 <individual review sentences> Negative: 10 <individual review sentences> … Fig. 11.1. An example of a feature-based summary of opinions satisfied with. The summary can also be visualized using a bar chart. Figure 11.2(A) shows the feature-based opinion summary of a digital camera. In the figure, the bars above the X-axis in the middle show the percentages of positive opinions on various features (given at the top), and the bars below the X-axis show the percentages of negative opinions on the same features. positive negative Picture Battery Zoom Size Weight Digital Camera 1 (A) Feature-based summary of opinions on a digital camera positive negative Picture Battery Digital Camera 1 Zoom Size Weight Digital Camera 2 (B) Opinion comparison of two digital cameras Fig. 11.2. Visualization of feature-based opinion summary and comparison 11.2 Feature-Based Opinion Mining and Summarization 423 Comparing the opinion summaries of a few competing products is even more interesting. Figure 11.2(B) shows a visual comparison of consumer opinions on two competing digital cameras. We can clearly see how consumers view different features of each product. Digital camera 1 is clearly superior to digital camera 2. Specifically, most customers have negative opinions about the picture quality, battery and zoom of digital camera 2. However, on the same three features, customers are mostly positive about digital camera 1. Regarding size and weight, customers have similar opinions on both cameras. Thus, the visualization enables users to clearly see how the cameras compare with each other along each feature dimension. Below, we discuss four other important issues. Separation of Opinions on the Object itself and its Features: It is often useful to separate opinions on the object itself and opinions on the features of the object. The opinion on the object itself reflects the general sentiment of the author (or the opinion holder) on the object, which is what sentiment classification tries to discover at the document level. Granularity of Analysis: Let us go back to the general representation of an object with a component tree and each component with a set of attributes. We can study opinions at any level. At level 1: We identify opinions on the object itself and its attributes. At level 2: We identify opinions on the major components of the object, and also opinions on the attributes of the components. At other levels, similar tasks can be performed. However, in practice, analysis at level 1 and level 2 are usually sufficient. Example 8: Given the following review of a camera (the object), “I like this camera. Its picture quality is amazing. However, the battery life is a little short”, in the first sentence, the positive opinion is at level 1, i.e., a positive opinion on the camera itself. The positive opinion on the picture quality in the second sentence is also at level 1 as “picture quality” is an attribute of the camera. The third sentence expresses a negative opinion on an attribute of the battery (at level 2), which is a component of the camera. Opinion Holder Identification: In some applications, it is useful to identify and extract opinion holders, i.e., persons or organizations that have expressed certain opinions. As we mentioned earlier, opinion holders are more useful for news articles and other types of formal documents, in which the person or organization that expressed an opinion is usually stated in the text explicitly. However, such holders need to be identified by 424 11 Opinion Mining the system. In the case of the user-generated content on the Web, the opinion holders are often the authors of discussion posts, bloggers, or reviewers, whose login ids are often known although their true identities in the real-world may be unknown. We will not discuss opinion holders in the chapter further due to our focus on the user-generated content on the Web. Interested readers, please refer to [276]. Opinioned Object Identification and Pronoun Resolution: In product reviews, the reviewed objects are usually known. However, this is not the case for opinions expressed in blogs and discussions. For example, in the following post “I have a Canon S50 camera purchased from Amazon. It takes great photos.”, two interesting questions can be asked: (1) what object does the post praise? and (2) what “it” means in the second sentence? Clearly, we know that the post praises “Canon S50 camera”, which is the problem of opinioned object identification, and we also know that “it” here means “Canon S50 camera” too, which is the problem of pronoun resolution. However, to automatically discover answers to the questions is a very challenging problem. So far, little work has been done. 11.2.2 Object Feature Extraction Current research on feature extraction is mainly carried out from online product reviews. We focus on such reviews in this subsection as well. It is a common practice for online merchants (e.g., amazon.com) to ask their customers to review the products that they have purchased. There are also dedicated review sites like epinions.com. There are three main review formats on the Web. Different review formats may need different techniques to perform the feature extraction task. Format 1 Pros, cons and the detailed review: The reviewer is asked to describe pros and cons separately and also write a detailed review. An example of such a review is given in Fig. 11.3. Format 2 Pros and cons: The reviewer is asked to describe pros and cons separately, but there is not a separate detailed review as in format 1. That is, the details are in pros and cons. An example of such a review is given in Fig. 11.4. Format 3 Free format: The reviewer can write freely, i.e., no separation of pros and cons. An example of such a review is given in Fig. 11.5. For formats 1 and 2, opinion (or semantic) orientations (positive or negative) of the features are known because pros and cons are separated. Only product features need to be identified. For format 3, we need to identify both product features and opinion orientations. 11.2 Feature-Based Opinion Mining and Summarization 425 My SLR is on the shelf by camerafun4. Aug 09 ‘04 Pros: Great photos, easy to use, very small Cons: Battery usage; included memory is stingy. I had never used a digital camera prior to purchasing this Canon A70. I have always used a SLR … Read the full review Fig. 11.3. An example of a review of format 1. “It is a great digital still camera for this century” September 1 2004. Pros: It’s small in size, and the rotatable lens is great. It’s very easy to use, and has fast response from the shutter. The LCD … Cons: It almost has no cons. It could be better if the LCD is bigger and it’s going to be best if the model is designed to a smaller size. Fig. 11.4. An example of a review of format 2. GREAT Camera., Jun 3, 2004 Reviewer: jprice174 from Atlanta, Ga. I did a lot of research last year before I bought this camera... It kinda hurt to leave behind my beloved nikon 35mm SLR, but I was going to Italy, and I needed something smaller, and digital. The pictures coming out of this camera are amazing. The 'auto' feature takes great pictures most of the time. And with digital, you're not wasting film if the picture doesn't come out. … Fig. 11.5. An example of a review of format 3. In both formats 2 and 3, reviewers typically use full sentences. However, for format 1, pros and cons tend to be very brief. For example, in Fig. 11.3, under pros, we have “Great photos, easy to use, take videos”, which are elaborated in the detailed review. Let us deal with pros and cons of format 1 first. The detailed reviews of format 1 are not used as they are elaborations of pros and cons. Analyzing short sentence segments in pros and cons produces more accurate results. Detailed reviews of format 1 are the same as reviews of format 3. 11.2.3 Feature Extraction from Pros and Cons of Format 1 We now describe a supervised pattern learning approach to extract product features from pros and cons in the reviews of format 1. These patterns are 426 11 Opinion Mining generated from label sequential rules (LSR) (see Sect. 2.9.2). This method is based on the algorithm in [247, 347]. A product feature can be expressed with a noun, adjective, verb or adverb. The labels and their POS tags used in mining LSRs are: {$feature, NN}, {$feature, JJ}, {$feature, VB} and {$feature, RB}, where $feature denotes a feature to be extracted, and NN stands for noun, VB for verb, JJ for adjective, and RB for adverb. They represent both explicit features and implicit feature indicators. We call a word that indicates an implicit feature an implicit feature indicator. For example, in the sentence “this camera is too heavy”, “heavy” is an adjective and is an implicit feature indicator for feature “weight”. The feature extraction technique is based on the following observation: Each sentence segment in pros and cons contains only one feature. Sentence segments are separated by commas, periods, semi-colons, hyphens, ‘&’’s, ‘and’’s, ‘but’’s, etc. Example 9: Pros in Fig. 11.3 can be separated into three segments: great photos easy to use very small photo use small size . Cons in Fig. 11.3 can be separated into two segments: battery usage included memory is stingy battery memory We can see that each segment describes a product feature, which is listed within . Notice that small is an implicit feature indicator and size is the implicit feature. One point to note is that an explicit feature may not be a noun or noun phrase. Verbs can be explicit features as well, e.g., “use” in “easy to use”. In general, 60 70% of the features are explicit noun features. A small proportion of explicit features are verbs. 20 30% of the features are implicit features represented by their indicators. Let us now describe the method. Given a set of reviews, this method consists of the following two steps: 1. Training data preparation for LSR mining: It consists of 4 sub-steps: Part-Of-Speech (POS) tagging and sequence generation: For each sentence segment, the algorithm first performs POS tagging, and then produces a sequence. For example, the sentence segment, “Included memory is stingy”. is turned into a sequence with POS tags: {included, VB}{memory, NN}{is, VB}{stingy, JJ} . 11.2 Feature-Based Opinion Mining and Summarization 427 Replace the actual feature words with {$feature, <tag>}, where $feature represents a feature. This replacement is necessary because different products have different features, and the replacement ensures that we can find general language patterns to extract any product feature. After replacement, the above example becomes: {included, VB}{$feature, NN}{is, VB}{stingy, JJ} . Use an n-gram to produce shorter segments from long ones: For example, the above sequence will generate two trigram sequences: {included, VB}{$feature, NN}{is, VB} {$feature, NN}{is, VB}{stingy, JJ} . Trigrams are usually sufficient. The reason for using n-grams rather than full sentences is because most product features can be found based on local information and POS tags. Using long sentences tend to generate a large number of spurious rules. Perform word stemming: This reduces a word to its stem (see Sect. 6.5.2). After the four-step pre-processing, the resulting sentence (trigram) segments are saved in a sequence database for label sequential rule mining. In this file, each line contains one processed sequence. 2. Label sequential rule mining: A LSR mining system is applied to find all rules that involve a feature, i.e., $feature. An example rule is: {easy, JJ }{to}{*, VB} {easy, JJ}{to}{$feature, VB} . Note that both POS tags and words may appear in a rule. A suitable minimum confidence and minimum support should be used, which can be chosen based on experiments. The right-hand-side of the rule is also called a language pattern. 3. Feature extraction: The resulting language patterns are used to match each sentence segment in a new review to extract product features. That is, the word in the sentence segment that matches $feature in a language pattern is extracted. Three situations are considered in extraction: If a sentence segment satisfies multiple rules, we search for a matching rule in the following order: {$feature, NN}, {$feature, JJ}, {$feature, VB} and {$feature, RB}. The reason for this ordering is that noun features appear more frequently than other types. For rules of the same tag, the rule with the highest confidence is used since higher confidence indicates higher predictive accuracy. 428 11 Opinion Mining For sentence segments that no rules apply, nouns or noun phrases produced by a POS tagger are extracted as features if such nouns or noun phrases exist. For a sentence segment with only a single word (e.g., “heavy” and “big”), this pattern-based method does not apply. In such cases, the single words are treated as (implicit or explicit) features. After extraction, we need to deal with several other important problems: Mapping to Implicit Features: There are many types of implicit feature indicators. Adjectives are perhaps the most common type. Many adjectives modify or describe some specific attributes or properties of objects. For example, the adjective “heavy” usually describes the attribute “weight” of an object. “Beautiful” is normally used to describe (positively) the attribute “look” or “appearance” of an object. By no means, however, does this say that these adjectives only describe such attributes. Their exact meaning can be domain dependent. For example, “heavy” in the sentence “the traffic is heavy” does not describe the “weight” of the traffic. One way to map indicator words to implicit features is to manually compile a list of such mappings during training data annotation, which can then be used in the same domain in the future. However, it is not clear whether this is an effective approach as little research has been done. Grouping Synonyms: It is common that people use different words or phrases to describe the same feature. For example, “photo” and “picture” refer to the same feature in digital camera reviews. Identifying and grouping synonyms is essential for practical applications. Although WordNet [175] and other thesaurus dictionaries help to some extent, they are far from sufficient due to the fact that many synonyms are domain dependent. For example, “picture” and “movie” are synonyms in movie reviews. However, they are not synonyms in digital camera reviews as “picture” is more related to “photo” while “movie” refers to “video”. Liu et al. [347] made an attempt using synonyms in WordNet. Carenini et al. [80] proposes a more sophisticated method based on several similarity metrics that require the taxonomy of features to be given. The system merges each discovered feature to a feature node in the taxonomy. The similarity metrics are defined based on string similarity, synonyms and other distances measured using WordNet. Experimental results based on digital camera and DVD reviews show promising results. Clearly, many ideas and techniques described in Chap. 10 for information integration are applicable here. Granularity of Features: In the sentence segment “great photos”, it is easy to decide that “photo” is the feature. However, in “battery usage”, we 11.2 Feature-Based Opinion Mining and Summarization 429 can use either “battery usage” or “battery” as the feature. As we discussed in Sect. 11.2.1, each object has a component/part tree and each component node has a set of attributes. In a practical application, we need to determine the right level of analysis. If it is too general, it may not be useful. If it is too specific, it may result in a large number of features and also make the extraction very difficult and inaccurate. 11.2.4 Feature Extraction from Reviews of Formats 2 and 3 Pros and cons of format 1 mainly consist of short phrases and incomplete sentences. The reviews of formats 2 and 3 usually consist of complete sentences. To extract features from such reviews, the above algorithm can also be applied. However, some preliminary experiments show that it is not effective because complete sentences are more complex and contain a large amount of irrelevant information. Below, we describe an unsupervised method for finding explicit features that are nouns and noun phrases. This method requires a large number of reviews, and consists of two steps: 1. Finding frequent nouns and noun phrases. Nouns and noun phrases (or groups) are identified by using a POS tagger. We then count their frequency and only keep the frequent ones. A frequency threshold can be decided experimentally. The reason for using this approach is that most product features are nouns, and those nouns that are frequently talked about are usually genuine and important features. Irrelevant contents (see Fig. 11.5) in reviews are often diverse, i.e., they are quite different in different reviews. When people comment on product features, the vocabulary that they use converges. Those nouns that are infrequent are likely to be non-features or less important features. 2. Finding infrequent features by making use of sentiment words. Sentiment words (also called opinion words) are usually adjectives and adverbs that express positive or negative opinions, e.g., great, amazing, bad, and expensive. The idea is as follows: The same opinion word can be used to describe different objects. Opinion words that modify frequent features can be used to find infrequent features. For example, “picture” is found to be a frequent feature, and we have the sentence, “The pictures are absolutely amazing.” We also know that “amazing” is a positive opinion word (to be discussed in Sect. 11.2.5). Then “software” may also be extracted as a feature from the following sentence, “The software is amazing.” 430 11 Opinion Mining because the two sentences follow the same language pattern and “software” in the sentence is also a noun. This two-step approach is based on the work of Hu and Liu [245]. At the time this book was written, the shopping site Froogle of the search engine Google implemented a method similar to step 1 of the algorithm. However, it does not restrict frequent terms to be nouns or noun phrases. The precision of step 1 of the above algorithm was improved by Popescu and Etzioni in [447]. Their algorithm tries to remove those noun phrases that may not be product features. It evaluates each noun phrase by computing a PMI score between the phrase and meronymy discriminators associated with the product class, e.g., a scanner class. The meronymy discriminators for the scanner class are, “of scanner”, “scanner has”, “scanner comes with”, etc., which are used to find components or parts of scanners by searching on the Web (see [166] also). The PMI measure is a simplified version of the measure given in Sect. 11.1.1: PMI ( f , d ) hits ( f d ) , hits ( f )hits (d ) (7) where f is a candidate feature identified in step 1 and d is a discriminator. Web search is used to find the number of hits. The idea of this approach is clear. If the PMI value of a candidate feature is too low, it may not be a component of the product because f and d do not co-occur frequently. The algorithm also distinguishes components/parts from attributes/properties using WordNet’s is-a hierarchy (which enumerates different kinds of properties) and morphological cues (e.g., “-iness”, “-ity” suffixes). Finally, we note that many information extraction techniques are also applicable, e.g., conditional random fields (CRF) [298], hidden Markov models (HMM) [185], and many others. However, no comparative evaluation of these methods on this problem has been reported so far. 11.2.5 Opinion Orientation Classification For reviews of format 3, we need to classify each sentence that contains a product feature as positive, negative or neutral. This classification may also be needed for reviews of format 2 because although pros and cons are separated in format 2, some sentences containing features are neutral. We describe two main techniques below. The accuracy is usually reasonable (greater than 80%) if the sentences are either positive or negative, but if neutral sentences are included, the accuracy often drops significantly. Sentences containing negations also pose difficulties. 11.2 Feature-Based Opinion Mining and Summarization 431 1. Using sentiment words and phrases: As explained above, sentiment words and phrases are words and phrases that express positive or negative sentiments (or opinions). They are mostly adjectives and adverbs, but can be verbs and nouns too. Researchers have compiled sets of such words and phrases for adjectives, adverbs, verbs, and nouns respectively. Each set is usually obtained through a bootstrapping process: Manually find a set of seed positive and negative words. Separate seed sets are prepared for adjectives, adverbs, verbs and nouns. Grow each of the seed set by iteratively searching for their synonyms and antonyms in WordNet until convergence, i.e., until no new words can be added to the set. Antonyms of positive (or negative) words will be added to the negative (or positive) set. Manually inspect the results to remove those incorrect words. Although this step is time consuming, it is only an one-time effort. Apart from a set of opinion words, there are also idioms, which can be classified as positive, negative and neutral as well