-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp.pl
135 lines (118 loc) · 4.18 KB
/
nlp.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
:- module(nlp, [similarity/3, sub_similarity/3, setup_tokenizer/0]).
:- use_module(library(http/http_client)).
:- dynamic streams/2.
:- table(similarity).
:- table(sub_similarity).
:- at_halt(close_tokenizer).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helper functions for OS detection
get_os(unix) :-
current_prolog_flag(unix, true), !.
get_os(apple) :-
current_prolog_flag(apple, true), !.
get_os(windows) :-
current_prolog_flag(windows, true), !.
relative_python_path("./venv/bin/python") :-
get_os(unix).
relative_python_path("./venv/bin/python") :-
get_os(apple).
relative_python_path("./venv/scripts/python.exe") :-
get_os(windows).
sys_python('python3') :- get_os(unix).
sys_python('python3') :- get_os(apple).
sys_python('python') :- get_os(windows).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Helps with various things
install_spacy :-
relative_python_path(Python),
process_create(
Python,
['-m', 'pip', 'install', '-U', 'pip', 'setuptools', 'wheel', 'spacy'],
[process(PID0), stdout(std), stderr(std)]
),
process_wait(PID0, exit(0)),
process_create(
Python,
['-m', 'spacy', 'download', 'en_core_web_md'],
[process(PID1), stdout(std), stderr(std)]
),
process_wait(PID1, exit(0)).
%% Performs the necessary setup steps to create the initial Python environment
% for the tokenizer, provided that ./venv does not exist.
%
% If the environment is not successfully created, simply delete it and run this
% again.
setup_tokenizer :-
exists_directory('./venv'),
format("Virtual environment already exists.~n"), !.
setup_tokenizer :-
\+exists_directory('./venv'),
sys_python(Python),
format("Creating virtual environment...~n"),
process_create(
path(Python),
['-m', 'venv', './venv'],
[process(PID), stdout(std), stderr(std)]
),
process_wait(PID, exit(0)),
format("Created virtual environment.~n"),
install_spacy,
format("Installed spaCy.~n"), !.
%% launch_tokenizer(-In:stream, -Out:stream)
% If a tokenizer is currently running, unifies In and Out with the tokenizer's
% input and output streams, respectively. Otherwise, creates a tokenizer instance,
% and unifies the input and output streams with In and Out, respectively.
launch_tokenizer(In, Out) :-
streams(In, Out), !.
launch_tokenizer(In, Out) :-
\+streams(_, _),
relative_python_path(Python),
process_create(
Python,
['nlp.py'],
[stdin(pipe(In)), stdout(pipe(Out)), stderr(null)]
),
mutex_create(tokenizer),
assertz(streams(In, Out)), !.
%% Closes the tokenizer by writing exit to the input stream.
close_tokenizer :-
(
streams(In, _) -> (
write(In, "exit\n"),
flush_output(In),
mutex_destroy(tokenizer),
retractall(streams(_, _))
); true
).
%% similarity(+Docs:string, +Needle:string, -Similarity:float)
% Unifies Similarity with the similarity between Docs and Needle, as computed
% by spaCy's similarity method.
similarity(Docs, Needle, Similarity) :-
launch_tokenizer(In, Out),
with_mutex(tokenizer,
(
string_length(Docs, LenA),
string_length(Needle, LenB),
format(In, "similarity ~w ~w~n~w~w", [LenA, LenB, Docs, Needle]),
flush_output(In),
read_line_to_string(Out, "OK"),
read_line_to_string(Out, String),
number_string(Similarity, String)
)
), !.
%% similarity(+Docs:string, +Needle:string, -Similarity:float)
% Unifies Similarity with the similarity between Docs and Needle, as determined
% by comparing a sliding window of words in Docs to Needle.
sub_similarity(Docs, Needle, Similarity) :-
launch_tokenizer(In, Out),
with_mutex(tokenizer,
(
string_length(Docs, LenA),
string_length(Needle, LenB),
format(In, "sub_similarity ~w ~w~n~w~w", [LenA, LenB, Docs, Needle]),
flush_output(In),
read_line_to_string(Out, "OK"),
read_line_to_string(Out, String),
number_string(Similarity, String)
)
), !.