2016-10-14 2 views
0

Ich stieß auf dieses Problem aus einem Forum, wo diese Dinge getan werden müssen: Sie werden eine Sequenz von Passagen gegeben, und müssen jede Passage, deren Text (Sequenz von Leerzeichen begrenzt Wörter) ist vollständig als Unterpassage von einer oder mehreren der anderen Passagen enthalten.Iteration durch Passagen für String Extrusion

Wenn für Rückhaltung zu vergleichen, bestimmte Regeln beachtet werden müssen: Der Fall von alphabetischen Zeichen ignoriert werden sollte Führende und nachfolgende Leerzeichen sollte als ein einzelnes Leerzeichen behandelt werden ignoriert nicht-alphanumerische Jeder andere Block zusammenhängenden Leerzeichen sollte Zeichen sollte ignoriert werden, Leerraum sollte beibehalten werden Duplikate müssen auch gefiltert werden - wenn zwei Passagen in Bezug auf die oben aufgeführten Vergleichsregeln als gleich angesehen werden, sollte nur der kürzeste beibehalten werden. Wenn sie auch die gleiche Länge haben, sollte die frühere in der Eingabesequenz beibehalten werden. Die beibehaltenen Passagen sollten in ihrer ursprünglichen Form (identisch mit der Eingangspassage) und in der gleichen Reihenfolge ausgegeben werden.

Input1: IBM kognitiven Computing | IBM "kognitiven" Computing ist eine Revolution | IBM Cognitive Computing | IBM Cognitive Computing ist eine Revolution?

Output1: IBM "kognitive" Computing ist eine Revolution

Input2: IBM Cognitive Computing | IBM "kognitiven" Computing eine Revolution | die kognitive Computing ist eine Revolution

Output2: IBM "kognitive" Computing ist eine Revolution | das kognitive Computing ist eine Revolution

ich den folgenden Code in python geschrieben, aber es ist mir eine andere Ausgabe eher als der erste Testfall geben:

f = open("input.txt",'r') 
s = (f.read()).split('|') 
str = '' 
for a in s: 
    for b in s: 
     if(''.join(e for e in a.lower() if e.isalnum()))not in (''.join(e for e in b.lower() if e.isalnum())): 
      str = a.translate(None, "'?") 

print str 

input.txt enthält den ersten Testfalleingang. Und ich bekomme die Ausgabe als: IBM Cognitive Computing ist eine Revolution. Könnte jemand bitte hereinspielen und mir helfen. Danke

Antwort

0

Ich habe das buchstäblich für Sie kodiert, hoffentlich ist es leicht zu verstehen (ich habe es als solches weniger optimal gemacht). Lassen Sie es mich wissen, wenn Sie es beschleunigen müssen oder wenn Sie Fragen haben!

BTW, ist dieser Python 3, entfernen Sie einfach die Klammern aus den print Aussagen und es wird kopieren und einfügen und für Python 2.

import re 

def clean_input(text): 
    #non-alphanumeric character should be ignored 
    text = re.sub('[^a-zA-Z\s]', '', text) 
    #Any other block of contiguous whitespace should be treated as a single space 
    #white space should be retained 
    text = re.sub(' +',' ',text) 
    #Leading and trailing whitespace should be ignored 
    text = text.strip(' \t\n\r') 
    # You probably want this too 
    text = text.lower() 
    return text 

def process(text): 
    #If they are also the same length, the earlier one in the input sequence should be kept. 
    # Using arrays (can use OrderedDict too, probably easier and nice, although below is clearer for you. 
    original_parts = text.split('|') 
    clean_parts = [clean_input(x) for x in original_parts] 
    original_parts_to_check = [] 
    ignore_idx = [] 
    for idx, ele in enumerate(original_parts): 
     if idx in ignore_idx: 
      continue 
     #The case of alphabetic characters should be ignored 
     if len(ele) < 2: 
      continue 
     #Duplicates must also be filtered -if two passages are considered equal with respect to the comparison rules listed above, only the shortest should be retained. 
     if clean_parts[idx] in clean_parts[:idx]+clean_parts[idx+1:]: 
      indices = [i for i, x in enumerate(clean_parts) if x == clean_parts[idx]] 
      use = indices[0] 
      for i in indices[1:]: 
       if len(original_parts[i]) < len(original_parts[use]): 
        use = i 
      if idx == use: 
       ignore_idx += indices 
      else: 
       ignore_idx += [x for x in indices if x != use] 
       continue 
     original_parts_to_check.append(idx) 
    # Doing the text in text matching here. Depending on size and type of dataset, 
    # Which you should test as it would affect this, you may want this as part 
    # of the function above, or before, etc. If you're only doing 100 bits of 
    # data, then it doesn't matter. Optimize accordingly. 
    text_to_return = [] 
    clean_to_check = [clean_parts[x] for x in original_parts_to_check] 
    for idx in original_parts_to_check: 
     # This bit can be done better, but I have no more time to work on this. 
     if any([(clean_parts[idx] in clean_text) for clean_text in [x for x in clean_to_check if x != clean_parts[idx]]]): 
      continue 
     text_to_return.append(original_parts[idx]) 
    #The retained passages should be output in their original form (identical to the input passage), and in the same order. 
    return '|'.join(text_to_return) 

assert(process('IBM cognitive computing|IBM "cognitive" computing is a revolution| ibm cognitive computing|\'IBM Cognitive Computing\' is a revolution?') == 
     'IBM "cognitive" computing is a revolution') 
print(process('IBM cognitive computing|IBM "cognitive" computing is a revolution| ibm cognitive computing|\'IBM Cognitive Computing\' is a revolution?')) 
assert(process('IBM cognitive computing|IBM "cognitive" computing is a revolution|the cognitive computing is a revolution') == 
     'IBM "cognitive" computing is a revolution|the cognitive computing is a revolution') 
print(process('IBM cognitive computing|IBM "cognitive" computing is a revolution|the cognitive computing is a revolution')) 

auch laufen, wenn diese Ihnen geholfen, wäre toll zu bekommen einige Punkte, also wäre ein Akzeptieren nett :) (Ich sehe, du bist neu hier).

+1

Vielen Dank dafür. Danke noch einmal – GoRion

Verwandte Themen