【问题标题】:Using Python, NLTK, to analyse German text使用 Python、NLTK 分析德语文本
【发布时间】:2021-02-18 17:22:51
【问题描述】:

我是 Python 的初学者,目前正在尝试使用 NLTK 来分析德语文本(提取德语名词及其在德语文本中的频率),请遵循以下教程:https://datascience.blog.wzb.eu/2016/07/13/accurate-part-of-speech-tagging-of-german-texts-with-nltk/

在这个过程中我遇到了几个问题,我无法解决。

当我按照网站执行以下代码时:

import random

tagged_sents = list(corp.tagged_sents())
random.shuffle(tagged_sents)
split_perc = 0.1
split_size = int(len(tagged_sents) * split_perc)
train_sents, test_sents = tagged_sents[split_size:], tagged_sents[:split_size]

结果就是这样

Traceback (most recent call last):
  File "test2.py", line 7, in <module>
    tagged_sents = list(corp.tagged_sents())
  File "C:\Users\User\anaconda3\lib\site-packages\nltk\corpus\reader\conll.py", line 130, in tagged_sents
    return LazyMap(get_tagged_words, self._grids(fileids))
  File "C:\Users\User\anaconda3\lib\site-packages\nltk\corpus\reader\conll.py", line 215, in _grids
    return concat(
  File "C:\Users\User\anaconda3\lib\site-packages\nltk\corpus\reader\util.py", line 433, in concat
    raise ValueError("concat() expects at least one object!")
ValueError: concat() expects at least one object!

然后我尝试按照这个解决方案来修复https://teamtreehouse.com/community/randomshuffle-crashes-when-passed-a-range-somenums-randomshufflerange5250

并改变 tagged_sents = list(corp.tagged_sents())

tagged_sents = list(range(5,250))

而且ValueError没有出来,我不知道(5,250)是什么意思,虽然我看过解释。

那我继续执行下面的步骤

from ClassifierBasedGermanTagger.ClassifierBasedGermanTagger import ClassifierBasedGermanTagger
tagger = ClassifierBasedGermanTagger(train=train_sents) 

它显示

Traceback (most recent call last):
  File "test1.py", line 90, in <module>
    from ClassifierBasedGermanTagger.ClassifierBasedGermanTagger import ClassifierBasedGermanTagger
ModuleNotFoundError: No module named 'ClassifierBasedGermanTagger' 

我已经下载了ClassifierBasedGermanTagger.py和init.py,放到VS CODE链接的文件夹下,不知道是否如文中所说:

'使用他的 Python 类 ClassifierBasedGermanTagger(您可以从 github 页面下载),我们可以创建一个标注器并使用来自 TIGER 语料库的数据对其进行训练:'

请帮我解决这些问题,谢谢!

【问题讨论】:

    标签: python nltk tokenize corpus


    【解决方案1】:

    首先,欢迎来到 StackOverflow!在发布问题之前,请确保您已经完成了自己的研究,并且大部分时间都可以解决问题。

    其次,range(start, end) 是 Python 中一个非常基本的函数,用于根据输入获取数字列表,我不认为像您那样使用它可以解决问题。我建议您使用print 查看corp 中填充了哪些类型的数据,然后从那里开始调试。也许corp 只是空的,这就是为什么你没有得到任何tagged_sents

    对于导入部分,我不清楚您将ClassifierBasedGermanTagger.py 放在哪里,但无论它在哪里,您的代码都看不到它。你可以尝试把你的代码(test2.py)和ClassifierBasedGermanTagger.py放在同一个目录下。阅读下面的链接,了解有关如何在 Python 中正确导入模块的更多详细信息。

    https://docs.python.org/3/reference/import.html

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 2014-01-25
      • 2018-09-04
      • 1970-01-01
      相关资源
      最近更新 更多