This article mainly introduces the solution of Python user comment tag matching in detail, which has certain reference value. Interested friends can refer to it
We observed user comments and found: attribute words They often appear together with emotional words because users usually express emotions when describing attributes, and attributes are the objects of emotional expression. It was also found that attribute words and special emotion words are basically nouns or adjectives (formal predicates).
The algorithm flow chart is as follows:
The comment data is as follows:
The code is as follows:
#encoding=utf-8 ############################# # # 功能:给定一些中文的产品评论,希望从中找到评价对象及评价词。 # # @author:licl # ############################## fdata = open('JD_DFB_comments_out.txt','r') Output = open('Pattern_Result.txt','a') try: data = fdata.readlines() listline = [] for line in data: listline = line.replace(" ","/") listline = listline.split("/") i = 1 while i < len(listline): if listline[i] != "名词": i = i+2 else: new_list = ["","",""] new_list[0] = listline[i-1] a = i-1 i = i+2 while i < len(listline): if listline[i] == "标点": i = i+2 break else: if listline[i-1]=='不' or listline[i-1]=='不怎么样' or listline[i-1]=='不怎么' or listline[i-1]=='不太': new_list[1] = listline[i-1] if listline[i] == "形容词" or listline[i] == "形谓词": new_list[1] += listline[i-1] b = i-1 t = (b-a)/2 new_list[2] = str(t) for line in new_list: Output.write(line + " ") Output.write("\n") break else: i = i+2 except: print "‘文件不存在'或者‘文件无法打开'" finally: fdata.close() Output.close()
Related recommendations:
Example explanation of python user management system
The above is the detailed content of Solution to Python user comment tag matching. For more information, please follow other related articles on the PHP Chinese website!