Here are the minimum preprocessing steps required
when performing text analysis in Python:
- Read the text
- Tokenize the text
- Count the tokenized words
Below is a sample:
First, prepare the data.
Refer to a Wikipedia Japanese page
and store it as "test_dir/isaac_asimov_wikipedia.txt".
アイザック・アシモフ(Isaac Asimov、1920年1月2日 - 1992年4月6日)は、アメリカ合衆国の生化学者(ボストン大学教授)・作家。その著作は500冊以上を数える[2]。彼が扱うテーマは科学、言語、歴史、聖書など多岐にわたり、デューイ十進分類法の10ある主要カテゴリのうち9つにわたるが[3][注 1]、特にSF、一般向け科学解説書、推理小説によってよく知られている。
Below is the code to be stored as "test1.py".
It utilizes the simplest CountVectorizer approach for counting,
as per the flow from the previous post.
#!/usr/bin/env python3
# -*- coding: utf8 -*-from sklearn.feature_extraction.text import CountVectorizer
from separatewordsj2 import separatewordsj2target_text = 'test_dir/isaac_asimov_wikipedia.txt'
lines = [ ]
with open(target_text, encoding='utf-8') as f:
lines.append(f.read())wd = separatewordsj2()
vec = CountVectorizer(analyzer=wd.extract_words)
X = vec.fit_transform(lines)names_list = vec.get_feature_names_out()
x_list = X.toarray()print(','.join(names_list))
print(','.join(map(str, x_list[0])))
Here is the result of test1.py execution
formatted in CSV.
(base) C:\pytest>python test1.py
-,1,10,1920,1992,2,3,4,500,6,9,Asimov,Isaac,SF,[,][,]、,]。,ある,いる,うち,が,その,つ,て,など,によって,にわたり,にわたる,の,は,よく,れる,を,アイザック,アシモフ,アメリカ合衆国,カテゴリ,テーマ,デューイ,ボストン,一般,主要,以上,作家,冊,分類,十,向け,多岐,大学,小説,年,彼,扱う,推理,教授,数える,日,書,月,歴史,法,特に,生化学,知る,科学,者,聖書,著作,解説,言語,進
1,2,1,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,1,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,2,1,1,1,1,1,1