작업 : 두 개의 텍스트 파일을 가져 와서 100 % 일치 및 75 % 일치를 출력합니다.Python에서 퍼지 매칭 알고리즘 개선
솔루션 :
import difflib
import csv
# Imports and parses the files
fileA = open("H:/comm.names.txt", 'r')
try:
setA = fileA.readlines()
finally:
fileA.close()
fileB = open("H:/acad.names.txt", 'r')
try:
setB = fileB.readlines()
finally:
fileB.close()
# 100% Match
setMatch100 = set(setA).intersection(setB)
Match100 = open("H:\Match100.txt", 'w')
try:
for item in setMatch100:
Match100.write(item)
finally:
Match100.close()
# Remove 100% matches from the two lists
setA_LeftOver = set(setA).difference(setMatch100)
setB_LeftOver = set(setB).difference(setMatch100)
#Return the best match for setA_LeftOver[i] in setB_LeftOver that is at least 75% matching.
fMatch75 = open("H:\Match75.csv", 'w')
Match75 = csv.writer(fMatch75)
try:
Match75.writerow(['File A', 'File B'])
for item in setA_LeftOver:
match = difflib.get_close_matches(item, setB_LeftOver, 1, 0.75)
if len(match) > 0:
row = [item.rstrip(), match[0].rstrip()]
Match75.writerow(row)
finally:
fMatch75.close()
문제 :이 작품은, 그러나 결과는 매우 좋지 않다. 다음은 일치의 예입니다.
Fovea Pharmaceuticals SA Kobe Pharmaceutical UnivUniversity와 Univ를 일치시킬 수 있어야하므로 Diff에서 최소 퍼센트를 너무 많이 올릴 수 없습니다. 또한 일부 문자열은 "The"로 시작하고 "The"를 제외하는 문자열과 일치해야하기 때문에 첫 번째 단어가 일치하는지 확인할 수 없습니다. 누구든지 기술적으로 비슷한 75 % 일치하는 것을 던지는 방향으로 나를 가리킬 수 있습니까? 그러나 인간에게는 전혀 비슷하지 않습니까?
제안 해 주셔서 감사합니다. 그러나 이것이 작동하지 않을 것이라고 생각합니다. Diff를 사용하면 모든 단어를 동일하게 취급한다는 점에서 동일한 문제가 있습니다. 특정 단어가 일치하지 않도록 제외하는 방법이 거의 필요하므로 나머지 문자열에는 더 중요한 단어가 포함됩니다. – MikeKusold
난 그 문제를 해결할 수 있을지 모르겠다. (difflib은 "human"이 btw와 더 잘 맞아야한다.하지만 "The"는 중요하지 않고 "Univ"는 "University"의 약자이다.) – Steven
For that that 모든 문자열을 공통된 것으로 대체하고 중요하지 않은 단어를 스트라이핑하여 문자열을 사전 처리 할 수 있습니다. –