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