Помогите мне преобразовать приведенный ниже код из класса Python в класс java?
from Products.ECAssignmentBox.PlagDetector.errors import NoValidArgumentError import re commentPatterns = ['#.*', '\"\"\"(.|\n)*?\"\"\"'] stringConstantPatterns = ['\"(.|\n)*?\"', '\'(.|\n)*?\''] synonyms = {'\+=' : '=', '-=' : '=', '\*=' : '=', '/=' : '=' } keywords = ['and', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yield'] ownkeywords = ['BEGIN_METHOD', 'END_METHOD', 'TEST', 'ASSIGN', 'BEGIN_CLASS', 'END_CLASS', 'FUN', 'APPLY'] patterns = {'[^=\n]+=' : 'ASSIGN ', 'if .*:' : 'if TEST', '(\w+\.)*\w+\((\s|,|\[|\]|\w|\w+\(.*\)|\n)*\)' : 'APPLY' } def normalize(string, returnLinesForEachToken=False): if string == '': return '' elif type(string) != type(''): raise NoValidArgumentError, 'Input must be of type String.' normalizedString = '' lineNrList = range(len(string.splitlines())) string = preprocess(string, lineNrList) normalizedString = removeComments(string, lineNrList) normalizedString = removeStringConstants(normalizedString, lineNrList) normalizedString = normalizedString.lower() normalizedString = mapSynonyms(normalizedString) normalizedString = reorderFunctions(normalizedString, lineNrList) normalizedString = removeAllNonTargetLanguageToken(normalizedString, lineNrList) if returnLinesForEachToken: return normalizedString, lineNrList else: return normalizedString def removeComments(s, lineNrList): for pattern in commentPatterns: s = removePatternMatchesFromString(s, pattern, lineNrList) return s def removeStringConstants(s, lineNrList): for pattern in stringConstantPatterns: s = removePatternMatchesFromString(s, pattern, lineNrList) return s def mapSynonyms(s): for pattern in synonyms: s = re.sub(pattern, synonyms.get(pattern), s) return s def reorderFunctions(s, lineNrList): to be very helpful. class funStruct(object): name = '' posStart = 0 posEnd = 0 expanded = False className = None funcs = [] class classStruct(object): name = '' posStart = 0 posEnd = 0 className = None classes = [] functionPattern = 'def\s+(\w+)\s*\(.*\):' classPattern = 'class\s+(\w+)\s*\(.*\):' lines = s.splitlines() fun = funStruct() cl = classStruct() inDef = False funcWS = 0 classWS = [] inClass = [] tmpClasses = [] for lineNr in xrange(0, len(lines)): line = lines[lineNr] if inDef and (funcWS < getFirstWhiteSpaces(line) or isEmptyLine(line)): fun.posEnd = lineNr else: if inDef: funcs.append(fun) inDef = False funMatch = re.search(functionPattern, line) if funMatch: fun = funStruct() fun.name = funMatch.group(1) fun.posStart = lineNr fun.posEnd = lineNr inDef = True funcWS = getFirstWhiteSpaces(line) if inClass: fun.className = cl.name for clNr in xrange(0, len(tmpClasses)): if inClass[clNr] and (classWS[clNr] < getFirstWhiteSpaces(line) or isEmptyLine(line)): tmpClasses[clNr].posEnd = lineNr else: if inClass[clNr]: classes.append(tmpClasses[clNr]) inClass[clNr] = False classMatch = re.search(classPattern, line) if classMatch: cl = classStruct() cl.name = classMatch.group(1) cl.posStart = lineNr cl.posEnd = lineNr classWs = getFirstWhiteSpaces(line) tmpClasses.append(cl) classWS.append(classWs) inClass.append(True) if lineNr == len(lines)-1: if inDef: funcs.append(fun) for clNr in xrange(0, len(tmpClasses)): if inClass[clNr]: classes.append(tmpClasses[clNr]) for f in funcs: lines[f.posStart] = re.sub(functionPattern, 'BEGIN_METHOD', lines[f.posStart]) if isEmptyLine(lines[f.posEnd]): lines[f.posEnd] = 'END_METHOD' else: lines[f.posEnd] = lines[f.posEnd] + '\nEND_METHOD' lineNrList[f.posEnd] = [lineNrList[f.posEnd], -1] for c in classes: lines[c.posStart] = re.sub(classPattern, 'BEGIN_CLASS', lines[c.posStart]) if isEmptyLine(lines[c.posEnd]): lines[c.posEnd] = 'END_CLASS' else: lines[c.posEnd] = lines[c.posEnd] + '\nEND_CLASS' if type(lineNrList[c.posEnd]) == type([]): lineNrList[c.posEnd].append(-1) else: lineNrList[c.posEnd] = [lineNrList[c.posEnd], -1] for lineNr in xrange(0, len(lines)): line = lines[lineNr] match = re.search('(\w+)\(.*\)', line) if match: body, start, end = getMethodBody(match.group(1), funcs, lines) if body: lines[lineNr] = re.sub(match.group(1)+'.*', '\n'+body, line) nr = lineNrList[lineNr] if body != 'FUN\n': if type(nr) == type([]): bodyList = lineNrList[start:end+1] bodyList.reverse() for x in bodyList: lineNrList[lineNr].insert(1, x) else: lineNrList[lineNr] = [nr]+lineNrList[start:end+1] else: if type(nr) == type([]): lineNrList[lineNr] = nr.extend([-1, -1]) else: lineNrList[lineNr] = [nr, -1, -1] newlineNrList = lineNrList[:] del(lineNrList[0:len(lineNrList)]) for l in expandList(newlineNrList): lineNrList.append(l) return '\n'.join(lines)+'\n' def expandList(list): tmplist = [] for l in list: if type(l) == type([]): tmplist.extend(expandList(l)) else: tmplist.append(l) return tmplist def getFirstWhiteSpaces(line): return (len(line.expandtabs())-len(line.expandtabs().lstrip())) def isEmptyLine(line): if len([s for s in line if re.search('\s', s)]) == len(line): return True else: return False def getMethodBody(funName, funList, lines): f = None for fun in funList: if fun.name == funName: f = fun break if f: if f.expanded: return 'FUN\n', -1, -1 else: f.expanded = True body = [lines[i]+'\n' for i in xrange(fun.posStart+1, fun.posEnd)] return ''.join(body), fun.posStart+1, fun.posEnd else: return None, -1, -1 def removeAllNonTargetLanguageToken(s, lineNrList): for pattern in patterns: s = replacePatternMatchesInString(s, pattern, patterns.get(pattern), lineNrList) lines = s.splitlines() sList = [] tmplineNrList = lineNrList[:] del(lineNrList[0:len(lineNrList)]) for lineNr in xrange(0, len(lines)): line = lines[lineNr] if not isEmptyLine(line): lineList = [s.rstrip(': ') for s in line.split() if s.rstrip(': ') in keywords or s.rstrip(': ') in ownkeywords] sList.extend(lineList) lineNrList.extend([tmplineNrList[lineNr] for s in lineList]) return " ".join(sList) def preprocess(s, lineNrList): lines = s.splitlines(True) lineNr = 0 while lineNr < len(lines): if re.search('class\s+\w+\(', lines[lineNr]) or re.search('def\s+\w+\s*\(', lines[lineNr]): cnt = 0 s = lines[lineNr].rstrip('\n ') openP = countParenthesis(lines[lineNr]) while openP != 0: cnt += 1 openP += countParenthesis(lines[lineNr+cnt]) s += ' '+lines[lineNr+cnt].rstrip('\n ') lines[lineNr+cnt] = '' lines[lineNr] = s+'\n' del(lineNrList[lineNr+1: lineNr+cnt+1]) lineNr += cnt+1 continue lineNr += 1 return ''.join(lines) def countParenthesis(string): openP = 0 for s in string: if s == '(': openP += 1 elif s == ')': openP -= 1 return openP def removePatternMatchesFromString(s, pattern, lineNrList): while True: m = re.search(pattern, s) if m: cntNL = s[m.start():m.end()].count('\n') if cntNL > 0: start = s[:m.start()].count('\n') end = start + cntNL del(lineNrList[start:end]) s = s[:m.start()]+s[m.end():] else: break return s def replacePatternMatchesInString(s, pattern, replace, lineNrList): while True: m = re.search(pattern, s) if m: cntNL = s[m.start():m.end()].count('\n') if cntNL > 0: start = s[:m.start()].count('\n') end = start + cntNL del(lineNrList[start:end]) s = s[:m.start()]+replace+s[m.end():] else: break return s
Что я уже пробовал:
пожалуйста, помогите мне преобразовать блок кода python в код java
Patrice T
А вы знаете, что должен делать этот код ?
Member 13768114
да, это детектор plag, любезно помогите как можно скорее