Guest User

Untitled

a guest
Jan 17th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. 0,今日も夜まで働きました。
  2. 1,オフィスには誰もいませんが、エラーと格闘中
  3. 2,デバッグばかりしていますが、どうにもなりません。
  4.  
  5. import pandas as pd
  6. import MeCab
  7. # https://en.wikipedia.org/wiki/MeCab
  8. from tqdm import tqdm_notebook as tqdm
  9. # This is working...
  10. df = pd.read_csv('sample.csv', encoding='utf-8')
  11.  
  12. m = MeCab.Tagger ("-Ochasen")
  13.  
  14. text = "りんごを食べました、そして、みかんも食べました"
  15. a = m.parse(text)
  16.  
  17. print(a)# working!
  18.  
  19. # But I want to use Pandas's Series
  20.  
  21.  
  22.  
  23. def extractKeyword(text):
  24. """Morphological analysis of text and returning a list of only nouns"""
  25. tagger = MeCab.Tagger('-Ochasen')
  26. node = tagger.parseToNode(text)
  27. keywords = []
  28. while node:
  29. if node.feature.split(",")[0] == u"名詞": # this means noun
  30. keywords.append(node.surface)
  31. node = node.next
  32. return keywords
  33.  
  34.  
  35.  
  36. aa = extractKeyword(text) #working!!
  37.  
  38. me = df.apply(lambda x: extractKeyword(x))
  39.  
  40. #TypeError: ("in method 'Tagger_parseToNode', argument 2 of type 'char const *'", 'occurred at index 0')
  41.  
  42. りんご リンゴ りんご 名詞-一般
  43. を ヲ を 助詞-格助詞-一般
  44. 食べ タベ 食べる 動詞-自立 一段 連用形
  45. まし マシ ます 助動詞 特殊・マス 連用形
  46. た タ た 助動詞 特殊・タ 基本形
  47. 、 、 、 記号-読点
  48. そして ソシテ そして 接続詞
  49. 、 、 、 記号-読点
  50. みかん ミカン みかん 名詞-一般
  51. も モ も 助詞-係助詞
  52. 食べ タベ 食べる 動詞-自立 一段 連用形
  53. まし マシ ます 助動詞 特殊・マス 連用形
  54. た タ た 助動詞 特殊・タ 基本形
  55. EOS
  56.  
  57. ---------------------------------------------------------------------------
  58. TypeError Traceback (most recent call last)
  59. <ipython-input-174-81a0d5d62dc4> in <module>()
  60. 32 aa = extractKeyword(text) #working!!
  61. 33
  62. ---> 34 me = df.apply(lambda x: extractKeyword(x))
  63.  
  64. ~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
  65. 4260 f, axis,
  66. 4261 reduce=reduce,
  67. -> 4262 ignore_failures=ignore_failures)
  68. 4263 else:
  69. 4264 return self._apply_broadcast(f, axis)
  70.  
  71. ~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
  72. 4356 try:
  73. 4357 for i, v in enumerate(series_gen):
  74. -> 4358 results[i] = func(v)
  75. 4359 keys.append(v.name)
  76. 4360 except Exception as e:
  77.  
  78. <ipython-input-174-81a0d5d62dc4> in <lambda>(x)
  79. 32 aa = extractKeyword(text) #working!!
  80. 33
  81. ---> 34 me = df.apply(lambda x: extractKeyword(x))
  82.  
  83. <ipython-input-174-81a0d5d62dc4> in extractKeyword(text)
  84. 20 """Morphological analysis of text and returning a list of only nouns"""
  85. 21 tagger = MeCab.Tagger('-Ochasen')
  86. ---> 22 node = tagger.parseToNode(text)
  87. 23 keywords = []
  88. 24 while node:
  89.  
  90. ~/anaconda3/lib/python3.6/site-packages/MeCab.py in parseToNode(self, *args)
  91. 280 __repr__ = _swig_repr
  92. 281 def parse(self, *args): return _MeCab.Tagger_parse(self, *args)
  93. --> 282 def parseToNode(self, *args): return _MeCab.Tagger_parseToNode(self, *args)
  94. 283 def parseNBest(self, *args): return _MeCab.Tagger_parseNBest(self, *args)
  95. 284 def parseNBestInit(self, *args): return _MeCab.Tagger_parseNBestInit(self, *args)
  96.  
  97. TypeError: ("in method 'Tagger_parseToNode', argument 2 of type 'char const *'", 'occurred at index 0')w
  98.  
  99. >>> import pandas as pd
  100. >>> import io
  101. >>> df = pd.read_csv(io.StringIO("""0,今日も夜まで働きました。
  102. ... 1,オフィスには誰もいませんが、エラーと格闘中
  103. ... 2,デバッグばかりしていますが、どうにもなりません。"""))
  104. >>>
  105. >>> df
  106. 0 今日も夜まで働きました。
  107. 0 1 オフィスには誰もいませんが、エラーと格闘中
  108. 1 2 デバッグばかりしていますが、どうにもなりません。
  109.  
  110. import pandas as pd
  111. import MeCab
  112. import io
  113. df = pd.read_csv(io.StringIO("""0,今日も夜まで働きました。
  114. 1,オフィスには誰もいませんが、エラーと格闘中
  115. 2,デバッグばかりしていますが、どうにもなりません。"""), names=['id','sentence'])
  116.  
  117. tagger = MeCab.Tagger("-Ochasen")
  118.  
  119. def extractKeyword(text):
  120. """Morphological analysis of text and returning a list of only nouns"""
  121. node = tagger.parseToNode(text)
  122. keywords = []
  123. while node:
  124. if node.feature.split(",")[0] == u"名詞": # this means noun
  125. keywords.append(node.surface)
  126. node = node.next
  127. return keywords
  128.  
  129. me = df['sentence'].apply(extractKeyword)
  130.  
  131. >>> df
  132. id sentence
  133. 0 0 今日も夜まで働きました。
  134. 1 1 オフィスには誰もいませんが、エラーと格闘中
  135. 2 2 デバッグばかりしていますが、どうにもなりません。
  136. >>> me
  137. 0 [今日, ]
  138. 1 [オフィス, 誰, エラー, 格闘, 中]
  139. 2 [デバッグ]
  140. Name: sentence, dtype: object
Add Comment
Please, Sign In to add comment