2016-10-14 7 views
0

私はこのようなことが必要なフォーラムからこの問題を遭遇しました: あなたは一連の継承が与えられ、そのテキストを含む継承をフィルタリングしなければなりません(空白で区切られたシーケンス言葉)は、1つ以上の他の通路の副通路として完全に含まれる。封じ込めのために比較した場合、一定の規則に従わなければならない文字列消滅のための継承を経由して

: アルファベットの場合は 先頭と末尾の空白を無視すべき連続する空白の他のブロックは、単一の空間 非英数字として扱われるべき 無視されるべき文字を無視する必要があります。空白は保持する必要があります。 重複もフィルタリングする必要があります。上記の比較ルールに対して2つの節が等しいと見なされた場合、最短のものだけを保持してください。同じ長さの場合は、入力シーケンスの最初のものを保持する必要があります。保持された通路は、元の形(入力通路と同一)で同じ順序で出力されるべきである。

入力1:IBM認知コンピューティング| IBM "認知"コンピューティングは革命です。 ibm認知コンピューティング|「IBM認知コンピューティング」は革命ですか? IBM「認知」コンピューティング:

出力1:| | IBMは、「認知」コンピューティングは革命である認知コンピューティングは革命です

出力2 IBM認知コンピューティング:IBMは、「認知」コンピューティングは革命

入力2です

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 
:認知コンピューティングは、私はPythonで、次のコードを書きましたが、それは私にいくつかの他の出力ではなく、最初のテストケースを与えている革命

です|革命です

input.txtには、最初のテストケース入力が含まれています。そして出力を次のようにしています。 IBM Cognitive Computingは革命です。 誰かがチャイムをして助けてくれますか?ありがとう

答えて

0

私は文字通りこれをあなたのためにコード化しましたが、うまくいけばわかりやすいです(私はそのように最適化されていません)。あなたがそれをスピードアップする必要があるのか​​、何か質問がある場合は私に教えてください!

ところで、これはPythonの3で、これだけはあなたを助けた場合にも、取得するために素晴らしいことだprint文からブラケットを取り外して、それがコピー&ペーストや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')) 

のために実行されますいくつかの点は、受け入れがいい:)(私はあなたがここに新しい参照してください)。

+1

ありがとうございました。再度、感謝します – GoRion

関連する問題