This project is a simple text classifier based on Pearson correlation and F1 score for similarity selection. It makes use of the Combine Framework provided by the Apple SDK.
import UnsupervisedTextClassifier
A protocol Article is provided to interoperate with the package functions. It can be used as shown:
protocolArticle:Codable{vartext:String?{getset}varkeywords:[String]?{getset}varurl:URL?{getset}}structTestArticle:Article{vartext:String?varkeywords:[String]?varurl:URL?varoptionalFields:....}The Cluster struct:
structCluster{init(articles:[Article], maxSimilarity:Double=0.5)varpublisher:AnyPublisher<SegmentResultGroup,Never>}The Cluster struct publishes the SegmentResultGroup which contains the groupped Articles.
@PublishedvarsegmentResults:[SegmentResultGroup]=[]...letsampleHeadlines=["How many people can I have a drink with? And other questions","Full interview: Hillary Clinton, January 17","Covid lockdown eases: Celebrations as pub gardens and shops reopen","Chauvin Trial Judge Denies Request For Jury Sequestration After Police Shooting","Psaki says Biden 'does not spend his time tweeting conspiracy theories' after a GOP senator criticized the president's social-media use","Police release bodycam footage from officer involved shooting in Minnesota"]letarticles= sampleHeadlines.map{ headline inletkeywords:Set<String>=UnsupervisedTextClassifier.extractKeywords(text: headline)returnTestArticle(text: headline, keywords:Array(keywords))}letcluster=Cluster(articles: articles)
cluster
.publisher
.collect().assign(to:&$segmentResults)Each SegmentResultGroup represents a cluster with similar elements based on keywords matching.
structResultGroup:Identifiable{publicvarsimilarity:Doublepublicvararticle:Articlepublicvarrow:Int}publicstructCorrelationResult{publicvarscore:Doublepublicvartokens:(a:Int, b:Int)}structSegmentResultGroup:Identifiable{publicvarcorrelation:CorrelationResultpublicvarresultGroup:[ResultGroup]}