2017-10-23 2 views
1

두 개의 텍스트 파일이 있고 하나의 파일에는 Neo4j 스크립트가 들어 있고 다른 파일에는 일부 문서 ID 및 색인이있는 국가 및 도시 목록이 들어 있습니다. 아래에 주어진 :파이썬에서 조합하면서 텍스트 문서 분할하기

사이퍼 파일 :

MATCH (t:Country {name:'%a'}),(o:City {name:'%b'}) 
WITH point({ longitude: toFloat(t.longitude), latitude: toFloat(t.latitude) }) AS copoint, point({ longitude: toFloat(o.longitude), latitude: toFloat(o.latitude) }) AS cipoint 
RETURN distance(copoint, cipoint) 

텍스트 파일 :

 5 <DOCID>GH950102-000000<DOCID>/O 
    114 Cardiff/LOCATION 
    321 United States'/LOCATION 
    898 Alps/LOCATION 
    1029 Dresden/LOCATION 
    1150 Scotland/LOCATION 
    1162 Gasforth/LOCATION 
    1258 Arabia/LOCATION 
    1261 Hejaz/LOCATION 
    1265 Aleppo/LOCATION 
    1267 Northern Syria/LOCATION 
    1269 Aqaba/LOCATION 
    1271 Jordan./LOCATION 
    1543 London/LOCATION 
    1556 London/LOCATION 
    1609 London/LOCATION 
    2040 <DOCID>GH950102-000001<DOCID>/O 
    2317 America/LOCATION 
    3096 New York./LOCATION 
    3131 Great Britain/LOCATION 
    3147 <DOCID>GH950102-000002<DOCID>/O 
    3184 Edinburgh/LOCATION 
<DOCID>GH950102-000003<DOCID>/O 
    3243 Australia/LOCATION 
    3360 England/LOCATION 
    3414 India/LOCATION 
    3474 Melbourne/LOCATION 
    3497 England/LOCATION 

내 질문은이 문서를 분할 할 때마다 DOCID 나타납니다 각 사이의 모든 위치 이름 사이의 조합을 촬영하는 방법입니다 DOCID. Index Number을 제거하고 Cypher 스크립트에서 위치 이름을 복사하는 동안 /location도 제거해야합니다.

이 코드로 시도했지만 도움이되지 않았습니다.

from itertools import combinations 

with open ("results.txt") as f: 
    for line in f: 
     for "DOCID" in line.split(): 
      cities = (city.strip() for city in f.readlines()) 

with open ("cypher.txt") as g: 
    cypher_query =g.readlines() 

with open ("resultfile.txt","w") as f: 
    for city1,city2 in combinations (cities,2): 
     f.writelines(line.replace("%a",city1).replace("%b",city2) for line in cypher_query) 
     f.write("\n") 
+1

더 DOCID 문자열을 존재하지 않는, 당신은 DOCID 무엇을 의미 명확히하십시오. – voiDnyx

+0

나는 편집을 시도했으나 일어나지 않고 누군가가 이미 편집을 제안했지만 승인 할 수 없으므로 승인을 기다리고 있으므로 DOCID가 표시 될 수 있습니다 – Moizzy

답변

0

난 당신이 직접 그에 맞게해야 할 수도 있으므로 사이퍼를 잘 모릅니다, 그러나 이것은 당신 조합 제공 :

import re 
import itertools 

with open ("cypher.txt") as g: 
    cypher_query =g.readlines() 

with open("textFile", "r") as inputFile: 

    locations = set() 

    for line in inputFile: 
     if "DOCID" in line and len(locations) > 1: 

      for city1, city2 in itertools.combinations(locations,2): 


       # 
       # here call cypher script with cities as parameter 
       # 

       with open ("resultfile.txt","a") as f: 
         f.writelines(line.replace("%a",city1.strip()).replace("%b",city2.strip()) for line in cypher_query) 
         f.write("\n") 

      locations.clear() 

     else: 
      location = re.search("(\D+)/LOCATION$", line) 
      if location: 
       locations.add(location.group(1)) 

편집 : 선을 고정이 지금 1 사이퍼와 파일을 생성 명령을 사용하여 각각의 2 조합의 위치를 ​​찾거나 별도의 파일을 원할 경우 카운터 또는 이와 유사한 파일을 resultfile-filename에 추가하십시오. 또한 요르단과 같은 이름이 있음에 유의하십시오. (끝과 함께) 차이가 있다면.

예 출력 : 귀하의 예제 텍스트 파일에서

MATCH (t:Country {name:'Alps'}),(o:City {name:'Scotland'}) 
WITH point({ longitude: toFloat(t.longitude), latitude: toFloat(t.latitude) }) AS copoint, point({ longitude: toFloat(o.longitude), latitude: toFloat(o.latitude) }) AS cipoint 
RETURN distance(copoint, cipoint) 

MATCH (t:Country {name:'Alps'}),(o:City {name:'Dresden'}) 
WITH point({ longitude: toFloat(t.longitude), latitude: toFloat(t.latitude) }) AS copoint, point({ longitude: toFloat(o.longitude), latitude: toFloat(o.latitude) }) AS cipoint 
RETURN distance(copoint, cipoint) 

MATCH (t:Country {name:'Alps'}),(o:City {name:'Gasforth'}) 
WITH point({ longitude: toFloat(t.longitude), latitude: toFloat(t.latitude) }) AS copoint, point({ longitude: toFloat(o.longitude), latitude: toFloat(o.latitude) }) AS cipoint 
RETURN distance(copoint, cipoint) 
+0

주어진 결과로 시도했지만 작동하지 않았습니다. . – Moizzy

+0

제 대답을 편집했습니다. 이것이 효과가 있는지보십시오. – voiDnyx

관련 문제