本文整理汇总了Python中nltk.tokenize.PunktSentenceTokenizer.sentences_from_text方法的典型用法代码示例。如果您正苦于以下问题:Python PunktSentenceTokenizer.sentences_from_text方法的具体用法?Python PunktSentenceTokenizer.sentences_from_text怎么用?Python PunktSentenceTokenizer.sentences_from_text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.tokenize.PunktSentenceTokenizer
的用法示例。
在下文中一共展示了PunktSentenceTokenizer.sentences_from_text方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: teeth
# 需要导入模块: from nltk.tokenize import PunktSentenceTokenizer [as 别名]
# 或者: from nltk.tokenize.PunktSentenceTokenizer import sentences_from_text [as 别名]
from nltk.tokenize import PunktSentenceTokenizer
from nltk.collocations import *
from nltk.tokenize import word_tokenize
from nltk.util import ngrams
txt = "I was in the room in Los Angeles in 1988, about 200 feet from Michael Dukakis, when Bernard Shaw asked him what he'd do if his wife were raped. Now that really was a sucker punch of a question. I was on the other side of the arena, in Cleveland, when Donald Trump bared his teeth (metaphorically speaking) at Megyn Kelly."
pst = PunktSentenceTokenizer()
sents = pst.sentences_from_text(txt.encode('utf-8').replace('\n',''))
print sents
bigrams = ngrams(word_tokenize(sents[1].replace('.','')),2)
print bigrams
示例2: PunktSentenceTokenizer
# 需要导入模块: from nltk.tokenize import PunktSentenceTokenizer [as 别名]
# 或者: from nltk.tokenize.PunktSentenceTokenizer import sentences_from_text [as 别名]
pst = PunktSentenceTokenizer()
files =[]
words = []
##allow to take a directory
for i in sys.argv:
match = re.match(".*\.txt$", i)
if match:
files.append(i)
print 'file_name' + '\t' + 'politcal_stance'
for f in files:
with open(f,"rb") as class_file:
if sys.argv[1] == '--sents':
data = class_file.read().replace('\n', '')
sents = pst.sentences_from_text(data)
for sent in sents:
sent_words = nltk.word_tokenize(sent)
for word in sent_words:
words.append(word)
feats = dict([(word, True) for word in words])
else:
for line in class_file:
line_words = nltk.word_tokenize(line)
for word in line_words:
words.append(word)
feats = dict([(word, True) for word in words])
print f + '\t' + classifier.classify(feats)
示例3: sentence_parser
# 需要导入模块: from nltk.tokenize import PunktSentenceTokenizer [as 别名]
# 或者: from nltk.tokenize.PunktSentenceTokenizer import sentences_from_text [as 别名]
def sentence_parser( input_str ):
sentences_tokenize = PunktSentenceTokenizer()
for sentence in sentences_tokenize.sentences_from_text( input_str ):
if sentence.strip() != u'':
yield sentence