0
1

More than 1 year has passed since last update.

Python を使用して、ユーザーが検索した単語を登録する辞書システムの例

Last updated at Posted at 2023-02-16

自己学習のメモです。

まず、scikit-learn ライブラリをインストールする。

pip install scikit-learn

下記の様なシンプルな辞書を作成。

from sklearn.tree import DecisionTreeClassifier
from sklearn.feature_extraction.text import CountVectorizer
from collections import Counter

class Dictionary:
    def __init__(self):
        self.vectorizer = CountVectorizer()
        self.classifier = DecisionTreeClassifier()
        self.words = []
        self.counts = Counter()

    def add_word(self, word):
        self.words.append(word)
        self.counts[word] += 1
        X = self.vectorizer.fit_transform(self.words)
        y = self.vectorizer.get_feature_names()
        self.classifier.fit(X, y)

    def predict(self, word):
        return self.classifier.predict(self.vectorizer.transform([word]))

scikit-learnのDecisionTreeClassifierを使用して検索された単語を分類するDictionaryクラスを定義。
add_word メソッドは、辞書に新しい単語を追加するために使用。
predict メソッドは、指定された単語の予測するために使用。
このメソッドは単語を入力として受け取り、それをCountVectorizerを使用して変換してから、それをDecisionTreeClassifierに渡し、予測された単語を取得する。

Dictionary クラスの使用例は下記の通り。

dictionary = Dictionary()

dictionary.add_word("apple")
dictionary.add_word("banana")
dictionary.add_word("cherry")

print(dictionary.predict("apple"))
# Output: ['apple']

print(dictionary.predict("berry"))
# Output: ['cherry']

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1