このチュートリアルは、Pythonを使用して単語頻度を分析することにより、ドキュメントのメイントピックをすばやく決定する方法を示しています。 手動での単語の発生を数えることは退屈です。この自動化されたアプローチは、プロセスを簡素化します
サンプルテキストファイル(ダウンロードしますが、覗かないでください!)を使用して説明します。 目標は、単語の頻度に基づいてチュートリアルの主題を推測することです。
test.txt
ファイルを読む:
プログラムは、テキストファイルを文字列に読み取ることから始まります:-
document_text = open('test.txt', 'r') text_string = document_text.read().lower()
3〜15文字の正規表現をフィルターします: -
match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string)
辞書は単語の周波数を追跡します: -
frequency = {} for word in match_pattern: count = frequency.get(word, 0) frequency[word] = count + 1
プログラムは、各単語とその頻度を印刷します。 -
完全なプログラム
frequency_list = frequency.keys() for word in frequency_list: print(word, frequency[word])
ここに合わせたPythonコードがあります:
これを実行すると、単語周波数リストが出力されます。 最も頻繁な言葉は、元のチュートリアルのトピックを示唆しています。
import re frequency = {} document_text = open('test.txt', 'r') text_string = document_text.read().lower() match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string) for word in match_pattern: count = frequency.get(word, 0) frequency[word] = count + 1 frequency_list = frequency.keys() for word in frequency_list: print(word, frequency[word])大きなテキストファイルの処理
大きなファイルの場合、周波数辞書をソートすると、最も頻繁な単語を見つけることが簡素化されます。
一般的な単語を除外します
import re frequency = {} document_text = open('dracula.txt', 'r') # Example: dracula.txt text_string = document_text.read().lower() match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string) for word in match_pattern: count = frequency.get(word, 0) frequency[word] = count + 1 most_frequent = dict(sorted(frequency.items(), key=lambda elem: elem[1], reverse=True)) most_frequent_count = most_frequent.keys() for word in most_frequent_count: print(word, most_frequent[word])これにより、より焦点の整った分析が提供されます。
以上がPythonを使用してファイルで単語頻度をカウントしますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

SublimeText3 中国語版
中国語版、とても使いやすい

WebStorm Mac版
便利なJavaScript開発ツール

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

ホットトピック









