Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- За секоја прочитан документ од стандарден влез да се испечатат зборовите кои се употребуваат за класификација, класата и
- веројатноста на зборот да е од таа класа (заокружено на 4 децимали), како и тежинската веројатност на зборот да припаѓа
- на класата (заокружено на 4 децимали). На крај да се испечати предвидената класа на документот и логаритам со основа
- 2 од веројатноста со која се предвидува (заокружено на 4 децимали).
- """
- import re
- import math
- train_data = [
- ("""What Are We Searching for on Mars?
- Martians terrified me growing up. I remember watching the 1996 movie Mars Attacks! and fearing that the Red Planet harbored hostile alien neighbors. Though I was only 6 at the time, I was convinced life on Mars meant little green men wielding vaporizer guns. There was a time, not so long ago, when such an assumption about Mars wouldn’t have seemed so far-fetched.
- Like a child watching a scary movie, people freaked out after listening to “The War of the Worlds,” the now-infamous 1938 radio drama that many listeners believed was a real report about an invading Martian army. Before humans left Earth, humanity’s sense of what—or who—might be in our galactic neighborhood was, by today’s standards, remarkably optimistic.
- """,
- "science"),
- ("""Mountains of Ice are Melting, But Don't Panic (Op-Ed)
- If the planet lost the entire West Antarctic ice sheet, global sea level would rise 11 feet, threatening nearly 13 million people worldwide and affecting more than $2 trillion worth of property.
- Ice loss from West Antarctica has been increasing nearly three times faster in the past decade than during the previous one — and much more quickly than scientists predicted.
- This unprecedented ice loss is occurring because warm ocean water is rising from below and melting the base of the glaciers, dumping huge volumes of additional water — the equivalent of a Mt. Everest every two years — into the ocean.
- """,
- "science"),
- ("""Some scientists think we'll find signs of aliens within our lifetimes. Here's how.
- Finding extraterrestrial life is the essence of science fiction. But it's not so far-fetched to predict that we might find evidence of life on a distant planet within a generation.
- "With new telescopes coming online within the next five or ten years, we'll really have a chance to figure out whether we're alone in the universe," says Lisa Kaltenegger, an astronomer and director of Cornell's new Institute for Pale Blue Dots, which will search for habitable planets. "For the first time in human history, we might have the capability to do this."
- """,
- "science"),
- ("""'Magic' Mushrooms in Royal Garden: What Is Fly Agaric?
- Hallucinogenic mushrooms are perhaps the last thing you'd expect to find growing in the Queen of England's garden.
- Yet a type of mushroom called Amanita muscaria — commonly known as fly agaric, or fly amanita — was found growing in the gardens of Buckingham Palace by the producers of a television show, the Associated Press reported on Friday (Dec. 12).
- A. muscaria is a bright red-and-white mushroom, and the fungus is psychoactive when consumed.
- """,
- "science"),
- ("""Upcoming Parks : 'Lost Corner' Finds New Life in Sandy Springs
- At the corner of Brandon Mill Road, where Johnson Ferry Road turns into Dalrymple Road, tucked among 24 forested acres, sits an early 20th Century farmhouse. A vestige of Sandy Springs' past, the old home has found new life as the centerpiece of Lost Forest Preserve. While the preserve isn't slated to officially debut until some time next year, the city has opened the hiking trails to the public until construction begins on the permanent parking lot (at the moment the parking lot is a mulched area). The new park space includes community garden plots, a 4,000-foot-long hiking trail and an ADA-accessible trail through the densely wooded site. For Atlantans seeking an alternate escape to serenity (or those who dig local history), it's certainly worth a visit.
- """,
- "science"),
- ("""Stargazers across the world got a treat this weekend when the Geminids meteor shower gave the best holiday displays a run for their money.
- The meteor shower is called the "Geminids" because they appear as though they are shooting out of the constellation of Gemini. The meteors are thought to be small pieces of an extinct comment called 3200 Phaeton, a dust cloud revolving around the sun. Phaeton is thought to have lost all of its gas and to be slowly breaking apart into small particles.
- Earth runs into a stream of debris from 3200 Phaethon every year in mid-December, causing a shower of meteors, which hit its peak over the weekend.
- """,
- "science"),
- ("""Envisioning a River of Air
- By the classification rules of the world of physics, we all know that the Earth's atmosphere is made of gas (rather than liquid, solid, or plasma). But in the world of flying it's often useful to think
- """,
- "science"),
- ("""Following Sunday's 17-7 loss to the Seattle Seahawks, the San Francisco 49ers were officially eliminated from playoff contention, and they have referee Ed Hochuli to blame. OK, so they have a lot of folks to point the finger at for their 7-7 record, but Hochuli's incorrect call is the latest and easiest scapegoat.
- """
- , "sport"),
- ("""Kobe Bryant and his teammates have an odd relationship. That makes sense: Kobe Bryant is an odd guy, and the Los Angeles Lakers are an odd team.
- They’re also, for the first time this season, the proud owners of a three-game winning streak. On top of that, you may have heard, Kobe Bryant passed Michael Jordan on Sunday evening to move into third place on the NBA’s all-time scoring list.
- """
- , "sport"),
- ("""The Patriots continued their divisional dominance and are close to clinching home-field advantage throughout the AFC playoffs. Meanwhile, both the Colts and Broncos again won their division titles with head-to-head wins.The Bills' upset of the Packers delivered a big blow to Green Bay's shot at clinching home-field advantage throughout the NFC playoffs. Detroit seized on the opportunity and now leads the NFC North.
- """
- , "sport"),
- ("""If you thought the Washington Redskins secondary was humbled by another scintillating performance from New Yorks Giants rookie wide receiver sensation Odell Beckham Jr., think again.In what is becoming a weekly occurrence, Beckham led NFL highlight reels on Sunday, collecting 12 catches for 143 yards and three touchdowns in Sunday's 24-13 victory against an NFC East rival.
- """
- , "sport")
- , ("""That was two touchdowns and 110 total yards for the three running backs. We break down the fantasy implications.The New England Patriots' rushing game has always been tough to handicap. Sunday, all three of the team's primary running backs put up numbers, and all in different ways, but it worked for the team, as the Patriots beat the Miami Dolphins, 41-13.
- """
- , "sport"),
- ("""General Santos (Philippines) (AFP) - Philippine boxing legend Manny Pacquiao vowed to chase Floyd Mayweather into ring submission after his US rival offered to fight him next year in a blockbuster world title face-off. "He (Mayweather) has reached a dead end. He has nowhere to run but to fight me," Pacquiao told AFP late Saturday, hours after the undefeated Mayweather issued the May 2 challenge on US television. The two were long-time rivals as the "best pound-for-pound" boxers of their generation, but the dream fight has never materialised to the disappointment of the boxing world.
- """
- , "sport"),
- ("""When St. John's landed Rysheed Jordan, the consensus was that he would be an excellent starter.
- So far, that's half true.
- Jordan came off the bench Sunday and tied a career high by scoring 24 points to lead No. 24 St. John's to a 74-53 rout of Fordham in the ECAC Holiday Festival.
- ''I thought Rysheed played with poise,'' Red Storm coach Steve Lavin said. ''Played with the right pace. Near perfect game.''
- """
- , "sport"),
- ("""Five-time world player of the year Marta scored three goals to lead Brazil to a 3-2 come-from-behind win over the U.S. women's soccer team in the International Tournament of Brasilia on Sunday. Carli Lloyd and Megan Rapinoe scored a goal each in the first 10 minutes to give the U.S. an early lead, but Marta netted in the 19th, 55th and 66th minutes to guarantee the hosts a spot in the final of the four-team competition.
- """
- , "sport"),
- ]
- def getwords(doc):
- splitter = re.compile('\\W*')
- words = set()
- for word in splitter.split(doc):
- if 2 < len(word) < 20:
- words.add(word.lower())
- return words
- class classifier:
- def __init__(self,getfeatures,filename=None):
- # Broj na parovi atribut/kategorija (feature/category)
- self.fc={}
- # Broj na dokumenti vo sekoja kategorija
- self.cc={}
- # funkcija za dobivanje na atributite (zborovite) vo dokumentot
- self.getfeatures=getfeatures
- # Zgolemuvanje na brojot na parovi atribut/kategorija
- def incf(self,f,cat):
- self.fc.setdefault(f,{})
- self.fc[f].setdefault(cat,0)
- self.fc[f][cat]+=1
- # Zgolemuvanje na brojot na predmeti(dokumenti) vo kategorija
- def incc(self,cat):
- self.cc.setdefault(cat,0)
- self.cc[cat]+=1
- # Dobivanje na brojot kolku pati
- # odreden atribut se ima pojaveno vo odredena kategorija
- def fcount(self,f,cat):
- if f in self.fc and cat in self.fc[f]:
- return float(self.fc[f][cat])
- return 0.0
- # Treniranje na klasifikatorot
- # Noviot predmet (dokument) item pripagja na kategorijata cat
- def train(self, item, cat):
- # Se zemaat atributite (zborovite) vo predmetot(dokumentot)
- features = self.getfeatures(item)
- # Se zgolemuva brojot na sekoj atribut vo ovaa kategorija
- for f in features:
- self.incf(f, cat)
- # Se zgolemuva brojot na predmeti
- # (dokumenti) vo ovaa kategorija
- self.incc(cat)
- # Dobivanje na brojot kolku pati
- # odreden atribut se ima pojaveno vo odredena kategorija
- def getFeatureCountsPerCategory(self, currentFeature, currentCategory):
- if currentFeature in self.fc and currentCategory in self.fc[currentFeature]:
- v= float(self.fc[currentFeature][currentCategory])
- else:
- v= 0.0
- #print('getFeatureCountsPerCategory',currentFeature, currentCategory, v)\n",
- return v
- # Dobivanje na brojot na predmeti(dokumenti) vo kategorija
- def getCategoryCount(self,currentCategory):
- if currentCategory in self.cc:
- return float(self.cc[currentCategory])
- return 0
- # Dobivanje na vkupniot broj na predmeti
- def getTotalCount(self):
- v=sum(self.cc.values())
- # print('Dobivanje na vkupniot broj na dokumenti:', v)
- return v
- def categories(self):
- return self.cc.keys()
- def fprob(self, f, cat):
- if self.getCategoryCount(cat) == 0: return 0
- # Verojatnosta e vkupniot broj na pati koga
- # ovoj atribut f (zbor) se pojavil vo ovaa
- # kategorija podeleno so vkupniot broj na
- # predmeti (dokumenti) vo ovaa kategorija
- return self.fcount(f, cat) / self.getCategoryCount(cat)
- def weightedprob(self, f, cat, prf, weight=1.0, ap=0.5):
- # Presmetaj ja osnovnata verojatnost
- basicprob = prf(f, cat)
- # Izbroj kolku pati se ima pojaveno ovoj atribut (zbor)
- # vo site kategorii
- totals = sum([self.fcount(f, c) for c in self.categories()])
- # Presmetaj ja tezinski usrednetata verojatnost
- bp = ((weight * ap) + (totals * basicprob)) / (weight + totals)
- return bp
- class naivebayes(classifier): # nasleduva od classifier
- def __init__(self, getfeatures):
- classifier.__init__(self, getfeatures)
- self.thresholds = {}
- def setthreshold(self, cat, t):
- self.thresholds[cat] = t
- def getthreshold(self, cat):
- if cat not in self.thresholds: return 1.0
- return self.thresholds[cat]
- def docprob(self,item,cat):
- # zemi gi zborovite vo dokumentot item
- features=self.getfeatures(item)
- # pomnozi gi verojatnostite na site zborovi
- p=1
- for f in features:
- p*=self.weightedprob(f,cat,self.fprob)
- return p
- def totalcount(self):
- v=sum(self.cc.values())
- #print('Dobivanje na vkupniot broj na dokumenti:', v)
- return v
- def prob(self, item, currentCategory):
- catprob = self.getCategoryCount(currentCategory) / self.getTotalCount()
- caclulateDocumentProbabilityInClass = self.docprob( item, currentCategory)
- # Bayes Theorem
- return caclulateDocumentProbabilityInClass * catprob / (1.0 / self.getTotalCount())
- # klasificiranje na dokument
- def classifyDocument(self, item, default=None):
- probs={}
- max=0.0
- for cat in self.categories():
- probs[cat]=self.prob(item, cat)
- #print(probs[cat], cat)
- if probs[cat]>max:
- max=probs[cat]
- best=cat
- # proveri dali verojatnosta e pogolema od
- # threshold*next best (sledna najdobra)
- for cat in probs:
- if cat==best: continue
- if probs[cat]*self.getthreshold(best)>probs[best]:
- return default
- return best, format(math.log(max,2) ,'.4f') # max
- if __name__=='__main__':
- cl = naivebayes(getwords)
- for i in train_data:
- cl.train(i[0], i[1])
- data = ["""Japan on Tuesday ordered the slaughter of some 4,000 chickens after officials confirmed bird flu at a poultry farm in the southwest of the country. DNA tests confirmed the H5 strain of the virus at a farm in Miyazaki after its owner reported more
- than 20 sudden deaths among his poultry on Sunday and Monday, the agriculture ministry said.""",
- """Mayweather can get the amount he wants. As early as January this year, I challenged him to a charity fight. Until now, he has not agreed to it. So, money is not the issue in our fight," Pacquiao said on his official website. "This fight is about legacy, this is about making the fans happy and, above all, this is for the good of boxing.""",
- """Joe Philbin arrived at work at 5:50 a.m. Monday, anxious to get the Miami Dolphins headed in the right direction. He's running out of time. Consecutive dismal defeats have left the Dolphins all but eliminated from the playoffs with two games to go, and there's widespread speculation Philbin will be fired after three seasons as coach. Predictably, Philbin deflected questions about his future and said he's only focused on the upcoming game Sunday against Minnesota.""",
- """ Former Arsenal and French striker Thierry Henry announced Tuesday he was retiring from football after a glittering two decades long career and will become a TV analyst for Sky Sports in England."After 20 years in the game I have decided to retire from professional football," Henry said in a statement on his Facebook page. Henry, 37, a world and European champion with France, and the scorer of 228 goals with Arsenal including 175 English Premier League goals for the club, finished his playing career at New York Red Bulls just days ago.""",
- """Liverpool had its chances, but David De Gea was monstrous in goal and his attack justified his strength between the sticks as Manchester United sunk the leaky visitors 3-0 at Old Trafford. De Gea has grown into one of the world’s best goalkeepers, and his best was on display today as Liverpool couldn’t find a way through despite great chances early and often for Raheem Sterling and Mario Balotelli. Liverpool began the brightest, with a testing corner inside five minutes, and a solid run from Adam Lallana down the flank which drew a very early yellow for Marouane Fellaini.""",
- """HONOLULU (AP) — Lava from a volcano on Hawaii's Big Island is on course to reach a shopping center with a gas station and a supermarket in seven to 10 days, officials said Monday.""",
- """To say that Osservoort's odyssey took five years wouldn't be accurate, but it has been quite a journey. She left her home town near Amsterdam in 2005, driving her tractor across Europe, and Africa and in an expedition that took four years. """,
- """Just last week, preservationists at the Old Pejeta animal sanctuary in Kenya conceded that their one male and two female northern white rhinos will not reproduce naturally. The animals were flown from the Czech zoo to the Kenyan conservancy in December 2009 in hopes that the natural environment could be easier for them to breed there than in captivity."""
- ]
- for ins in data:
- words=getwords(ins)
- category = cl.categories()
- for j in words:
- for k in category:
- print j, k, round(cl.fprob(j,k),4), round(cl.weightedprob(j,k,cl.fprob),4)
- x,y = cl.classifyDocument(ins)
- print x,y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement