Python:编码字符,但仍然可以使用lis

2024-09-28 19:06:15 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,对于文本挖掘任务,我们尝试收集tweet(尤其是文本),并运行stanford NER tagger来找出是否有提到的人或地点。这也可以通过检查hashtags来实现,但其思想是使用一些文本挖掘工具

假设我们从一个cPickle文件中加载了数据,这个文件被保存、加载并在空白处分割

hil_text = [[u'Man', u'is', u'not', u'a', u'issue', u'cah', u'me', u'pum', u'pum', u'tun', u'up', u'#InternationalWomensDay', u'#cham', u'#empowerment', u'#Clinton2016', u'#PiDay2016'], [u'Shonda', u'Land', u'came', u'out', u'with', u'a', u'great', u'ad', u'for', u'Clinton:https://t.co/Vfg9lAKNaH#Clinton2016'], [u'RT', u'@BeaverforBernie:', u'Trump', u'and', u'the', u"#Clinton's", u'are', u'the', u'same.', u'They', u'worship', u'$$$$$.', u'https://t.co/yUXoJaL6mJ'], [u'.@GloriaLaRiva', u'on', u'#Clinton,', u'Reagans', u'&', u'#AIDS:', u'\u201cClinton', u'just', u're-wrote', u'history\u201d', u'https://t.co/L3YuIyFjxo', u'Clinton', u'incapable', u'of', u'telling', u'truth.'], [u'#KKK', u'Leader', u'Gets', u'Behind', u'This', u'Democratic', u'Candidate', u'https://t.co/p9yTQ2sXmV', u'How', u'fitting!', u'#Hillary2016', u'#HillaryClinton', u'#Hillary', u'#Killary', u'#tcot'], [u'#KKK', u'Leader', u'Gets', u'Behind', u'This', u'Democratic', u'Candidate', u'https://t.co/p9yTQ2sXmV', u'How', u'fitting!', u'#Hillary2016', u'#HillaryClinton', u'#Hillary', u'#Killary', u'#tcot'], [u'RT', u'@jvlibrarylady:', u'President', u'Clinton', u'at', u'rally', u'for', u'Hillary', u'at', u'Teamsters', u'Local', u'245', u'in', u'Springfield,', u'Mo.', u'#HillaryClintonForPresident', u'https://t.\u2026'], [u'RT', u'@jvlibrarylady:', u'President', u'Clinton', u'at', u'rally', u'for', u'Hillary', u'at', u'Teamsters', u'Local', u'245', u'in', u'Springfield,', u'Mo.', u'#HillaryClintonForPresident', u'https://t.\u2026']]

标记器不接受unicode,因此在尝试让它工作时,我们尝试执行以下操作

for word in hil_text:
    for x in word:
        print x.encode('utf-8',errors='ignore')
        print tagger.tag(x.encode('utf-8',errors='ignore')

这导致x是印刷的单词,但是标记者分别标记每个字母

有没有办法把它编码并作为一个单词通过标记器发送出去?或者换句话说,对列表的一部分进行编码,但仍将该部分保留在列表中

为什么标签工要标记每个字母而不仅仅是整个x


Tags: 文件textinhttps标记文本fortagger
1条回答
网友
1楼 · 发布于 2024-09-28 19:06:15

看起来tagger.tag需要一个序列。但是您正在传入一个单个字符串,python将其视为字符序列。要解决这个问题,请尝试以下方法:

for section in hil_text:
    # encode each word in the section, and put them in a new list
    words = [word.encode('utf-8') for word in section]
    # pass the list of encoded words to the tagger
    print tagger.tag(words)

相关问题 更多 >