Python joins MongoDB and compares two string similarity for a simple example

Source: Internet
Author: User
Tags for in range joins mongoclient mongodb query

This article describes an example: using Pymongo to connect MongoDB, querying string Records in MongoDB, and comparing the similarity between strings.

One, Python connects MongoDB

Approximate steps: Create mongoclient---> Get DataBase---> Get collection, the code is as follows:

Client = mongoclient (host="127.0.0.1", port=10001= client['  database_name']db.authenticate (name="user_name", password= " Password ")
coll = db.get_collection ("collection_name")

Two, Python MongoDB query

The query is based on the UID condition. Because more than one field is defined in collection_name, you only want to return the contents of the Chat field and not return the contents of the _id field. So the query conditions are as follows:

Coll.find ({"uid": 123456789}, {"_id""Chat  ": 1})

each record returned by a MongoDB query is a dict: {"Chat": "This is a statement"}, then convert it to the Chats list store each statement:

List_chat = List (Coll.find ({"uid": 123456789}, {"_id""  chat": 1= [d['chat' for in List_chat]

Third, Python compares the similarity of two strings

Given a list, each element in the list is a string that calculates the similarity of the two adjacent elements in the list.

# find the similarity between adjacent strings in the Chats list def compute_similar ():     = uid_chats ()    for in range (len (chats)-1):        = Similar_ratio (chats[index ], chats[index+1])        print(ratios)

A specific string similarity calculation, implemented by Sequencematcher, ignores the presence of spaces in a string.

# The lambda expression means ignore "  " (space), the space does not participate in the similarity calculation of Sequencematcher (Lambda x:x=="", Stra, StrB). Ratio ()

Four, complete code

System Environment pycharm2016.3 Anaconda3 Python3.6

 fromPymongoImportmongoclient fromDifflibImportsequencematcherclient= Mongoclient (host="127.0.0.1", port=10001) DB= client['database_name']db.authenticate (Name="user_name", password="Password") Coll= Db.get_collection ("collection_name")defuid_chats (): List_chat= List (Coll.find ({"UID": 123456789}, {"_id": 0,"Chat": 1})) Chats= [d['Chat'] forDinchList_chat]Print(CHATS)returnChatsdefSimilar_ratio (Stra, StrB):returnSequencematcher (Lambdax:x==" ", Stra, StrB). Ratio ()#find similarity between adjacent strings in listdefcompute_similar (): Chats=uid_chats () forIndexinchRange (len (chats)-1): Ratios= Similar_ratio (Chats[index], chats[index+1])        Print(ratios)if __name__=="__main__": Compute_similar ()

Original: http://www.cnblogs.com/hapjin/p/7895027.html

Python joins MongoDB and compares two string similarity for a simple example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.