python - NLTK 标记化问题

标签 python nltk

这里只是一个有关 NLTK 的快速初学者问题。 我正在尝试从语料库中标记并生成二元组、三元组和四元组。

我需要添加<s>到我的列表的开头和 </s>如果有句点,则以句点结尾。

该列表取自 nltk 中的棕色语料库。 (以及其中的特定部分)

所以..我有

from nltk.corpus import brown
news = brown.sents(categories = 'editorial')

我让这件事变得太困难了吗? 多谢。

最佳答案

import nltk.corpus as corpus

def mark_sentence(row):
    if row[-1] == '.':
        row[-1] = '</s>'
    else:
        row.append('</s>')
    return ['<s>'] + row

news = corpus.brown.sents(categories = 'editorial')
for row in news[:5]:
    print(mark_sentence(row))

产量

['<s>', 'Assembly', 'session', 'brought', 'much', 'good', '</s>']
['<s>', 'The', 'General', 'Assembly', ',', 'which', 'adjourns', 'today', ',', 'has', 'performed', 'in', 'an', 'atmosphere', 'of', 'crisis', 'and', 'struggle', 'from', 'the', 'day', 'it', 'convened', '</s>']
['<s>', 'It', 'was', 'faced', 'immediately', 'with', 'a', 'showdown', 'on', 'the', 'schools', ',', 'an', 'issue', 'which', 'was', 'met', 'squarely', 'in', 'conjunction', 'with', 'the', 'governor', 'with', 'a', 'decision', 'not', 'to', 'risk', 'abandoning', 'public', 'education', '</s>']
['<s>', 'There', 'followed', 'the', 'historic', 'appropriations', 'and', 'budget', 'fight', ',', 'in', 'which', 'the', 'General', 'Assembly', 'decided', 'to', 'tackle', 'executive', 'powers', '</s>']
['<s>', 'The', 'final', 'decision', 'went', 'to', 'the', 'executive', 'but', 'a', 'way', 'has', 'been', 'opened', 'for', 'strengthening', 'budgeting', 'procedures', 'and', 'to', 'provide', 'legislators', 'information', 'they', 'need', '</s>']

关于python - NLTK 标记化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13060859/

相关文章:

python - Python 中 NLTK 的命名实体识别。识别网元

python - 如何用Python逐句解析文件

python - 使用 PlainTextCorpusReader 创建语料库并进行分析

python - PyInstaller 在导入 Flask.ext 时遇到问题

python - Zapier 频繁超时 10.01 秒

Python:如何使用变量传递参数名称

python - 在 nltk 中扩展一个类。 - Python

python - 导入已安装的模块时为 "ImportError: DLL load failed: The specified module could not be found"

python - Pandas Dataframe 按时间序列进行透视和重新索引

python - 如何让 CatBoost get_object_importance 与 AUC 配合使用?