質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

自然言語処理

自然言語処理は、日常的に使用される自然言語をコンピューターに処理させる技術やソフトウェアの総称です。

Q&A

解決済

3回答

497閲覧

文字の抜き出しが適切にできない

Newcomer_DS

総合スコア15

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

自然言語処理

自然言語処理は、日常的に使用される自然言語をコンピューターに処理させる技術やソフトウェアの総称です。

0グッド

0クリップ

投稿2020/02/22 13:06

「それぞれのwordがある文章の中で、何回登場するのか?」のタスクをやろうとしてます。
textと言う文章が与えられて、その文章のwordを小文字にし、それぞれのuniqueなwordの数を数えようとしているのですが、
下記の出力のように、wordだけでなく、「”」も取り出してしまっています。

原因と解決策をご教示いただけると幸いでございます。
下記は、私が試していることです。

python

1text = "As I was waiting, a man came out of a side room, and at a glance I was sure he must be Long John. His left leg was cut off close by the hip, and under the left shoulder he carried a crutch, which he managed with wonderful dexterity, hopping about upon it like a bird. He was very tall and strong, with a face as big as a ham—plain and pale, but intelligent and smiling. Indeed, he seemed in the most cheerful spirits, whistling as he moved about among the tables, with a merry word or a slap on the shoulder for the more favoured of his guests." 2 3def count_words(text): 4 """Count how many times each unique word occurs in text.""" 5 counts = dict() # dictionary of { <word>: <count> } pairs to return 6 # TODO: Convert to lowercase 7 text = text.lower() 8 # TODO: Split text into tokens (words), leaving out punctuation 9 # (Hint: Use regex to split on non-alphanumeric characters) 10 text = re.split(r"\W+", text) 11 # TODO: Aggregate word counts using a dictionary 12 for char in text: 13 counts[char] = text.count(char) 14 return counts 15 16print(count_words(text))

出力
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

ベストアンサー

” ではありません。'' です。空の文字列です。
\W+で分割しているので、文末の 'guests.' が 'guests' と '' に別れているのでしょう。


\W+で分割するのではなく、\w+を抽出するようアプローチを替えてみては。

Python

1text = re.findall(r'\w+', text)

投稿2020/02/22 13:10

編集2020/02/22 13:12
LouiS0616

総合スコア35660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

自分で集計せずに、 groupbuy というメソッドを使う方法もあります。

w.py

python3

1from itertools import groupby 2 3text = "As I was waiting, a man came out of a side room, and at a glance I was sure he must be Long John. His left leg was cut off clos\ 4e by the hip, and under the left shoulder he carried a crutch, which he managed with wonderful dexterity, hopping about upon it like a \ 5bird. He was very tall and strong, with a face as big as a ham—plain and pale, but intelligent and smiling. Indeed, he seemed in the m\ 6ost cheerful spirits, whistling as he moved about among the tables, with a merry word or a slap on the shoulder for the more favoured o\ 7f his guests." 8 9for key, group in groupby(sorted(text.lower().split()), key=lambda x: x): 10 print(key, len(list(group)))

参考情報

  • 意外と知られていない!? Python標準ライブラリの関数groupby

https://qiita.com/tag1216/items/9aecc6816371ee35215e

投稿2020/02/22 19:43

katoy

総合スコア22324

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

どうでしょうか

text = "As I was waiting, a man came out of a side room, and at a glance I was sure he must be Long John. His left leg was cut off close by the hip, and under the left shoulder he carried a crutch, which he managed with wonderful dexterity, hopping about upon it like a bird. He was very tall and strong, with a face as big as a ham—plain and pale, but intelligent and smiling. Indeed, he seemed in the most cheerful spirits, whistling as he moved about among the tables, with a merry word or a slap on the shoulder for the more favoured of his guests." def count_words(text): """Count how many times each unique word occurs in text.""" counts = dict() words = text.split() text = [word.lower() for word in words] for char in text: counts[char] = text.count(char) return counts print(count_words(text))

投稿2020/02/22 13:54

sinzou

総合スコア392

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問