Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
open-source
MetaGer
Commits
da019d08
Commit
da019d08
authored
Oct 13, 2016
by
Dominik Hebeler
Browse files
Sprachfilter angepasst, sodass alle Ergebnisse mit einem Prozess ausgewertet werden
parent
8d1fd39d
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/MetaGer.php
View file @
da019d08
...
...
@@ -157,6 +157,10 @@ class MetaGer
// combine
$combinedResults
=
$this
->
combineResults
(
$engines
);
# Wir bestimmen die Sprache eines jeden Suchergebnisses
$this
->
results
=
$this
->
addLangCodes
(
$this
->
results
);
// sort
//$sortedResults = $this->sortResults($engines);
// filter
...
...
@@ -260,6 +264,47 @@ class MetaGer
}
private
function
addLangCodes
(
$results
)
{
# Bei der Spracheinstellung "all" wird nicht gefiltert
if
(
$this
->
getLang
()
===
"all"
)
{
return
$results
;
}
else
{
# Ansonsten müssen wir jedem Result einen Sprachcode hinzufügen
$id
=
0
;
$langStrings
=
[];
foreach
(
$results
as
$result
)
{
# Wir geben jedem Ergebnis eine ID um später die Sprachcodes zuordnen zu können
$result
->
id
=
$id
;
$langStrings
[
"result_"
.
$id
]
=
utf8_encode
(
$result
->
getLangString
());
$id
++
;
}
# Wir schreiben die Strings in eine temporäre JSON-Datei,
# Da das Array unter umständen zu groß ist für eine direkte Übergabe an das Skript
$filename
=
"/tmp/"
.
getmypid
();
file_put_contents
(
$filename
,
json_encode
(
$langStrings
));
$langDetectorPath
=
app_path
()
.
"/Models/lang.pl"
;
$lang
=
exec
(
"echo '
$filename
' |
$langDetectorPath
"
);
$lang
=
json_decode
(
$lang
,
true
);
# Wir haben nun die Sprachcodes der einzelnen Ergebnisse.
# Diese müssen wir nur noch korrekt zuordnen, dann sind wir fertig.
foreach
(
$lang
as
$key
=>
$langCode
)
{
# Prefix vom Key entfernen:
$id
=
intval
(
str_replace
(
"result_"
,
""
,
$key
));
foreach
(
$this
->
results
as
$result
)
{
if
(
$result
->
id
===
$id
)
{
$result
->
langCode
=
$langCode
;
break
;
}
}
}
return
$results
;
}
}
public
function
combineResults
(
$engines
)
{
foreach
(
$engines
as
$engine
)
{
...
...
app/Models/Result.php
View file @
da019d08
...
...
@@ -189,12 +189,8 @@ class Result
}
# Eventueller Sprachfilter
if
(
$metager
->
getLang
()
!==
"all"
)
{
$text
=
$this
->
titel
.
" "
.
$this
->
descr
;
$path
=
app_path
()
.
"/Models/lang.pl"
;
$lang
=
exec
(
"echo '
$text
' |
$path
"
);
if
(
$metager
->
getLang
()
!==
$lang
)
{
if
(
$metager
->
getLang
()
!==
"all"
&&
isset
(
$this
->
langCode
))
{
if
(
$metager
->
getLang
()
!==
$this
->
langCode
)
{
return
false
;
}
...
...
@@ -310,4 +306,14 @@ class Result
{
return
$this
->
rank
;
}
public
function
getLangString
()
{
$string
=
""
;
$string
.
=
$this
->
titel
;
$string
.
=
$this
->
descr
;
return
$string
;
}
}
app/Models/lang.pl
View file @
da019d08
#!/usr/bin/perl
use
Lingua::
Identify
qw(:language_identification)
;
use
JSON
;
use
warnings
;
use
strict
;
binmode
STDOUT
,
"
:utf8
";
binmode
STDIN
,
"
:utf8
";
use
utf8
;
$text
=
<
STDIN
>
;
chomp
(
my
$filename
=
<
STDIN
>
)
;
$a
=
langof
(
$text
);
# Lets open the given file:
open
(
my
$fh
,
"
<
",
$filename
)
or
die
"
Can't open <
$filename
: $!
";
my
$json
=
<
$fh
>
;
close
$fh
;
print
$a
;
# Decode the JSON String
my
$data
=
decode_json
(
$json
);
# Wir durchlaufen den Hash:
foreach
my
$key
(
keys
%
{
$data
}){
$data
->
{
$key
}
=
langof
(
$data
->
{
$key
});
}
$data
=
encode_json
(
$data
);
# Nur noch die temporäre Datei löschen:
unlink
(
$filename
);
print
$data
;
readme.md
View file @
da019d08
...
...
@@ -13,7 +13,9 @@
*
php7.0-zip
*
sqlite3
*
redis-server
*
Das Perl-Paket: Lingua::Identify (http://search.cpan.org/~ambs/Lingua-Identify-0.56/lib/Lingua/Identify.pm)
*
Die Perl-Pakete
*
Lingua::Identify (http://search.cpan.org/~ambs/Lingua-Identify-0.56/lib/Lingua/Identify.pm)
*
JSON (http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm)
## Offizielle Dokumentation
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment