假设我有以下一组输入和输出:
输入:
A full sentence: (S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (
VBG eating) (NP (NNS sausages))))))
输出:
James is a boy eating sausages
输入:(NNS Sausages)
输出:Sausages
我如何构建一个Java程序来从每个程序中提取单词来创建一个句子? (不使用外部库)
答案 0 :(得分:1)
您可以使用.*?(\\w+)(?:\\)+)
\\w
:捕获一个或多个)
后跟一个或多个\\w
其中[a-zA-Z0-9_]
表示([a-zA-Z]+)(?:\\)+)
,您也可以使用 String s="(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))";
System.out.println(s.replaceAll(".*?(\\w+)(?:\\)+)", "$1 ").trim());
仅捕获字词
注意:使用.*?(\\w+)\\)+
可以提高shmosel in comments
JAVA演示
James is a boy eating sausages
输出:
const regex = /.*?(\w+)(?:\)+)/g;
const str = `(S (NP (NNP James)) (VP (VBZ is) (NP (NP (DT a) (NN boy)) (VP (VBG eating) (NP (NNS sausages))))))
(NNS Sausages)`;
const subst = `$1 `;
const result = str.replace(regex, subst);
console.log(result);
演示:
%Conncectivity Matrix
success = 0;
n = input('Enter the No. of Nodes'); %size of matrix
k = input('Enter the max connectivity'); %maximal number of 1s
p = 0.5;
Result_Matrix = zeros(n,n);
while (success == 0)
Result_Matrix = (rand(n,n) < p);
Result_Matrix(logical(eye(n))) = 0;
Result_Matrix = max(Result_Matrix, Result_Matrix');
s = sum(Result_Matrix,1);
success = 1;
if min(s) == 0
success = 0; p = p*2; % too few 1s, increase p
end
if max(s) > k
success = 0; p = p/2; % too many 1s, decrease p
end
end
m=Result_Matrix;
conn_mat=m;
disp('connection matrix');
disp(m);
[r,c] = find(m);
A = [r,c]
%3D-GRAPH
PlotSizex=100;
PlotSizey=100;
PlotSizez=-100;
x=PlotSizex*rand(1,n)
y=PlotSizey*rand(1,n)
z=PlotSizez*rand(1,n)
plot3(x(A).', y(A).',z(A).', 'O-')
%Zaxis values multiply with Connectivity
d=zeros(n,n);
z % values of zaxis
for i=1:n
for j=i+1:n
d(i,j)= z(i);
d(j,i)=d(i,j);
end
end
New matrix= d.*m %d is zaxis values and m is connectivity matrix.
&#13;
答案 1 :(得分:0)
鉴于你的问题用parse-tree标记,习惯的方法是:
每个步骤本身都相对简单。