當前位置: 首頁>>代碼示例>>Python>>正文


Python nltk.wordpunct_tokenize方法代碼示例

本文整理匯總了Python中nltk.wordpunct_tokenize方法的典型用法代碼示例。如果您正苦於以下問題:Python nltk.wordpunct_tokenize方法的具體用法?Python nltk.wordpunct_tokenize怎麽用?Python nltk.wordpunct_tokenize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在nltk的用法示例。


在下文中一共展示了nltk.wordpunct_tokenize方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: tokenize

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def tokenize(self, text):
        """
        Returns a list of individual tokens from the text utilizing NLTK's
        tokenize built in utility (far better than split on space). It also
        removes any stopwords and punctuation from the text, as well as
        ensure that every token is normalized.

        For now, token = word as in bag of words (the feature we're using).
        """
        for token in wordpunct_tokenize(text):
            token = self.normalize(token)
            if token in self.punctuation: continue
            if token in self.stopwords: continue
            yield token 
開發者ID:georgetown-analytics,項目名稱:product-classifier,代碼行數:16,代碼來源:features.py

示例2: parse

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def parse(sent):
    parser = nltk.ChartParser(grammar)
    tokens = nltk.wordpunct_tokenize(sent)
    return parser.parse(tokens) 
開發者ID:foxbook,項目名稱:atap,代碼行數:6,代碼來源:parse.py

示例3: recommend

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def recommend(self, terms):
        """
        Given input list of ingredient terms,
        return the k closest matching recipes.

        :param terms: list of strings
        :return: list of document indices of documents
        """
        vect_doc = self.vect.transform(wordpunct_tokenize(terms))
        distance_matches = self.knn.transform(vect_doc)
        # the result is a list with a 2-tuple of arrays
        matches = distance_matches[0][1][0]
        # the matches are the indices of documents
        return matches 
開發者ID:foxbook,項目名稱:atap,代碼行數:16,代碼來源:recommender.py

示例4: query

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def query(self, terms):
        """
        Given input list of ingredient terms,
        return the k closest matching recipes.

        :param terms: list of strings
        :return: list of document indices of documents
        """
        vect_doc = self.transformer.named_steps['transform'].fit_transform(
            wordpunct_tokenize(terms)
        )
        dists, inds = self.tree.query(vect_doc, k=self.k)
        return inds[0] 
開發者ID:foxbook,項目名稱:atap,代碼行數:15,代碼來源:recommender.py

示例5: words

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def words(self, fileids=None, categories=None):
        """
        Uses the built in word tokenizer to extract tokens from sentences.
        Note that this method uses BeautifulSoup to parse HTML content.
        """
        for sentence in self.sents(fileids, categories):
            for token in wordpunct_tokenize(sentence):
                yield token 
開發者ID:foxbook,項目名稱:atap,代碼行數:10,代碼來源:reader.py

示例6: tokenize

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def tokenize(self, fileids=None, categories=None):
        """
        Segments, tokenizes, and tags a document in the corpus.
        """
        for paragraph in self.corpus.paras(fileids=fileid):
            yield [
                pos_tag(nltk.wordpunct_tokenize(sent))
                for sent in nltk.sent_tokenize(paragraph)
            ] 
開發者ID:foxbook,項目名稱:atap,代碼行數:11,代碼來源:reader.py

示例7: tokenize

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def tokenize(self, fileids=None, categories=None):
        """
        Segments, tokenizes, and tags a document in the corpus.
        """
        for paragraph in self.paras(fileids=fileids):
            yield [
                pos_tag(wordpunct_tokenize(sent))
                for sent in sent_tokenize(paragraph)
            ] 
開發者ID:foxbook,項目名稱:atap,代碼行數:11,代碼來源:reader.py

示例8: describe

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def describe(self, fileids=None, categories=None):
        """
        Performs a single pass of the corpus and
        returns a dictionary with a variety of metrics
        concerning the state of the corpus.
        """
        started = time.time()

        # Structures to perform counting.
        counts  = nltk.FreqDist()
        tokens  = nltk.FreqDist()

        # Perform single pass over paragraphs, tokenize and count
        for para in self.paras(fileids, categories):
            counts['paras'] += 1

            for sent in sent_tokenize(para):
                counts['sents'] += 1

                for word in wordpunct_tokenize(sent):
                    counts['words'] += 1
                    tokens[word] += 1

        # Compute the number of files and categories in the corpus
        n_fileids = len(self.resolve(fileids, categories) or self.fileids())
        n_topics  = len(self.categories(self.resolve(fileids, categories)))

        # Return data structure with information
        return {
            'files':  n_fileids,
            'topics': n_topics,
            'paras':  counts['paras'],
            'sents':  counts['sents'],
            'words':  counts['words'],
            'vocab':  len(tokens),
            'lexdiv': float(counts['words']) / float(len(tokens)),
            'ppdoc':  float(counts['paras']) / float(n_fileids),
            'sppar':  float(counts['sents']) / float(counts['paras']),
            'secs':   time.time() - started,
        } 
開發者ID:foxbook,項目名稱:atap,代碼行數:42,代碼來源:reader.py

示例9: tokenize

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def tokenize(self, fileid):
        """
        Segments, tokenizes, and tags a document in the corpus. Returns a
        generator of paragraphs, which are lists of sentences, which in turn
        are lists of part of speech tagged words.
        """
        for paragraph in self.corpus.paras(fileids=fileid):
            yield [
                pos_tag(wordpunct_tokenize(sent))
                for sent in sent_tokenize(paragraph)
            ] 
開發者ID:foxbook,項目名稱:atap,代碼行數:13,代碼來源:preprocess.py

示例10: words

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def words(self):
        """
        Returns a generator of words.
        """
        for sent in self.sents():
            for word in nltk.wordpunct_tokenize(sent):
                yield word 
開發者ID:foxbook,項目名稱:atap,代碼行數:9,代碼來源:am_reader.py

示例11: tagged_tokens

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def tagged_tokens(self):
        for sent in self.sents():
            for word in nltk.wordpunct_tokenize(sent):
                yield nltk.pos_tag(word) 
開發者ID:foxbook,項目名稱:atap,代碼行數:6,代碼來源:reader.py

示例12: calculate_language_scores

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def calculate_language_scores(text):
    """
    Calculate probability of given text to be written in several languages and
    return a dictionary that looks like {'french': 2, 'spanish': 4, 'english': 0}.

    :param text: Text to analyze.
    :type text: str

    :return: Dictionary with languages and unique stopwords seen in analyzed text.
    :rtype: dict(str -> int)

    :raises: TypeError
    """
    if not isinstance(text, basestring):
        raise TypeError("Expected basestring, got '%s' instead" % type(text))
    if not text:
        return {}

    languages_ratios = {}

    # Split the text into separate tokens, using natural language punctuation signs.
    tokens = wordpunct_tokenize(text)
    tokenized_words = [word.lower() for word in tokens]

    for language in stopwords.fileids():
        stopwords_set = set(stopwords.words(language))
        words_set = set(tokenized_words)
        common_elements = words_set.intersection(stopwords_set)
        languages_ratios[language] = len(common_elements)  # language "score"

    return languages_ratios


#------------------------------------------------------------------------------ 
開發者ID:blackye,項目名稱:luscan-devel,代碼行數:36,代碼來源:natural_language.py

示例13: normalize

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def normalize(sent):
    return wordpunct_tokenize(sent.lower()) 
開發者ID:edward-zhu,項目名稱:dialog,代碼行數:4,代碼來源:cluster.py

示例14: tokenize

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def tokenize(sent):
    tokens = tokenizer.tokenize(sent)
    ret = []
    for t in tokens:
        if '<' not in t:
            ret.extend(wordpunct_tokenize(t))
        else:
            ret.append(t)
    return ret 
開發者ID:edward-zhu,項目名稱:dialog,代碼行數:11,代碼來源:utils.py

示例15: tokenize_and_normalize

# 需要導入模塊: import nltk [as 別名]
# 或者: from nltk import wordpunct_tokenize [as 別名]
def tokenize_and_normalize(s):
    """Tokenize and normalize string."""
    token_list = []
    tokens = wordpunct_tokenize(s.lower())
    token_list.extend([x for x in tokens if not re.fullmatch('[' + string.punctuation + ']+', x)])
    return token_list 
開發者ID:wasiahmad,項目名稱:transferable_sent2vec,代碼行數:8,代碼來源:helper.py


注:本文中的nltk.wordpunct_tokenize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。