{"id":17381,"date":"2024-01-21T05:14:02","date_gmt":"2024-01-20T20:14:02","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=17381"},"modified":"2024-01-21T05:14:03","modified_gmt":"2024-01-20T20:14:03","slug":"python-%e6%a4%9c%e7%b4%a2%e3%82%a8%e3%83%b3%e3%82%b8%e3%83%b3","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=17381","title":{"rendered":"python \u691c\u7d22\u30a8\u30f3\u30b8\u30f3"},"content":{"rendered":"\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-plain\"><code># \u30c7\u30fc\u30bf\r\ndocuments = [\r\n    &quot;Python is a popular programming language.&quot;,\r\n    &quot;It is known for its simplicity and readability.&quot;,\r\n    &quot;Python has a large community of developers.&quot;,\r\n    &quot;Python is widely used in web development.&quot;\r\n]\r\n\r\n# \u30c7\u30fc\u30bf\u306e\u524d\u51e6\u7406\r\ndef preprocess(text):\r\n    return text.lower()\r\n\r\n# \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u4f5c\u6210\r\ndef create_index(documents):\r\n    index = {}\r\n    for doc_id, doc in enumerate(documents):\r\n        doc = preprocess(doc)\r\n        words = doc.split()\r\n        for word in words:\r\n            if word not in index:\r\n                index[word] = []\r\n            index[word].append(doc_id)\r\n    return index\r\n\r\n# \u30af\u30a8\u30ea\u51e6\u7406\r\ndef search(query, index):\r\n    query = preprocess(query)\r\n    words = query.split()\r\n    result = set(range(len(documents)))\r\n\r\n    for word in words:\r\n        if word in index:\r\n            result &amp;= set(index[word])\r\n\r\n    return [documents[i] for i in result]\r\n\r\n# \u30e1\u30a4\u30f3\r\nif __name__ == &quot;__main__&quot;:\r\n    index = create_index(documents)\r\n\r\n    while True:\r\n        query = input(&quot;\u691c\u7d22\u30af\u30a8\u30ea\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044 (\u7d42\u4e86\u3059\u308b\u306b\u306f &#39;exit&#39; \u3092\u5165\u529b): &quot;)\r\n        if query == &#39;exit&#39;:\r\n            break\r\n        results = search(query, index)\r\n        if results:\r\n            for i, result in enumerate(results, start=1):\r\n                print(f&quot;{i}. {result}&quot;)\r\n        else:\r\n            print(&quot;\u4e00\u81f4\u3059\u308b\u6587\u66f8\u306f\u3042\u308a\u307e\u305b\u3093\u3002&quot;)\r\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[4,65],"tags":[3,39],"class_list":["post-17381","post","type-post","status-publish","format-standard","hentry","category-programming","category-python","tag-programming","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17381"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17381\/revisions"}],"predecessor-version":[{"id":17382,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17381\/revisions\/17382"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17381"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}