8

我编写了以下代码来标记来自文件 samp.txt 的输入段落。任何人都可以帮我查找并打印文件中的句子、单词和字符的数量吗?为此,我在 python 中使用了 NLTK。

>>>import nltk.data
>>>import nltk.tokenize
>>>f=open('samp.txt')
>>>raw=f.read()
>>>tokenized_sentences=nltk.sent_tokenize(raw)
>>>for each_sentence in tokenized_sentences:
...   words=nltk.tokenize.word_tokenize(each_sentence)
...   print each_sentence   #prints tokenized sentences from samp.txt
>>>tokenized_words=nltk.word_tokenize(raw)
>>>for each_word in tokenized_words:
...   words=nltk.tokenize.word_tokenize(each_word)
...   print each_words      #prints tokenized words from samp.txt
4

8 回答 8

11

试试这个方法(这个程序假设你正在使用由 指定的目录中的一个文本文件dirpath):

import nltk
folder = nltk.data.find(dirpath)
corpusReader = nltk.corpus.PlaintextCorpusReader(folder, '.*\.txt')

print "The number of sentences =", len(corpusReader.sents())
print "The number of patagraphs =", len(corpusReader.paras())
print "The number of words =", len([word for sentence in corpusReader.sents() for word in sentence])
print "The number of characters =", len([char for sentence in corpusReader.sents() for word in sentence for char in word])

希望这可以帮助

于 2011-02-22T06:38:55.673 回答
3

使用 nltk,您还可以使用 FreqDist(参见O'Reillys Book Ch3.1

在你的情况下:

import nltk
raw = open('samp.txt').read()
raw = nltk.Text(nltk.word_tokenize(raw.decode('utf-8')))
fdist = nltk.FreqDist(raw)
print fdist.N()
于 2014-12-20T14:43:34.143 回答
3

如果有人来这里有什么价值。这解决了我认为 OP 提出的所有问题。如果使用该textstat软件包,计算句子和字符非常容易。每个句子末尾的标点符号都有一定的重要性。

import textstat

your_text = "This is a sentence! This is sentence two. And this is the final sentence?"
print("Num sentences:", textstat.sentence_count(your_text))
print("Num chars:", textstat.char_count(your_text, ignore_spaces=True))
print("Num words:", len(your_text.split()))
于 2017-08-21T20:14:23.167 回答
0
  • 字符很容易数。
  • 段落通常也很容易计算。每当您看到两个连续的换行符时,您可能有一个段落。您可能会说枚​​举或无序列表是一个段落,即使它们的条目可以由两个换行符分隔。标题或标题后面也可以跟两个换行符,尽管它们显然不是段落。还要考虑文件中单个段落的情况,后面有一个或没有换行符。
  • 句子很棘手。您可以选择句号、感叹号或问号,后跟空格或文件结尾。这很棘手,因为有时冒号标志着句子的结尾,有时却没有。通常,如果是英文,下一个非空白字符将是大写字母。但有时不是;例如,如果它是一个数字。有时一个开括号标志着句子的结尾(但这是有争议的,就像在这种情况下一样)。
  • 言语也很棘手。通常单词由空格或标点符号分隔。有时破折号分隔一个单词,有时则不是。例如,连字符就是这种情况。

对于单词和句子,您可能需要清楚地说明您对句子的定义以及一个单词和程序。

于 2011-02-22T06:06:21.730 回答
0

不是 100% 正确,但我只是试了一下。我没有考虑到@wilhelmtell 的所有观点。我一有时间就试试...

if __name__ == "__main__":
   f = open("1.txt")
   c=w=0
   s=1
   prevIsSentence = False
   for x in f:
      x = x.strip()
      if x != "":
        words = x.split()
        w = w+len(words)
        c = c + sum([len(word) for word in words])
        prevIsSentence = True
      else:
        if prevIsSentence:
           s = s+1
        prevIsSentence = False

   if not prevIsSentence:
      s = s-1
   print "%d:%d:%d" % (c,w,s)

这里 1.txt 是文件名。

于 2011-02-22T06:24:52.967 回答
0

解决这个问题的唯一方法是创建一个使用自然语言处理的 AI程序并不容易做到

输入:

“这是一段关于图灵机的文章。艾伦·图灵博士发明了图灵机。它解决了一个问题,但有0.1%的变化被解决了。”

结帐OpenNLP

https://sourceforge.net/projects/opennlp/

http://opennlp.apache.org/

于 2019-06-03T07:58:08.477 回答
0

我相信这是正确的解决方案,因为它正确地计算了诸如“...”和“??”之类的东西。作为一个句子

len(re.findall(r"[^?!.][?!.]", paragraph))
于 2021-03-09T20:27:14.447 回答
-4

已经有一个计算单词和字符的程序—— wc

于 2011-02-22T06:01:38.987 回答