当前位置: 首页>>代码示例>>Python>>正文


Python NaiveBayesClassifier.classify方法代码示例

本文整理汇总了Python中textblob.classifiers.NaiveBayesClassifier.classify方法的典型用法代码示例。如果您正苦于以下问题:Python NaiveBayesClassifier.classify方法的具体用法?Python NaiveBayesClassifier.classify怎么用?Python NaiveBayesClassifier.classify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在textblob.classifiers.NaiveBayesClassifier的用法示例。


在下文中一共展示了NaiveBayesClassifier.classify方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class ExpenseClassifier:

    def __init__(self):
        training_data = self._load_data("data")
        self.category_classifier  = NaiveBayesClassifier([(x[0], x[1]) for x in  training_data])
        self.avoidability_classifier = NaiveBayesClassifier([(x[0], x[2]) for x in  training_data])
        self.ordinary_classifier =  NaiveBayesClassifier([(x[0], x[3]) for x in  training_data])

    def classify(self, description):
        res = {}
        res['category'] = self.category_classifier.classify(description)
        res['avoidable'] = self.avoidability_classifier.classify(description)
        res['ordinary'] = self.ordinary_classifier.classify(description)
        return res

    def accuracy(self):
        test_data = self._load_data("test")
        res = {}
        res['category'] = self.category_classifier.accuracy([(x[0], x[1]) for x in test_data])
        res['avoidable'] = self.avoidability_classifier.accuracy([(x[0], x[2]) for x in test_data])
        res['ordinary'] = self.ordinary_classifier.accuracy([(x[0], x[3]) for x in test_data])
        return res

    def _load_data(self, folder):
        data = []
        for f in glob.glob(folder + "/*.csv"):
            with open(f) as csvfile:
                spamreader = csv.reader(csvfile, delimiter=',')
                for row in spamreader:
                    if row[DESCRIPTION] and row[CATEGORY] and row[AVOIDABLE] and row[ORDINARY]:
                        data.append((norm(row[DESCRIPTION]), row[CATEGORY], row[AVOIDABLE], row[ORDINARY]))
        return data
开发者ID:giuse88,项目名称:expense_classifier,代码行数:34,代码来源:classifier.py

示例2: main

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
def main():
    json = raw_input("Where is the json training set?")
    print "Program start", time.ctime() #debug
    with open(json, 'r') as file:
        classifier = NaiveBayesClassifier(file, format='json')
        print "Classifier done!", time.ctime() #debug
    test = raw_input("Where is the test eml_folder?")
    print "Testing...", time.ctime()
    for emails in dir_list(test):
        print classifier.classify(emails)
    print "Testing done", time.ctime()
开发者ID:findingnino,项目名称:SentimentAnalyzer,代码行数:13,代码来源:SentimentAnalysis.py

示例3: get_analysis

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
def get_analysis(s):

    train = [
        ('I love this sandwich.', 'pos'),
        ('This is an amazing place!', 'pos'),
        ('I feel very good about these beers.', 'pos'),
        ('This is my best work.', 'pos'),
        ("What an awesome view", 'pos'),
        ('I do not like this restaurant', 'neg'),
        ('I am tired of this stuff.', 'neg'),
        ("I can't deal with this", 'neg'),
        ('He is my sworn enemy!', 'neg'),
        ('My boss is horrible.', 'neg')
    ]


    cl = NaiveBayesClassifier(train)

    tweets = Tweet.objects.filter(search_term = s)

    result = []

    for t in tweets:
        d = {}
        c = cl.classify(t.tw_text)
        d['text'] = t.tw_text
        d['res'] = c
        result.append(d)

    return result
开发者ID:harshvb7,项目名称:twitter_analysis,代码行数:32,代码来源:get_analysis.py

示例4: TimeLogicAdapter

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class TimeLogicAdapter(LogicAdapter):
    """
    The TimeLogicAdapter returns the current time.
    """

    def __init__(self, **kwargs):
        super(TimeLogicAdapter, self).__init__(**kwargs)

        training_data = [
            ('what time is it', 1),
            ('do you know the time', 1),
            ('do you know what time it is', 1),
            ('what is the time', 1),
            ('it is time to go to sleep', 0),
            ('what is your favorite color', 0),
            ('i had a great time', 0),
            ('what is', 0)
        ]

        self.classifier = NaiveBayesClassifier(training_data)

    def process(self, statement):
        now = datetime.now()

        confidence = self.classifier.classify(statement.text.lower())
        response = Statement('The current time is ' + now.strftime('%I:%M %p'))

        return confidence, response
开发者ID:AugustoQueiroz,项目名称:ChatterBot,代码行数:30,代码来源:time_adapter.py

示例5: TimeLogicAdapter

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class TimeLogicAdapter(LogicAdapter):

    def __init__(self, **kwargs):
        super(TimeLogicAdapter, self).__init__(**kwargs)

        training_data = [
            ("what time is it", 1),
            ("do you know the time", 1),
            ("do you know what time it is", 1),
            ("what is the time", 1),
            ("do you know the time", 0),
            ("it is time to go to sleep", 0),
            ("what is your favorite color", 0),
            ("i had a great time", 0),
            ("what is", 0)
        ]

        self.classifier = NaiveBayesClassifier(training_data)

    def process(self, statement):
        now = datetime.now()

        confidence = self.classifier.classify(statement.text.lower())
        response = Statement("The current time is " + now.strftime("%I:%M %p"))

        return confidence, response
开发者ID:fmoliveira,项目名称:ChatterBot,代码行数:28,代码来源:time_adapter.py

示例6: main

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
def main():
    data =[]
    train =[]
    test =[] 
    with open('hellopeter_labelled.csv', 'rb') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',')
        spamreader = list(spamreader)
        for row in spamreader:
            if (row[13] =='strongly positive'): 
                data.append((row[8],'pos'))
            if (row[13] =='positive' ): 
                data.append((row[8],'pos'))
            if ( row[13] =='neutral' ): 
                data.append((row[8],'neu'))
            if ( row[13] =='negative'): 
                data.append((row[8],'neg'))
            if (row[13] =='strongly negative' ): 
                data.append((row[8],'neg'))
                
                
    train = data[:1000]
    test = data[1001:]
    
    for innf in test:
        print innf
            
    cl = NaiveBayesClassifier(train)
   
    for tnew in test: 
            print '%%%%%%%'
            print ' '
            print  tnew[0]
            print  tnew[1]
            print '%%%%%%%'
            print '#######'
            cl.classify(tnew[0])
            prob_class =  cl.prob_classify(tnew[0])
            print '----max prob---'
            print prob_class.max()
            print '-----+ve-----'
            print prob_class.prob("pos")
            print '-----neutral-----'
            print prob_class.prob("neu")
            print '------ve-----'
            print prob_class.prob("neg")
            
    cl.accuracy(test)
开发者ID:dlunga,项目名称:serViz,代码行数:49,代码来源:HelloPeterSentiments.py

示例7: TimeLogicAdapter

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class TimeLogicAdapter(LogicAdapter):
    """
    The TimeLogicAdapter returns the current time.
    """

    def __init__(self, **kwargs):
        super(TimeLogicAdapter, self).__init__(**kwargs)

        training_data = [
            ("what time is it", 1),
            ("do you know the time", 1),
            ("do you know what time it is", 1),
            ("what is the time", 1),
            ("do you know the time", 0),
            ("it is time to go to sleep", 0),
            ("what is your favorite color", 0),
            ("i had a great time", 0),
            ("what is", 0)
        ]

        self.classifier = NaiveBayesClassifier(training_data)

    def process(self, statement, tag_processing = None):

        user_input = statement.text.lower()
        if "time" not in user_input:
            return 0, Statement("")

        try:
            # Find the time zone of the user based on latitude and longitude to get the correct time
            g          = geocoders.GoogleV3()
            user       = tag_processing.user
            lat,lon    = user.get_latitude_longitude()
            timezone   = g.timezone((lat,lon))

            now = datetime.now(timezone)

            confidence = self.classifier.classify(user_input)
            response = Statement("The current time is " + now.strftime("%I:%M %p"))
        except:
            confidence = self.classifier.classify(user_input)
            response = Statement("Sorry. I cannot find the current time. Possible bad user location based on latitude and longitude. Please try again later")

        return confidence, response
开发者ID:machinelliummavericks,项目名称:chatbot-hibot,代码行数:46,代码来源:time_adapter.py

示例8: __init__

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class NaiveBayesAnalyzer:
    cl = None

    def __init__(self):
        with open("training_data.json", "r") as f:
            self.cl = NaiveBayesClassifier(f, format="json")
        self.cl.show_informative_features(20)

    def analyze(self, text):
        return self.cl.classify(text)
开发者ID:ayhun,项目名称:MMDS,代码行数:12,代码来源:sentiment.py

示例9: TwitterTrendAdapter

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class TwitterTrendAdapter(LogicAdapter):
    def __init__(self, **kwargs):
        super(TwitterTrendAdapter, self).__init__(**kwargs)

        training_data = [
            ("what's trending in ", 1),
            ('what is trending in', 1),
            ('what is', 0),
            ('who is', 0),
            ('who was', 0),
            ('what can you tell me about', 0),
            ('what do you know about', 0),
            ('any clue about', 0),
            ('where is',0),
            ('located', 0),
            ('what is happening', 1)
        ]

        self.classifier = NaiveBayesClassifier(training_data)

    def process(self, statement):
        confidence = self.classifier.classify(statement.text.lower())
        tokens = nltk.word_tokenize(str(statement))
        tagged = nltk.pos_tag(tokens)
        nouns = [word for word, pos in tagged if (pos == 'NN' or pos == 'NNP' or pos =='JJ' or pos == 'NNS' or pos == 'NNPS')]
        auth = OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
        auth.set_access_token(twitter_access_key, twitter_access_secret)
        api = tweepy.API(auth)
        trendsName = ""
        for noun in nouns:
            try:
                html = urllib.urlopen(
                    'http://where.yahooapis.com/v1/places.q(' + noun + ')?appid=' + yahoo_client_Id).read()
                soup = BeautifulSoup(html, 'html.parser')
                woeids = soup.find('woeid').contents
                for woeid in woeids:
                    id = ' '.join(woeid.string.split())
                    trends1 = api.trends_place(str(id))
                    data = trends1[0]
                    # grab the trends
                    trends = data['trends']
                    names1 = [trend['name'] for trend in trends]
                    trendsName += ' '.join(names1)
            except:
                pass
        if len(nouns) != 0 and len(trendsName)!=0:
            response = Statement("Jarvis: "+trendsName)
        else:
            response = Statement("")
            confidence=0
        return confidence, response
开发者ID:anirudh24,项目名称:self_aware_bot,代码行数:53,代码来源:tweet_trend.py

示例10: TwitterTagAdapter

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class TwitterTagAdapter(LogicAdapter):
    def __init__(self, **kwargs):
        super(TwitterTagAdapter, self).__init__(**kwargs)

        training_data = [
            ('what are people talking about', 1),
            ("what's trending in", 0),
            ('what is going on with', 1),
            ('what are reviews', 1),
            ('what is going on',1),
            ('tweetind',1),
            ('what can you tell me about', 0),
            ('what do you know about', 0),
            ('any clue about', 0),
            ('where is',0),
            ('located', 0),
            ('what is happening', 0)
        ]

        self.classifier = NaiveBayesClassifier(training_data)

    def process(self, statement):
        confidence = self.classifier.classify(statement.text.lower())
        tokens = nltk.word_tokenize(str(statement))
        tagged = nltk.pos_tag(tokens)
        nouns = [word for word, pos in tagged if
                 (pos == 'NN' or pos == 'NNP' or pos =='JJ' or pos == 'NNS' or pos == 'NNPS')]
        downcased = [x.lower() for x in nouns]
        searchTerm = " ".join(downcased).encode('utf-8')
        #"http://where.yahooapis.com/v1/places.q('Place name')?appid=yourappidhere"
        st=""
        if len(nouns) != 0:
            auth = OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
            auth.set_access_token(twitter_access_key, twitter_access_secret)

            api = tweepy.API(auth)
            for status in tweepy.Cursor(api.search, q='#'+searchTerm).items(20):
                st = st+status.text
            response = Statement("Jarvis: "+st)
        else:
            response = Statement("Jarvis: "+"Sorry sir, Nothing Found")
        return confidence, response


#what's trending in city
#movie reviews
#people talking about some topic
开发者ID:anirudh24,项目名称:self_aware_bot,代码行数:49,代码来源:tweet_tag.py

示例11: Scraper

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class Scraper ():
    def __init__(self, traing_data):
        self.cl = NaiveBayesClassifier(traing_data)

    def classifier(self, data):
        return self.cl.classify(data)

    def fetch_data(self):
        BASEURL = "https://news.ycombinator.com/news?p="
        for n in range(1):
            r = requests.get(BASEURL + str(n))
            soup = BeautifulSoup(r.content, "html.parser")
            for title in soup.findAll('tr', {'class': 'athing'}):  # Fetch Title
                for t in title.findAll('a', text=True):
                    art_title = t.text.encode("utf8")
                    art_link = t['href']
                    print (self.classifier(art_title), art_title)
开发者ID:InSertCod3,项目名称:Hacker-News-Scrapper,代码行数:19,代码来源:scrap.py

示例12: App

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class App(DictMixin):
    """ 
    Aggregation of intents.
    """
    def __init__(self,name,greeting):
        self.name = name
        self.greeting = greeting
        self.intents = {}
        self.classifier = None
    
    def __getitem__(self,key):
        return self.intents[key]
    
    def __setitem__(self,key,value):
        self.intents[key] = value
        
        # train classifier
        phrase_file = file(value.phrases,'r')
        phrase_data = yaml.safe_load(phrase_file)
        phrases = [(phrase,value.name) for phrase in phrase_data['Phrases']]
        
        if self.classifier:
            self.classifier.update(phrases)
        else:
            self.classifier = Classifier(phrases)
    
    def __delitem__(self,key):
        del self.intents[key]
    
    def keys(self):
        return self.intents.keys()
    
    def intent_for(self,phrase):
        """ 
        Attempt to match an intent to the supplied phrase, using the onboard classifier.
        """
        if not self.classifier:
            # has not been properly initializes
            raise IntentNotFound('Classifier not initialized')
        
        try:
            return self.intents[self.classifier.classify(phrase)]
        except KeyError:
            raise IntentNotFound
开发者ID:Axilent,项目名称:talkback,代码行数:46,代码来源:core.py

示例13: nayebayesreport

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
def nayebayesreport(fileFullPath):
    print  "nayebayesreport came"
    print (fileFullPath)
    sentimentDtls = []
    patternCountMap = {
                       "Negative" : 0,
                       "Positive" : 0,
                       "Neutral" : 0,
                       "Total" : 0,
                       }
    
    
    cl = NaiveBayesClassifier(getTrainData())

    print "train data loaded"
    with open(fileFullPath, 'r') as f:
        for line in f:
            try:
                print line
                if line and len(line.strip()) > 0:
                    trainedResult = cl.classify(line)
                        
                    patternResult = "Negative"
                    if "pos" == trainedResult:
                        patternResult = "Positive"
                    
                    patternCountMap[patternResult] = patternCountMap[patternResult] + 1
                    patternCountMap["Total"] = patternCountMap["Total"] + 1
                    
                    sentimentDtls.append({
                                          "sentiment" : patternResult,
                                          "feedback" : line
                                         })
            except Exception:
                print(traceback.format_exc())
                print(line)
    
    addBayesClassifierResult(sentimentDtls)
    return
开发者ID:mkumar87,项目名称:DartsVDSIHackathon,代码行数:41,代码来源:util.py

示例14: __init__

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
class Classifier:
    def __init__(self):
        self.cachedStopWords = stopwords.words("english")
        self.path = os.path.dirname(os.path.abspath(__file__))

    def train(self, train_set):
        train_data = []
        for t in train_set:
            train_data.append((self._cvobj_to_string(t[0]),t[1]))
        print "Training model..."
        #print train_data
        self.cl = NaiveBayesClassifier(train_data)
        #print self._cvobj_to_string(train_set[0][0])

    def _cvobj_to_string(self, cv):
        str = ""
        for exp in cv['experience']:
            str += (exp['description']+" ")
        for proj in cv['project']:
            str += (proj['title']+" ")
            str += (proj['description']+" ")
        for skill in cv['skill']:
            str += (skill+" ")
        str = str.decode("utf-8", "replace")
        str = ' '.join([word for word in str.split() if word not in self.cachedStopWords])
        return str

    def classify(self, cv):
        return self.cl.classify(self._cvobj_to_string(cv))

    def save(self):
        pickle.dump( self.cl, open( self.path+"/cv_model.cvm", "wb" ) )
        print "CV classifier saved."

    def load(self):
        self.cl = pickle.load( open( self.path+"/cv_model.cvm", "rb" ) )
        print "CV classifier loaded."
开发者ID:yjlo123,项目名称:CViA,代码行数:39,代码来源:Classifier.py

示例15: NaiveBayesAnalyzerParser

# 需要导入模块: from textblob.classifiers import NaiveBayesClassifier [as 别名]
# 或者: from textblob.classifiers.NaiveBayesClassifier import classify [as 别名]
def NaiveBayesAnalyzerParser(text):
    
    train =[('creates jobs', 'pos'),
            ('create jobs', 'pos'),
            ('created jobs', 'pos'),
            ('new jobs', 'pos'),
            ('jobs wanted', 'pos'),
            ('jobs needed', 'pos'),
            ('jobs call by', 'pos'),
            ('unemployment falls', 'pos'),
            ('bring jobs', 'pos'),
            ('jobs comming', 'pos'),
            ('unemployment drops', 'pos'),
            ('cut jobs', 'neg'),
            ('cutting jobs', 'neg'),
            ('cuts jobs', 'neg'),
            ('lost jobs', 'neg'),
            ('job loss', 'neg'),
            ('losing jobs', 'neg'),
            ('lose jobs', 'neg'),
            ('jobs not kept', 'neg'),
            ('jobs trim', 'neg'),
            ('unemployment rises', 'neg'),
            ('drops', 'neg'),
            ('drop', 'neg'),
            ('dollar falls', 'neg'),
        ]
    cl = NaiveBayesClassifier(train)
    sentiment = TextBlob(text, analyzer=NaiveBayesAnalyzer()).sentiment
    #Sentiment(classification='pos', p_pos=0.6023632501327671, p_neg=0.3976367498672331)
    #print(sentiment)
    subjectivity = 1 - (max(sentiment.p_pos,sentiment.p_neg) - min(sentiment.p_pos,sentiment.p_neg))
    if cl.classify(text) == 'pos':
        return (sentiment.p_pos, subjectivity)
    else:
        return (sentiment.p_neg*-1, subjectivity)
开发者ID:nimmaj,项目名称:sentiment-trading,代码行数:38,代码来源:events.py


注:本文中的textblob.classifiers.NaiveBayesClassifier.classify方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。