Beispiel #1
0
def getLCSFromFiles(fileOne,
                    fileTwo,
                    ignoreLineDir=True,
                    ignoreComments=True,
                    ignoreFileSpecifics=False):
    """
	Process an LCS object given two files

	see getStringsFromFiles for arg info
	"""
    seqOne, seqTwo = getStringsFromFiles(fileOne, fileTwo, ignoreLineDir,
                                         ignoreComments, ignoreFileSpecifics)
    retLCS = LCS.LCS(seqOne, seqTwo)
    return retLCS
    def __calcEdgeWeights(self, content):

        edgeWeights = [
        ]  #holds the edge weight values where entry at index i is
        #the edge weight from the new article to the article with
        #id number i

        c2 = content.lower()
        Arr2len = len(c2)
        c2 = nltk.word_tokenize(c2)
        tagged2 = nltk.pos_tag(c2)
        c1 = [
            w for w, t in tagged2
            if t != "DT" and t != "IN" and t != "WP" and t != "WRB" and
            t != "WP$" and t != "WDT" and t != "EX" and t != "TO" and t != "RP"
        ]
        c2 = c2[0:500]

        for i in range(len(self.contents)):
            c1 = self.contents[i].lower()
            Arr1len = len(c1)
            c1 = nltk.word_tokenize(c1)
            tagged1 = nltk.pos_tag(c1)
            c1 = [
                w for w, t in tagged1 if t != "DT" and t != "IN" and t != "WP"
                and t != "WRB" and t != "WP$" and t != "WDT" and t != "EX"
                and t != "TO" and t != "RP"
            ]
            c1 = c1[0:500]

            weight = LCS.LCS(c1, c2)

            weight = weight / ((Arr1len + Arr2len) / 2)

            #weight = weight * weight

            edgeWeights.append(weight)

        edgeWeights.append(0)
        return edgeWeights
Beispiel #3
0
#!/usr/bin/python
# Filename: Longest-Common-Subsequence.py

import random 
import LCSLength
import LCS

arrayX = [0, 'a', 'b', 'c', 'b', 'd', 'a', 'b']
arrayY = [0, 'b', 'd', 'c', 'a', 'b', 'a']

m = len(arrayX) - 1
n = len(arrayY) - 1

c = []
b = []

for i in range(0, m + 1):
	c.append([])
	for j in range(0, n + 1):
		c[i].append(0)

for i in range(0, m + 1):
	b.append([])
	for j in range(0, n + 1):
		b[i].append(0)

LCSLength.LCSLength(m, n, arrayX, arrayY, c, b)
LCS.LCS(m, n, arrayX, b)	
Beispiel #4
0
import os
import time
import sys
import LCS

if __name__ == '__main__':
    size = readSize.readSize('input/size.txt')
    #每轮都会随机产生字符串,所以这里写入时为了保存,而不是读取
    inputA = open('input/inputA.txt', 'w')
    inputB = open('input/inputB.txt', 'w')
    timeTXT = open('output/time.txt', 'w')
    for key, value in size.items():
        for m, n in value:
            str1 = createString.createString(m)  #产生指定长度的字符串
            str2 = createString.createString(n)
            if (key == str('A')):  #将产生的字符串保存起来
                inputA.writelines(str1 + '\n')
                inputA.writelines(str2 + '\n')
            else:
                inputB.writelines(str1 + '\n')
                inputB.writelines(str2 + '\n')
            t1 = time.time()
            b, c = LCS.LCS(str1, str2)
            print('{}组规模为({}, {})的字符串组的LCS长度为:{},其中一个解为:'.format(
                key, m, n, c[-1][-1]),
                  end='')
            LCS.print_LCS(b, str1, m, n)
            print()
            t2 = time.time()
            timeTXT.writelines(str(t2 - t1) + ' ms\n')
    new_list1 = []
    new_list2 = []
    for i in range(size1):
        new_list1.append(choice(ascii_uppercase))
    for j in range(size2):
        new_list2.append(choice(ascii_uppercase))
    return new_list1, new_list2


if __name__ == "__main__":
    # input length of sequences
    m = int(input("Set the length of the first sequence: "))
    n = int(input("Set the length of the second sequence: "))

    # generate two random character sequences
    new_lists = generate_List(10, m, n)
    print(new_lists[0])
    print(new_lists[1])

    # create an instance of a naiveMethod algorithm and run it to solve the lcs problem
    nm = nm.naiveMethod()
    nm_lcs = lcs.LCS(new_lists[0], new_lists[1], nm)
    nm_result, nm_time = nm_lcs.run()
    print("The result: " + str(nm_result) + "\nThe time: " + str(nm_time))

    # create an instance of a dynamicProgramming algorithm and run it to solve the lcs problem
    dp = dp.dynamicProgramming(m, n)
    dp_lcs = lcs.LCS(new_lists[0], new_lists[1], dp)
    dp_result, dp_time = dp_lcs.run()
    print("The result: " + str(dp_result) + "\nThe time: " + str(dp_time - 1))
 def test_ExpectedOutput(self):
     '''
     Checks if returned output is as expected.
     '''
     output = LCS.LCS(["abcabb", "bacb"])
     self.assertEqual(output, 3)