Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

python - Connecting input sentences with overlapping words

The task is to connect the input sentences which are overlapping. My problem is how to remove the overlapping parts properly.

Input: first line is number of sentences to be connected. Next following lines are sentences. Output: connected sentence

Examples:

Input:

2
The harder you work for something, the
something, the greater you?ll feel when you achieve it.

Output:

The harder you work for something, the greater you?ll feel when you achieve it.

My code:

def connect(sentence1,sentence2):
  x= None
  y= None
  for i in range(len(sentence2)):
    if sentence2[:len(sentence2)-i] in sentence1 and len(sentence2[:len(sentence2)-i]) != 1:
        y =(sentence1+' '+sentence2[len(sentence2)-i:].strip())
        x =True
        break
  return x,y
n = int(input())
lst = []
for i in range(n):
  a = input()
  lst.append(a)
for i in lst:
  for j in lst:
    if i ==j:
        pass
    elif True in connect(i,j):
        lst.remove(i)
        lst.remove(j)
        lst.append(connect(i,j)[1])
print(lst[0])

Input 1:

3
The fool doth think he is wise,
wise man knows himself to be a fool.
wise, but the wise

Output 1: incorrect

The fool doth think he is wise, man knows himself to be a fool. but the wise

Expected output 1:

The fool doth think he is wise, but the wise man knows himself to be a fool.

Input 2:

7
afraid of greatness.
Be not afraid
some achieve greatness,
greatness thrust upon them.
greatness. Some
Some are born great, some
greatness, and others have greatness

Output 2: error

line 21, in 
    lst.remove(i)
ValueError: list.remove(x): x not in list

Expected output 2:

Be not afraid of greatness. Some are born great, some achieve greatness, and others have greatness thrust upon them.
question from:https://stackoverflow.com/questions/65891258/connecting-input-sentences-with-overlapping-words

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to keep track of the length of the overlap (either end-start or start-end) in order to cut the appropriate number of characters from one of the parts that you concatenate:

sentences = """afraid of greatness.
Be not afraid
some achieve greatness,
greatness thrust upon them.
greatness. Some
Some are born great, some
greatness, and others have greatness""".split("
")

result = sentences.pop(0) # start with any part, and take it out

while sentences:
    for s in list(sentences):
        atEnd   = next((p for p in range(1,len(s)) if result[-p:]==s[:p]),0)
        if atEnd:                        # length of overlapping end-start
            result = result + s[atEnd:]  # append to end, cut starting overlap
            sentences.remove(s)
            continue
        atStart = next((p for p in range(1,len(s)) if result[:p]==s[-p:]),0)
        if atStart:                      # length of overlapping start-end
            result = s[:-atStart]+result # insert at start, cut ending overlap
            sentences.remove(s)

print(result)

Be not afraid of greatness. Some are born great, some achieve greatness, and others have greatness thrust upon them.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...