[python jieba]词性标注 2018年4月16日10:40:07

>>> import jieba

>>> import jieba.posseg
>>> string = '陈晨和林迪是好朋友'
>>> seg = jieba.posseg.cut(string)
>>> print(seg)
<generator object cut at 0x00000145F7601FC0>
>>> l = []
>>> for i in seg:
...   l.append(i.word,i.flag)
...
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\oil\AppData\Local\Temp\jieba.cache
Loading model cost 0.937 seconds.
Prefix dict has been built succesfully.
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: append() takes exactly one argument (2 given)
>>> for i in seg:
...   l.append(i.word,i.flag)
... for element in l:
  File "<stdin>", line 3
    for element in l:
      ^
SyntaxError: invalid syntax
>>> for i in seg:
...   l.append((i.word,i.flag))
...
>>> for element in l:
...   print(element[0],element[1])
...
和 c
林迪 nr
是 v
好 a
朋友 n
>>> print(type(seg))
<class 'generator'>
>>> print(type(element))
<class 'tuple'>
>>> print(type(l))
<class 'list'>
>>> print(list)
<class 'list'>
>>> print(l)

[('和', 'c'), ('林迪', 'nr'), ('是', 'v'), ('好', 'a'), ('朋友', 'n')]

-----------------------------------------------------------------------------------


阅读更多
个人分类: python txt jieba
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭